Skip to content

Commit e20003f

Browse files
committed
Fix timing issue with ALTER TABLE's validate constraint
An ALTER TABLE to validate a foreign key in which another subcommand already caused a pending table rewrite could fail due to ALTER TABLE attempting to validate the foreign key before the actual table rewrite takes place. This situation could result in an error such as: ERROR: could not read block 0 in file "base/nnnnn/nnnnn": read only 0 of 8192 bytes The failure here was due to the SPI call which validates the foreign key trying to access an index which is yet to be rebuilt. Similarly, we also incorrectly tried to validate CHECK constraints before the heap had been rewritten. The fix for both is to delay constraint validation until phase 3, after the table has been rewritten. For CHECK constraints this means a slight behavioral change. Previously ALTER TABLE VALIDATE CONSTRAINT on inheritance tables would be validated from the bottom up. This was different from the order of evaluation when a new CHECK constraint was added. The changes made here aligns the VALIDATE CONSTRAINT evaluation order for inheritance tables to be the same as ADD CONSTRAINT, which is generally top-down. Reported-by: Nazli Ugur Koyluoglu, using SQLancer Discussion: https://postgr.es/m/CAApHDvp%3DZXv8wiRyk_0rWr00skhGkt8vXDrHJYXRMft3TjkxCA%40mail.gmail.com Backpatch-through: 9.5 (all supported versions)
1 parent 80d8f6d commit e20003f

File tree

3 files changed

+95
-108
lines changed

3 files changed

+95
-108
lines changed

src/backend/commands/tablecmds.c

Lines changed: 52 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ static void AlterSeqNamespaces(Relation classRel, Relation rel,
288288
LOCKMODE lockmode);
289289
static ObjectAddress ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd,
290290
bool recurse, bool recursing, LOCKMODE lockmode);
291-
static ObjectAddress ATExecValidateConstraint(Relation rel, char *constrName,
292-
bool recurse, bool recursing, LOCKMODE lockmode);
291+
static ObjectAddress ATExecValidateConstraint(List **wqueue, Relation rel,
292+
char *constrName, bool recurse, bool recursing,
293+
LOCKMODE lockmode);
293294
static int transformColumnNameList(Oid relId, List *colList,
294295
int16 *attnums, Oid *atttypids);
295296
static int transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
@@ -302,7 +303,6 @@ static Oid transformFkeyCheckAttrs(Relation pkrel,
302303
static void checkFkeyPermissions(Relation rel, int16 *attnums, int natts);
303304
static CoercionPathType findFkeyCast(Oid targetTypeId, Oid sourceTypeId,
304305
Oid *funcid);
305-
static void validateCheckConstraint(Relation rel, HeapTuple constrtup);
306306
static void validateForeignKeyConstraint(char *conname,
307307
Relation rel, Relation pkrel,
308308
Oid pkindOid, Oid constraintOid);
@@ -3582,13 +3582,13 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
35823582
address = ATExecAlterConstraint(rel, cmd, false, false, lockmode);
35833583
break;
35843584
case AT_ValidateConstraint: /* VALIDATE CONSTRAINT */
3585-
address = ATExecValidateConstraint(rel, cmd->name, false, false,
3586-
lockmode);
3585+
address = ATExecValidateConstraint(wqueue, rel, cmd->name, false,
3586+
false, lockmode);
35873587
break;
35883588
case AT_ValidateConstraintRecurse: /* VALIDATE CONSTRAINT with
35893589
* recursion */
3590-
address = ATExecValidateConstraint(rel, cmd->name, true, false,
3591-
lockmode);
3590+
address = ATExecValidateConstraint(wqueue, rel, cmd->name, true,
3591+
false, lockmode);
35923592
break;
35933593
case AT_DropConstraint: /* DROP CONSTRAINT */
35943594
ATExecDropConstraint(rel, cmd->name, cmd->behavior,
@@ -6851,8 +6851,8 @@ ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd,
68516851
* was already validated, InvalidObjectAddress is returned.
68526852
*/
68536853
static ObjectAddress
6854-
ATExecValidateConstraint(Relation rel, char *constrName, bool recurse,
6855-
bool recursing, LOCKMODE lockmode)
6854+
ATExecValidateConstraint(List **wqueue, Relation rel, char *constrName,
6855+
bool recurse, bool recursing, LOCKMODE lockmode)
68566856
{
68576857
Relation conrel;
68586858
SysScanDesc scan;
@@ -6899,27 +6899,31 @@ ATExecValidateConstraint(Relation rel, char *constrName, bool recurse,
68996899

69006900
if (!con->convalidated)
69016901
{
6902+
AlteredTableInfo *tab;
69026903
HeapTuple copyTuple;
69036904
Form_pg_constraint copy_con;
69046905

69056906
if (con->contype == CONSTRAINT_FOREIGN)
69066907
{
6907-
Relation refrel;
6908+
NewConstraint *newcon;
6909+
Constraint *fkconstraint;
69086910

6909-
/*
6910-
* Triggers are already in place on both tables, so a concurrent
6911-
* write that alters the result here is not possible. Normally we
6912-
* can run a query here to do the validation, which would only
6913-
* require AccessShareLock. In some cases, it is possible that we
6914-
* might need to fire triggers to perform the check, so we take a
6915-
* lock at RowShareLock level just in case.
6916-
*/
6917-
refrel = heap_open(con->confrelid, RowShareLock);
6911+
/* Queue validation for phase 3 */
6912+
fkconstraint = makeNode(Constraint);
6913+
/* for now this is all we need */
6914+
fkconstraint->conname = constrName;
69186915

6919-
validateForeignKeyConstraint(constrName, rel, refrel,
6920-
con->conindid,
6921-
HeapTupleGetOid(tuple));
6922-
heap_close(refrel, NoLock);
6916+
newcon = (NewConstraint *) palloc0(sizeof(NewConstraint));
6917+
newcon->name = constrName;
6918+
newcon->contype = CONSTR_FOREIGN;
6919+
newcon->refrelid = con->confrelid;
6920+
newcon->refindid = con->conindid;
6921+
newcon->conid = HeapTupleGetOid(tuple);
6922+
newcon->qual = (Node *) fkconstraint;
6923+
6924+
/* Find or create work queue entry for this table */
6925+
tab = ATGetQueueEntry(wqueue, rel);
6926+
tab->constraints = lappend(tab->constraints, newcon);
69236927

69246928
/*
69256929
* Foreign keys do not inherit, so we purposely ignore the
@@ -6930,6 +6934,10 @@ ATExecValidateConstraint(Relation rel, char *constrName, bool recurse,
69306934
{
69316935
List *children = NIL;
69326936
ListCell *child;
6937+
NewConstraint *newcon;
6938+
bool isnull;
6939+
Datum val;
6940+
char *conbin;
69336941

69346942
/*
69356943
* If we're recursing, the parent has already done this, so skip
@@ -6968,12 +6976,31 @@ ATExecValidateConstraint(Relation rel, char *constrName, bool recurse,
69686976
/* find_all_inheritors already got lock */
69696977
childrel = heap_open(childoid, NoLock);
69706978

6971-
ATExecValidateConstraint(childrel, constrName, false,
6979+
ATExecValidateConstraint(wqueue, childrel, constrName, false,
69726980
true, lockmode);
69736981
heap_close(childrel, NoLock);
69746982
}
69756983

6976-
validateCheckConstraint(rel, tuple);
6984+
/* Queue validation for phase 3 */
6985+
newcon = (NewConstraint *) palloc0(sizeof(NewConstraint));
6986+
newcon->name = constrName;
6987+
newcon->contype = CONSTR_CHECK;
6988+
newcon->refrelid = InvalidOid;
6989+
newcon->refindid = InvalidOid;
6990+
newcon->conid = HeapTupleGetOid(tuple);
6991+
6992+
val = SysCacheGetAttr(CONSTROID, tuple,
6993+
Anum_pg_constraint_conbin, &isnull);
6994+
if (isnull)
6995+
elog(ERROR, "null conbin for constraint %u",
6996+
HeapTupleGetOid(tuple));
6997+
6998+
conbin = TextDatumGetCString(val);
6999+
newcon->qual = (Node *) make_ands_implicit((Expr *) stringToNode(conbin));
7000+
7001+
/* Find or create work queue entry for this table */
7002+
tab = ATGetQueueEntry(wqueue, rel);
7003+
tab->constraints = lappend(tab->constraints, newcon);
69777004

69787005
/*
69797006
* Invalidate relcache so that others see the new validated
@@ -7345,88 +7372,6 @@ checkFkeyPermissions(Relation rel, int16 *attnums, int natts)
73457372
}
73467373
}
73477374

7348-
/*
7349-
* Scan the existing rows in a table to verify they meet a proposed
7350-
* CHECK constraint.
7351-
*
7352-
* The caller must have opened and locked the relation appropriately.
7353-
*/
7354-
static void
7355-
validateCheckConstraint(Relation rel, HeapTuple constrtup)
7356-
{
7357-
EState *estate;
7358-
Datum val;
7359-
char *conbin;
7360-
Expr *origexpr;
7361-
List *exprstate;
7362-
TupleDesc tupdesc;
7363-
HeapScanDesc scan;
7364-
HeapTuple tuple;
7365-
ExprContext *econtext;
7366-
MemoryContext oldcxt;
7367-
TupleTableSlot *slot;
7368-
Form_pg_constraint constrForm;
7369-
bool isnull;
7370-
Snapshot snapshot;
7371-
7372-
/* VALIDATE CONSTRAINT is a no-op for foreign tables */
7373-
if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
7374-
return;
7375-
7376-
constrForm = (Form_pg_constraint) GETSTRUCT(constrtup);
7377-
7378-
estate = CreateExecutorState();
7379-
7380-
/*
7381-
* XXX this tuple doesn't really come from a syscache, but this doesn't
7382-
* matter to SysCacheGetAttr, because it only wants to be able to fetch
7383-
* the tupdesc
7384-
*/
7385-
val = SysCacheGetAttr(CONSTROID, constrtup, Anum_pg_constraint_conbin,
7386-
&isnull);
7387-
if (isnull)
7388-
elog(ERROR, "null conbin for constraint %u",
7389-
HeapTupleGetOid(constrtup));
7390-
conbin = TextDatumGetCString(val);
7391-
origexpr = (Expr *) stringToNode(conbin);
7392-
exprstate = (List *)
7393-
ExecPrepareExpr((Expr *) make_ands_implicit(origexpr), estate);
7394-
7395-
econtext = GetPerTupleExprContext(estate);
7396-
tupdesc = RelationGetDescr(rel);
7397-
slot = MakeSingleTupleTableSlot(tupdesc);
7398-
econtext->ecxt_scantuple = slot;
7399-
7400-
snapshot = RegisterSnapshot(GetLatestSnapshot());
7401-
scan = heap_beginscan(rel, snapshot, 0, NULL);
7402-
7403-
/*
7404-
* Switch to per-tuple memory context and reset it for each tuple
7405-
* produced, so we don't leak memory.
7406-
*/
7407-
oldcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
7408-
7409-
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
7410-
{
7411-
ExecStoreTuple(tuple, slot, InvalidBuffer, false);
7412-
7413-
if (!ExecQual(exprstate, econtext, true))
7414-
ereport(ERROR,
7415-
(errcode(ERRCODE_CHECK_VIOLATION),
7416-
errmsg("check constraint \"%s\" is violated by some row",
7417-
NameStr(constrForm->conname)),
7418-
errtableconstraint(rel, NameStr(constrForm->conname))));
7419-
7420-
ResetExprContext(econtext);
7421-
}
7422-
7423-
MemoryContextSwitchTo(oldcxt);
7424-
heap_endscan(scan);
7425-
UnregisterSnapshot(snapshot);
7426-
ExecDropSingleTupleTableSlot(slot);
7427-
FreeExecutorState(estate);
7428-
}
7429-
74307375
/*
74317376
* Scan the existing rows in a table to verify they meet a proposed FK
74327377
* constraint.

src/test/regress/expected/alter_table.out

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ NOTICE: boo: 18
436436
ALTER TABLE tmp3 ADD CONSTRAINT IDENTITY check (b = boo(b)) NOT VALID;
437437
NOTICE: merging constraint "identity" with inherited definition
438438
ALTER TABLE tmp3 VALIDATE CONSTRAINT identity;
439-
NOTICE: boo: 16
440439
NOTICE: boo: 20
440+
NOTICE: boo: 16
441441
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
442442
create table parent_noinh_convalid (a int);
443443
create table child_noinh_convalid () inherits (parent_noinh_convalid);
@@ -932,6 +932,26 @@ ERROR: column "test2" contains null values
932932
-- now add a primary key column with a default (succeeds).
933933
alter table atacc1 add column test2 int default 0 primary key;
934934
drop table atacc1;
935+
-- additionally, we've seen issues with foreign key validation not being
936+
-- properly delayed until after a table rewrite. Check that works ok.
937+
create table atacc1 (a int primary key);
938+
alter table atacc1 add constraint atacc1_fkey foreign key (a) references atacc1 (a) not valid;
939+
alter table atacc1 validate constraint atacc1_fkey, alter a type bigint;
940+
drop table atacc1;
941+
-- we've also seen issues with check constraints being validated at the wrong
942+
-- time when there's a pending table rewrite.
943+
create table atacc1 (a bigint, b int);
944+
insert into atacc1 values(1,1);
945+
alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
946+
alter table atacc1 validate constraint atacc1_chk, alter a type int;
947+
drop table atacc1;
948+
-- same as above, but ensure the constraint violation is detected
949+
create table atacc1 (a bigint, b int);
950+
insert into atacc1 values(1,2);
951+
alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
952+
alter table atacc1 validate constraint atacc1_chk, alter a type int;
953+
ERROR: check constraint "atacc1_chk" is violated by some row
954+
drop table atacc1;
935955
-- something a little more complicated
936956
create table atacc1 ( test int, test2 int);
937957
-- add a primary key constraint

src/test/regress/sql/alter_table.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,28 @@ alter table atacc1 add column test2 int primary key;
710710
alter table atacc1 add column test2 int default 0 primary key;
711711
drop table atacc1;
712712

713+
-- additionally, we've seen issues with foreign key validation not being
714+
-- properly delayed until after a table rewrite. Check that works ok.
715+
create table atacc1 (a int primary key);
716+
alter table atacc1 add constraint atacc1_fkey foreign key (a) references atacc1 (a) not valid;
717+
alter table atacc1 validate constraint atacc1_fkey, alter a type bigint;
718+
drop table atacc1;
719+
720+
-- we've also seen issues with check constraints being validated at the wrong
721+
-- time when there's a pending table rewrite.
722+
create table atacc1 (a bigint, b int);
723+
insert into atacc1 values(1,1);
724+
alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
725+
alter table atacc1 validate constraint atacc1_chk, alter a type int;
726+
drop table atacc1;
727+
728+
-- same as above, but ensure the constraint violation is detected
729+
create table atacc1 (a bigint, b int);
730+
insert into atacc1 values(1,2);
731+
alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
732+
alter table atacc1 validate constraint atacc1_chk, alter a type int;
733+
drop table atacc1;
734+
713735
-- something a little more complicated
714736
create table atacc1 ( test int, test2 int);
715737
-- add a primary key constraint

0 commit comments

Comments
 (0)