Re: [PATCH] Add pretty formatting to pg_get_triggerdef - Mailing list pgsql-hackers

From Philip Alger
Subject Re: [PATCH] Add pretty formatting to pg_get_triggerdef
Date
Msg-id CAPXBC8J2njTfUeP_SmwQZC4K3S8gRajy1Nss0nMETvGxFaFodQ@mail.gmail.com
Whole thread Raw
In response to Re: [PATCH] Add pretty formatting to pg_get_triggerdef  (Chao Li <li.evan.chao@gmail.com>)
Responses Re: [PATCH] Add pretty formatting to pg_get_triggerdef
List 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

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [BUG] PostgreSQL crashes with ThreadSanitizer during early initialization
Next
From: Peter Eisentraut
Date:
Subject: Re: Spacing of options in getopt_long processing