diff --git a/src/backend/distributed/deparser/deparse_table_stmts.c b/src/backend/distributed/deparser/deparse_table_stmts.c index a69d17afe..c12e5401a 100644 --- a/src/backend/distributed/deparser/deparse_table_stmts.c +++ b/src/backend/distributed/deparser/deparse_table_stmts.c @@ -168,7 +168,7 @@ AppendAlterTableCmdAddConstraint(StringInfo buf, Constraint *constraint, if (constraint->contype == CONSTR_PRIMARY) { appendStringInfoString(buf, - " PRIMARY KEY ("); + " PRIMARY KEY "); } else { @@ -180,44 +180,15 @@ AppendAlterTableCmdAddConstraint(StringInfo buf, Constraint *constraint, appendStringInfoString(buf, " NULLS NOT DISTINCT"); } #endif - appendStringInfoString(buf, " ("); } - ListCell *lc; - bool firstkey = true; - - foreach(lc, constraint->keys) - { - if (firstkey == false) - { - appendStringInfoString(buf, ", "); - } - - appendStringInfo(buf, "%s", quote_identifier(strVal(lfirst(lc)))); - firstkey = false; - } - - appendStringInfoString(buf, ")"); + AppendColumnNameList(buf, constraint->keys); if (constraint->including != NULL) { - appendStringInfoString(buf, " INCLUDE ("); + appendStringInfoString(buf, " INCLUDE "); - firstkey = true; - - foreach(lc, constraint->including) - { - if (firstkey == false) - { - appendStringInfoString(buf, ", "); - } - - appendStringInfo(buf, "%s", quote_identifier(strVal(lfirst( - lc)))); - firstkey = false; - } - - appendStringInfoString(buf, " )"); + AppendColumnNameList(buf, constraint->including); } } else if (constraint->contype == CONSTR_EXCLUSION) @@ -404,6 +375,12 @@ AppendAlterTableCmdAddConstraint(StringInfo buf, Constraint *constraint, } } + /* FOREIGN KEY and CHECK constraints migth have NOT VALID option */ + if (constraint->skip_validation) + { + appendStringInfoString(buf, " NOT VALID "); + } + if (constraint->deferrable) { appendStringInfoString(buf, " DEFERRABLE"); diff --git a/src/test/regress/expected/multi_alter_table_add_constraints_without_name.out b/src/test/regress/expected/multi_alter_table_add_constraints_without_name.out index 72bd738f6..e1da7b3dc 100644 --- a/src/test/regress/expected/multi_alter_table_add_constraints_without_name.out +++ b/src/test/regress/expected/multi_alter_table_add_constraints_without_name.out @@ -268,7 +268,7 @@ SELECT con.conname, con.connoinherit (1 row) \c - - :public_worker_1_host :worker_1_port -SELECT con.conname, connoinherit +SELECT con.conname, con.connoinherit FROM pg_catalog.pg_constraint con INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace @@ -278,6 +278,31 @@ SELECT con.conname, connoinherit products_check_5410000 | t (1 row) +\c - - :master_host :master_port +ALTER TABLE AT_AddConstNoName.products DROP CONSTRAINT products_check; +-- Check "ADD CHECK ... NOT VALID" +ALTER TABLE AT_AddConstNoName.products ADD CHECK (product_no > 0 AND price > 0) NOT VALID; +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname = 'products'; + conname | convalidated +--------------------------------------------------------------------- + products_check | f +(1 row) + +\c - - :public_worker_1_host :worker_1_port +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname = 'products_5410000'; + conname | convalidated +--------------------------------------------------------------------- + products_check_5410000 | f +(1 row) + \c - - :master_host :master_port ALTER TABLE AT_AddConstNoName.products DROP CONSTRAINT products_check; DROP TABLE AT_AddConstNoName.products; diff --git a/src/test/regress/expected/multi_alter_table_add_foreign_key_without_name.out b/src/test/regress/expected/multi_alter_table_add_foreign_key_without_name.out index 30af3cdd1..c27e6a425 100644 --- a/src/test/regress/expected/multi_alter_table_add_foreign_key_without_name.out +++ b/src/test/regress/expected/multi_alter_table_add_foreign_key_without_name.out @@ -248,6 +248,34 @@ SELECT con.conname, con.confupdtype, con.confdeltype, con.confmatchtype referencing_table_ref_id_fkey_1770043 | a | c | s (3 rows) +\c - - :master_host :master_port +SET SEARCH_PATH = at_add_fk; +ALTER TABLE referencing_table DROP CONSTRAINT referencing_table_ref_id_fkey; +-- test NOT VALID +ALTER TABLE referencing_table ADD FOREIGN KEY(ref_id) REFERENCES referenced_table(id) NOT VALID; +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname = 'referencing_table'; + conname | convalidated +--------------------------------------------------------------------- + referencing_table_ref_id_fkey | f +(1 row) + +\c - - :public_worker_1_host :worker_1_port +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname LIKE 'referencing_table%' ORDER BY con.conname ASC; + conname | convalidated +--------------------------------------------------------------------- + referencing_table_ref_id_fkey | f + referencing_table_ref_id_fkey_1770041 | f + referencing_table_ref_id_fkey_1770043 | f +(3 rows) + \c - - :master_host :master_port SET SEARCH_PATH = at_add_fk; ALTER TABLE referencing_table DROP CONSTRAINT referencing_table_ref_id_fkey; diff --git a/src/test/regress/sql/multi_alter_table_add_constraints_without_name.sql b/src/test/regress/sql/multi_alter_table_add_constraints_without_name.sql index b532577fa..db620be02 100644 --- a/src/test/regress/sql/multi_alter_table_add_constraints_without_name.sql +++ b/src/test/regress/sql/multi_alter_table_add_constraints_without_name.sql @@ -212,7 +212,26 @@ SELECT con.conname, con.connoinherit WHERE rel.relname = 'products'; \c - - :public_worker_1_host :worker_1_port -SELECT con.conname, connoinherit +SELECT con.conname, con.connoinherit + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname = 'products_5410000'; + +\c - - :master_host :master_port +ALTER TABLE AT_AddConstNoName.products DROP CONSTRAINT products_check; + +-- Check "ADD CHECK ... NOT VALID" +ALTER TABLE AT_AddConstNoName.products ADD CHECK (product_no > 0 AND price > 0) NOT VALID; + +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname = 'products'; + +\c - - :public_worker_1_host :worker_1_port +SELECT con.conname, con.convalidated FROM pg_catalog.pg_constraint con INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace diff --git a/src/test/regress/sql/multi_alter_table_add_foreign_key_without_name.sql b/src/test/regress/sql/multi_alter_table_add_foreign_key_without_name.sql index 6a39430f1..330ac0c45 100644 --- a/src/test/regress/sql/multi_alter_table_add_foreign_key_without_name.sql +++ b/src/test/regress/sql/multi_alter_table_add_foreign_key_without_name.sql @@ -154,6 +154,25 @@ SET SEARCH_PATH = at_add_fk; ALTER TABLE referencing_table DROP CONSTRAINT referencing_table_ref_id_fkey; +-- test NOT VALID +ALTER TABLE referencing_table ADD FOREIGN KEY(ref_id) REFERENCES referenced_table(id) NOT VALID; +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname = 'referencing_table'; + +\c - - :public_worker_1_host :worker_1_port +SELECT con.conname, con.convalidated + FROM pg_catalog.pg_constraint con + INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace + WHERE rel.relname LIKE 'referencing_table%' ORDER BY con.conname ASC; + +\c - - :master_host :master_port +SET SEARCH_PATH = at_add_fk; +ALTER TABLE referencing_table DROP CONSTRAINT referencing_table_ref_id_fkey; + -- test ON DELETE NO ACTION + DEFERABLE + INITIALLY DEFERRED ALTER TABLE referencing_table ADD FOREIGN KEY(ref_id) REFERENCES referenced_table(id) ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED;