From 8b571ae4a8c2fff42da26b24b8a3177dc03848dd Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Wed, 3 Feb 2021 20:12:08 +1100 Subject: [PATCH v1] DROP TABLE breaks sync worker relid. Previously we were allowing to drop the table when logical replication table synchronization was still in progress. This caused the sync worker's relid to become invalid, and then any subsequent use of that relid would cause problems. e.g. In the test case which exposed this bug, the problem manifested as a stacktrace when the sync worker LOG message attempted to display the table name using the dropped relid. --- src/backend/catalog/pg_subscription.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c index 44cb285..d15c5d8 100644 --- a/src/backend/catalog/pg_subscription.c +++ b/src/backend/catalog/pg_subscription.c @@ -29,6 +29,7 @@ #include "utils/array.h" #include "utils/builtins.h" #include "utils/fmgroids.h" +#include "utils/lsyscache.h" #include "utils/pg_lsn.h" #include "utils/rel.h" #include "utils/syscache.h" @@ -403,6 +404,35 @@ RemoveSubscriptionRel(Oid subid, Oid relid) scan = table_beginscan_catalog(rel, nkeys, skey); while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection))) { + Form_pg_subscription_rel subrel; + + subrel = (Form_pg_subscription_rel) GETSTRUCT(tup); + + /* + * We don't allow to drop the relation mapping when the table + * synchronization is in progress unless the caller updates the + * corresponding subscription as well. This is to ensure that we don't + * leave tablesync slots or origins in the system when the + * corresponding table is dropped. + */ + if (!OidIsValid(subid) && subrel->srsubstate != SUBREL_STATE_READY) + { + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("could not drop relation mapping for subscription \"%s\"", + get_subscription_name(subrel->srsubid, false)), + errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".", + get_rel_name(relid), subrel->srsubstate), + + /* + * translator: first %s is a SQL ALTER command and second %s is a + * SQL DROP command + */ + errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.", + "ALTER SUBSCRIPTION ... ENABLE", + "DROP SUBSCRIPTION ..."))); + } + CatalogTupleDelete(rel, &tup->t_self); } table_endscan(scan); -- 1.8.3.1