Skip to content

Commit 3b992cf

Browse files
committed
Adjust psql to use pg_get_triggerdef(pretty=true) to remove extra ()'s
from description of triggers with WHEN clause. Thanks to Brad T. Sliger for the review.
1 parent 58565d7 commit 3b992cf

File tree

4 files changed

+19
-43
lines changed

4 files changed

+19
-43
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.319 2010/01/17 22:56:22 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.320 2010/01/21 06:11:45 itagaki Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -518,10 +518,9 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
518518
initStringInfo(&buf);
519519

520520
tgname = NameStr(trigrec->tgname);
521-
appendStringInfo(&buf, "CREATE %sTRIGGER %s",
521+
appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
522522
OidIsValid(trigrec->tgconstraint) ? "CONSTRAINT " : "",
523523
quote_identifier(tgname));
524-
appendStringInfoString(&buf, pretty ? "\n " : " ");
525524

526525
if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
527526
appendStringInfo(&buf, "BEFORE");
@@ -573,33 +572,27 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
573572
appendStringInfo(&buf, " TRUNCATE");
574573
findx++;
575574
}
576-
appendStringInfo(&buf, " ON %s",
575+
appendStringInfo(&buf, " ON %s ",
577576
generate_relation_name(trigrec->tgrelid, NIL));
578-
appendStringInfoString(&buf, pretty ? "\n " : " ");
579577

580578
if (OidIsValid(trigrec->tgconstraint))
581579
{
582580
if (OidIsValid(trigrec->tgconstrrelid))
583-
{
584-
appendStringInfo(&buf, "FROM %s",
581+
appendStringInfo(&buf, "FROM %s ",
585582
generate_relation_name(trigrec->tgconstrrelid, NIL));
586-
appendStringInfoString(&buf, pretty ? "\n " : " ");
587-
}
588583
if (!trigrec->tgdeferrable)
589584
appendStringInfo(&buf, "NOT ");
590585
appendStringInfo(&buf, "DEFERRABLE INITIALLY ");
591586
if (trigrec->tginitdeferred)
592-
appendStringInfo(&buf, "DEFERRED");
587+
appendStringInfo(&buf, "DEFERRED ");
593588
else
594-
appendStringInfo(&buf, "IMMEDIATE");
595-
appendStringInfoString(&buf, pretty ? "\n " : " ");
589+
appendStringInfo(&buf, "IMMEDIATE ");
596590
}
597591

598592
if (TRIGGER_FOR_ROW(trigrec->tgtype))
599-
appendStringInfo(&buf, "FOR EACH ROW");
593+
appendStringInfo(&buf, "FOR EACH ROW ");
600594
else
601-
appendStringInfo(&buf, "FOR EACH STATEMENT");
602-
appendStringInfoString(&buf, pretty ? "\n " : " ");
595+
appendStringInfo(&buf, "FOR EACH STATEMENT ");
603596

604597
/* If the trigger has a WHEN qualification, add that */
605598
value = fastgetattr(ht_trig, Anum_pg_trigger_tgqual,
@@ -643,12 +636,12 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
643636
context.windowClause = NIL;
644637
context.windowTList = NIL;
645638
context.varprefix = true;
646-
context.prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0;
639+
context.prettyFlags = pretty ? PRETTYFLAG_PAREN : 0;
647640
context.indentLevel = PRETTYINDENT_STD;
648641

649642
get_rule_expr(qual, &context, false);
650643

651-
appendStringInfo(&buf, ")%s", pretty ? "\n " : " ");
644+
appendStringInfo(&buf, ") ");
652645
}
653646

654647
appendStringInfo(&buf, "EXECUTE PROCEDURE %s(",

src/bin/psql/describe.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
1010
*
11-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.234 2010/01/17 22:56:23 tgl Exp $
11+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.235 2010/01/21 06:11:46 itagaki Exp $
1212
*/
1313
#include "postgres_fe.h"
1414

@@ -1854,10 +1854,11 @@ describeOneTableDetails(const char *schemaname,
18541854
{
18551855
printfPQExpBuffer(&buf,
18561856
"SELECT t.tgname, "
1857-
"pg_catalog.pg_get_triggerdef(t.oid), "
1857+
"pg_catalog.pg_get_triggerdef(t.oid%s), "
18581858
"t.tgenabled\n"
18591859
"FROM pg_catalog.pg_trigger t\n"
18601860
"WHERE t.tgrelid = '%s' AND ",
1861+
(pset.sversion >= 80500 ? ", true" : ""),
18611862
oid);
18621863
if (pset.sversion >= 80500)
18631864
appendPQExpBuffer(&buf, "NOT t.tgisinternal");

src/test/regress/expected/triggers.out

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,9 @@ SELECT * FROM main_table ORDER BY a, b;
375375
(8 rows)
376376

377377
SELECT pg_get_triggerdef(oid, true) FROM pg_trigger WHERE tgrelid = 'main_table'::regclass AND tgname = 'modified_a';
378-
pg_get_triggerdef
379-
--------------------------------------------------
380-
CREATE TRIGGER modified_a +
381-
BEFORE UPDATE OF a ON main_table +
382-
FOR EACH ROW +
383-
WHEN (old.a <> new.a) +
384-
EXECUTE PROCEDURE trigger_func('modified_a')
378+
pg_get_triggerdef
379+
--------------------------------------------------------------------------------------------------------------------------------------------
380+
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')
385381
(1 row)
386382

387383
SELECT pg_get_triggerdef(oid, false) FROM pg_trigger WHERE tgrelid = 'main_table'::regclass AND tgname = 'modified_a';
@@ -391,13 +387,9 @@ SELECT pg_get_triggerdef(oid, false) FROM pg_trigger WHERE tgrelid = 'main_table
391387
(1 row)
392388

393389
SELECT pg_get_triggerdef(oid, true) FROM pg_trigger WHERE tgrelid = 'main_table'::regclass AND tgname = 'modified_any';
394-
pg_get_triggerdef
395-
----------------------------------------------------
396-
CREATE TRIGGER modified_any +
397-
BEFORE UPDATE OF a ON main_table +
398-
FOR EACH ROW +
399-
WHEN (old.* IS DISTINCT FROM new.*) +
400-
EXECUTE PROCEDURE trigger_func('modified_any')
390+
pg_get_triggerdef
391+
--------------------------------------------------------------------------------------------------------------------------------------------------------------
392+
CREATE TRIGGER modified_any BEFORE UPDATE OF a ON main_table FOR EACH ROW WHEN (old.* IS DISTINCT FROM new.*) EXECUTE PROCEDURE trigger_func('modified_any')
401393
(1 row)
402394

403395
DROP TRIGGER modified_a ON main_table;
@@ -424,15 +416,6 @@ SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgrelid = 'main_table'::regc
424416
CREATE TRIGGER after_upd_a_b_row_trig AFTER UPDATE OF a, b ON main_table FOR EACH ROW EXECUTE PROCEDURE trigger_func('after_upd_a_b_row')
425417
(1 row)
426418

427-
SELECT pg_get_triggerdef(oid, true) FROM pg_trigger WHERE tgrelid = 'main_table'::regclass AND tgname = 'after_upd_a_b_row_trig';
428-
pg_get_triggerdef
429-
---------------------------------------------------------
430-
CREATE TRIGGER after_upd_a_b_row_trig +
431-
AFTER UPDATE OF a, b ON main_table +
432-
FOR EACH ROW +
433-
EXECUTE PROCEDURE trigger_func('after_upd_a_b_row')
434-
(1 row)
435-
436419
UPDATE main_table SET a = 50;
437420
NOTICE: trigger_func(before_upd_a_stmt) called: action = UPDATE, when = BEFORE, level = STATEMENT
438421
NOTICE: trigger_func(before_upd_a_row) called: action = UPDATE, when = BEFORE, level = ROW

src/test/regress/sql/triggers.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ CREATE TRIGGER after_upd_b_stmt_trig AFTER UPDATE OF b ON main_table
304304
FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func('after_upd_b_stmt');
305305

306306
SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgrelid = 'main_table'::regclass AND tgname = 'after_upd_a_b_row_trig';
307-
SELECT pg_get_triggerdef(oid, true) FROM pg_trigger WHERE tgrelid = 'main_table'::regclass AND tgname = 'after_upd_a_b_row_trig';
308307

309308
UPDATE main_table SET a = 50;
310309
UPDATE main_table SET b = 10;

0 commit comments

Comments
 (0)