]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Small cleanups related to PUBLICATION framework code
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 30 Dec 2021 22:24:26 +0000 (19:24 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 30 Dec 2021 22:24:26 +0000 (19:24 -0300)
Discussion: https://postgr.es/m/202112302021.ca7ihogysgh3@alvherre.pgsql

src/backend/catalog/pg_publication.c
src/backend/commands/publicationcmds.c
src/backend/commands/tablecmds.c
src/backend/parser/gram.y
src/include/nodes/parsenodes.h
src/test/regress/expected/publication.out

index 62f10bcbd28792cf9e12372e1b4bf707a0c20aa1..b307bc2ed593ebacd87b70f0481f1f027642c51c 100644 (file)
@@ -287,7 +287,7 @@ publication_add_relation(Oid pubid, PublicationRelInfo *targetrel,
        Datum           values[Natts_pg_publication_rel];
        bool            nulls[Natts_pg_publication_rel];
        Oid                     relid = RelationGetRelid(targetrel->relation);
-       Oid                     prrelid;
+       Oid                     pubreloid;
        Publication *pub = GetPublication(pubid);
        ObjectAddress myself,
                                referenced;
@@ -320,9 +320,9 @@ publication_add_relation(Oid pubid, PublicationRelInfo *targetrel,
        memset(values, 0, sizeof(values));
        memset(nulls, false, sizeof(nulls));
 
-       prrelid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId,
-                                                                Anum_pg_publication_rel_oid);
-       values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(prrelid);
+       pubreloid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId,
+                                                                  Anum_pg_publication_rel_oid);
+       values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(pubreloid);
        values[Anum_pg_publication_rel_prpubid - 1] =
                ObjectIdGetDatum(pubid);
        values[Anum_pg_publication_rel_prrelid - 1] =
@@ -334,7 +334,8 @@ publication_add_relation(Oid pubid, PublicationRelInfo *targetrel,
        CatalogTupleInsert(rel, tup);
        heap_freetuple(tup);
 
-       ObjectAddressSet(myself, PublicationRelRelationId, prrelid);
+       /* Register dependencies as needed */
+       ObjectAddressSet(myself, PublicationRelRelationId, pubreloid);
 
        /* Add dependency on the publication */
        ObjectAddressSet(referenced, PublicationRelationId, pubid);
index 404bb5d0c875bf73fb56ee865d4a4c7353aad280..f932f47a08615f954a83efde3ec80f615cd471ce 100644 (file)
@@ -48,7 +48,7 @@
 #include "utils/syscache.h"
 #include "utils/varlena.h"
 
-static List *OpenReliIdList(List *relids);
+static List *OpenRelIdList(List *relids);
 static List *OpenTableList(List *tables);
 static void CloseTableList(List *rels);
 static void LockSchemaList(List *schemalist);
@@ -169,13 +169,13 @@ ObjectsInPublicationToOids(List *pubobjspec_list, ParseState *pstate,
                        case PUBLICATIONOBJ_TABLE:
                                *rels = lappend(*rels, pubobj->pubtable);
                                break;
-                       case PUBLICATIONOBJ_TABLE_IN_SCHEMA:
+                       case PUBLICATIONOBJ_TABLES_IN_SCHEMA:
                                schemaid = get_namespace_oid(pubobj->name, false);
 
                                /* Filter out duplicates if user specifies "sch1, sch1" */
                                *schemas = list_append_unique_oid(*schemas, schemaid);
                                break;
-                       case PUBLICATIONOBJ_TABLE_IN_CUR_SCHEMA:
+                       case PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA:
                                search_path = fetch_search_path(false);
                                if (search_path == NIL) /* nothing valid in search_path? */
                                        ereport(ERROR,
@@ -214,7 +214,7 @@ CheckObjSchemaNotAlreadyInPublication(List *rels, List *schemaidlist,
 
                if (list_member_oid(schemaidlist, relSchemaId))
                {
-                       if (checkobjtype == PUBLICATIONOBJ_TABLE_IN_SCHEMA)
+                       if (checkobjtype == PUBLICATIONOBJ_TABLES_IN_SCHEMA)
                                ereport(ERROR,
                                                errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                                errmsg("cannot add schema \"%s\" to publication",
@@ -499,8 +499,9 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup,
        Oid                     pubid = pubform->oid;
 
        /*
-        * It is quite possible that for the SET case user has not specified any
-        * tables in which case we need to remove all the existing tables.
+        * Nothing to do if no objects, except in SET: for that it is quite
+        * possible that user has not specified any tables in which case we need
+        * to remove all the existing tables.
         */
        if (!tables && stmt->action != DEFELEM_SET)
                return;
@@ -593,8 +594,9 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt,
        Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup);
 
        /*
-        * It is quite possible that for the SET case user has not specified any
-        * schemas in which case we need to remove all the existing schemas.
+        * Nothing to do if no objects, except in SET: for that it is quite
+        * possible that user has not specified any schemas in which case we need
+        * to remove all the existing schemas.
         */
        if (!schemaidlist && stmt->action != DEFELEM_SET)
                return;
@@ -610,10 +612,10 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt,
                List       *reloids;
 
                reloids = GetPublicationRelations(pubform->oid, PUBLICATION_PART_ROOT);
-               rels = OpenReliIdList(reloids);
+               rels = OpenRelIdList(reloids);
 
                CheckObjSchemaNotAlreadyInPublication(rels, schemaidlist,
-                                                                                         PUBLICATIONOBJ_TABLE_IN_SCHEMA);
+                                                                                         PUBLICATIONOBJ_TABLES_IN_SCHEMA);
 
                CloseTableList(rels);
                PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt);
@@ -813,7 +815,7 @@ RemovePublicationById(Oid pubid)
        if (!HeapTupleIsValid(tup))
                elog(ERROR, "cache lookup failed for publication %u", pubid);
 
-       pubform = (Form_pg_publication)GETSTRUCT(tup);
+       pubform = (Form_pg_publication) GETSTRUCT(tup);
 
        /* Invalidate relcache so that publication info is rebuilt. */
        if (pubform->puballtables)
@@ -868,7 +870,7 @@ RemovePublicationSchemaById(Oid psoid)
  * add them to a publication.
  */
 static List *
-OpenReliIdList(List *relids)
+OpenRelIdList(List *relids)
 {
        ListCell   *lc;
        List       *rels = NIL;
index 45e59e3d5c6d00d6e3d3838f43d2ff6bb7b10a87..3631b8a9298f0d41239405e6cc07bd22dbd3ae17 100644 (file)
@@ -40,8 +40,8 @@
 #include "catalog/pg_inherits.h"
 #include "catalog/pg_namespace.h"
 #include "catalog/pg_opclass.h"
-#include "catalog/pg_tablespace.h"
 #include "catalog/pg_statistic_ext.h"
+#include "catalog/pg_tablespace.h"
 #include "catalog/pg_trigger.h"
 #include "catalog/pg_type.h"
 #include "catalog/storage.h"
@@ -15626,7 +15626,6 @@ ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode
        else
                elog(ERROR, "unexpected identity type %u", stmt->identity_type);
 
-
        /* Check that the index exists */
        indexOid = get_relname_relid(stmt->name, rel->rd_rel->relnamespace);
        if (!OidIsValid(indexOid))
index 2a319eecda0cd30dd79de6ff4f9ff0586b63f6ae..f3c232842d66d23a71f00a5fac6e1ff64812e362 100644 (file)
@@ -9750,14 +9750,14 @@ PublicationObjSpec:
                        | ALL TABLES IN_P SCHEMA ColId
                                {
                                        $$ = makeNode(PublicationObjSpec);
-                                       $$->pubobjtype = PUBLICATIONOBJ_TABLE_IN_SCHEMA;
+                                       $$->pubobjtype = PUBLICATIONOBJ_TABLES_IN_SCHEMA;
                                        $$->name = $5;
                                        $$->location = @5;
                                }
                        | ALL TABLES IN_P SCHEMA CURRENT_SCHEMA
                                {
                                        $$ = makeNode(PublicationObjSpec);
-                                       $$->pubobjtype = PUBLICATIONOBJ_TABLE_IN_CUR_SCHEMA;
+                                       $$->pubobjtype = PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA;
                                        $$->location = @5;
                                }
                        | ColId
@@ -17411,7 +17411,8 @@ preprocess_pubobj_list(List *pubobjspec_list, core_yyscan_t yyscanner)
        if (pubobj->pubobjtype == PUBLICATIONOBJ_CONTINUATION)
                ereport(ERROR,
                                errcode(ERRCODE_SYNTAX_ERROR),
-                               errmsg("TABLE/ALL TABLES IN SCHEMA should be specified before the table/schema name(s)"),
+                               errmsg("invalid publication object list"),
+                               errdetail("One of TABLE or ALL TABLES IN SCHEMA must be specified before a standalone table or schema name."),
                                parser_errposition(pubobj->location));
 
        foreach(cell, pubobjspec_list)
@@ -17433,23 +17434,24 @@ preprocess_pubobj_list(List *pubobjspec_list, core_yyscan_t yyscanner)
                        {
                                /* convert it to PublicationTable */
                                PublicationTable *pubtable = makeNode(PublicationTable);
-                               pubtable->relation = makeRangeVar(NULL, pubobj->name,
-                                                                                                 pubobj->location);
+
+                               pubtable->relation =
+                                       makeRangeVar(NULL, pubobj->name, pubobj->location);
                                pubobj->pubtable = pubtable;
                                pubobj->name = NULL;
                        }
                }
-               else if (pubobj->pubobjtype == PUBLICATIONOBJ_TABLE_IN_SCHEMA ||
-                                pubobj->pubobjtype == PUBLICATIONOBJ_TABLE_IN_CUR_SCHEMA)
+               else if (pubobj->pubobjtype == PUBLICATIONOBJ_TABLES_IN_SCHEMA ||
+                                pubobj->pubobjtype == PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA)
                {
                        /*
                         * We can distinguish between the different type of schema
                         * objects based on whether name and pubtable is set.
                         */
                        if (pubobj->name)
-                               pubobj->pubobjtype = PUBLICATIONOBJ_TABLE_IN_SCHEMA;
+                               pubobj->pubobjtype = PUBLICATIONOBJ_TABLES_IN_SCHEMA;
                        else if (!pubobj->name && !pubobj->pubtable)
-                               pubobj->pubobjtype = PUBLICATIONOBJ_TABLE_IN_CUR_SCHEMA;
+                               pubobj->pubobjtype = PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA;
                        else
                                ereport(ERROR,
                                                errcode(ERRCODE_SYNTAX_ERROR),
index 4c5a8a39bf1ea1712dcba7d5a87640e9b0a542d9..784164b32aaf0f31f3b84fff7d2efe7b84c536f8 100644 (file)
@@ -3649,10 +3649,10 @@ typedef struct PublicationTable
  */
 typedef enum PublicationObjSpecType
 {
-       PUBLICATIONOBJ_TABLE,           /* Table type */
-       PUBLICATIONOBJ_TABLE_IN_SCHEMA, /* Tables in schema type */
-       PUBLICATIONOBJ_TABLE_IN_CUR_SCHEMA, /* Get the first element from
-                                                                                * search_path */
+       PUBLICATIONOBJ_TABLE,           /* A table */
+       PUBLICATIONOBJ_TABLES_IN_SCHEMA,        /* All tables in schema */
+       PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA,    /* All tables in first element of
+                                                                                        * search_path */
        PUBLICATIONOBJ_CONTINUATION /* Continuation of previous type */
 } PublicationObjSpecType;
 
index 5ac2d666a20449e15a29651c05900b179f831894..12c5f67080221533bbbe186ef9e798895503fdc5 100644 (file)
@@ -509,9 +509,10 @@ RESET SEARCH_PATH;
 -- check create publication on CURRENT_SCHEMA where TABLE/ALL TABLES in SCHEMA
 -- is not specified
 CREATE PUBLICATION testpub_forschema1 FOR CURRENT_SCHEMA;
-ERROR:  TABLE/ALL TABLES IN SCHEMA should be specified before the table/schema name(s)
+ERROR:  invalid publication object list
 LINE 1: CREATE PUBLICATION testpub_forschema1 FOR CURRENT_SCHEMA;
                                                   ^
+DETAIL:  One of TABLE or ALL TABLES IN SCHEMA must be specified before a standalone table or schema name.
 -- check create publication on CURRENT_SCHEMA along with FOR TABLE
 CREATE PUBLICATION testpub_forschema1 FOR TABLE CURRENT_SCHEMA;
 ERROR:  syntax error at or near "CURRENT_SCHEMA"
@@ -778,9 +779,10 @@ Tables from schemas:
 -- fail specifying table without any of 'FOR ALL TABLES IN SCHEMA' or
 --'FOR TABLE' or 'FOR ALL TABLES'
 CREATE PUBLICATION testpub_error FOR pub_test2.tbl1;
-ERROR:  TABLE/ALL TABLES IN SCHEMA should be specified before the table/schema name(s)
+ERROR:  invalid publication object list
 LINE 1: CREATE PUBLICATION testpub_error FOR pub_test2.tbl1;
                                              ^
+DETAIL:  One of TABLE or ALL TABLES IN SCHEMA must be specified before a standalone table or schema name.
 DROP VIEW testpub_view;
 DROP PUBLICATION testpub_default;
 DROP PUBLICATION testpib_ins_trunct;