Fix partitioned index creation bug with dropped columns
authorAlvaro Herrera
Tue, 26 Mar 2019 23:19:39 +0000 (20:19 -0300)
committerAlvaro Herrera
Tue, 26 Mar 2019 23:19:39 +0000 (20:19 -0300)
ALTER INDEX .. ATTACH PARTITION fails if the partitioned table where the
index is defined contains more dropped columns than its partition, with
this message:
  ERROR:  incorrect attribute map
The cause was that one caller of CompareIndexInfo was passing the number
of attributes of the partition rather than the parent, which confused
the length check.  Repair.

This can cause pg_upgrade to fail when used on such a database.  Leave
some more objects around after regression tests, so that the case is
detected by pg_upgrade test suite.

Remove some spurious empty lines noticed while looking for other cases
of the same problem.

Discussion: https://postgr.es/m/20190326213924[email protected]

src/backend/catalog/index.c
src/backend/commands/indexcmds.c
src/backend/commands/tablecmds.c
src/test/regress/expected/indexing.out
src/test/regress/sql/indexing.sql

index 44625a507be89ef733d22690bb11e35e4cab9956..7857df6bcd8f24ab1ec67bfd417916f09808f291 100644 (file)
@@ -1871,7 +1871,6 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
    if (info1->ii_NumIndexKeyAttrs != info2->ii_NumIndexKeyAttrs)
        return false;
 
-
    /*
     * and columns match through the attribute map (actual attribute numbers
     * might differ!)  Note that this implies that index columns that are
index fec5bc5dd64d3e2febd3b2478d9401fe54d06235..f8ee4b0a84b9c70216ef226c24d44c88308b25ca 100644 (file)
@@ -928,7 +928,6 @@ DefineIndex(Oid relationId,
                                               gettext_noop("could not convert row type"));
                maplen = parentDesc->natts;
 
-
                foreach(cell, childidxs)
                {
                    Oid         cldidxid = lfirst_oid(cell);
index 0f72f51d372190e015f1e4885cf3b1c03917bdb1..23c2e923757483eea4f4bfd40b4376661372f614 100644 (file)
@@ -15518,7 +15518,7 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name)
                              partIdx->rd_opfamily,
                              parentIdx->rd_opfamily,
                              attmap,
-                             RelationGetDescr(partTbl)->natts))
+                             RelationGetDescr(parentTbl)->natts))
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                     errmsg("cannot attach index \"%s\" as a partition of index \"%s\"",
index 769b7b83d434131a5bd3d90e3509e7d86be72baa..6a21c0c2e1949787830cdd1632ddbfadcb59eceb 100644 (file)
@@ -1360,6 +1360,11 @@ alter index idxpart2_a_idx attach partition idxpart22_a_idx;
 create index on idxpart (a);
 create table idxpart_another (a int, b int, primary key (a, b)) partition by range (a);
 create table idxpart_another_1 partition of idxpart_another for values from (0) to (100);
+create table idxpart3 (c int, b int, a int) partition by range (a);
+alter table idxpart3 drop column b, drop column c;
+create table idxpart31 partition of idxpart3 for values from (1000) to (1200);
+create table idxpart32 partition of idxpart3 for values from (1200) to (1400);
+alter table idxpart attach partition idxpart3 for values from (1000) to (2000);
 -- Test that covering partitioned indexes work in various cases
 create table covidxpart (a int, b int) partition by list (a);
 create unique index on covidxpart (a) include (b);
index 9adc5be10c27ccb57f6eaa6305f21827945aae0d..d14d9f85287d4b9ed9dc0a14f439c075b6afbeb7 100644 (file)
@@ -720,6 +720,11 @@ alter index idxpart2_a_idx attach partition idxpart22_a_idx;
 create index on idxpart (a);
 create table idxpart_another (a int, b int, primary key (a, b)) partition by range (a);
 create table idxpart_another_1 partition of idxpart_another for values from (0) to (100);
+create table idxpart3 (c int, b int, a int) partition by range (a);
+alter table idxpart3 drop column b, drop column c;
+create table idxpart31 partition of idxpart3 for values from (1000) to (1200);
+create table idxpart32 partition of idxpart3 for values from (1200) to (1400);
+alter table idxpart attach partition idxpart3 for values from (1000) to (2000);
 
 -- Test that covering partitioned indexes work in various cases
 create table covidxpart (a int, b int) partition by list (a);