Re: [PATCH] Add pg_get_trigger_ddl() to retrieve the CREATE TRIGGER statement

Philip Alger <paalger0@gmail.com>

From: Philip Alger <paalger0@gmail.com>
To: Josef Šimánek <josef.simanek@gmail.com>
Cc: Josef Šimánek <retro@ballgag.cz>, Chao Li <li.evan.chao@gmail.com>, jian he <jian.universality@gmail.com>, Jim Jones <jim.jones@uni-muenster.de>, Andrew Dunstan <andrew@dunslane.net>, Cary Huang <cary.huang@highgo.ca>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2025-11-07T22:06:59Z
Lists: pgsql-hackers
Hello Hackers,

I was thinking about the patch a little more, and I think some people may
want the pretty formatted option.

I was going to create another patch for pg_get_triggerdef to add that
functionality, like pg_get_functiondef and pg_get_viewdef. but I think it
will break it since pg_get_triggerdef is used for the \d table command and
requires the trigger to be printed as one line. The idea I had was to
create another function called pg_get_triggerdef_worker_formatted(oid) that
takes the OID and behaves the same way as pg_get_triggerdef_worker but adds
the pretty formatting capability using appendContextKeyword() and the
PRETTYFLAG_INDENT.

You might be saying, "there already is a pretty printing capability for
pg_get_triggerdef". No, it just removes the schema name. What I
am proposing is similar to the output of pg_get_functiondef or
pg_get_viewdef. The proposal is this, I can add the
pg_get_triggerdef_worker_formatted function and refactor pg_get_trigger_ddl
so that we get a formatted output like this:

 CREATE TRIGGER trg BEFORE INSERT
     ON main_table
     FOR EACH STATEMENT
     EXECUTE FUNCTION trgf()

versus one line:

  CREATE TRIGGER trg BEFORE INSERT ON main_table FOR EACH STATEMENT EXECUTE
FUNCTION trgf();

The function pg_get_triggerdef_worker_formatted will receive an OID as a
parameter, while pg_get_trigger_ddl will remain the same taking the
regclass and trigger name as parameters.

The other proposal is to leave it as it is.

-- 
Best,
Phil Alger
EDB: https://www.enterprisedb.com