diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 15f5dcc..2e2a745 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -247,6 +247,13 @@ ExecInsert(TupleTableSlot *slot, else { /* + * Set the relation OID in tuple here so that if tableOID is used as + * part of constraint check then it will be able to evaluate result + * of constraint correctly. + */ + tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + + /* * Check the constraints of the tuple */ if (resultRelationDesc->rd_att->constr) @@ -654,6 +661,13 @@ ExecUpdate(ItemPointer tupleid, LockTupleMode lockmode; /* + * Set the relation OID in tuple here so that if tableOID is used as + * part of constraint check then it will be able to evaluate result + * of constraint correctly. + */ + 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 39922d3..46c04c1 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -551,6 +551,15 @@ 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 2a63037..16d38f6 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 18a5dd8..2ffd263 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,