Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b865d27

Browse files
committed
Use pg_get_triggerdef in pg_dump
Add a variant of pg_get_triggerdef with a second argument "pretty" that causes the output to be formatted in the way pg_dump used to do. Use this variant in pg_dump with server versions >= 8.5. This insulates pg_dump from most future trigger feature additions, such as the upcoming column triggers patch. Author: Itagaki Takahiro <itagaki.takahiro@oss.ntt.co.jp>
1 parent c970292 commit b865d27

File tree

7 files changed

+196
-135
lines changed

7 files changed

+196
-135
lines changed

doc/src/sgml/func.sgml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.487 2009/08/16 19:55:21 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.488 2009/10/09 21:02:55 petere Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -12369,6 +12369,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1236912369
<entry><type>text</type></entry>
1237012370
<entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry>
1237112371
</row>
12372+
<row>
12373+
<entry><function>pg_get_triggerdef</function>(<parameter>trigger_oid</parameter>, <parameter>pretty_bool</>)</entry>
12374+
<entry><type>text</type></entry>
12375+
<entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry>
12376+
</row>
1237212377
<row>
1237312378
<entry><literal><function>pg_get_userbyid</function>(<parameter>role_oid</parameter>)</literal></entry>
1237412379
<entry><type>name</type></entry>

src/backend/utils/adt/ruleutils.c

+34-12
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.307 2009/10/08 02:39:23 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.308 2009/10/09 21:02:55 petere Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -139,6 +139,7 @@ static char *deparse_expression_pretty(Node *expr, List *dpcontext,
139139
bool forceprefix, bool showimplicit,
140140
int prettyFlags, int startIndent);
141141
static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags);
142+
static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
142143
static void decompile_column_index_array(Datum column_index_array, Oid relId,
143144
StringInfo buf);
144145
static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
@@ -462,6 +463,22 @@ Datum
462463
pg_get_triggerdef(PG_FUNCTION_ARGS)
463464
{
464465
Oid trigid = PG_GETARG_OID(0);
466+
467+
PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, false)));
468+
}
469+
470+
Datum
471+
pg_get_triggerdef_ext(PG_FUNCTION_ARGS)
472+
{
473+
Oid trigid = PG_GETARG_OID(0);
474+
bool pretty = PG_GETARG_BOOL(1);
475+
476+
PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, pretty)));
477+
}
478+
479+
static char *
480+
pg_get_triggerdef_worker(Oid trigid, bool pretty)
481+
{
465482
HeapTuple ht_trig;
466483
Form_pg_trigger trigrec;
467484
StringInfoData buf;
@@ -498,9 +515,10 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
498515
initStringInfo(&buf);
499516

500517
tgname = NameStr(trigrec->tgname);
501-
appendStringInfo(&buf, "CREATE %sTRIGGER %s ",
518+
appendStringInfo(&buf, "CREATE %sTRIGGER %s",
502519
trigrec->tgisconstraint ? "CONSTRAINT " : "",
503520
quote_identifier(tgname));
521+
appendStringInfoString(&buf, pretty ? "\n " : " ");
504522

505523
if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
506524
appendStringInfo(&buf, "BEFORE");
@@ -533,29 +551,33 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
533551
else
534552
appendStringInfo(&buf, " TRUNCATE");
535553
}
536-
appendStringInfo(&buf, " ON %s ",
554+
appendStringInfo(&buf, " ON %s",
537555
generate_relation_name(trigrec->tgrelid, NIL));
556+
appendStringInfoString(&buf, pretty ? "\n " : " ");
538557

539558
if (trigrec->tgisconstraint)
540559
{
541560
if (trigrec->tgconstrrelid != InvalidOid)
542-
appendStringInfo(&buf, "FROM %s ",
543-
generate_relation_name(trigrec->tgconstrrelid,
544-
NIL));
561+
{
562+
appendStringInfo(&buf, "FROM %s",
563+
generate_relation_name(trigrec->tgconstrrelid, NIL));
564+
appendStringInfoString(&buf, pretty ? "\n " : " ");
565+
}
545566
if (!trigrec->tgdeferrable)
546567
appendStringInfo(&buf, "NOT ");
547568
appendStringInfo(&buf, "DEFERRABLE INITIALLY ");
548569
if (trigrec->tginitdeferred)
549-
appendStringInfo(&buf, "DEFERRED ");
570+
appendStringInfo(&buf, "DEFERRED");
550571
else
551-
appendStringInfo(&buf, "IMMEDIATE ");
552-
572+
appendStringInfo(&buf, "IMMEDIATE");
573+
appendStringInfoString(&buf, pretty ? "\n " : " ");
553574
}
554575

555576
if (TRIGGER_FOR_ROW(trigrec->tgtype))
556-
appendStringInfo(&buf, "FOR EACH ROW ");
577+
appendStringInfo(&buf, "FOR EACH ROW");
557578
else
558-
appendStringInfo(&buf, "FOR EACH STATEMENT ");
579+
appendStringInfo(&buf, "FOR EACH STATEMENT");
580+
appendStringInfoString(&buf, pretty ? "\n " : " ");
559581

560582
appendStringInfo(&buf, "EXECUTE PROCEDURE %s(",
561583
generate_function_name(trigrec->tgfoid, 0,
@@ -594,7 +616,7 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
594616

595617
heap_close(tgrel, AccessShareLock);
596618

597-
PG_RETURN_TEXT_P(string_to_text(buf.data));
619+
return buf.data;
598620
}
599621

600622
/* ----------

0 commit comments

Comments
 (0)