Re: [PATCH] Add pretty formatting to pg_get_triggerdef

Chao Li <li.evan.chao@gmail.com>

From: Chao Li <li.evan.chao@gmail.com>
To: Philip Alger <paalger0@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2025-11-04T09:40:19Z
Lists: pgsql-hackers

> On Nov 4, 2025, at 11:36, Philip Alger <paalger0@gmail.com> wrote:
> 
> -- 
> Best, 
> Phil Alger
> <v1-0001-Add-pretty-formatting-to-pg_get_triggerdef.patch>

1
```
+/*
+ * pg_get_triggerdef_compact
+ *		Returns trigger definition in a compact, single-line format without
+ *		schema qualification designed for the psql \d command.
+ */
+Datum
+pg_get_triggerdef_compact(PG_FUNCTION_ARGS)
+{
+	Oid			trigid = PG_GETARG_OID(0);
+	char	   *res;
+
+	res = pg_get_triggerdef_worker(trigid, PRETTYFLAG_SCHEMA);
```

I think this is a mis-use of PRETTYFLAG_SCHEMA that is for printing schema-qualified names but omitting schema.

2
```
+	if (prettyFlags & PRETTYFLAG_INDENT) 
+		appendStringInfoString(&buf, "\n    ");
```

We should not hardcode 4 white-spaces, instead, we should use the const PRETTYINDENT_STD. Did you ever consider using appendContextKeyword()? Checking for an existing usage:
```
static void
get_rule_windowclause(Query *query, deparse_context *context)
{
StringInfo buf = context->buf;
const char *sep;
ListCell *l;

sep = NULL;
foreach(l, query->windowClause)
{
WindowClause *wc = (WindowClause *) lfirst(l);

if (wc->name == NULL)
continue; /* ignore anonymous windows */

if (sep == NULL)
appendContextKeyword(context, " WINDOW ",
-PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
else
appendStringInfoString(buf, sep);

appendStringInfo(buf, "%s AS ", quote_identifier(wc->name));

get_rule_windowspec(wc, query->targetList, context);

sep = ", ";
}
}
```

3 Looks like you forgot to update pg_get_triggerdef(), when it calls pg_get_triggerdef_worker(tirgid, false), the second parameter “false” should be updated to an int flag.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/