1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-05 09:19:17 +03:00

Preserve clustered index after rewrites with ALTER TABLE

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
This commit is contained in:
Michael Paquier 2020-04-06 11:06:04 +09:00
parent f7f55d02c2
commit 61a161fe44
5 changed files with 143 additions and 0 deletions

View File

@ -169,6 +169,7 @@ typedef struct AlteredTableInfo
List *changedIndexOids; /* OIDs of indexes to rebuild */ List *changedIndexOids; /* OIDs of indexes to rebuild */
List *changedIndexDefs; /* string definitions of same */ List *changedIndexDefs; /* string definitions of same */
char *replicaIdentityIndex; /* index to reset as REPLICA IDENTITY */ char *replicaIdentityIndex; /* index to reset as REPLICA IDENTITY */
char *clusterOnIndex; /* index to use for CLUSTER */
} AlteredTableInfo; } AlteredTableInfo;
/* Struct describing one new constraint to check in Phase 3 scan */ /* Struct describing one new constraint to check in Phase 3 scan */
@ -8613,6 +8614,21 @@ RememberReplicaIdentityForRebuilding(Oid indoid, AlteredTableInfo *tab)
tab->replicaIdentityIndex = get_rel_name(indoid); 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 * Subroutine for ATExecAlterColumnType: remember that a constraint needs
* to be rebuilt (which we might already know). * to be rebuilt (which we might already know).
@ -8657,9 +8673,18 @@ RememberConstraintForRebuilding(Oid conoid, AlteredTableInfo *tab,
defstring); 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); indoid = get_constraint_index(conoid);
if (OidIsValid(indoid)) if (OidIsValid(indoid))
{
RememberReplicaIdentityForRebuilding(indoid, tab); RememberReplicaIdentityForRebuilding(indoid, tab);
RememberClusterOnForRebuilding(indoid, tab);
}
} }
} }
@ -8704,7 +8729,13 @@ RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab)
tab->changedIndexDefs = lappend(tab->changedIndexDefs, tab->changedIndexDefs = lappend(tab->changedIndexDefs,
defstring); 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); RememberReplicaIdentityForRebuilding(indoid, tab);
RememberClusterOnForRebuilding(indoid, tab);
} }
} }
} }
@ -8916,6 +8947,21 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
lappend(tab->subcmds[AT_PASS_OLD_CONSTR], cmd); 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);
}
/* /*
* Now we can drop the existing constraints and indexes --- constraints * Now we can drop the existing constraints and indexes --- constraints
* first, since some of them might depend on the indexes. In fact, we * first, since some of them might depend on the indexes. In fact, we

View File

@ -3113,3 +3113,26 @@ get_index_isreplident(Oid index_oid)
return result; 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;
}

View File

@ -161,6 +161,7 @@ extern char *get_namespace_name_or_temp(Oid nspid);
extern Oid get_range_subtype(Oid rangeOid); extern Oid get_range_subtype(Oid rangeOid);
extern Oid get_range_collation(Oid rangeOid); extern Oid get_range_collation(Oid rangeOid);
extern bool get_index_isreplident(Oid index_oid); 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) #define type_is_array(typid) (get_element_type(typid) != InvalidOid)
/* type_is_array_domain accepts both plain arrays and domains over arrays */ /* type_is_array_domain accepts both plain arrays and domains over arrays */

View File

@ -3286,3 +3286,51 @@ Table "public.test_add_column"
c4 | integer | c4 | integer |
DROP TABLE test_add_column; DROP TABLE test_add_column;
-- 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;

View File

@ -2021,3 +2021,28 @@ ALTER TABLE test_add_column
ADD COLUMN c4 integer; ADD COLUMN c4 integer;
\d test_add_column \d test_add_column
DROP TABLE test_add_column; DROP TABLE test_add_column;
-- 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;