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
defaultrel = heap_open(defaultPartOid, NoLock);
defPartConstraint =
get_proposed_default_constraint(partBoundConstraint);
+ /*
+ * Map the Vars in the constraint expression from rel's attnos to
+ * defaultrel's.
+ */
+ defPartConstraint =
+ map_partition_varattnos(defPartConstraint,
+ 1, defaultrel, rel, NULL);
QueuePartitionConstraintValidation(wqueue, defaultrel,
defPartConstraint, true);
: 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
{
Oid part_relid = lfirst_oid(lc);
Relation part_rel;
- Expr *constr;
Expr *partition_constraint;
EState *estate;
HeapTuple tuple;
{
part_rel = heap_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
}
}
else
+ {
part_rel = default_rel;
+ partition_constraint = make_ands_explicit(def_part_constraints);
+ }
/*
* Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
}
tupdesc = CreateTupleDescCopy(RelationGetDescr(part_rel));
- 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 */
-- default partition
create table defpart_attach_test (a int) partition by list (a);
create table defpart_attach_test1 partition of defpart_attach_test for values in (1);
-create table defpart_attach_test_d (like defpart_attach_test);
+create table defpart_attach_test_d (b int, a int);
+alter table defpart_attach_test_d drop b;
insert into defpart_attach_test_d values (1), (2);
-- error because its constraint as the default partition would be violated
-- by the row containing 1
-- should be attached successfully and without needing to be scanned
alter table defpart_attach_test attach partition defpart_attach_test_d default;
INFO: partition constraint for table "defpart_attach_test_d" is implied by existing constraints
+-- check that attaching a partition correctly reports any rows in the default
+-- partition that should not be there for the new partition to be attached
+-- successfully
+create table defpart_attach_test_2 (like defpart_attach_test_d);
+alter table defpart_attach_test attach partition defpart_attach_test_2 for values in (2);
+ERROR: updated partition constraint for default partition would be violated by some row
drop table defpart_attach_test;
-- check combinations of temporary and permanent relations when attaching
-- partitions.
-- default partition
create table defpart_attach_test (a int) partition by list (a);
create table defpart_attach_test1 partition of defpart_attach_test for values in (1);
-create table defpart_attach_test_d (like defpart_attach_test);
+create table defpart_attach_test_d (b int, a int);
+alter table defpart_attach_test_d drop b;
insert into defpart_attach_test_d values (1), (2);
-- error because its constraint as the default partition would be violated
-- should be attached successfully and without needing to be scanned
alter table defpart_attach_test attach partition defpart_attach_test_d default;
+-- check that attaching a partition correctly reports any rows in the default
+-- partition that should not be there for the new partition to be attached
+-- successfully
+create table defpart_attach_test_2 (like defpart_attach_test_d);
+alter table defpart_attach_test attach partition defpart_attach_test_2 for values in (2);
+
drop table defpart_attach_test;
-- check combinations of temporary and permanent relations when attaching