diff options
author | Robert Haas <rhaas@postgresql.org> | 2015-12-16 07:43:56 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2015-12-16 07:43:56 -0500 |
commit | f27a6b15e6566fba7748d0d9a3fc5bcfd52c4a1b (patch) | |
tree | 8fbfe141a6684f3517078414fb007b496d007be3 /src/backend/parser/parse_utilcmd.c | |
parent | 0625dbb0b96e2ecd557eb5bcdc458679123951db (diff) | |
download | postgresql-f27a6b15e6566fba7748d0d9a3fc5bcfd52c4a1b.tar.gz postgresql-f27a6b15e6566fba7748d0d9a3fc5bcfd52c4a1b.zip |
Mark CHECK constraints declared NOT VALID valid if created with table.
FOREIGN KEY constraints have behaved this way for a long time, but for
some reason the behavior of CHECK constraints has been inconsistent up
until now.
Amit Langote and Amul Sul, with assorted tweaks by me.
Diffstat (limited to 'src/backend/parser/parse_utilcmd.c')
-rw-r--r-- | src/backend/parser/parse_utilcmd.c | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 344a40cf58e..4c24c13cf8d 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -120,6 +120,8 @@ static IndexStmt *transformIndexConstraint(Constraint *constraint, static void transformFKConstraints(CreateStmtContext *cxt, bool skipValidation, bool isAddConstraint); +static void transformCheckConstraints(CreateStmtContext *cxt, + bool skipValidation); static void transformConstraintAttrs(CreateStmtContext *cxt, List *constraintList); static void transformColumnType(CreateStmtContext *cxt, ColumnDef *column); @@ -320,6 +322,11 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) transformFKConstraints(&cxt, true, false); /* + * Postprocess check constraints. + */ + transformCheckConstraints(&cxt, true); + + /* * Output results. */ stmt->tableElts = cxt.columns; @@ -1915,6 +1922,40 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) } /* + * transformCheckConstraints + * handle CHECK constraints + * + * Right now, there's nothing to do here when called from ALTER TABLE, + * but the other constraint-transformation functions are called in both + * the CREATE TABLE and ALTER TABLE paths, so do the same here, and just + * don't do anything if we're not authorized to skip validation. + */ +static void +transformCheckConstraints(CreateStmtContext *cxt, bool skipValidation) +{ + ListCell *ckclist; + + if (cxt->ckconstraints == NIL) + return; + + /* + * If creating a new table, we can safely skip validation of check + * constraints, and nonetheless mark them valid. (This will override + * any user-supplied NOT VALID flag.) + */ + if (skipValidation) + { + foreach(ckclist, cxt->ckconstraints) + { + Constraint *constraint = (Constraint *) lfirst(ckclist); + + constraint->skip_validation = true; + constraint->initially_valid = true; + } + } +} + +/* * transformFKConstraints * handle FOREIGN KEY constraints */ @@ -2567,10 +2608,10 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, save_alist = cxt.alist; cxt.alist = NIL; - /* Postprocess index and FK constraints */ + /* Postprocess constraints */ transformIndexConstraints(&cxt); - transformFKConstraints(&cxt, skipValidation, true); + transformCheckConstraints(&cxt, false); /* * Push any index-creation commands into the ALTER, so that they can be |