From e73312a6eae422404d67f413f009cbf8ef1d2b21 Mon Sep 17 00:00:00 2001 From: Shlok Kyal Date: Wed, 11 Jun 2025 11:41:18 +0530 Subject: [PATCH v21 1/3] Add RESET clause to Alter Publication which will reset the publication with default values. This patch adds a new RESET clause to ALTER PUBLICATION which will reset the publication to the default state which includes resetting the publication parameters, setting ALL TABLES flag to false and dropping the relations and schemas that are associated with the publication. Usage: ALTER PUBLICATION pub1 RESET; --- doc/src/sgml/ref/alter_publication.sgml | 34 +++++-- src/backend/commands/publicationcmds.c | 116 +++++++++++++++++++-- src/backend/parser/gram.y | 9 ++ src/bin/psql/tab-complete.in.c | 2 +- src/include/nodes/parsenodes.h | 1 + src/test/regress/expected/publication.out | 118 ++++++++++++++++++++++ src/test/regress/sql/publication.sql | 57 +++++++++++ 7 files changed, 323 insertions(+), 14 deletions(-) diff --git a/doc/src/sgml/ref/alter_publication.sgml b/doc/src/sgml/ref/alter_publication.sgml index d5ea383e8bc..178f39d9575 100644 --- a/doc/src/sgml/ref/alter_publication.sgml +++ b/doc/src/sgml/ref/alter_publication.sgml @@ -27,6 +27,7 @@ ALTER PUBLICATION name DROP name SET ( publication_parameter [= value] [, ... ] ) ALTER PUBLICATION name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ALTER PUBLICATION name RENAME TO new_name +ALTER PUBLICATION name RESET where publication_object is one of: @@ -69,18 +70,31 @@ ALTER PUBLICATION name RENAME TO - The remaining variants change the owner and the name of the publication. + The OWNER clause will change the owner of the + publication. + + + + The RENAME clause will change the name of the + publication. + + + + The RESET clause will reset the publication to the default + state. This includes resetting all publication parameters, setting the + ALL TABLES flag to false, and + removing all associated tables and schemas from the publication. You must own the publication to use ALTER PUBLICATION. Adding a table to a publication additionally requires owning that table. - The ADD TABLES IN SCHEMA and - SET TABLES IN SCHEMA to a publication requires the - invoking user to be a superuser. - To alter the owner, you must be able to SET ROLE to the - new owning role, and that role must have CREATE - privilege on the database. + The ADD TABLES IN SCHEMA, + SET TABLES IN SCHEMA to a publication and + RESET of publication requires the invoking user to be a + superuser. To alter the owner, you must be able to + SET ROLE to the new owning role, and that role must have + CREATE privilege on the database. Also, the new owner of a FOR ALL TABLES or FOR TABLES IN SCHEMA @@ -230,6 +244,12 @@ ALTER PUBLICATION sales_publication ADD TABLES IN SCHEMA marketing, sales; production_publication: ALTER PUBLICATION production_publication ADD TABLE users, departments, TABLES IN SCHEMA production; + + + + Reset the publication production_publication: + +ALTER PUBLICATION production_publication RESET; diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 803c26ab216..06f6f45526b 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -49,6 +49,15 @@ #include "utils/varlena.h" +/* CREATE PUBLICATION default values for flags and publication parameters */ +#define PUB_DEFAULT_ACTION_INSERT true +#define PUB_DEFAULT_ACTION_UPDATE true +#define PUB_DEFAULT_ACTION_DELETE true +#define PUB_DEFAULT_ACTION_TRUNCATE true +#define PUB_DEFAULT_VIA_ROOT false +#define PUB_DEFAULT_ALL_TABLES false +#define PUB_DEFAULT_GENCOLS PUBLISH_GENCOLS_NONE + /* * Information used to validate the columns in the row filter expression. See * contain_invalid_rfcolumn_walker for details. @@ -91,12 +100,12 @@ parse_publication_options(ParseState *pstate, *publish_generated_columns_given = false; /* defaults */ - pubactions->pubinsert = true; - pubactions->pubupdate = true; - pubactions->pubdelete = true; - pubactions->pubtruncate = true; - *publish_via_partition_root = false; - *publish_generated_columns = PUBLISH_GENCOLS_NONE; + pubactions->pubinsert = PUB_DEFAULT_ACTION_INSERT; + pubactions->pubupdate = PUB_DEFAULT_ACTION_UPDATE; + pubactions->pubdelete = PUB_DEFAULT_ACTION_DELETE; + pubactions->pubtruncate = PUB_DEFAULT_ACTION_TRUNCATE; + *publish_via_partition_root = PUB_DEFAULT_VIA_ROOT; + *publish_generated_columns = PUB_DEFAULT_GENCOLS; /* Parse options */ foreach(lc, options) @@ -1187,6 +1196,99 @@ InvalidatePublicationRels(List *relids) CacheInvalidateRelcacheAll(); } +/* + * Reset the publication. + * + * Reset the publication parameters, setting ALL TABLES flag to false and drop + * all relations and schemas that are associated with the publication. + */ +static void +AlterPublicationReset(ParseState *pstate, AlterPublicationStmt *stmt, + Relation rel, HeapTuple tup) +{ + Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); + Oid pubid = pubform->oid; + List *schemaids = NIL; + List *rels = NIL; + List *relids = NIL; + bool nulls[Natts_pg_publication]; + bool replaces[Natts_pg_publication]; + Datum values[Natts_pg_publication]; + + /* RESET publication requires superuser */ + if (!superuser()) + ereport(ERROR, + errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("must be superuser to RESET publication")); + + memset(values, 0, sizeof(values)); + memset(nulls, false, sizeof(nulls)); + memset(replaces, false, sizeof(replaces)); + + /* Reset the publication parameters */ + values[Anum_pg_publication_pubinsert - 1] = BoolGetDatum(PUB_DEFAULT_ACTION_INSERT); + replaces[Anum_pg_publication_pubinsert - 1] = true; + + values[Anum_pg_publication_pubupdate - 1] = BoolGetDatum(PUB_DEFAULT_ACTION_UPDATE); + replaces[Anum_pg_publication_pubupdate - 1] = true; + + values[Anum_pg_publication_pubdelete - 1] = BoolGetDatum(PUB_DEFAULT_ACTION_DELETE); + replaces[Anum_pg_publication_pubdelete - 1] = true; + + values[Anum_pg_publication_pubtruncate - 1] = BoolGetDatum(PUB_DEFAULT_ACTION_TRUNCATE); + replaces[Anum_pg_publication_pubtruncate - 1] = true; + + values[Anum_pg_publication_pubviaroot - 1] = BoolGetDatum(PUB_DEFAULT_VIA_ROOT); + replaces[Anum_pg_publication_pubviaroot - 1] = true; + + values[Anum_pg_publication_pubgencols - 1] = CharGetDatum(PUB_DEFAULT_GENCOLS); + replaces[Anum_pg_publication_pubgencols - 1] = true; + + /* Set ALL TABLES flag to false */ + if (pubform->puballtables) + { + values[Anum_pg_publication_puballtables - 1] = BoolGetDatum(PUB_DEFAULT_ALL_TABLES); + replaces[Anum_pg_publication_puballtables - 1] = true; + CacheInvalidateRelcacheAll(); + } + + tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls, + replaces); + + /* Update the catalog. */ + CatalogTupleUpdate(rel, &tup->t_self, tup); + + /* Remove the associated schemas from the publication */ + schemaids = GetPublicationSchemas(pubid); + + /* + * Schema lock is held until the publication is altered to prevent + * concurrent schema deletion. + */ + LockSchemaList(schemaids); + + /* Remove Schemas */ + PublicationDropSchemas(pubid, schemaids, true); + + /* Get all relations associated with the publication */ + relids = GetPublicationRelations(pubid, PUBLICATION_PART_ROOT); + + foreach_oid(relid, relids) + { + PublicationRelInfo *rel; + + rel = palloc(sizeof(PublicationRelInfo)); + rel->whereClause = NULL; + rel->columns = NIL; + rel->relation = table_open(relid, ShareUpdateExclusiveLock); + rels = lappend(rels, rel); + } + + /* Remove the associated relations from the publication */ + PublicationDropTables(pubid, rels, true); + CloseTableList(rels); +} + /* * Add or remove table to/from publication. */ @@ -1501,6 +1603,8 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) if (stmt->options) AlterPublicationOptions(pstate, stmt, rel, tup); + else if (stmt->action == AP_ResetPublication) + AlterPublicationReset(pstate, stmt, rel, tup); else { List *relations = NIL; diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index db43034b9db..4a4010296af 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -10850,6 +10850,8 @@ pub_obj_list: PublicationObjSpec * * ALTER PUBLICATION name SET pub_obj [, ...] * + * ALTER PUBLICATION name RESET + * * pub_obj is one of: * * TABLE table_name [, ...] @@ -10896,6 +10898,13 @@ AlterPublicationStmt: n->action = AP_DropObjects; $$ = (Node *) n; } + | ALTER PUBLICATION name RESET + { + AlterPublicationStmt *n = makeNode(AlterPublicationStmt); + n->pubname = $3; + n->action = AP_ResetPublication; + $$ = (Node *)n; + } ; /***************************************************************************** diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index 8b10f2313f3..f07af7f71d3 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -2266,7 +2266,7 @@ match_previous_words(int pattern_id, /* ALTER PUBLICATION */ else if (Matches("ALTER", "PUBLICATION", MatchAny)) - COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME TO", "SET"); + COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME TO", "RESET", "SET"); /* ALTER PUBLICATION ADD */ else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD")) COMPLETE_WITH("TABLES IN SCHEMA", "TABLE"); diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 86a236bd58b..af220b02788 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -4305,6 +4305,7 @@ typedef enum AlterPublicationAction AP_AddObjects, /* add objects to publication */ AP_DropObjects, /* remove objects from publication */ AP_SetObjects, /* set list of objects */ + AP_ResetPublication, /* reset the publication */ } AlterPublicationAction; typedef struct AlterPublicationStmt diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out index 53268059142..74009a92f3f 100644 --- a/src/test/regress/expected/publication.out +++ b/src/test/regress/expected/publication.out @@ -1923,6 +1923,124 @@ Tables: DROP PUBLICATION pub1; DROP PUBLICATION pub2; DROP TABLE gencols; +-- Tests for ALTER PUBLICATION ... RESET +CREATE SCHEMA pub_sch1; +CREATE TABLE pub_sch1.tbl1 (a int); +SET client_min_messages = 'ERROR'; +CREATE PUBLICATION testpub_reset FOR ALL TABLES; +RESET client_min_messages; +-- Verify that 'ALL TABLES' flag is reset +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | t | t | t | t | t | none | f +(1 row) + +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +(1 row) + +ALTER PUBLICATION testpub_reset ADD TABLE pub_sch1.tbl1; +-- Verify that associated tables are removed from the publication after RESET +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +Tables: + "pub_sch1.tbl1" + +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +(1 row) + +ALTER PUBLICATION testpub_reset ADD TABLES IN SCHEMA public; +-- Verify that associated schemas are reomved from the publication after RESET +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +Tables from schemas: + "public" + +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +(1 row) + +ALTER PUBLICATION testpub_reset SET (PUBLISH = ''); +-- Verify that 'PUBLISH' parameter is reset +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | f | f | f | f | none | f +(1 row) + +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +(1 row) + +ALTER PUBLICATION testpub_reset SET (PUBLISH_VIA_PARTITION_ROOT = 'true'); +-- Verify that 'PUBLISH_VIA_PARTITION_ROOT' parameter is reset +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | t +(1 row) + +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +(1 row) + +ALTER PUBLICATION testpub_reset SET (PUBLISH_GENERATED_COLUMNS = stored); +-- Verify that 'PUBLISH_GENERATED_COLUMNS' parameter is reset +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | stored | f +(1 row) + +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + Publication testpub_reset + Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root +--------------------------+------------+---------+---------+---------+-----------+-------------------+---------- + regress_publication_user | f | t | t | t | t | none | f +(1 row) + +-- Verify that only superuser can reset a publication +ALTER PUBLICATION testpub_reset OWNER TO regress_publication_user2; +SET ROLE regress_publication_user2; +ALTER PUBLICATION testpub_reset RESET; -- fail - must be superuser +ERROR: must be superuser to RESET publication +SET ROLE regress_publication_user; +DROP PUBLICATION testpub_reset; +DROP TABLE pub_sch1.tbl1; +DROP SCHEMA pub_sch1; RESET client_min_messages; RESET SESSION AUTHORIZATION; DROP ROLE regress_publication_user, regress_publication_user2; diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql index deddf0da844..1366b11bba0 100644 --- a/src/test/regress/sql/publication.sql +++ b/src/test/regress/sql/publication.sql @@ -1222,6 +1222,63 @@ DROP PUBLICATION pub1; DROP PUBLICATION pub2; DROP TABLE gencols; +-- Tests for ALTER PUBLICATION ... RESET +CREATE SCHEMA pub_sch1; +CREATE TABLE pub_sch1.tbl1 (a int); +SET client_min_messages = 'ERROR'; +CREATE PUBLICATION testpub_reset FOR ALL TABLES; +RESET client_min_messages; + +-- Verify that 'ALL TABLES' flag is reset +\dRp+ testpub_reset +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + +ALTER PUBLICATION testpub_reset ADD TABLE pub_sch1.tbl1; + +-- Verify that associated tables are removed from the publication after RESET +\dRp+ testpub_reset +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + +ALTER PUBLICATION testpub_reset ADD TABLES IN SCHEMA public; + +-- Verify that associated schemas are reomved from the publication after RESET +\dRp+ testpub_reset +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + +ALTER PUBLICATION testpub_reset SET (PUBLISH = ''); + +-- Verify that 'PUBLISH' parameter is reset +\dRp+ testpub_reset +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + +ALTER PUBLICATION testpub_reset SET (PUBLISH_VIA_PARTITION_ROOT = 'true'); + +-- Verify that 'PUBLISH_VIA_PARTITION_ROOT' parameter is reset +\dRp+ testpub_reset +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + +ALTER PUBLICATION testpub_reset SET (PUBLISH_GENERATED_COLUMNS = stored); + +-- Verify that 'PUBLISH_GENERATED_COLUMNS' parameter is reset +\dRp+ testpub_reset +ALTER PUBLICATION testpub_reset RESET; +\dRp+ testpub_reset + +-- Verify that only superuser can reset a publication +ALTER PUBLICATION testpub_reset OWNER TO regress_publication_user2; +SET ROLE regress_publication_user2; +ALTER PUBLICATION testpub_reset RESET; -- fail - must be superuser +SET ROLE regress_publication_user; + +DROP PUBLICATION testpub_reset; +DROP TABLE pub_sch1.tbl1; +DROP SCHEMA pub_sch1; + RESET client_min_messages; RESET SESSION AUTHORIZATION; DROP ROLE regress_publication_user, regress_publication_user2; -- 2.34.1