Commit b865d275 authored by Peter Eisentraut's avatar Peter Eisentraut

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>
parent c970292a
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.487 2009/08/16 19:55:21 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.488 2009/10/09 21:02:55 petere Exp $ -->
<chapter id="functions"> <chapter id="functions">
<title>Functions and Operators</title> <title>Functions and Operators</title>
...@@ -12369,6 +12369,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype); ...@@ -12369,6 +12369,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
<entry><type>text</type></entry> <entry><type>text</type></entry>
<entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry> <entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry>
</row> </row>
<row>
<entry><function>pg_get_triggerdef</function>(<parameter>trigger_oid</parameter>, <parameter>pretty_bool</>)</entry>
<entry><type>text</type></entry>
<entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry>
</row>
<row> <row>
<entry><literal><function>pg_get_userbyid</function>(<parameter>role_oid</parameter>)</literal></entry> <entry><literal><function>pg_get_userbyid</function>(<parameter>role_oid</parameter>)</literal></entry>
<entry><type>name</type></entry> <entry><type>name</type></entry>
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.307 2009/10/08 02:39:23 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.308 2009/10/09 21:02:55 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -139,6 +139,7 @@ static char *deparse_expression_pretty(Node *expr, List *dpcontext, ...@@ -139,6 +139,7 @@ static char *deparse_expression_pretty(Node *expr, List *dpcontext,
bool forceprefix, bool showimplicit, bool forceprefix, bool showimplicit,
int prettyFlags, int startIndent); int prettyFlags, int startIndent);
static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags); static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags);
static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
static void decompile_column_index_array(Datum column_index_array, Oid relId, static void decompile_column_index_array(Datum column_index_array, Oid relId,
StringInfo buf); StringInfo buf);
static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags); static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
...@@ -462,6 +463,22 @@ Datum ...@@ -462,6 +463,22 @@ Datum
pg_get_triggerdef(PG_FUNCTION_ARGS) pg_get_triggerdef(PG_FUNCTION_ARGS)
{ {
Oid trigid = PG_GETARG_OID(0); Oid trigid = PG_GETARG_OID(0);
PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, false)));
}
Datum
pg_get_triggerdef_ext(PG_FUNCTION_ARGS)
{
Oid trigid = PG_GETARG_OID(0);
bool pretty = PG_GETARG_BOOL(1);
PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, pretty)));
}
static char *
pg_get_triggerdef_worker(Oid trigid, bool pretty)
{
HeapTuple ht_trig; HeapTuple ht_trig;
Form_pg_trigger trigrec; Form_pg_trigger trigrec;
StringInfoData buf; StringInfoData buf;
...@@ -498,9 +515,10 @@ pg_get_triggerdef(PG_FUNCTION_ARGS) ...@@ -498,9 +515,10 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
initStringInfo(&buf); initStringInfo(&buf);
tgname = NameStr(trigrec->tgname); tgname = NameStr(trigrec->tgname);
appendStringInfo(&buf, "CREATE %sTRIGGER %s ", appendStringInfo(&buf, "CREATE %sTRIGGER %s",
trigrec->tgisconstraint ? "CONSTRAINT " : "", trigrec->tgisconstraint ? "CONSTRAINT " : "",
quote_identifier(tgname)); quote_identifier(tgname));
appendStringInfoString(&buf, pretty ? "\n " : " ");
if (TRIGGER_FOR_BEFORE(trigrec->tgtype)) if (TRIGGER_FOR_BEFORE(trigrec->tgtype))
appendStringInfo(&buf, "BEFORE"); appendStringInfo(&buf, "BEFORE");
...@@ -533,29 +551,33 @@ pg_get_triggerdef(PG_FUNCTION_ARGS) ...@@ -533,29 +551,33 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
else else
appendStringInfo(&buf, " TRUNCATE"); appendStringInfo(&buf, " TRUNCATE");
} }
appendStringInfo(&buf, " ON %s ", appendStringInfo(&buf, " ON %s",
generate_relation_name(trigrec->tgrelid, NIL)); generate_relation_name(trigrec->tgrelid, NIL));
appendStringInfoString(&buf, pretty ? "\n " : " ");
if (trigrec->tgisconstraint) if (trigrec->tgisconstraint)
{ {
if (trigrec->tgconstrrelid != InvalidOid) if (trigrec->tgconstrrelid != InvalidOid)
appendStringInfo(&buf, "FROM %s ", {
generate_relation_name(trigrec->tgconstrrelid, appendStringInfo(&buf, "FROM %s",
NIL)); generate_relation_name(trigrec->tgconstrrelid, NIL));
appendStringInfoString(&buf, pretty ? "\n " : " ");
}
if (!trigrec->tgdeferrable) if (!trigrec->tgdeferrable)
appendStringInfo(&buf, "NOT "); appendStringInfo(&buf, "NOT ");
appendStringInfo(&buf, "DEFERRABLE INITIALLY "); appendStringInfo(&buf, "DEFERRABLE INITIALLY ");
if (trigrec->tginitdeferred) if (trigrec->tginitdeferred)
appendStringInfo(&buf, "DEFERRED "); appendStringInfo(&buf, "DEFERRED");
else else
appendStringInfo(&buf, "IMMEDIATE "); appendStringInfo(&buf, "IMMEDIATE");
appendStringInfoString(&buf, pretty ? "\n " : " ");
} }
if (TRIGGER_FOR_ROW(trigrec->tgtype)) if (TRIGGER_FOR_ROW(trigrec->tgtype))
appendStringInfo(&buf, "FOR EACH ROW "); appendStringInfo(&buf, "FOR EACH ROW");
else else
appendStringInfo(&buf, "FOR EACH STATEMENT "); appendStringInfo(&buf, "FOR EACH STATEMENT");
appendStringInfoString(&buf, pretty ? "\n " : " ");
appendStringInfo(&buf, "EXECUTE PROCEDURE %s(", appendStringInfo(&buf, "EXECUTE PROCEDURE %s(",
generate_function_name(trigrec->tgfoid, 0, generate_function_name(trigrec->tgfoid, 0,
...@@ -594,7 +616,7 @@ pg_get_triggerdef(PG_FUNCTION_ARGS) ...@@ -594,7 +616,7 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
heap_close(tgrel, AccessShareLock); heap_close(tgrel, AccessShareLock);
PG_RETURN_TEXT_P(string_to_text(buf.data)); return buf.data;
} }
/* ---------- /* ----------
......
This diff is collapsed.
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.h,v 1.158 2009/10/05 19:24:46 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.h,v 1.159 2009/10/09 21:02:56 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -328,6 +328,7 @@ typedef struct _triggerInfo ...@@ -328,6 +328,7 @@ typedef struct _triggerInfo
char tgenabled; char tgenabled;
bool tgdeferrable; bool tgdeferrable;
bool tginitdeferred; bool tginitdeferred;
char *tgdef;
} TriggerInfo; } TriggerInfo;
/* /*
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.543 2009/10/08 02:39:23 tgl Exp $ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.544 2009/10/09 21:02:56 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200910072 #define CATALOG_VERSION_NO 200910101
#endif #endif
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.551 2009/09/26 22:42:02 tgl Exp $ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.552 2009/10/09 21:02:56 petere Exp $
* *
* NOTES * NOTES
* The script catalog/genbki.sh reads this file and generates .bki * The script catalog/genbki.sh reads this file and generates .bki
...@@ -4083,6 +4083,8 @@ DATA(insert OID = 2599 ( pg_timezone_abbrevs PGNSP PGUID 12 1 1000 0 f f f t t ...@@ -4083,6 +4083,8 @@ DATA(insert OID = 2599 ( pg_timezone_abbrevs PGNSP PGUID 12 1 1000 0 f f f t t
DESCR("get the available time zone abbreviations"); DESCR("get the available time zone abbreviations");
DATA(insert OID = 2856 ( pg_timezone_names PGNSP PGUID 12 1 1000 0 f f f t t s 0 0 2249 "" "{25,25,1186,16}" "{o,o,o,o}" "{name,abbrev,utc_offset,is_dst}" _null_ pg_timezone_names _null_ _null_ _null_ )); DATA(insert OID = 2856 ( pg_timezone_names PGNSP PGUID 12 1 1000 0 f f f t t s 0 0 2249 "" "{25,25,1186,16}" "{o,o,o,o}" "{name,abbrev,utc_offset,is_dst}" _null_ pg_timezone_names _null_ _null_ _null_ ));
DESCR("get the available time zone names"); DESCR("get the available time zone names");
DATA(insert OID = 2730 ( pg_get_triggerdef PGNSP PGUID 12 1 0 0 f f f t f s 2 0 25 "26 16" _null_ _null_ _null_ _null_ pg_get_triggerdef_ext _null_ _null_ _null_ ));
DESCR("trigger description with pretty-print option");
/* non-persistent series generator */ /* non-persistent series generator */
DATA(insert OID = 1066 ( generate_series PGNSP PGUID 12 1 1000 0 f f f t t i 3 0 23 "23 23 23" _null_ _null_ _null_ _null_ generate_series_step_int4 _null_ _null_ _null_ )); DATA(insert OID = 1066 ( generate_series PGNSP PGUID 12 1 1000 0 f f f t t i 3 0 23 "23 23 23" _null_ _null_ _null_ _null_ generate_series_step_int4 _null_ _null_ _null_ ));
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.339 2009/09/09 19:00:09 petere Exp $ * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.340 2009/10/09 21:02:56 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -590,6 +590,7 @@ extern Datum pg_get_indexdef_ext(PG_FUNCTION_ARGS); ...@@ -590,6 +590,7 @@ extern Datum pg_get_indexdef_ext(PG_FUNCTION_ARGS);
extern char *pg_get_indexdef_string(Oid indexrelid); extern char *pg_get_indexdef_string(Oid indexrelid);
extern char *pg_get_indexdef_columns(Oid indexrelid, bool pretty); extern char *pg_get_indexdef_columns(Oid indexrelid, bool pretty);
extern Datum pg_get_triggerdef(PG_FUNCTION_ARGS); extern Datum pg_get_triggerdef(PG_FUNCTION_ARGS);
extern Datum pg_get_triggerdef_ext(PG_FUNCTION_ARGS);
extern Datum pg_get_constraintdef(PG_FUNCTION_ARGS); extern Datum pg_get_constraintdef(PG_FUNCTION_ARGS);
extern Datum pg_get_constraintdef_ext(PG_FUNCTION_ARGS); extern Datum pg_get_constraintdef_ext(PG_FUNCTION_ARGS);
extern char *pg_get_constraintdef_string(Oid constraintId); extern char *pg_get_constraintdef_string(Oid constraintId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment