CREATE TABLE LIKE INCLUDING TRIGGERS - Mailing list pgsql-hackers

From jian he
Subject CREATE TABLE LIKE INCLUDING TRIGGERS
Date
Msg-id CACJufxHJAr2FjbeB6ghg_-N5dxX5JVnjKSLOUxOyt4TeaAWQkg@mail.gmail.com
Whole thread Raw
Responses Re: CREATE TABLE LIKE INCLUDING TRIGGERS
List pgsql-hackers
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.

---------------
v1-0001:  "refactor CreateTrigger and CreateTriggerFiringOn".

Similar to CreateStatistics, some of the expressions stored in the
catalog pg_trigger are
already transformed, when we retrieve it as a base model for constructing a new
CreateTrigStmt, we can not do parse analysis of it again.
see transformStatsStmt for similar handling.
The CreateTrigger function, (Node *whenClause) is always NULL,
so I think it's safe to remove the argument whenClause.

v1-0002: CREATE TABLE LIKE INCLUDING TRIGGERS.

Attachment

pgsql-hackers by date:

Previous
From: John Naylor
Date:
Subject: Re: GB18030-2022 Support in PostgreSQL
Next
From: Alena Vinter
Date:
Subject: Re: Resetting recovery target parameters in pg_createsubscriber