@@ -120,6 +120,8 @@ static IndexStmt *transformIndexConstraint(Constraint *constraint,
120
120
static void transformFKConstraints (CreateStmtContext * cxt ,
121
121
bool skipValidation ,
122
122
bool isAddConstraint );
123
+ static void transformCheckConstraints (CreateStmtContext * cxt ,
124
+ bool skipValidation );
123
125
static void transformConstraintAttrs (CreateStmtContext * cxt ,
124
126
List * constraintList );
125
127
static void transformColumnType (CreateStmtContext * cxt , ColumnDef * column );
@@ -319,6 +321,11 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
319
321
*/
320
322
transformFKConstraints (& cxt , true, false);
321
323
324
+ /*
325
+ * Postprocess check constraints.
326
+ */
327
+ transformCheckConstraints (& cxt , true);
328
+
322
329
/*
323
330
* Output results.
324
331
*/
@@ -1914,6 +1921,40 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
1914
1921
return index ;
1915
1922
}
1916
1923
1924
+ /*
1925
+ * transformCheckConstraints
1926
+ * handle CHECK constraints
1927
+ *
1928
+ * Right now, there's nothing to do here when called from ALTER TABLE,
1929
+ * but the other constraint-transformation functions are called in both
1930
+ * the CREATE TABLE and ALTER TABLE paths, so do the same here, and just
1931
+ * don't do anything if we're not authorized to skip validation.
1932
+ */
1933
+ static void
1934
+ transformCheckConstraints (CreateStmtContext * cxt , bool skipValidation )
1935
+ {
1936
+ ListCell * ckclist ;
1937
+
1938
+ if (cxt -> ckconstraints == NIL )
1939
+ return ;
1940
+
1941
+ /*
1942
+ * If creating a new table, we can safely skip validation of check
1943
+ * constraints, and nonetheless mark them valid. (This will override
1944
+ * any user-supplied NOT VALID flag.)
1945
+ */
1946
+ if (skipValidation )
1947
+ {
1948
+ foreach (ckclist , cxt -> ckconstraints )
1949
+ {
1950
+ Constraint * constraint = (Constraint * ) lfirst (ckclist );
1951
+
1952
+ constraint -> skip_validation = true;
1953
+ constraint -> initially_valid = true;
1954
+ }
1955
+ }
1956
+ }
1957
+
1917
1958
/*
1918
1959
* transformFKConstraints
1919
1960
* handle FOREIGN KEY constraints
@@ -2567,10 +2608,10 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
2567
2608
save_alist = cxt .alist ;
2568
2609
cxt .alist = NIL ;
2569
2610
2570
- /* Postprocess index and FK constraints */
2611
+ /* Postprocess constraints */
2571
2612
transformIndexConstraints (& cxt );
2572
-
2573
2613
transformFKConstraints (& cxt , skipValidation , true);
2614
+ transformCheckConstraints (& cxt , false);
2574
2615
2575
2616
/*
2576
2617
* Push any index-creation commands into the ALTER, so that they can be
0 commit comments