From 298869f57494660ac08b07414ef5e3b553b7b94c Mon Sep 17 00:00:00 2001 From: "houzj.fnst" Date: Thu, 23 Sep 2021 14:32:14 +0800 Subject: [PATCH] move check into a separate function --- src/backend/commands/publicationcmds.c | 57 +++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 2153578..9ecae0d 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -523,14 +523,6 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, if (!tables && stmt->action != DEFELEM_SET) return; - /* Check that user is allowed to manipulate the publication tables. */ - if (tables && pubform->puballtables) - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("publication \"%s\" is defined as FOR ALL TABLES", - NameStr(pubform->pubname)), - errdetail("Tables cannot be added to or dropped from FOR ALL TABLES publications."))); - rels = OpenTableList(tables); if (stmt->action == DEFELEM_ADD) @@ -614,20 +606,6 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, if (!schemaidlist && stmt->action != DEFELEM_SET) return; - /* Check that user is allowed to manipulate the publication tables */ - if (schemaidlist && pubform->puballtables) - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("publication \"%s\" is defined as FOR ALL TABLES", - NameStr(pubform->pubname)), - errdetail("Tables from schema cannot be added to, dropped from, or set on FOR ALL TABLES publications."))); - - if (schemaidlist && (stmt->action == DEFELEM_ADD || stmt->action == DEFELEM_SET) && - !superuser()) - ereport(ERROR, - (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), - errmsg("must be superuser to add or set schemas"))); - /* * Schema lock is held until the publication is altered to prevent * concurrent schema deletion. No need to unlock the schemas, the locks @@ -673,6 +651,39 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, } /* + * Check if relations and schemas can be in given publication and throws + * appropriate error if not. + */ +static void +CheckAlterPublication(AlterPublicationStmt *stmt, HeapTuple tup, + List *tables, List *schemaidlist) +{ + Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); + + if ((stmt->action == DEFELEM_ADD || stmt->action == DEFELEM_SET) && + schemaidlist && !superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("must be superuser to add or set schemas"))); + + /* Check that user is allowed to manipulate the publication tables in schema */ + if (schemaidlist && pubform->puballtables) + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("publication \"%s\" is defined as FOR ALL TABLES", + NameStr(pubform->pubname)), + errdetail("Tables from schema cannot be added to, dropped from, or set on FOR ALL TABLES publications."))); + + /* Check that user is allowed to manipulate the publication tables. */ + if (tables && pubform->puballtables) + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("publication \"%s\" is defined as FOR ALL TABLES", + NameStr(pubform->pubname)), + errdetail("Tables cannot be added to or dropped from FOR ALL TABLES publications."))); +} + +/* * Alter the existing publication. * * This is dispatcher function for AlterPublicationOptions, @@ -713,6 +724,8 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &schemaidlist); + CheckAlterPublication(stmt, tup, relations, schemaidlist); + /* Lock the publication so nobody else can do anything with it. */ LockDatabaseObject(PublicationRelationId, pubform->oid, 0, AccessExclusiveLock); -- 2.7.2.windows.1