diff options
author | Robert Haas <rhaas@postgresql.org> | 2017-08-04 21:54:28 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2017-08-04 22:01:37 -0400 |
commit | f85f88bcc270cf12defc34f143456834d8d8c6f8 (patch) | |
tree | 2c534351ba189bc7bddca96033365b6442d3d831 /src/backend/commands/tablecmds.c | |
parent | 7e174fa793a2df89fe03d002a5087ef67abcdde8 (diff) | |
download | postgresql-f85f88bcc270cf12defc34f143456834d8d8c6f8.tar.gz postgresql-f85f88bcc270cf12defc34f143456834d8d8c6f8.zip |
Fix bug in deciding whether to scan newly-attached partition.
If the table being attached had different attribute numbers than the
parent, the old code could incorrectly decide it needed to be scanned.
Amit Langote, reviewed by Ashutosh Bapat
Discussion: http://postgr.es/m/CA+TgmobexgbBr2+Utw-pOMw9uxaBRKRjMW_-mmzKKx9PejPLMg@mail.gmail.com
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 7859ef13ac4..1b8d4b3d17e 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -13433,6 +13433,7 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) bool skip_validate = false; ObjectAddress address; const char *trigger_name; + bool found_whole_row; attachrel = heap_openrv(cmd->name, AccessExclusiveLock); @@ -13614,6 +13615,16 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) partConstraint = list_make1(make_ands_explicit(partConstraint)); /* + * Adjust the generated constraint to match this partition's attribute + * numbers. + */ + partConstraint = map_partition_varattnos(partConstraint, 1, attachrel, + rel, &found_whole_row); + /* There can never be a whole-row reference here */ + if (found_whole_row) + elog(ERROR, "unexpected whole-row reference found in partition key"); + + /* * Check if we can do away with having to scan the table being attached to * validate the partition constraint, by *proving* that the existing * constraints of the table *imply* the partition predicate. We include @@ -13712,8 +13723,7 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) AlteredTableInfo *tab; Oid part_relid = lfirst_oid(lc); Relation part_rel; - Expr *constr; - bool found_whole_row; + List *my_partconstr = partConstraint; /* Lock already taken */ if (part_relid != RelationGetRelid(attachrel)) @@ -13732,18 +13742,24 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) continue; } + if (part_rel != attachrel) + { + /* + * Adjust the constraint that we constructed above for + * attachRel so that it matches this partition's attribute + * numbers. + */ + my_partconstr = map_partition_varattnos(my_partconstr, 1, + part_rel, attachrel, + &found_whole_row); + /* There can never be a whole-row reference here */ + if (found_whole_row) + elog(ERROR, "unexpected whole-row reference found in partition key"); + } + /* Grab a work queue entry. */ tab = ATGetQueueEntry(wqueue, part_rel); - - /* Adjust constraint to match this partition */ - constr = linitial(partConstraint); - tab->partition_constraint = (Expr *) - map_partition_varattnos((List *) constr, 1, - part_rel, rel, - &found_whole_row); - /* There can never be a whole-row reference here */ - if (found_whole_row) - elog(ERROR, "unexpected whole-row reference found in partition key"); + tab->partition_constraint = (Expr *) linitial(my_partconstr); /* keep our lock until commit */ if (part_rel != attachrel) |