Hi,

> Improving it might require teaching the dependency machinery (e.g.,
> maybe via a new PERFORM_DELETION_* flag) whether the caller supports
> CASCADE, so I think we could consider it as a separate improvement.

Right. A patch like attached fixes the issue, but it may be to be adopted to
other areas. See the output [1].

[1]:
```
postgres=# CREATE SUBSCRIPTION sub CONNECTION 'user=postgres' PUBLICATION pub 
WITH (connect=false, enabled=false, conflict_log_destination=all);
NOTICE:  created conflict log table "pg_conflict.pg_conflict_log_16384" for 
subscription "sub"
WARNING:  subscription was created, but is not connected
HINT:  To initiate replication, you must manually create the replication slot, 
enable the subscription, and alter the subscription to refresh publications.
CREATE SUBSCRIPTION
postgres=# CREATE VIEW v1 AS SELECT * FROM pg_conflict.pg_conflict_log_16384;
CREATE VIEW
postgres=# ALTER SUBSCRIPTION sub SET (conflict_log_destination = log);
ERROR:  cannot drop table pg_conflict.pg_conflict_log_16384 because other 
objects depend on it
DETAIL:  view v1 depends on table pg_conflict.pg_conflict_log_16384
postgres=#
```

Best regards,
Hayato Kuroda
FUJITSU LIMITED

diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c
index e4c73cc8686..4be6c5dab08 100644
--- a/src/backend/catalog/dependency.c
+++ b/src/backend/catalog/dependency.c
@@ -271,6 +271,9 @@ deleteObjectsInList(ObjectAddresses *targetObjects, 
Relation *depRel,
  * PERFORM_DELETION_CONCURRENT_LOCK: perform the drop normally but with a lock
  * as if it were concurrent.  This is used by REINDEX CONCURRENTLY.
  *
+ * PERFORM_DELETION_SKIP_CASCADE_HINT: omit the DROP ... CASCADE hint when
+ * RESTRICT fails, for callers that cannot offer cascading deletion.
+ *
  */
 void
 performDeletion(const ObjectAddress *object,
@@ -1169,6 +1172,7 @@ reportDependentObjects(const ObjectAddresses 
*targetObjects,
                                                        
getObjectDescription(origObject, false)),
                                         errdetail_internal("%s", 
clientdetail.data),
                                         errdetail_log("%s", logdetail.data),
+                                        (flags & 
PERFORM_DELETION_SKIP_CASCADE_HINT) ? 0 :
                                         errhint("Use DROP ... CASCADE to drop 
the dependent objects too.")));
                else
                        ereport(ERROR,
@@ -1176,6 +1180,7 @@ reportDependentObjects(const ObjectAddresses 
*targetObjects,
                                         errmsg("cannot drop desired object(s) 
because other objects depend on them"),
                                         errdetail_internal("%s", 
clientdetail.data),
                                         errdetail_log("%s", logdetail.data),
+                                        (flags & 
PERFORM_DELETION_SKIP_CASCADE_HINT) ? 0 :
                                         errhint("Use DROP ... CASCADE to drop 
the dependent objects too.")));
        }
        else if (numReportedClient > 1)
diff --git a/src/backend/commands/subscriptioncmds.c 
b/src/backend/commands/subscriptioncmds.c
index cb06018b0d8..65b034ed6fa 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -2689,7 +2689,9 @@ drop_sub_conflict_log_table(Oid subid, char *subname, Oid 
subconflictlogrelid,
                        CommandCounterIncrement();
 
                        ObjectAddressSet(object, RelationRelationId, 
subconflictlogrelid);
-                       performDeletion(&object, behavior, 
PERFORM_DELETION_INTERNAL);
+                       performDeletion(&object, behavior,
+                                                       
PERFORM_DELETION_INTERNAL |
+                                                       
PERFORM_DELETION_SKIP_CASCADE_HINT);
                }
 
                ereport(NOTICE,
diff --git a/src/include/catalog/dependency.h b/src/include/catalog/dependency.h
index 9782d0df2a7..9ff6e3c5ed3 100644
--- a/src/include/catalog/dependency.h
+++ b/src/include/catalog/dependency.h
@@ -96,6 +96,7 @@ typedef struct ObjectAddresses ObjectAddresses;
 #define PERFORM_DELETION_SKIP_EXTENSIONS       0x0010  /* keep extensions */
 #define PERFORM_DELETION_CONCURRENT_LOCK       0x0020  /* normal drop with
                                                                                
                         * concurrent lock mode */
+#define PERFORM_DELETION_SKIP_CASCADE_HINT     0x0040  /* omit CASCADE hint */
 
 
 /* in dependency.c */

Reply via email to