Re: CREATE TABLE LIKE INCLUDING TRIGGERS

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE TABLE LIKE INCLUDING TRIGGERS
Date: 2025-10-01 09:28:24
Message-ID: CACJufxHQ8tDG09mXW=8xfEK2_h+By1zLgrFd0EcshHp-TDGiGw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Sep 29, 2025 at 5:35 PM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> hi.
>
> poc demo:
> CREATE TABLE main_table (a int, b int);
> CREATE FUNCTION trigger_func() RETURNS trigger LANGUAGE plpgsql AS '
> BEGIN
> RAISE NOTICE ''trigger_func(%) called: action = %, when = %, level = %'',
> TG_ARGV[0], TG_OP, TG_WHEN, TG_LEVEL;
> RETURN NULL;
> END;';
> CREATE TRIGGER modified_a BEFORE UPDATE OF a ON main_table
> FOR EACH ROW WHEN (OLD.a <> NEW.a) EXECUTE PROCEDURE trigger_func('modified_a');
>
> CREATE TABLE main_table1(LIKE main_table INCLUDING TRIGGERS INCLUDING COMMENTS);
> \d main_table1
> Table "public.main_table1"
> Column | Type | Collation | Nullable | Default
> --------+---------+-----------+----------+---------
> a | integer | | |
> b | integer | | |
> Triggers:
> modified_a BEFORE UPDATE OF a ON main_table1 FOR EACH ROW WHEN
> (old.a <> new.a) EXECUTE FUNCTION trigger_func('modified_a')
>
> foreign key associated internal triggers won't be copied to the new table.
> source table trigger associated comment will be copied to the new table,
> if INCLUDING COMMENTS is specified.
>

per
https://api.cirrus-ci.com/v1/artifact/task/5194724417470464/testrun/build/testrun/regress/regress/regression.diffs

there are many table name as t1 in regress test, add tests like
""CREATE TABLE t1 (a int, b text, c int);"
may result in error
+ERROR: relation "t1" already exists
in some OS.
So I changed the table name to avoid parallel regess test failure.

Attachment Content-Type Size
v2-0001-refactor-CreateTrigger-and-CreateTriggerFiringOn.patch text/x-patch 23.1 KB
v2-0002-CREATE-TABLE-LIKE-INCLUDING-TRIGGERS.patch text/x-patch 30.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2025-10-01 09:29:54 Re: Fix incorrect function reference BufFileOpenShared in comment.
Previous Message Anthonin Bonnefoy 2025-10-01 09:25:00 Infinite loop in pgbench when running COPY command