diff options
author | Robert Haas <rhaas@postgresql.org> | 2013-09-23 13:31:22 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2013-09-23 13:31:22 -0400 |
commit | ba3d39c96921c96de114f6c22a9572bff24708b5 (patch) | |
tree | 394fd3046d1903f16dfdb4152082f7f2bfd336c8 /src | |
parent | ff2a1f5e84ee9984b33ee31e6fb9c6f2760a820e (diff) | |
download | postgresql-ba3d39c96921c96de114f6c22a9572bff24708b5.tar.gz postgresql-ba3d39c96921c96de114f6c22a9572bff24708b5.zip |
Don't allow system columns in CHECK constraints, except tableoid.
Previously, arbitray system columns could be mentioned in table
constraints, but they were not correctly checked at runtime, because
the values weren't actually set correctly in the tuple. Since it
seems easy enough to initialize the table OID properly, do that,
and continue allowing that column, but disallow the rest unless and
until someone figures out a way to make them work properly.
No back-patch, because this doesn't seem important enough to take the
risk of destabilizing the back branches. In fact, this will pose a
dump-and-reload hazard for those upgrading from previous versions:
constraints that were accepted before but were not correctly enforced
will now either be enforced correctly or not accepted at all. Either
could result in restore failures, but in practice I think very few
users will notice the difference, since the use case is pretty
marginal anyway and few users will be relying on features that have
not historically worked.
Amit Kapila, reviewed by Rushabh Lathia, with doc changes by me.
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/commands/copy.c | 6 | ||||
-rw-r--r-- | src/backend/commands/tablecmds.c | 6 | ||||
-rw-r--r-- | src/backend/executor/nodeModifyTable.c | 12 | ||||
-rw-r--r-- | src/backend/parser/parse_relation.c | 10 | ||||
-rw-r--r-- | src/test/regress/input/constraints.source | 22 | ||||
-rw-r--r-- | src/test/regress/output/constraints.source | 24 |
6 files changed, 80 insertions, 0 deletions
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 31819cce1d8..6b20144a48c 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -2217,6 +2217,12 @@ CopyFrom(CopyState cstate) if (loaded_oid != InvalidOid) HeapTupleSetOid(tuple, loaded_oid); + /* + * Constraints might reference the tableoid column, so initialize + * t_tableOid before evaluating them. + */ + tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc); + /* Triggers and stuff need to be invoked in query context. */ MemoryContextSwitchTo(oldcontext); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index adc74dd7e40..8839f986b4b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -3857,6 +3857,12 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode) /* Preserve OID, if any */ if (newTupDesc->tdhasoid) HeapTupleSetOid(tuple, tupOid); + + /* + * Constraints might reference the tableoid column, so initialize + * t_tableOid before evaluating them. + */ + tuple->t_tableOid = RelationGetRelid(oldrel); } /* Now check any constraints on the possibly-changed tuple */ diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 15f5dccb82a..189df713eab 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -247,6 +247,12 @@ ExecInsert(TupleTableSlot *slot, else { /* + * Constraints might reference the tableoid column, so initialize + * t_tableOid before evaluating them. + */ + tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + + /* * Check the constraints of the tuple */ if (resultRelationDesc->rd_att->constr) @@ -654,6 +660,12 @@ ExecUpdate(ItemPointer tupleid, LockTupleMode lockmode; /* + * Constraints might reference the tableoid column, so initialize + * t_tableOid before evaluating them. + */ + tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + + /* * Check the constraints of the tuple * * If we generate a new candidate tuple after EvalPlanQual testing, we diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index 39922d32c5c..5f469135cbf 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -551,6 +551,16 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, { /* quick check to see if name could be a system column */ attnum = specialAttNum(colname); + + /* In constraint check, no system column is allowed except tableOid */ + if (pstate->p_expr_kind == EXPR_KIND_CHECK_CONSTRAINT && + attnum < InvalidAttrNumber && attnum != TableOidAttributeNumber) + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), + errmsg("system column \"%s\" reference in check constraint is invalid", + colname), + parser_errposition(pstate, location))); + if (attnum != InvalidAttrNumber) { /* now check to see if column actually is defined */ diff --git a/src/test/regress/input/constraints.source b/src/test/regress/input/constraints.source index 2a630378882..16d38f6d1e7 100644 --- a/src/test/regress/input/constraints.source +++ b/src/test/regress/input/constraints.source @@ -127,6 +127,28 @@ INSERT INTO INSERT_TBL VALUES (null, null, null); SELECT '' AS nine, * FROM INSERT_TBL; -- +-- Check constraints on system columns +-- + +CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool, + altitude int, + CHECK (NOT (is_capital AND tableoid::regclass::text = 'sys_col_check_tbl'))); + +INSERT INTO SYS_COL_CHECK_TBL VALUES ('Seattle', 'Washington', false, 100); +INSERT INTO SYS_COL_CHECK_TBL VALUES ('Olympia', 'Washington', true, 100); + +SELECT *, tableoid::regclass::text FROM SYS_COL_CHECK_TBL; + +DROP TABLE SYS_COL_CHECK_TBL; + +-- +-- Check constraints on system columns other then TableOid should return error +-- +CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool, + altitude int, + CHECK (NOT (is_capital AND ctid::text = 'sys_col_check_tbl'))); + +-- -- Check inheritance of defaults and constraints -- diff --git a/src/test/regress/output/constraints.source b/src/test/regress/output/constraints.source index 18a5dd8ab19..2ffd263dd35 100644 --- a/src/test/regress/output/constraints.source +++ b/src/test/regress/output/constraints.source @@ -205,6 +205,30 @@ SELECT '' AS nine, * FROM INSERT_TBL; (7 rows) -- +-- Check constraints on system columns +-- +CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool, + altitude int, + CHECK (NOT (is_capital AND tableoid::regclass::text = 'sys_col_check_tbl'))); +INSERT INTO SYS_COL_CHECK_TBL VALUES ('Seattle', 'Washington', false, 100); +INSERT INTO SYS_COL_CHECK_TBL VALUES ('Olympia', 'Washington', true, 100); +ERROR: new row for relation "sys_col_check_tbl" violates check constraint "sys_col_check_tbl_check" +DETAIL: Failing row contains (Olympia, Washington, t, 100). +SELECT *, tableoid::regclass::text FROM SYS_COL_CHECK_TBL; + city | state | is_capital | altitude | tableoid +---------+------------+------------+----------+------------------- + Seattle | Washington | f | 100 | sys_col_check_tbl +(1 row) + +DROP TABLE SYS_COL_CHECK_TBL; +-- +-- Check constraints on system columns other then TableOid should return error +-- +CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool, + altitude int, + CHECK (NOT (is_capital AND ctid::text = 'sys_col_check_tbl'))); +ERROR: system column "ctid" reference in check constraint is invalid +-- -- Check inheritance of defaults and constraints -- CREATE TABLE INSERT_CHILD (cx INT default 42, |