Skip to content

Commit b699b5d

Browse files
author
Commitfest Bot
committed
[CF 6087] v11 - CREATE TABLE LIKE INCLUDING TRIGGERS
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6087 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CACJufxE6RsYTWPwMMaTwfHeVsWHzbfdj-uh-+L2jaciAuB28Wg@mail.gmail.com Author(s): Jian He
2 parents de8370d + fbcaded commit b699b5d

15 files changed

Lines changed: 649 additions & 18 deletions

File tree

doc/src/sgml/ref/create_foreign_table.sgml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name
6060

6161
<phrase>and <replaceable class="parameter">like_option</replaceable> is:</phrase>
6262

63-
{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | ALL }
63+
{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | TRIGGERS | ALL }
6464

6565
<phrase>and <replaceable class="parameter">partition_bound_spec</replaceable> is:</phrase>
6666

@@ -232,7 +232,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
232232
<term><literal>INCLUDING COMMENTS</literal></term>
233233
<listitem>
234234
<para>
235-
Comments for the copied columns, constraints, and extended statistics
235+
Comments for the copied columns, constraints, extended statistics, and triggers
236236
will be copied. The default behavior is to exclude comments,
237237
resulting in the corresponding objects in the new table having no
238238
comments.
@@ -284,6 +284,21 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
284284
</listitem>
285285
</varlistentry>
286286

287+
<varlistentry id="sql-createforeigntable-parms-like-opt-triggers">
288+
<term><literal>INCLUDING TRIGGERS</literal></term>
289+
<listitem>
290+
<para>
291+
All non-internal triggers are copied to the new table.
292+
Because triggers on views differ significantly from those on tables, triggers from
293+
source view are not be copied.
294+
Furthermore constraint triggers and triggers that use transition tables
295+
will not be copied because foreign tables do not support these.
296+
The enabled state of each trigger (references <link linkend="catalog-pg-trigger"><structname>pg_trigger</structname></link>.<structfield>tgenabled</structfield>)
297+
is preserved on the new table.
298+
</para>
299+
</listitem>
300+
</varlistentry>
301+
287302
<varlistentry id="sql-createforeigntable-parms-like-opt-all">
288303
<term><literal>INCLUDING ALL</literal></term>
289304
<listitem>

doc/src/sgml/ref/create_table.sgml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable cl
100100

101101
<phrase>and <replaceable class="parameter">like_option</replaceable> is:</phrase>
102102

103-
{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }
103+
{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | TRIGGERS | ALL }
104104

105105
<phrase>and <replaceable class="parameter">partition_bound_spec</replaceable> is:</phrase>
106106

@@ -693,7 +693,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
693693
<listitem>
694694
<para>
695695
Comments for the copied columns, check constraints,
696-
not-null constraints, indexes, and extended statistics will be
696+
not-null constraints, indexes, extended statistics, and triggers will be
697697
copied. The default behavior is to exclude comments, resulting in
698698
the corresponding objects in the new table having no
699699
comments.
@@ -797,6 +797,19 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
797797
</listitem>
798798
</varlistentry>
799799

800+
<varlistentry id="sql-createtable-parms-like-opt-triggers">
801+
<term><literal>INCLUDING TRIGGERS</literal></term>
802+
<listitem>
803+
<para>
804+
All non-internal triggers are copied to the new table.
805+
Because triggers on views differ significantly from those on tables, triggers from
806+
source view are not copied.
807+
The enabled state of each trigger (references <link linkend="catalog-pg-trigger"><structname>pg_trigger</structname></link>.<structfield>tgenabled</structfield>)
808+
is preserved on the new table.
809+
</para>
810+
</listitem>
811+
</varlistentry>
812+
800813
<varlistentry id="sql-createtable-parms-like-opt-all">
801814
<term><literal>INCLUDING ALL</literal></term>
802815
<listitem>

src/backend/catalog/index.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,7 @@ index_constraint_create(Relation heapRelation,
20562056
CreateTrigStmt *trigger = makeNode(CreateTrigStmt);
20572057

20582058
trigger->replace = false;
2059+
trigger->tgenabled = TRIGGER_FIRES_ON_ORIGIN;
20592060
trigger->isconstraint = true;
20602061
trigger->trigname = (constraintType == CONSTRAINT_PRIMARY) ?
20612062
"PK_ConstraintTrigger" :
@@ -2072,6 +2073,8 @@ index_constraint_create(Relation heapRelation,
20722073
trigger->deferrable = true;
20732074
trigger->initdeferred = initdeferred;
20742075
trigger->constrrel = NULL;
2076+
trigger->trigcomment = NULL;
2077+
trigger->transformed = true;
20752078

20762079
(void) CreateTrigger(trigger, NULL, RelationGetRelid(heapRelation),
20772080
InvalidOid, conOid, indexRelationId, InvalidOid,

src/backend/commands/tablecmds.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14356,6 +14356,7 @@ CreateFKCheckTrigger(Oid myRelOid, Oid refRelOid, Constraint *fkconstraint,
1435614356
*/
1435714357
fk_trigger = makeNode(CreateTrigStmt);
1435814358
fk_trigger->replace = false;
14359+
fk_trigger->tgenabled = TRIGGER_FIRES_ON_ORIGIN;
1435914360
fk_trigger->isconstraint = true;
1436014361
fk_trigger->trigname = "RI_ConstraintTrigger_c";
1436114362
fk_trigger->relation = NULL;
@@ -14381,6 +14382,8 @@ CreateFKCheckTrigger(Oid myRelOid, Oid refRelOid, Constraint *fkconstraint,
1438114382
fk_trigger->deferrable = fkconstraint->deferrable;
1438214383
fk_trigger->initdeferred = fkconstraint->initdeferred;
1438314384
fk_trigger->constrrel = NULL;
14385+
fk_trigger->trigcomment = NULL;
14386+
fk_trigger->transformed = true;
1438414387

1438514388
trigAddress = CreateTrigger(fk_trigger, NULL, myRelOid, refRelOid,
1438614389
constraintOid, indexOid, InvalidOid,
@@ -14415,6 +14418,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
1441514418
*/
1441614419
fk_trigger = makeNode(CreateTrigStmt);
1441714420
fk_trigger->replace = false;
14421+
fk_trigger->tgenabled = TRIGGER_FIRES_ON_ORIGIN;
1441814422
fk_trigger->isconstraint = true;
1441914423
fk_trigger->trigname = "RI_ConstraintTrigger_a";
1442014424
fk_trigger->relation = NULL;
@@ -14426,6 +14430,8 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
1442614430
fk_trigger->whenClause = NULL;
1442714431
fk_trigger->transitionRels = NIL;
1442814432
fk_trigger->constrrel = NULL;
14433+
fk_trigger->trigcomment = NULL;
14434+
fk_trigger->transformed = true;
1442914435

1443014436
switch (fkconstraint->fk_del_action)
1443114437
{
@@ -14475,6 +14481,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
1447514481
*/
1447614482
fk_trigger = makeNode(CreateTrigStmt);
1447714483
fk_trigger->replace = false;
14484+
fk_trigger->tgenabled = TRIGGER_FIRES_ON_ORIGIN;
1447814485
fk_trigger->isconstraint = true;
1447914486
fk_trigger->trigname = "RI_ConstraintTrigger_a";
1448014487
fk_trigger->relation = NULL;
@@ -14486,6 +14493,8 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
1448614493
fk_trigger->whenClause = NULL;
1448714494
fk_trigger->transitionRels = NIL;
1448814495
fk_trigger->constrrel = NULL;
14496+
fk_trigger->trigcomment = NULL;
14497+
fk_trigger->transformed = true;
1448914498

1449014499
switch (fkconstraint->fk_upd_action)
1449114500
{
@@ -21705,6 +21714,7 @@ CloneRowTriggersToPartition(Relation parent, Relation partition)
2170521714

2170621715
trigStmt = makeNode(CreateTrigStmt);
2170721716
trigStmt->replace = false;
21717+
trigStmt->tgenabled = trigForm->tgenabled;
2170821718
trigStmt->isconstraint = OidIsValid(trigForm->tgconstraint);
2170921719
trigStmt->trigname = NameStr(trigForm->tgname);
2171021720
trigStmt->relation = NULL;
@@ -21719,6 +21729,8 @@ CloneRowTriggersToPartition(Relation parent, Relation partition)
2171921729
trigStmt->deferrable = trigForm->tgdeferrable;
2172021730
trigStmt->initdeferred = trigForm->tginitdeferred;
2172121731
trigStmt->constrrel = NULL; /* passed separately */
21732+
trigStmt->trigcomment = NULL;
21733+
trigStmt->transformed = true;
2172221734

2172321735
CreateTriggerFiringOn(trigStmt, NULL, RelationGetRelid(partition),
2172421736
trigForm->tgconstrrelid, InvalidOid, InvalidOid,

0 commit comments

Comments
 (0)