Allow publications with schema and table of the same schema.
authorAmit Kapila <akapila@postgresql.org>
Fri, 23 Sep 2022 02:51:26 +0000 (08:21 +0530)
committerAmit Kapila <akapila@postgresql.org>
Fri, 23 Sep 2022 02:51:26 +0000 (08:21 +0530)
We previously thought that allowing such cases can confuse users when they
specify DROP TABLES IN SCHEMA but that doesn't seem to be the case based
on discussion. This helps to uplift the restriction during
ALTER TABLE ... SET SCHEMA which used to ensure that we couldn't end up
with a publication having both a schema and the same schema's table.

To allow this, we need to forbid having any schema on a publication if
column lists on a table are specified (and vice versa). This is because
otherwise we still need a restriction during ALTER TABLE ... SET SCHEMA to
forbid cases where it could lead to a publication having both a schema and
the same schema's table with column list.

Based on suggestions by Peter Eisentraut.

Author: Hou Zhijie and Vignesh C
Reviewed-By: Peter Smith, Amit Kapila
Backpatch-through: 15, where it was introduced
Discussion: https://postgr.es/m/2729c9e2-9aac-8cda-f2f4-34f2bcc18f4e@enterprisedb.com

13 files changed:
doc/src/sgml/logical-replication.sgml
doc/src/sgml/ref/alter_publication.sgml
doc/src/sgml/ref/create_publication.sgml
src/backend/catalog/pg_publication.c
src/backend/commands/publicationcmds.c
src/backend/commands/tablecmds.c
src/backend/replication/pgoutput/pgoutput.c
src/bin/pg_dump/t/002_pg_dump.pl
src/test/regress/expected/alter_table.out
src/test/regress/expected/publication.out
src/test/regress/sql/alter_table.sql
src/test/regress/sql/publication.sql
src/test/subscription/t/028_row_filter.pl

index 7fe3a1043e625dfaf841af52f99852f005818461..77be4c37e7fa4c7b10274a81d89e0ffe88a12051 100644 (file)
@@ -1119,6 +1119,11 @@ test_sub=# SELECT * FROM child ORDER BY a;
    of columns in the list is not preserved.
   </para>
 
+  <para>
+   Specifying a column list when the publication also publishes
+   <literal>FOR TABLES IN SCHEMA</literal> is not supported.
+  </para>
+
   <para>
    For partitioned tables, the publication parameter
    <literal>publish_via_partition_root</literal> determines which column list
index fc2d6d4885fbc78e62dffa8d8cfaf9dde697585c..c84b11f47a744529bbcd08f8072a968401f48e44 100644 (file)
@@ -52,9 +52,11 @@ ALTER PUBLICATION <replaceable class="parameter">name</replaceable> RENAME TO <r
    remove one or more tables/schemas from the publication.  Note that adding
    tables/schemas to a publication that is already subscribed to will require an
    <literal>ALTER SUBSCRIPTION ... REFRESH PUBLICATION</literal> action on the
-   subscribing side in order to become effective. Note also that the combination
-   of <literal>DROP</literal> with a <literal>WHERE</literal> clause is not
-   allowed.
+   subscribing side in order to become effective. Note also that
+   <literal>DROP TABLES IN SCHEMA</literal> will not drop any schema tables
+   that were specified using <literal>FOR TABLE</literal>/
+   <literal>ADD TABLE</literal>, and the combination of <literal>DROP</literal>
+   with a <literal>WHERE</literal> clause is not allowed.
   </para>
 
   <para>
@@ -82,11 +84,8 @@ ALTER PUBLICATION <replaceable class="parameter">name</replaceable> RENAME TO <r
   </para>
 
   <para>
-   Adding/Setting a table that is part of schema specified in
-   <literal>TABLES IN SCHEMA</literal>, adding/setting a schema to a
-   publication that already has a table that is part of the specified schema or
-   adding/setting a table to a publication that already has a table's schema as
-   part of the specified schema is not supported.
+   Adding/Setting any schema when the publication also publishes a table with a
+   column list, and vice versa is not supported.
   </para>
  </refsect1>
 
index 2e097a81e52044d745365adca6fc940fef77f2dc..c5190f0ce6435271e1155ea56ed92ef798af3133 100644 (file)
@@ -102,6 +102,11 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
       materialized views, and regular views cannot be part of a publication.
      </para>
 
+     <para>
+      Specifying a column list when the publication also publishes
+      <literal>FOR TABLES IN SCHEMA</literal> is not supported.
+     </para>
+
      <para>
       When a partitioned table is added to a publication, all of its existing
       and future partitions are implicitly considered to be part of the
@@ -109,11 +114,6 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
       partition are also published via publications that its ancestors are
       part of.
      </para>
-
-     <para>
-      Specifying a table that is part of a schema specified by
-      <literal>FOR TABLES IN SCHEMA</literal> is not supported.
-     </para>
     </listitem>
    </varlistentry>
 
@@ -136,8 +136,8 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
      </para>
 
      <para>
-      Specifying a schema along with a table which belongs to the specified
-      schema using <literal>FOR TABLE</literal> is not supported.
+      Specifying a schema when the publication also publishes a table with a
+      column list is not supported.
      </para>
 
      <para>
@@ -273,6 +273,12 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
    system columns.
   </para>
 
+  <para>
+   The row filter on a table becomes redundant if
+   <literal>FOR TABLES IN SCHEMA</literal> is specified and the table
+   belongs to the referred schema.
+  </para>
+
   <para>
    For published partitioned tables, the row filter for each
    partition is taken from the published partitioned table if the
index e27db27f04a77c95dde4f17e0628029c720a9344..cd48cb6c469c9cfaa9ee353e03e74023b7662ab0 100644 (file)
@@ -1111,6 +1111,7 @@ pg_get_publication_tables(PG_FUNCTION_ARGS)
        HeapTuple   pubtuple = NULL;
        HeapTuple   rettuple;
        Oid         relid = list_nth_oid(tables, funcctx->call_cntr);
+       Oid         schemaid = get_rel_namespace(relid);
        Datum       values[NUM_PUBLICATION_TABLES_ELEM] = {0};
        bool        nulls[NUM_PUBLICATION_TABLES_ELEM] = {0};
 
@@ -1122,9 +1123,17 @@ pg_get_publication_tables(PG_FUNCTION_ARGS)
 
        values[0] = ObjectIdGetDatum(relid);
 
-       pubtuple = SearchSysCacheCopy2(PUBLICATIONRELMAP,
-                                      ObjectIdGetDatum(relid),
-                                      ObjectIdGetDatum(publication->oid));
+       /*
+        * We don't consider row filters or column lists for FOR ALL TABLES or
+        * FOR TABLES IN SCHEMA publications.
+        */
+       if (!publication->alltables &&
+           !SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP,
+                                  ObjectIdGetDatum(schemaid),
+                                  ObjectIdGetDatum(publication->oid)))
+           pubtuple = SearchSysCacheCopy2(PUBLICATIONRELMAP,
+                                          ObjectIdGetDatum(relid),
+                                          ObjectIdGetDatum(publication->oid));
 
        if (HeapTupleIsValid(pubtuple))
        {
index 15ab5aa99ec0f15ed017a97e374236f99db09114..7ffad096aa1420f41ede236c72662f953e1201ef 100644 (file)
@@ -66,7 +66,6 @@ typedef struct rf_context
    Oid         parentid;       /* relid of the parent relation */
 } rf_context;
 
-static List *OpenRelIdList(List *relids);
 static List *OpenTableList(List *tables);
 static void CloseTableList(List *rels);
 static void LockSchemaList(List *schemalist);
@@ -214,44 +213,6 @@ ObjectsInPublicationToOids(List *pubobjspec_list, ParseState *pstate,
    }
 }
 
-/*
- * Check if any of the given relation's schema is a member of the given schema
- * list.
- */
-static void
-CheckObjSchemaNotAlreadyInPublication(List *rels, List *schemaidlist,
-                                     PublicationObjSpecType checkobjtype)
-{
-   ListCell   *lc;
-
-   foreach(lc, rels)
-   {
-       PublicationRelInfo *pub_rel = (PublicationRelInfo *) lfirst(lc);
-       Relation    rel = pub_rel->relation;
-       Oid         relSchemaId = RelationGetNamespace(rel);
-
-       if (list_member_oid(schemaidlist, relSchemaId))
-       {
-           if (checkobjtype == PUBLICATIONOBJ_TABLES_IN_SCHEMA)
-               ereport(ERROR,
-                       errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                       errmsg("cannot add schema \"%s\" to publication",
-                              get_namespace_name(relSchemaId)),
-                       errdetail("Table \"%s\" in schema \"%s\" is already part of the publication, adding the same schema is not supported.",
-                                 RelationGetRelationName(rel),
-                                 get_namespace_name(relSchemaId)));
-           else if (checkobjtype == PUBLICATIONOBJ_TABLE)
-               ereport(ERROR,
-                       errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                       errmsg("cannot add relation \"%s.%s\" to publication",
-                              get_namespace_name(relSchemaId),
-                              RelationGetRelationName(rel)),
-                       errdetail("Table's schema \"%s\" is already part of the publication or part of the specified schema list.",
-                                 get_namespace_name(relSchemaId)));
-       }
-   }
-}
-
 /*
  * Returns true if any of the columns used in the row filter WHERE expression is
  * not part of REPLICA IDENTITY, false otherwise.
@@ -721,7 +682,7 @@ TransformPubWhereClauses(List *tables, const char *queryString,
  */
 static void
 CheckPubRelationColumnList(List *tables, const char *queryString,
-                          bool pubviaroot)
+                          bool publish_schema, bool pubviaroot)
 {
    ListCell   *lc;
 
@@ -732,6 +693,24 @@ CheckPubRelationColumnList(List *tables, const char *queryString,
        if (pri->columns == NIL)
            continue;
 
+       /*
+        * Disallow specifying column list if any schema is in the
+        * publication.
+        *
+        * XXX We could instead just forbid the case when the publication
+        * tries to publish the table with a column list and a schema for that
+        * table. However, if we do that then we need a restriction during
+        * ALTER TABLE ... SET SCHEMA to prevent such a case which doesn't
+        * seem to be a good idea.
+        */
+       if (publish_schema)
+           ereport(ERROR,
+                   errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                   errmsg("cannot use publication column list for relation \"%s.%s\"",
+                          get_namespace_name(RelationGetNamespace(pri->relation)),
+                          RelationGetRelationName(pri->relation)),
+                   errdetail("Column list cannot be specified if any schema is part of the publication or specified in the list."));
+
        /*
         * If the publication doesn't publish changes via the root partitioned
         * table, the partition's column list will be used. So disallow using
@@ -858,13 +837,11 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
            List       *rels;
 
            rels = OpenTableList(relations);
-           CheckObjSchemaNotAlreadyInPublication(rels, schemaidlist,
-                                                 PUBLICATIONOBJ_TABLE);
-
            TransformPubWhereClauses(rels, pstate->p_sourcetext,
                                     publish_via_partition_root);
 
            CheckPubRelationColumnList(rels, pstate->p_sourcetext,
+                                      schemaidlist != NIL,
                                       publish_via_partition_root);
 
            PublicationAddTables(puboid, rels, true, NULL);
@@ -1110,8 +1087,8 @@ InvalidatePublicationRels(List *relids)
  */
 static void
 AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup,
-                      List *tables, List *schemaidlist,
-                      const char *queryString)
+                      List *tables, const char *queryString,
+                      bool publish_schema)
 {
    List       *rels = NIL;
    Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup);
@@ -1129,19 +1106,12 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup,
 
    if (stmt->action == AP_AddObjects)
    {
-       List       *schemas = NIL;
-
-       /*
-        * Check if the relation is member of the existing schema in the
-        * publication or member of the schema list specified.
-        */
-       schemas = list_concat_copy(schemaidlist, GetPublicationSchemas(pubid));
-       CheckObjSchemaNotAlreadyInPublication(rels, schemas,
-                                             PUBLICATIONOBJ_TABLE);
-
        TransformPubWhereClauses(rels, queryString, pubform->pubviaroot);
 
-       CheckPubRelationColumnList(rels, queryString, pubform->pubviaroot);
+       publish_schema |= is_schema_publication(pubid);
+
+       CheckPubRelationColumnList(rels, queryString, publish_schema,
+                                  pubform->pubviaroot);
 
        PublicationAddTables(pubid, rels, false, stmt);
    }
@@ -1154,12 +1124,10 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup,
        List       *delrels = NIL;
        ListCell   *oldlc;
 
-       CheckObjSchemaNotAlreadyInPublication(rels, schemaidlist,
-                                             PUBLICATIONOBJ_TABLE);
-
        TransformPubWhereClauses(rels, queryString, pubform->pubviaroot);
 
-       CheckPubRelationColumnList(rels, queryString, pubform->pubviaroot);
+       CheckPubRelationColumnList(rels, queryString, publish_schema,
+                                  pubform->pubviaroot);
 
        /*
         * To recreate the relation list for the publication, look for
@@ -1308,16 +1276,35 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt,
    LockSchemaList(schemaidlist);
    if (stmt->action == AP_AddObjects)
    {
-       List       *rels;
+       ListCell   *lc;
        List       *reloids;
 
        reloids = GetPublicationRelations(pubform->oid, PUBLICATION_PART_ROOT);
-       rels = OpenRelIdList(reloids);
 
-       CheckObjSchemaNotAlreadyInPublication(rels, schemaidlist,
-                                             PUBLICATIONOBJ_TABLES_IN_SCHEMA);
+       foreach(lc, reloids)
+       {
+           HeapTuple   coltuple;
+
+           coltuple = SearchSysCache2(PUBLICATIONRELMAP,
+                                      ObjectIdGetDatum(lfirst_oid(lc)),
+                                      ObjectIdGetDatum(pubform->oid));
+
+           if (!HeapTupleIsValid(coltuple))
+               continue;
+
+           /*
+            * Disallow adding schema if column list is already part of the
+            * publication. See CheckPubRelationColumnList.
+            */
+           if (!heap_attisnull(coltuple, Anum_pg_publication_rel_prattrs, NULL))
+               ereport(ERROR,
+                       errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                       errmsg("cannot add schema to the publication"),
+                       errdetail("Schema cannot be added if any table that specifies column list is already part of the publication."));
+
+           ReleaseSysCache(coltuple);
+       }
 
-       CloseTableList(rels);
        PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt);
    }
    else if (stmt->action == AP_DropObjects)
@@ -1429,14 +1416,7 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt)
 
        heap_freetuple(tup);
 
-       /*
-        * Lock the publication so nobody else can do anything with it. This
-        * prevents concurrent alter to add table(s) that were already going
-        * to become part of the publication by adding corresponding schema(s)
-        * via this command and similarly it will prevent the concurrent
-        * addition of schema(s) for which there is any corresponding table
-        * being added by this command.
-        */
+       /* Lock the publication so nobody else can do anything with it. */
        LockDatabaseObject(PublicationRelationId, pubid, 0,
                           AccessExclusiveLock);
 
@@ -1453,8 +1433,8 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt)
                    errmsg("publication \"%s\" does not exist",
                           stmt->pubname));
 
-       AlterPublicationTables(stmt, tup, relations, schemaidlist,
-                              pstate->p_sourcetext);
+       AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext,
+                              schemaidlist != NIL);
        AlterPublicationSchemas(stmt, tup, schemaidlist);
    }
 
@@ -1569,32 +1549,6 @@ RemovePublicationSchemaById(Oid psoid)
    table_close(rel, RowExclusiveLock);
 }
 
-/*
- * Open relations specified by a relid list.
- * The returned tables are locked in ShareUpdateExclusiveLock mode in order to
- * add them to a publication.
- */
-static List *
-OpenRelIdList(List *relids)
-{
-   ListCell   *lc;
-   List       *rels = NIL;
-
-   foreach(lc, relids)
-   {
-       PublicationRelInfo *pub_rel;
-       Oid         relid = lfirst_oid(lc);
-       Relation    rel = table_open(relid,
-                                    ShareUpdateExclusiveLock);
-
-       pub_rel = palloc(sizeof(PublicationRelInfo));
-       pub_rel->relation = rel;
-       rels = lappend(rels, pub_rel);
-   }
-
-   return rels;
-}
-
 /*
  * Open relations specified by a PublicationTable list.
  * The returned tables are locked in ShareUpdateExclusiveLock mode in order to
index 6c52edd9bee3dbfc6c64cf0f6d765037ad00d027..7d8a75d23c2d7419546642ab436a08b1344493ef 100644 (file)
@@ -16409,33 +16409,6 @@ AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
    newrv = makeRangeVar(stmt->newschema, RelationGetRelationName(rel), -1);
    nspOid = RangeVarGetAndCheckCreationNamespace(newrv, NoLock, NULL);
 
-   /*
-    * Check that setting the relation to a different schema won't result in a
-    * publication having both a schema and the same schema's table, as this
-    * is not supported.
-    */
-   if (stmt->objectType == OBJECT_TABLE)
-   {
-       ListCell   *lc;
-       List       *schemaPubids = GetSchemaPublications(nspOid);
-       List       *relPubids = GetRelationPublications(RelationGetRelid(rel));
-
-       foreach(lc, relPubids)
-       {
-           Oid         pubid = lfirst_oid(lc);
-
-           if (list_member_oid(schemaPubids, pubid))
-               ereport(ERROR,
-                       errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                       errmsg("cannot move table \"%s\" to schema \"%s\"",
-                              RelationGetRelationName(rel), stmt->newschema),
-                       errdetail("The schema \"%s\" and same schema's table \"%s\" cannot be part of the same publication \"%s\".",
-                                 stmt->newschema,
-                                 RelationGetRelationName(rel),
-                                 get_publication_name(pubid, false)));
-       }
-   }
-
    /* common checks on switching namespaces */
    CheckSetNamespace(oldNspOid, nspOid);
 
index 92bbffbe7cd24654f353bcc5b05e6be8139827fa..2ecaa5b9074bebd719949a74f202b91411d0abbe 100644 (file)
@@ -854,6 +854,7 @@ pgoutput_row_filter_init(PGOutputData *data, List *publications,
    MemoryContext oldctx;
    int         idx;
    bool        has_filter = true;
+   Oid         schemaid = get_rel_namespace(entry->publish_as_relid);
 
    /*
     * Find if there are any row filters for this relation. If there are, then
@@ -867,26 +868,26 @@ pgoutput_row_filter_init(PGOutputData *data, List *publications,
     * are multiple lists (one for each operation) to which row filters will
     * be appended.
     *
-    * FOR ALL TABLES implies "don't use row filter expression" so it takes
-    * precedence.
+    * FOR ALL TABLES and FOR TABLES IN SCHEMA implies "don't use row
+    * filter expression" so it takes precedence.
     */
    foreach(lc, publications)
    {
        Publication *pub = lfirst(lc);
        HeapTuple   rftuple = NULL;
        Datum       rfdatum = 0;
-       bool        pub_no_filter = false;
+       bool        pub_no_filter = true;
 
-       if (pub->alltables)
-       {
-           /*
-            * If the publication is FOR ALL TABLES then it is treated the
-            * same as if this table has no row filters (even if for other
-            * publications it does).
-            */
-           pub_no_filter = true;
-       }
-       else
+       /*
+        * If the publication is FOR ALL TABLES, or the publication includes a
+        * FOR TABLES IN SCHEMA where the table belongs to the referred
+        * schema, then it is treated the same as if there are no row filters
+        * (even if other publications have a row filter).
+        */
+       if (!pub->alltables &&
+           !SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP,
+                                  ObjectIdGetDatum(schemaid),
+                                  ObjectIdGetDatum(pub->oid)))
        {
            /*
             * Check for the presence of a row filter in this publication.
@@ -902,10 +903,6 @@ pgoutput_row_filter_init(PGOutputData *data, List *publications,
                                          Anum_pg_publication_rel_prqual,
                                          &pub_no_filter);
            }
-           else
-           {
-               pub_no_filter = true;
-           }
        }
 
        if (pub_no_filter)
index 68fe9e2b73340303645bf60ad9e76b0333c20268..a869321cdfc3fc2b4b91082d5a91d891f506b0bd 100644 (file)
@@ -2564,6 +2564,20 @@ my %tests = (
        like => { %full_runs, section_post_data => 1, },
    },
 
+   'ALTER PUBLICATION pub3 ADD TABLE test_table' => {
+       create_order => 51,
+       create_sql =>
+         'ALTER PUBLICATION pub3 ADD TABLE dump_test.test_table;',
+       regexp => qr/^
+           \QALTER PUBLICATION pub3 ADD TABLE ONLY dump_test.test_table;\E
+           /xm,
+       like   => { %full_runs, section_post_data => 1, },
+       unlike => {
+           exclude_dump_test_schema => 1,
+           exclude_test_table       => 1,
+       },
+   },
+
    'ALTER PUBLICATION pub4 ADD TABLE test_table WHERE (col1 > 0);' => {
        create_order => 51,
        create_sql =>
index 656dac6801f6cd364a0c709a336a774261833061..346f594ad026e65278e23d57e8df9c0a225c6e9d 100644 (file)
@@ -4595,10 +4595,16 @@ create table alter1.t1 (a int);
 set client_min_messages = 'ERROR';
 create publication pub1 for table alter1.t1, tables in schema alter2;
 reset client_min_messages;
-alter table alter1.t1 set schema alter2; -- should fail
-ERROR:  cannot move table "t1" to schema "alter2"
-DETAIL:  The schema "alter2" and same schema's table "t1" cannot be part of the same publication "pub1".
+alter table alter1.t1 set schema alter2;
+\d+ alter2.t1
+                                    Table "alter2.t1"
+ Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
+--------+---------+-----------+----------+---------+---------+--------------+-------------
+ a      | integer |           |          |         | plain   |              | 
+Publications:
+    "pub1"
+
 drop publication pub1;
 drop schema alter1 cascade;
-NOTICE:  drop cascades to table alter1.t1
 drop schema alter2 cascade;
+NOTICE:  drop cascades to table alter2.t1
index ce63511b94e2a3eb099dae7a1feea1e9ad301eed..c0c0e7d92e01953c0ad45fc0d51bab7437fb82a6 100644 (file)
@@ -118,15 +118,42 @@ Tables from schemas:
 
 SET client_min_messages = 'ERROR';
 CREATE PUBLICATION testpub_forschema FOR TABLES IN SCHEMA pub_test;
-RESET client_min_messages;
--- fail - can't create publication with schema and table of the same schema
+-- should be able to create publication with schema and table of the same
+-- schema
 CREATE PUBLICATION testpub_for_tbl_schema FOR TABLES IN SCHEMA pub_test, TABLE pub_test.testpub_nopk;
-ERROR:  cannot add relation "pub_test.testpub_nopk" to publication
-DETAIL:  Table's schema "pub_test" is already part of the publication or part of the specified schema list.
--- fail - can't add a table of the same schema to the schema publication
+RESET client_min_messages;
+\dRp+ testpub_for_tbl_schema
+                             Publication testpub_for_tbl_schema
+          Owner           | All tables | Inserts | Updates | Deletes | Truncates | Via root 
+--------------------------+------------+---------+---------+---------+-----------+----------
+ regress_publication_user | f          | t       | t       | t       | t         | f
+Tables:
+    "pub_test.testpub_nopk"
+Tables from schemas:
+    "pub_test"
+
+-- should be able to add a table of the same schema to the schema publication
 ALTER PUBLICATION testpub_forschema ADD TABLE pub_test.testpub_nopk;
-ERROR:  cannot add relation "pub_test.testpub_nopk" to publication
-DETAIL:  Table's schema "pub_test" is already part of the publication or part of the specified schema list.
+\dRp+ testpub_forschema
+                               Publication testpub_forschema
+          Owner           | All tables | Inserts | Updates | Deletes | Truncates | Via root 
+--------------------------+------------+---------+---------+---------+-----------+----------
+ regress_publication_user | f          | t       | t       | t       | t         | f
+Tables:
+    "pub_test.testpub_nopk"
+Tables from schemas:
+    "pub_test"
+
+-- should be able to drop the table
+ALTER PUBLICATION testpub_forschema DROP TABLE pub_test.testpub_nopk;
+\dRp+ testpub_forschema
+                               Publication testpub_forschema
+          Owner           | All tables | Inserts | Updates | Deletes | Truncates | Via root 
+--------------------------+------------+---------+---------+---------+-----------+----------
+ regress_publication_user | f          | t       | t       | t       | t         | f
+Tables from schemas:
+    "pub_test"
+
 -- fail - can't drop a table from the schema publication which isn't in the
 -- publication
 ALTER PUBLICATION testpub_forschema DROP TABLE pub_test.testpub_nopk;
@@ -166,7 +193,7 @@ Publications:
 (1 row)
 
 DROP TABLE testpub_tbl2;
-DROP PUBLICATION testpub_foralltables, testpub_fortable, testpub_forschema;
+DROP PUBLICATION testpub_foralltables, testpub_fortable, testpub_forschema, testpub_for_tbl_schema;
 CREATE TABLE testpub_tbl3 (a int);
 CREATE TABLE testpub_tbl3a (b text) INHERITS (testpub_tbl3);
 SET client_min_messages = 'ERROR';
@@ -466,10 +493,19 @@ ERROR:  cannot use a WHERE clause when removing a table from a publication
 -- fail - cannot ALTER SET table which is a member of a pre-existing schema
 SET client_min_messages = 'ERROR';
 CREATE PUBLICATION testpub6 FOR TABLES IN SCHEMA testpub_rf_schema2;
+-- should be able to set publication with schema and table of the same schema
 ALTER PUBLICATION testpub6 SET TABLES IN SCHEMA testpub_rf_schema2, TABLE testpub_rf_schema2.testpub_rf_tbl6 WHERE (i < 99);
-ERROR:  cannot add relation "testpub_rf_schema2.testpub_rf_tbl6" to publication
-DETAIL:  Table's schema "testpub_rf_schema2" is already part of the publication or part of the specified schema list.
 RESET client_min_messages;
+\dRp+ testpub6
+                                    Publication testpub6
+          Owner           | All tables | Inserts | Updates | Deletes | Truncates | Via root 
+--------------------------+------------+---------+---------+---------+-----------+----------
+ regress_publication_user | f          | t       | t       | t       | t         | f
+Tables:
+    "testpub_rf_schema2.testpub_rf_tbl6" WHERE (i < 99)
+Tables from schemas:
+    "testpub_rf_schema2"
+
 DROP TABLE testpub_rf_tbl1;
 DROP TABLE testpub_rf_tbl2;
 DROP TABLE testpub_rf_tbl3;
@@ -812,8 +848,40 @@ ALTER TABLE testpub_tbl8_0 REPLICA IDENTITY FULL;
 UPDATE testpub_tbl8 SET a = 1;
 ERROR:  cannot update table "testpub_tbl8_0"
 DETAIL:  Column list used by the publication does not cover the replica identity.
+-- test that using column list for table is disallowed if any schemas are
+-- part of the publication
+SET client_min_messages = 'ERROR';
+-- failure - cannot use column list and schema together
+CREATE PUBLICATION testpub_tbl9 FOR TABLES IN SCHEMA public, TABLE public.testpub_tbl7(a);
+ERROR:  cannot use publication column list for relation "public.testpub_tbl7"
+DETAIL:  Column list cannot be specified if any schema is part of the publication or specified in the list.
+-- ok - only publish schema
+CREATE PUBLICATION testpub_tbl9 FOR TABLES IN SCHEMA public;
+-- failure - add a table with column list when there is already a schema in the
+-- publication
+ALTER PUBLICATION testpub_tbl9 ADD TABLE public.testpub_tbl7(a);
+ERROR:  cannot use publication column list for relation "public.testpub_tbl7"
+DETAIL:  Column list cannot be specified if any schema is part of the publication or specified in the list.
+-- ok - only publish table with column list
+ALTER PUBLICATION testpub_tbl9 SET TABLE public.testpub_tbl7(a);
+-- failure - specify a schema when there is already a column list in the
+-- publication
+ALTER PUBLICATION testpub_tbl9 ADD TABLES IN SCHEMA public;
+ERROR:  cannot add schema to the publication
+DETAIL:  Schema cannot be added if any table that specifies column list is already part of the publication.
+-- failure - cannot SET column list and schema together
+ALTER PUBLICATION testpub_tbl9 SET TABLES IN SCHEMA public, TABLE public.testpub_tbl7(a);
+ERROR:  cannot use publication column list for relation "public.testpub_tbl7"
+DETAIL:  Column list cannot be specified if any schema is part of the publication or specified in the list.
+-- ok - drop table
+ALTER PUBLICATION testpub_tbl9 DROP TABLE public.testpub_tbl7;
+-- failure - cannot ADD column list and schema together
+ALTER PUBLICATION testpub_tbl9 ADD TABLES IN SCHEMA public, TABLE public.testpub_tbl7(a);
+ERROR:  cannot use publication column list for relation "public.testpub_tbl7"
+DETAIL:  Column list cannot be specified if any schema is part of the publication or specified in the list.
+RESET client_min_messages;
 DROP TABLE testpub_tbl5, testpub_tbl6, testpub_tbl7, testpub_tbl8, testpub_tbl8_1;
-DROP PUBLICATION testpub_table_ins, testpub_fortable, testpub_fortable_insert, testpub_col_list;
+DROP PUBLICATION testpub_table_ins, testpub_fortable, testpub_fortable_insert, testpub_col_list, testpub_tbl9;
 -- ======================================================
 -- Test combination of column list and row filter
 SET client_min_messages = 'ERROR';
index 8846c14dbb2cc069eeff3e6e1bc54d953db367e7..9f773aeeb94c4436bd1334f55ea993cd20ad5e06 100644 (file)
@@ -3031,7 +3031,8 @@ create table alter1.t1 (a int);
 set client_min_messages = 'ERROR';
 create publication pub1 for table alter1.t1, tables in schema alter2;
 reset client_min_messages;
-alter table alter1.t1 set schema alter2; -- should fail
+alter table alter1.t1 set schema alter2;
+\d+ alter2.t1
 drop publication pub1;
 drop schema alter1 cascade;
 drop schema alter2 cascade;
index 231c9d5c53c0d407d902983827d6596ff05256ab..a47c5939d58e384dadff0242d50a564e0a963748 100644 (file)
@@ -73,11 +73,20 @@ ALTER PUBLICATION testpub_fortable SET TABLES IN SCHEMA pub_test;
 
 SET client_min_messages = 'ERROR';
 CREATE PUBLICATION testpub_forschema FOR TABLES IN SCHEMA pub_test;
-RESET client_min_messages;
--- fail - can't create publication with schema and table of the same schema
+-- should be able to create publication with schema and table of the same
+-- schema
 CREATE PUBLICATION testpub_for_tbl_schema FOR TABLES IN SCHEMA pub_test, TABLE pub_test.testpub_nopk;
--- fail - can't add a table of the same schema to the schema publication
+RESET client_min_messages;
+\dRp+ testpub_for_tbl_schema
+
+-- should be able to add a table of the same schema to the schema publication
 ALTER PUBLICATION testpub_forschema ADD TABLE pub_test.testpub_nopk;
+\dRp+ testpub_forschema
+
+-- should be able to drop the table
+ALTER PUBLICATION testpub_forschema DROP TABLE pub_test.testpub_nopk;
+\dRp+ testpub_forschema
+
 -- fail - can't drop a table from the schema publication which isn't in the
 -- publication
 ALTER PUBLICATION testpub_forschema DROP TABLE pub_test.testpub_nopk;
@@ -90,7 +99,7 @@ SELECT pubname, puballtables FROM pg_publication WHERE pubname = 'testpub_forall
 \dRp+ testpub_foralltables
 
 DROP TABLE testpub_tbl2;
-DROP PUBLICATION testpub_foralltables, testpub_fortable, testpub_forschema;
+DROP PUBLICATION testpub_foralltables, testpub_fortable, testpub_forschema, testpub_for_tbl_schema;
 
 CREATE TABLE testpub_tbl3 (a int);
 CREATE TABLE testpub_tbl3a (b text) INHERITS (testpub_tbl3);
@@ -242,8 +251,10 @@ ALTER PUBLICATION testpub5 DROP TABLE testpub_rf_tbl1 WHERE (e < 27);
 -- fail - cannot ALTER SET table which is a member of a pre-existing schema
 SET client_min_messages = 'ERROR';
 CREATE PUBLICATION testpub6 FOR TABLES IN SCHEMA testpub_rf_schema2;
+-- should be able to set publication with schema and table of the same schema
 ALTER PUBLICATION testpub6 SET TABLES IN SCHEMA testpub_rf_schema2, TABLE testpub_rf_schema2.testpub_rf_tbl6 WHERE (i < 99);
 RESET client_min_messages;
+\dRp+ testpub6
 
 DROP TABLE testpub_rf_tbl1;
 DROP TABLE testpub_rf_tbl2;
@@ -525,8 +536,31 @@ UPDATE testpub_tbl8 SET a = 1;
 ALTER TABLE testpub_tbl8_0 REPLICA IDENTITY FULL;
 UPDATE testpub_tbl8 SET a = 1;
 
+-- test that using column list for table is disallowed if any schemas are
+-- part of the publication
+SET client_min_messages = 'ERROR';
+-- failure - cannot use column list and schema together
+CREATE PUBLICATION testpub_tbl9 FOR TABLES IN SCHEMA public, TABLE public.testpub_tbl7(a);
+-- ok - only publish schema
+CREATE PUBLICATION testpub_tbl9 FOR TABLES IN SCHEMA public;
+-- failure - add a table with column list when there is already a schema in the
+-- publication
+ALTER PUBLICATION testpub_tbl9 ADD TABLE public.testpub_tbl7(a);
+-- ok - only publish table with column list
+ALTER PUBLICATION testpub_tbl9 SET TABLE public.testpub_tbl7(a);
+-- failure - specify a schema when there is already a column list in the
+-- publication
+ALTER PUBLICATION testpub_tbl9 ADD TABLES IN SCHEMA public;
+-- failure - cannot SET column list and schema together
+ALTER PUBLICATION testpub_tbl9 SET TABLES IN SCHEMA public, TABLE public.testpub_tbl7(a);
+-- ok - drop table
+ALTER PUBLICATION testpub_tbl9 DROP TABLE public.testpub_tbl7;
+-- failure - cannot ADD column list and schema together
+ALTER PUBLICATION testpub_tbl9 ADD TABLES IN SCHEMA public, TABLE public.testpub_tbl7(a);
+RESET client_min_messages;
+
 DROP TABLE testpub_tbl5, testpub_tbl6, testpub_tbl7, testpub_tbl8, testpub_tbl8_1;
-DROP PUBLICATION testpub_table_ins, testpub_fortable, testpub_fortable_insert, testpub_col_list;
+DROP PUBLICATION testpub_table_ins, testpub_fortable, testpub_fortable_insert, testpub_col_list, testpub_tbl9;
 -- ======================================================
 
 -- Test combination of column list and row filter
index ba07ed37b77d32ad264d0e1629e79572897ca42e..da52289dde83c7555f30db5a22c123e8793fcdac 100644 (file)
@@ -119,7 +119,7 @@ $node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_x FOR TABLE schema_rf_x.tab_rf_x WHERE (x > 10)"
 );
 $node_publisher->safe_psql('postgres',
-   "CREATE PUBLICATION tap_pub_allinschema FOR TABLES IN SCHEMA schema_rf_x"
+   "CREATE PUBLICATION tap_pub_allinschema FOR TABLES IN SCHEMA schema_rf_x, TABLE schema_rf_x.tab_rf_x WHERE (x > 10)"
 );
 $node_publisher->safe_psql('postgres',
    "ALTER PUBLICATION tap_pub_allinschema ADD TABLE public.tab_rf_partition WHERE (x > 10)"