Block ALTER TABLE .. DROP NOT NULL on columns in replica identity index
authorMichael Paquier <[email protected]>
Thu, 25 Nov 2021 06:05:37 +0000 (15:05 +0900)
committerMichael Paquier <[email protected]>
Thu, 25 Nov 2021 06:05:37 +0000 (15:05 +0900)
Replica identities that depend directly on an index rely on a set of
properties, one of them being that all the columns defined in this index
have to be marked as NOT NULL.  There was a hole in the logic with ALTER
TABLE DROP NOT NULL, where it was possible to remove the NOT NULL
property of a column part of an index used as replica identity, so block
it to avoid problems with logical decoding down the road.

The same check was already done columns part of a primary key, so the
fix is straight-forward.

Author: Haiying Tang, Hou Zhijie
Reviewed-by: Dilip Kumar, Michael Paquier
Discussion: https://postgr.es/m/OS0PR01MB6113338C102BEE8B2FFC5BD9FB619@OS0PR01MB6113.jpnprd01.prod.outlook.com
Backpatch-through: 10

src/backend/commands/tablecmds.c
src/test/regress/expected/replica_identity.out
src/test/regress/sql/replica_identity.sql

index c35bf678217281f78e578c762e94320b7eb477e9..ef88a10f0174abc7ce0cc3bd64b35f2e3a2c8a44 100644 (file)
@@ -5707,7 +5707,8 @@ ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode)
                        colName, RelationGetRelationName(rel))));
 
    /*
-    * Check that the attribute is not in a primary key
+    * Check that the attribute is not in a primary key or in an index used as
+    * a replica identity.
     *
     * Note: we'll throw error even if the pkey index is not valid.
     */
@@ -5727,20 +5728,32 @@ ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode)
            elog(ERROR, "cache lookup failed for index %u", indexoid);
        indexStruct = (Form_pg_index) GETSTRUCT(indexTuple);
 
-       /* If the index is not a primary key, skip the check */
-       if (indexStruct->indisprimary)
+       /*
+        * If the index is not a primary key or an index used as replica
+        * identity, skip the check.
+        */
+       if (indexStruct->indisprimary || indexStruct->indisreplident)
        {
            /*
-            * Loop over each attribute in the primary key and see if it
-            * matches the to-be-altered attribute
+            * Loop over each attribute in the primary key or the index used
+            * as replica identity and see if it matches the to-be-altered
+            * attribute.
             */
            for (i = 0; i < indexStruct->indnatts; i++)
            {
                if (indexStruct->indkey.values[i] == attnum)
-                   ereport(ERROR,
-                           (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
-                            errmsg("column \"%s\" is in a primary key",
-                                   colName)));
+               {
+                   if (indexStruct->indisprimary)
+                       ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+                                errmsg("column \"%s\" is in a primary key",
+                                       colName)));
+                   else
+                       ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+                                errmsg("column \"%s\" is in index used as replica identity",
+                                       colName)));
+               }
            }
        }
 
index 65c7c9ac9f803ffabc55dd3a2ba54dba3e1f0dd3..3fc1943f2c472d0eca3775dc2c7ba8442196b7c9 100644 (file)
@@ -230,6 +230,10 @@ ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
 Indexes:
     "test_replica_identity3_id_key" UNIQUE, btree (id) REPLICA IDENTITY
 
+-- ALTER TABLE DROP NOT NULL is not allowed for columns part of an index
+-- used as replica identity.
+ALTER TABLE test_replica_identity3 ALTER COLUMN id DROP NOT NULL;
+ERROR:  column "id" is in index used as replica identity
 DROP TABLE test_replica_identity;
 DROP TABLE test_replica_identity2;
 DROP TABLE test_replica_identity3;
index 5277ff39cd3913960f1c4781af0436b001d0eb7b..f2c848b980041bf9e97ecbf1d910e5f4daa4474f 100644 (file)
@@ -98,6 +98,10 @@ ALTER TABLE test_replica_identity3 REPLICA IDENTITY USING INDEX test_replica_ide
 ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
 \d test_replica_identity3
 
+-- ALTER TABLE DROP NOT NULL is not allowed for columns part of an index
+-- used as replica identity.
+ALTER TABLE test_replica_identity3 ALTER COLUMN id DROP NOT NULL;
+
 DROP TABLE test_replica_identity;
 DROP TABLE test_replica_identity2;
 DROP TABLE test_replica_identity3;