Re: CREATE TABLE LIKE INCLUDING TRIGGERS

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE TABLE LIKE INCLUDING TRIGGERS
Date: 2026-01-21 12:13:04
Message-ID: CAN4CZFM_8jL7Rf43zLa-9P2BwC+_H-oM9kYxVLGcwu83KC6faA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello!

I tested the patch, it works as described, but I did notice one possible issue:

Shouldn't this preserve the enabled state of the triggers, or if it
doesn't, should the documentation include this limitations?

Currently the new table will always use the default
TRIGGER_FIRES_ON_ORIGIN, regardless how it is configured for the
original table.

diff --git a/src/test/regress/sql/triggers.sql
b/src/test/regress/sql/triggers.sql
index a1e9dbba8bd..24f7bfb5837 100644
--- a/src/test/regress/sql/triggers.sql
+++ b/src/test/regress/sql/triggers.sql
@@ -260,6 +260,18 @@ ON pc.oid = pd.tgrelid AND pd.tgname =
'before_ins_stmt_trig'
ORDER BY 1;
COMMENT ON TRIGGER before_ins_stmt_trig ON main_table IS NULL;

+-- Test that trigger firing state is not preserved by LIKE INCLUDING TRIGGERS
+ALTER TABLE main_table DISABLE TRIGGER before_ins_stmt_trig;
+CREATE TABLE main_table2 (LIKE main_table INCLUDING TRIGGERS);
+SELECT c.relname, t.tgname, t.tgenabled
+FROM pg_trigger t
+JOIN pg_class c ON t.tgrelid = c.oid
+WHERE t.tgname = 'before_ins_stmt_trig'
+ AND c.relname IN ('main_table', 'main_table2')
+ORDER BY c.relname;
+DROP TABLE main_table2;
+ALTER TABLE main_table ENABLE TRIGGER before_ins_stmt_trig;
+
--
-- Test case for bug with BEFORE trigger followed by AFTER trigger with WHEN
--

On Wed, Jan 21, 2026 at 12:00 PM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> On Fri, Jan 2, 2026 at 5:25 PM Andrey Borodin <x4mmm(at)yandex-team(dot)ru> wrote:
> >
> > + char *trigcomment; /* comment to apply to trigger, or NULL */
> > No other Create*Stmt has a comment field. Comments seem to be handled through separate CommentStmt creation.
> >
>
> See CreateStatsStmt.stxcomment, IndexStmt.idxcomment.
> We need CreateTrigStmt.trigcomment, because if INCLUDING COMMENTS is specified,
> CreateTrigStmt.trigcomment can be used to hold the source object's comments.
>
> > Some nitpicking about tests:
> > 1. INSTEAD OF triggers on views - The error is tested, but should also test that statement-level VIEW triggers work
> ok. test added.
>
> > 2. Triggers on partitioned tables - What happens when you LIKE a partitioned table? Are partition triggers cloned?
> no. only the trigger on the partitioned table itself will be cloned.
> see tests ``create table parted_constr_copy (like parted_constr
> including all);``
>
> > 3. Cross-schema trigger functions - The function name reconstruction handles schemas, but is it tested?
> >
> ok. test added.
>
> > + funcname = list_make2(makeString(schemaname),makeString(NameStr(procform->proname)));
> > Other NameStr() are pstrdup()'d, maybe let's pstrdup() this too?
> >
> ok.
>
> > + /* Reconstruct trigger old transition table */
> > Second instance of this comment is wrong.
> >
> ok.
>
> > + PG_KEYWORD("triggers", TRIGGERS, UNRESERVED_KEYWORD, BARE_LABEL)
> > Won't this break some user SQLs?
> it's marked as an un-reserved word, so it won't break any SQL, i think.
>
> v4-0001, v4-0002 was removed, as Robert said in [1], now I am using
> the same ugly
> hack consistently, now code is more aligned with INCLUDING INDEXES, INCLUDING
> STATISTICS.
>
> pstrdup have been used in more places in generateClonedTriggerStmt.
>
>
>
> --
> jian
> https://url.avanan.click/v2/r01/___https://www.enterprisedb.com/___.YXAzOnBlcmNvbmE6YTpnOjE5NWMwMWNmNDE3Y2Y3Yzg3MGZkNWQyM2FkM2UxYjc4Ojc6NWM5NTo0YjU3NGI1Y2MzZjIyMjk5MzRiYWU0Njk1Yjk4NTJkNzg0YjMwYzMwZWFiMjIzYTQ4YjU1ZWUwZDIxZWI4ZWFhOnA6VDpO

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2026-01-21 12:15:25 Re: Emitting JSON to file using COPY TO
Previous Message Ajay Pal 2026-01-21 11:51:57 Re: pg_plan_advice