1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Fix for dropped columns in a partitioned table's default partition

We forgot to map column numbers to/from the default partition for
various operations, leading to valid cases failing with spurious
errors, such as
ERROR:  attribute N of type some_partition has been dropped

It was also possible that the search for conflicting rows in the default
partition when attaching another partition would fail to detect some.
Secondarily, it was also possible that such a search should be skipped
(because the constraint was implied) but wasn't.

Fix all this by mapping column numbers when necessary.

Reported by: Daniel Wilches
Author: Amit Langote
Discussion: https://postgr.es/m/15873-8c61945d6b3ef87c@postgresql.org
This commit is contained in:
Alvaro Herrera
2019-06-28 14:51:08 -04:00
parent 74b7cc8c02
commit 23cccb17fe
6 changed files with 70 additions and 7 deletions

View File

@ -1237,6 +1237,13 @@ check_default_partition_contents(Relation parent, Relation default_rel,
: get_qual_for_range(parent, new_spec, false);
def_part_constraints =
get_proposed_default_constraint(new_part_constraints);
/*
* Map the Vars in the constraint expression from parent's attnos to
* default_rel's.
*/
def_part_constraints =
map_partition_varattnos(def_part_constraints, 1, default_rel,
parent, NULL);
/*
* If the existing constraints on the default partition imply that it will
@ -1265,7 +1272,6 @@ check_default_partition_contents(Relation parent, Relation default_rel,
{
Oid part_relid = lfirst_oid(lc);
Relation part_rel;
Expr *constr;
Expr *partition_constraint;
EState *estate;
ExprState *partqualstate = NULL;
@ -1280,6 +1286,15 @@ check_default_partition_contents(Relation parent, Relation default_rel,
{
part_rel = table_open(part_relid, NoLock);
/*
* Map the Vars in the constraint expression from default_rel's
* the sub-partition's.
*/
partition_constraint = make_ands_explicit(def_part_constraints);
partition_constraint = (Expr *)
map_partition_varattnos((List *) partition_constraint, 1,
part_rel, default_rel, NULL);
/*
* If the partition constraints on default partition child imply
* that it will not contain any row that would belong to the new
@ -1297,7 +1312,10 @@ check_default_partition_contents(Relation parent, Relation default_rel,
}
}
else
{
part_rel = default_rel;
partition_constraint = make_ands_explicit(def_part_constraints);
}
/*
* Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
@ -1318,10 +1336,6 @@ check_default_partition_contents(Relation parent, Relation default_rel,
continue;
}
constr = linitial(def_part_constraints);
partition_constraint = (Expr *)
map_partition_varattnos((List *) constr,
1, part_rel, parent, NULL);
estate = CreateExecutorState();
/* Build expression execution states for partition check quals */