Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Preserve clustered index after rewrites with ALTER TABLE
authorMichael Paquier <michael@paquier.xyz>
Mon, 6 Apr 2020 02:05:54 +0000 (11:05 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 6 Apr 2020 02:05:54 +0000 (11:05 +0900)
A table rewritten by ALTER TABLE would lose tracking of an index usable
for CLUSTER.  This setting is tracked by pg_index.indisclustered and is
controlled by ALTER TABLE, so some extra work was needed to restore it
properly.  Note that ALTER TABLE only marks the index that can be used
for clustering, and does not do the actual operation.

Author: Amit Langote, Justin Pryzby
Reviewed-by: Ibrar Ahmed, Michael Paquier
Discussion: https://postgr.es/m/20200202161718.GI13621@telsasoft.com
Backpatch-through: 9.5

src/backend/commands/tablecmds.c
src/backend/utils/cache/lsyscache.c
src/include/utils/lsyscache.h
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 3a2e2ecc4f60b56a8a46224ec1539c0a6c8db074..28996f607cc49a01e92355f6e52b492fae7b7537 100644 (file)
@@ -176,6 +176,7 @@ typedef struct AlteredTableInfo
    List       *changedIndexOids;   /* OIDs of indexes to rebuild */
    List       *changedIndexDefs;   /* string definitions of same */
    char       *replicaIdentityIndex;   /* index to reset as REPLICA IDENTITY */
+   char       *clusterOnIndex; /* index to use for CLUSTER */
 } AlteredTableInfo;
 
 /* Struct describing one new constraint to check in Phase 3 scan */
@@ -10331,6 +10332,21 @@ RememberReplicaIdentityForRebuilding(Oid indoid, AlteredTableInfo *tab)
    tab->replicaIdentityIndex = get_rel_name(indoid);
 }
 
+/*
+ * Subroutine for ATExecAlterColumnType: remember any clustered index.
+ */
+static void
+RememberClusterOnForRebuilding(Oid indoid, AlteredTableInfo *tab)
+{
+   if (!get_index_isclustered(indoid))
+       return;
+
+   if (tab->clusterOnIndex)
+       elog(ERROR, "relation %u has multiple clustered indexes", tab->relid);
+
+   tab->clusterOnIndex = get_rel_name(indoid);
+}
+
 /*
  * Subroutine for ATExecAlterColumnType: remember that a constraint needs
  * to be rebuilt (which we might already know).
@@ -10356,9 +10372,18 @@ RememberConstraintForRebuilding(Oid conoid, AlteredTableInfo *tab)
        tab->changedConstraintDefs = lappend(tab->changedConstraintDefs,
                                             defstring);
 
+       /*
+        * For the index of a constraint, if any, remember if it is used for
+        * the table's replica identity or if it is a clustered index, so that
+        * ATPostAlterTypeCleanup() can queue up commands necessary to restore
+        * those properties.
+        */
        indoid = get_constraint_index(conoid);
        if (OidIsValid(indoid))
+       {
            RememberReplicaIdentityForRebuilding(indoid, tab);
+           RememberClusterOnForRebuilding(indoid, tab);
+       }
    }
 }
 
@@ -10402,7 +10427,13 @@ RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab)
            tab->changedIndexDefs = lappend(tab->changedIndexDefs,
                                            defstring);
 
+           /*
+            * Remember if this index is used for the table's replica identity
+            * or if it is a clustered index, so that ATPostAlterTypeCleanup()
+            * can queue up commands necessary to restore those properties.
+            */
            RememberReplicaIdentityForRebuilding(indoid, tab);
+           RememberClusterOnForRebuilding(indoid, tab);
        }
    }
 }
@@ -10635,6 +10666,21 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
            lappend(tab->subcmds[AT_PASS_OLD_CONSTR], cmd);
    }
 
+   /*
+    * Queue up command to restore marking of index used for cluster.
+    */
+   if (tab->clusterOnIndex)
+   {
+       AlterTableCmd *cmd = makeNode(AlterTableCmd);
+
+       cmd->subtype = AT_ClusterOn;
+       cmd->name = tab->clusterOnIndex;
+
+       /* do it after indexes and constraints */
+       tab->subcmds[AT_PASS_OLD_CONSTR] =
+           lappend(tab->subcmds[AT_PASS_OLD_CONSTR], cmd);
+   }
+
    /*
     * It should be okay to use DROP_RESTRICT here, since nothing else should
     * be depending on these objects.
index 047a8ced02feb529756a850cde9f59a244930e64..913cad35105782aa315c8473efbdf55f61a651e3 100644 (file)
@@ -3181,3 +3181,26 @@ get_index_isreplident(Oid index_oid)
 
    return result;
 }
+
+/*
+ * get_index_isclustered
+ *
+ *     Given the index OID, return pg_index.indisclustered.
+ */
+bool
+get_index_isclustered(Oid index_oid)
+{
+   bool        isclustered;
+   HeapTuple   tuple;
+   Form_pg_index rd_index;
+
+   tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
+   if (!HeapTupleIsValid(tuple))
+       elog(ERROR, "cache lookup failed for index %u", index_oid);
+
+   rd_index = (Form_pg_index) GETSTRUCT(tuple);
+   isclustered = rd_index->indisclustered;
+   ReleaseSysCache(tuple);
+
+   return isclustered;
+}
index 3832785c0fa21de8a304fd7f91a65a6d0ccea9d6..21ec4cbd355c2f7cac08cf793ee8b8d98659d2f4 100644 (file)
@@ -179,6 +179,7 @@ extern char *get_namespace_name_or_temp(Oid nspid);
 extern Oid get_range_subtype(Oid rangeOid);
 extern Oid get_range_collation(Oid rangeOid);
 extern bool    get_index_isreplident(Oid index_oid);
+extern bool get_index_isclustered(Oid index_oid);
 
 #define type_is_array(typid)  (get_element_type(typid) != InvalidOid)
 /* type_is_array_domain accepts both plain arrays and domains over arrays */
index 4e9c15bbd448fe07e932fb8b4a76c4b4a36e3a34..a65f706deeec9f75dbaba059c8f9a52d6cc419cd 100644 (file)
@@ -4212,3 +4212,51 @@ create trigger xtrig
 update bar1 set a = a + 1;
 INFO:  a=1, b=1
 /* End test case for bug #16242 */
+-- Test that ALTER TABLE rewrite preserves a clustered index
+-- for normal indexes and indexes on constraints.
+create table alttype_cluster (a int);
+alter table alttype_cluster add primary key (a);
+create index alttype_cluster_ind on alttype_cluster (a);
+alter table alttype_cluster cluster on alttype_cluster_ind;
+-- Normal index remains clustered.
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+      indexrelid      | indisclustered 
+----------------------+----------------
+ alttype_cluster_ind  | t
+ alttype_cluster_pkey | f
+(2 rows)
+
+alter table alttype_cluster alter a type bigint;
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+      indexrelid      | indisclustered 
+----------------------+----------------
+ alttype_cluster_ind  | t
+ alttype_cluster_pkey | f
+(2 rows)
+
+-- Constraint index remains clustered.
+alter table alttype_cluster cluster on alttype_cluster_pkey;
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+      indexrelid      | indisclustered 
+----------------------+----------------
+ alttype_cluster_ind  | f
+ alttype_cluster_pkey | t
+(2 rows)
+
+alter table alttype_cluster alter a type int;
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+      indexrelid      | indisclustered 
+----------------------+----------------
+ alttype_cluster_ind  | f
+ alttype_cluster_pkey | t
+(2 rows)
+
+drop table alttype_cluster;
index cfc09e859baddbd53177bb7894c8fa40cb8a71f7..9cdb92670e010f445bc223eca170fa9647bc6089 100644 (file)
@@ -2802,3 +2802,28 @@ create trigger xtrig
 update bar1 set a = a + 1;
 
 /* End test case for bug #16242 */
+
+-- Test that ALTER TABLE rewrite preserves a clustered index
+-- for normal indexes and indexes on constraints.
+create table alttype_cluster (a int);
+alter table alttype_cluster add primary key (a);
+create index alttype_cluster_ind on alttype_cluster (a);
+alter table alttype_cluster cluster on alttype_cluster_ind;
+-- Normal index remains clustered.
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+alter table alttype_cluster alter a type bigint;
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+-- Constraint index remains clustered.
+alter table alttype_cluster cluster on alttype_cluster_pkey;
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+alter table alttype_cluster alter a type int;
+select indexrelid::regclass, indisclustered from pg_index
+  where indrelid = 'alttype_cluster'::regclass
+  order by indexrelid::regclass::text;
+drop table alttype_cluster;