| From: | Philip Alger <paalger0(at)gmail(dot)com> |
|---|---|
| To: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
| Cc: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | Re: [PATCH] Add pretty formatting to pg_get_triggerdef |
| Date: | 2025-11-05 16:21:48 |
| Message-ID: | CAPXBC8J2njTfUeP_SmwQZC4K3S8gRajy1Nss0nMETvGxFaFodQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Chao,
> > I think this is a mis-use of PRETTYFLAG_SCHEMA that is for printing
> schema-qualified names but omitting schema.
> >
> > Yes, this is to omit the schema because the functions is used to print
> out the triggers when using \d in psql, The current practice isn't to print
> out a schema for the table/view/etc.
> >
>
> I guess I didn’t express myself clearly. My comment was that, this
> function wants to omit schema, so PRETTYFLAG_SCHEMA should not be used
> here, because it is for adding schema.
>
>
I am not sure that's right because I am using it in a similar way as what's
written in pg_get_index_worker:
appendStringInfo(&buf, "CREATE %sINDEX %s ON %s%s USING %s (",
idxrec->indisunique ? "UNIQUE " : "",
quote_identifier(NameStr(idxrelrec->relname)),
idxrelrec->relkind == RELKIND_PARTITIONED_INDEX
&& !inherits ? "ONLY " : "",
(prettyFlags & PRETTYFLAG_SCHEMA) ?
generate_relation_name(indrelid, NIL) :
generate_qualified_relation_name(indrelid),
quote_identifier(NameStr(amrec->amname)));
For that function, with `true` set you don't get the schema:
postgres=# select pg_get_indexdef(16395, 0, true);
pg_get_indexdef
--------------------------------------------------------------------
CREATE UNIQUE INDEX main_table_a_key ON main_table USING btree (a)
(1 row)
Similarly, in this patch, you get:
postgres=# \d child3
Table "public.child3"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
b | integer | | |
a | text | | |
Partition of: parent FOR VALUES IN ('CCC')
Triggers:
child3_delete_trig AFTER DELETE ON child3 REFERENCING OLD TABLE AS
old_table FOR EACH STATEMENT EXECUTE FUNCTION dump_delete()
Or this without the schema as well:
postgres=# SELECT pg_get_triggerdef(oid, true) FROM pg_trigger WHERE
tgrelid = 'child3'::regclass AND tgname = 'child3_insert_trig';
pg_get_triggerdef
------------------------------------------------
CREATE TRIGGER child3_insert_trig AFTER INSERT+
ON child3 +
REFERENCING NEW TABLE AS new_table +
FOR EACH STATEMENT +
EXECUTE FUNCTION dump_insert()
(1 row)
Setting prettyFlags = PRETTYFLAG_SCHEMA removes the schema.
--
Best,
Phil Alger
EDB: https://www.enterprisedb.com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Peter Eisentraut | 2025-11-05 16:41:08 | Re: Spacing of options in getopt_long processing |
| Previous Message | Tom Lane | 2025-11-05 16:05:00 | Re: [BUG] PostgreSQL crashes with ThreadSanitizer during early initialization |