Skip to content

Commit 7e07d3b

Browse files
jianhe-funCommitfest Bot
authored andcommitted
CREATE TABLE LIKE INCLUDING TRIGGERS
foreign key associated internal trigger won't being copied to new table. source table trigger associated comment will copied to new table, if INCLUDING COMMENTS is specified. Currently not support CREATE FOREIGN TABLE LIKE. (maybe we should). discussion: https://postgr.es/m/CACJufxHJAr2FjbeB6ghg_-N5dxX5JVnjKSLOUxOyt4TeaAWQkg@mail.gmail.com commitfest: https://commitfest.postgresql.org/patch/6087
1 parent 79a4090 commit 7e07d3b

File tree

10 files changed

+416
-4
lines changed

10 files changed

+416
-4
lines changed

doc/src/sgml/ref/create_table.sgml

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

8989
<phrase>and <replaceable class="parameter">like_option</replaceable> is:</phrase>
9090

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

9393
<phrase>and <replaceable class="parameter">partition_bound_spec</replaceable> is:</phrase>
9494

@@ -672,7 +672,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
672672
<term><literal>INCLUDING COMMENTS</literal></term>
673673
<listitem>
674674
<para>
675-
Comments for the copied columns, constraints, and indexes will be
675+
Comments for the copied columns, constraints, indexes and triggers will be
676676
copied. The default behavior is to exclude comments, resulting in
677677
the copied columns and constraints in the new table having no
678678
comments.
@@ -776,6 +776,15 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
776776
</listitem>
777777
</varlistentry>
778778

779+
<varlistentry id="sql-createtable-parms-like-opt-triggers">
780+
<term><literal>INCLUDING TRIGGERS</literal></term>
781+
<listitem>
782+
<para>
783+
Any non-internal triggers on the original table will be created on the new table.
784+
</para>
785+
</listitem>
786+
</varlistentry>
787+
779788
<varlistentry id="sql-createtable-parms-like-opt-all">
780789
<term><literal>INCLUDING ALL</literal></term>
781790
<listitem>

src/backend/catalog/index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,7 @@ index_constraint_create(Relation heapRelation,
20402040
trigger->initdeferred = initdeferred;
20412041
trigger->constrrel = NULL;
20422042
trigger->transformed = true;
2043+
trigger->trigcomment = NULL;
20432044

20442045
(void) CreateTrigger(trigger, NULL, RelationGetRelid(heapRelation),
20452046
InvalidOid, conOid, indexRelationId, InvalidOid,

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13826,6 +13826,7 @@ CreateFKCheckTrigger(Oid myRelOid, Oid refRelOid, Constraint *fkconstraint,
1382613826
fk_trigger->deferrable = fkconstraint->deferrable;
1382713827
fk_trigger->initdeferred = fkconstraint->initdeferred;
1382813828
fk_trigger->constrrel = NULL;
13829+
fk_trigger->trigcomment = NULL;
1382913830
fk_trigger->transformed = true;
1383013831

1383113832
trigAddress = CreateTrigger(fk_trigger, NULL, myRelOid, refRelOid,
@@ -13872,6 +13873,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
1387213873
fk_trigger->whenClause = NULL;
1387313874
fk_trigger->transitionRels = NIL;
1387413875
fk_trigger->constrrel = NULL;
13876+
fk_trigger->trigcomment = NULL;
1387513877
fk_trigger->transformed = true;
1387613878

1387713879
switch (fkconstraint->fk_del_action)
@@ -13933,6 +13935,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
1393313935
fk_trigger->whenClause = NULL;
1393413936
fk_trigger->transitionRels = NIL;
1393513937
fk_trigger->constrrel = NULL;
13938+
fk_trigger->trigcomment = NULL;
1393613939
fk_trigger->transformed = true;
1393713940

1393813941
switch (fkconstraint->fk_upd_action)
@@ -20865,6 +20868,7 @@ CloneRowTriggersToPartition(Relation parent, Relation partition)
2086520868
trigStmt->deferrable = trigForm->tgdeferrable;
2086620869
trigStmt->initdeferred = trigForm->tginitdeferred;
2086720870
trigStmt->constrrel = NULL; /* passed separately */
20871+
trigStmt->trigcomment = NULL;
2086820872
trigStmt->transformed = true; /* whenClause alerady transformed */
2086920873

2087020874
CreateTriggerFiringOn(trigStmt, NULL, RelationGetRelid(partition),

src/backend/commands/trigger.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "catalog/pg_proc.h"
3131
#include "catalog/pg_trigger.h"
3232
#include "catalog/pg_type.h"
33+
#include "commands/comment.h"
3334
#include "commands/trigger.h"
3435
#include "executor/executor.h"
3536
#include "miscadmin.h"
@@ -1122,6 +1123,11 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
11221123
/* Keep lock on target rel until end of xact */
11231124
table_close(rel, NoLock);
11241125

1126+
/* Add any requested comment */
1127+
if (stmt->trigcomment != NULL)
1128+
CreateComments(trigoid, TriggerRelationId, 0,
1129+
stmt->trigcomment);
1130+
11251131
return myself;
11261132
}
11271133

src/backend/parser/gram.y

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
783783

784784
TABLE TABLES TABLESAMPLE TABLESPACE TARGET TEMP TEMPLATE TEMPORARY TEXT_P THEN
785785
TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM
786-
TREAT TRIGGER TRIM TRUE_P
786+
TREAT TRIGGER TRIGGERS TRIM TRUE_P
787787
TRUNCATE TRUSTED TYPE_P TYPES_P
788788

789789
UESCAPE UNBOUNDED UNCONDITIONAL UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN
@@ -4243,6 +4243,7 @@ TableLikeOption:
42434243
| INDEXES { $$ = CREATE_TABLE_LIKE_INDEXES; }
42444244
| STATISTICS { $$ = CREATE_TABLE_LIKE_STATISTICS; }
42454245
| STORAGE { $$ = CREATE_TABLE_LIKE_STORAGE; }
4246+
| TRIGGERS { $$ = CREATE_TABLE_LIKE_TRIGGERS; }
42464247
| ALL { $$ = CREATE_TABLE_LIKE_ALL; }
42474248
;
42484249

@@ -6097,6 +6098,7 @@ CreateTrigStmt:
60976098
n->deferrable = false;
60986099
n->initdeferred = false;
60996100
n->constrrel = NULL;
6101+
n->trigcomment = NULL;
61006102
n->transformed = false;
61016103
$$ = (Node *) n;
61026104
}
@@ -6148,6 +6150,7 @@ CreateTrigStmt:
61486150
&n->deferrable, &n->initdeferred, &dummy,
61496151
NULL, NULL, yyscanner);
61506152
n->constrrel = $10;
6153+
n->trigcomment = NULL;
61516154
n->transformed = false;
61526155
$$ = (Node *) n;
61536156
}
@@ -18116,6 +18119,7 @@ unreserved_keyword:
1811618119
| TRANSACTION
1811718120
| TRANSFORM
1811818121
| TRIGGER
18122+
| TRIGGERS
1811918123
| TRUNCATE
1812018124
| TRUSTED
1812118125
| TYPE_P
@@ -18765,6 +18769,7 @@ bare_label_keyword:
1876518769
| TRANSFORM
1876618770
| TREAT
1876718771
| TRIGGER
18772+
| TRIGGERS
1876818773
| TRIM
1876918774
| TRUE_P
1877018775
| TRUNCATE

0 commit comments

Comments
 (0)