public inbox for pgsql-general@postgresql.org
help / color / mirror / Atom feedFrom: vignesh C <vignesh21@gmail.com>
To: Dilip Kumar <dilipbalaut@gmail.com>
Cc: Ajin Cherian <itsajin@gmail.com>
Cc: Zheng Li <zhengli10@gmail.com>
Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: houzj.fnst@fujitsu.com <houzj.fnst@fujitsu.com>
Cc: Peter Smith <smithpb2250@gmail.com>
Cc: Amit Kapila <amit.kapila16@gmail.com>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: Japin Li <japinli@hotmail.com>
Cc: rajesh singarapu <rajesh.rs0541@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Support logical replication of DDLs
Date: Wed, 19 Oct 2022 11:18:05 +0530
Message-ID: <CALDaNm08gZq9a7xnsbaJMmHmi29_kbEuyShHHfxAKLXPh6btWQ@mail.gmail.com> (raw)
In-Reply-To: <CAFiTN-tWkJW-JEGANkK+O3KXUjv_Yb5Tb+r83ujbognHG_brTA@mail.gmail.com>
References: <CAAD30UKg8rXeGM8Oy_MAmxKBL_K5DiHXdeNF=hUefcu1C_6VfQ@mail.gmail.com>
<20221006171601.6um4ey5idm4h62vf@alvherre.pgsql>
<CAAD30UJh+LcUND+zg_A5dbQ_Bi=m_n7qUfKrOwAx2v4jBKWvKQ@mail.gmail.com>
<CAFPTHDbVujj6C3TdMCmoBXovpc4=5Ow3i5M1_HNhmnqdiA5qSA@mail.gmail.com>
<CAAD30UKCCpMTZd4WSSxYu-qRAEFd+0kjXTgje+EXHdo1JTkz0g@mail.gmail.com>
<CALDaNm3F9GvQ9bPrxobhqkUKP3HmrRZGCU3EX0xt3=Ef0-Reaw@mail.gmail.com>
<CAFPTHDY2O42ouQFMeEbPt51CWQ=zyzYhgK6B9basyd5PLaOv0A@mail.gmail.com>
<CAFPTHDZmS2PrOyJx8OAe+Nt-Fx4-GZetJatqCEJhDNXA2orwCg@mail.gmail.com>
<CAFiTN-tWkJW-JEGANkK+O3KXUjv_Yb5Tb+r83ujbognHG_brTA@mail.gmail.com>
On Wed, 12 Oct 2022 at 11:38, Dilip Kumar <dilipbalaut@gmail.com> wrote:
>
> On Tue, Oct 11, 2022 at 7:00 PM Ajin Cherian <itsajin@gmail.com> wrote:
> >
>
> I was going through 0001 patch and I have a few comments/suggestions.
>
> 1.
>
> @@ -6001,7 +6001,7 @@ getObjectIdentityParts(const ObjectAddress *object,
> transformType = format_type_be_qualified(transform->trftype);
> transformLang = get_language_name(transform->trflang, false);
>
> - appendStringInfo(&buffer, "for %s on language %s",
> + appendStringInfo(&buffer, "for %s language %s",
> transformType,
> transformLang);
>
>
> How is this change related to this patch?
This change is required for ddl of transform commands, we have added a
note for the same in the patch:
Removed an undesirable 'on' from the identity string for TRANSFORM in
getObjectIdentityParts. This is needed to make deparse of DROP
TRANSFORM command work since 'on' is not present in the current DROP
TRANSFORM syntax. For example, the correct syntax is
drop transform trf for int language sql;
instead of
drop transform trf for int on language sql;
> 2.
> +typedef struct ObjTree
> +{
> + slist_head params;
> + int numParams;
> + StringInfo fmtinfo;
> + bool present;
> +} ObjTree;
> +
> +typedef struct ObjElem
> +{
> + char *name;
> + ObjType objtype;
> +
> + union
> + {
> + bool boolean;
> + char *string;
> + int64 integer;
> + float8 flt;
> + ObjTree *object;
> + List *array;
> + } value;
> + slist_node node;
> +} ObjElem;
>
> It would be good to explain these structure members from readability pov.
Modified
> 3.
>
> +
> +bool verbose = true;
> +
>
> I do not understand the usage of such global variables. Even if it is
> required, add some comments to explain the purpose of it.
Modified
>
> 4.
> +/*
> + * Given a CollectedCommand, return a JSON representation of it.
> + *
> + * The command is expanded fully, so that there are no ambiguities even in the
> + * face of search_path changes.
> + */
> +Datum
> +ddl_deparse_to_json(PG_FUNCTION_ARGS)
> +{
>
> It will be nice to have a test case for this utility function.
We are discussing how to test in a separate thread at [1]. We will
implement accordingly once it is concluded. This comment will be
handled at that time.
[1] - https://www.postgresql.org/message-id/flat/CAH8n8_jMTunxxtP4L-3tc%3DGNamg%3Dmg1X%3DtgHr9CqqjjzFLwQng...
This patch also includes changes for replication of:
CREATE/ALTER/DROP STATISTICS
and pgindent fixes for the ddl replication code.
Thanks for the comments, the attached v30 patch has the changes for the same.
Regards,
Vignesh
Attachments:
[text/x-patch] v30-0001-Functions-to-deparse-DDL-commands.patch (305.7K, 2-v30-0001-Functions-to-deparse-DDL-commands.patch)
download | inline diff:
From b50ee1969493402b5b2380da4ee411573f838b63 Mon Sep 17 00:00:00 2001
From: Ajin Cherian <ajinc@fast.au.fujitsu.com>
Date: Thu, 13 Oct 2022 08:08:47 -0400
Subject: [PATCH v30 1/4] Functions to deparse DDL commands.
This patch provides JSON blobs representing DDL commands, which can
later be re-processed into plain strings by well-defined sprintf-like
expansion. These JSON objects are intended to allow for machine-editing of
the commands, by replacing certain nodes within the objects.
Much of the information in the output blob actually comes from system
catalogs, not from the command parse node, as it is impossible to reliably
construct a fully-specified command (i.e. one not dependent on search_path
etc) looking only at the parse node.
This provides a base for logical replication of DDL statements. Currently,
the patch has support for:
CREATE/ALTER/DROP TABLE -- Note #1, Note #2
CREATE/ALTER/DROP SEQUENCE
CREATE/ALTER/DROP SCHEMA
CREATE/ALTER FUNCTION/PROCEDURE
CREATE/ALTER TRIGGER
CREATE/DROP INDEX
CREATE/DROP OPERATOR [CLASS/FAMILY]
CREATE/ALTER/DROP CAST
CREATE/ALTER/DROP DOMAIN
CREATE/ALTER/DROP TYPE
CREATE/ALTER/DROP CONVERSION
CREATE/ALTER/DROP POLICY
CREATE/ALTER/DROP EXTENSION
CREATE/ALTER/DROP FOREIGN DATA WRAPPER
CREATE/ALTER/DROP RULE
CREATE/ALTER/DROP SERVER
CREATE/ALTER/DROP COLLATION
GRANT
REVOKE
REFRESH MATERIALIZED VIEW
CREATE/ALTER/DROP TEXT SEARCH CONFIGURATION/PARSER/DICTIONARY/TEMPLATE
CREATE/DROP TRANSFORM
CREATE/ALTER/DROP USER MAPPING
CREATE/ALTER/DROP VIEW -- Note #4
CREATE/ALTER/DROP MATERIALIZED VIEW
CREATE/ALTER/DROP STATISTICS
(Note #1) Note that some recently introduced DDLs(e.g. DDLs related to
PARTITIONED TABLE) are unsupported. We can extend it as we need more
functionality for DDL replication.
(Note #2) Note that, for ATTACH/DETACH PARTITION, we haven't added extra logic on
the subscriber to handle the case where the table on the publisher is a PARTITIONED
TABLE while the target table on the subcriber side is a NORMAL table. We will
research this more and improve it later.
(Note #3) Removed an undesirable 'on' from the identity string for TRANSFORM
in getObjectIdentityParts. This is needed to make deparse of DROP
TRANSFORM command work since 'on' is not present in the current
DROP TRANSFORM syntax. For example, the correct syntax is
drop transform trf for int language sql;
instead of
drop transform trf for int on language sql;
(Note #4) Note that, for VIEWSTMT, We created pg_get_viewdef_internal to get the
SELECT query of the pending VIEWSTMT. In the case that the CREATE VIEW command
is still in progress, we would need to search the system cache RULERELNAME to get the
rewrite rule of the view as oppose to querying pg_rewrite as in pg_get_viewdef_worker(),
the latter will return empty result.
---
src/backend/catalog/aclchk.c | 4 +
src/backend/catalog/objectaddress.c | 2 +-
src/backend/commands/Makefile | 2 +
src/backend/commands/collationcmds.c | 9 +-
src/backend/commands/ddl_deparse.c | 8850 ++++++++++++++++++
src/backend/commands/ddl_json.c | 764 ++
src/backend/commands/meson.build | 2 +
src/backend/commands/sequence.c | 33 +
src/backend/tcop/utility.c | 3 +-
src/backend/utils/adt/format_type.c | 3 +-
src/backend/utils/adt/regproc.c | 52 +
src/backend/utils/adt/ruleutils.c | 252 +-
src/include/catalog/pg_proc.dat | 6 +
src/include/commands/collationcmds.h | 3 +-
src/include/commands/sequence.h | 1 +
src/include/tcop/ddl_deparse.h | 23 +
src/include/tcop/deparse_utility.h | 1 +
src/include/utils/aclchk_internal.h | 1 +
src/include/utils/builtins.h | 2 +
src/include/utils/ruleutils.h | 19 +
src/test/regress/expected/object_address.out | 2 +-
src/tools/pgindent/typedefs.list | 4 +
22 files changed, 9963 insertions(+), 75 deletions(-)
create mode 100755 src/backend/commands/ddl_deparse.c
create mode 100644 src/backend/commands/ddl_json.c
create mode 100644 src/include/tcop/ddl_deparse.h
diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c
index aa5a2ed948..7dc190ddeb 100644
--- a/src/backend/catalog/aclchk.c
+++ b/src/backend/catalog/aclchk.c
@@ -385,7 +385,11 @@ ExecuteGrantStmt(GrantStmt *stmt)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("grantor must be current user")));
+
+ istmt.grantor_uid = grantor;
}
+ else
+ istmt.grantor_uid = InvalidOid;
/*
* Turn the regular GrantStmt into the InternalGrant form.
diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c
index 284ca55469..084acebbe2 100644
--- a/src/backend/catalog/objectaddress.c
+++ b/src/backend/catalog/objectaddress.c
@@ -6001,7 +6001,7 @@ getObjectIdentityParts(const ObjectAddress *object,
transformType = format_type_be_qualified(transform->trftype);
transformLang = get_language_name(transform->trflang, false);
- appendStringInfo(&buffer, "for %s on language %s",
+ appendStringInfo(&buffer, "for %s language %s",
transformType,
transformLang);
if (objname)
diff --git a/src/backend/commands/Makefile b/src/backend/commands/Makefile
index 48f7348f91..171dfb2800 100644
--- a/src/backend/commands/Makefile
+++ b/src/backend/commands/Makefile
@@ -29,6 +29,8 @@ OBJS = \
copyto.o \
createas.o \
dbcommands.o \
+ ddl_deparse.o \
+ ddl_json.o \
define.o \
discard.o \
dropcmds.o \
diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c
index fcfc02d2ae..bb21ccd3f1 100644
--- a/src/backend/commands/collationcmds.c
+++ b/src/backend/commands/collationcmds.c
@@ -50,7 +50,8 @@ typedef struct
* CREATE COLLATION
*/
ObjectAddress
-DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_exists)
+DefineCollation(ParseState *pstate, List *names, List *parameters,
+ bool if_not_exists, ObjectAddress *from_collid)
{
char *collName;
Oid collNamespace;
@@ -137,6 +138,12 @@ DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_e
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for collation %u", collid);
+ /* make from existing collationid available to callers */
+ if (from_collid && OidIsValid(collid))
+ ObjectAddressSet(*from_collid,
+ CollationRelationId,
+ collid);
+
collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
collisdeterministic = ((Form_pg_collation) GETSTRUCT(tp))->collisdeterministic;
collencoding = ((Form_pg_collation) GETSTRUCT(tp))->collencoding;
diff --git a/src/backend/commands/ddl_deparse.c b/src/backend/commands/ddl_deparse.c
new file mode 100755
index 0000000000..a4ab3afca3
--- /dev/null
+++ b/src/backend/commands/ddl_deparse.c
@@ -0,0 +1,8850 @@
+/*-------------------------------------------------------------------------
+ *
+ * ddl_deparse.c
+ * Functions to convert utility commands to machine-parseable
+ * representation
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * NOTES
+ *
+ * This is intended to provide JSON blobs representing DDL commands, which can
+ * later be re-processed into plain strings by well-defined sprintf-like
+ * expansion. These JSON objects are intended to allow for machine-editing of
+ * the commands, by replacing certain nodes within the objects.
+ *
+ * Much of the information in the output blob actually comes from system
+ * catalogs, not from the command parse node, as it is impossible to reliably
+ * construct a fully-specified command (i.e. one not dependent on search_path
+ * etc) looking only at the parse node.
+ *
+ * IDENTIFICATION
+ * src/backend/commands/ddl_deparse.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+#include "tcop/ddl_deparse.h"
+#include "access/amapi.h"
+#include "access/table.h"
+#include "access/relation.h"
+#include "catalog/namespace.h"
+#include "catalog/pg_am.h"
+#include "catalog/pg_attribute.h"
+#include "catalog/pg_authid.h"
+#include "catalog/pg_cast.h"
+#include "catalog/pg_class.h"
+#include "catalog/pg_collation.h"
+#include "catalog/pg_constraint.h"
+#include "catalog/pg_conversion.h"
+#include "catalog/pg_depend.h"
+#include "catalog/pg_extension.h"
+#include "catalog/pg_foreign_data_wrapper.h"
+#include "catalog/pg_foreign_server.h"
+#include "catalog/pg_inherits.h"
+#include "catalog/pg_language.h"
+#include "catalog/pg_largeobject.h"
+#include "catalog/pg_namespace.h"
+#include "catalog/pg_opclass.h"
+#include "catalog/pg_operator.h"
+#include "catalog/pg_opfamily.h"
+#include "catalog/pg_policy.h"
+#include "catalog/pg_proc.h"
+#include "catalog/pg_range.h"
+#include "catalog/pg_rewrite.h"
+#include "catalog/pg_sequence.h"
+#include "catalog/pg_statistic_ext.h"
+#include "catalog/pg_transform.h"
+#include "catalog/pg_ts_config.h"
+#include "catalog/pg_ts_dict.h"
+#include "catalog/pg_ts_parser.h"
+#include "catalog/pg_ts_template.h"
+#include "catalog/pg_type.h"
+#include "catalog/pg_user_mapping.h"
+#include "commands/defrem.h"
+#include "commands/sequence.h"
+#include "commands/tablespace.h"
+#include "foreign/foreign.h"
+#include "funcapi.h"
+#include "lib/ilist.h"
+#include "mb/pg_wchar.h"
+#include "nodes/makefuncs.h"
+#include "nodes/nodeFuncs.h"
+#include "nodes/parsenodes.h"
+#include "optimizer/optimizer.h"
+#include "rewrite/rewriteHandler.h"
+#include "utils/builtins.h"
+#include "utils/fmgroids.h"
+#include "utils/guc.h"
+#include "utils/jsonb.h"
+#include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/rel.h"
+#include "utils/ruleutils.h"
+#include "utils/syscache.h"
+
+/* Estimated length of the generated jsonb string */
+#define JSONB_ESTIMATED_LEN 128
+
+/*
+ * Before they are turned into JSONB representation, each command is
+ * represented as an object tree, using the structs below.
+ */
+typedef enum
+{
+ ObjTypeNull,
+ ObjTypeBool,
+ ObjTypeString,
+ ObjTypeArray,
+ ObjTypeInteger,
+ ObjTypeFloat,
+ ObjTypeObject
+} ObjType;
+
+/*
+ * Represent the command as an object tree.
+ */
+typedef struct ObjTree
+{
+ slist_head params; /* Object tree parameters */
+ int numParams; /* Number of parameters in the object tree */
+ StringInfo fmtinfo; /* Format string of the ObjTree */
+ bool present; /* Indicates if boolean value should be stored */
+} ObjTree;
+
+/*
+ * An element of an object tree(ObjTree).
+ */
+typedef struct ObjElem
+{
+ char *name; /* Name of object element */
+ ObjType objtype; /* Object type */
+
+ union
+ {
+ bool boolean;
+ char *string;
+ int64 integer;
+ float8 flt;
+ ObjTree *object;
+ List *array;
+ } value; /* Store the object value based on the object
+ * type */
+ slist_node node; /* Used in converting back to ObjElem
+ * structure */
+} ObjElem;
+
+/*
+ * Reduce some unncessary string from the output json stuff when verbose
+ * and "present" member is false. This means these strings won't be merged into
+ * the last DDL command.
+ */
+bool verbose = true;
+
+static void append_array_object(ObjTree *tree, char *name, List *array);
+static void append_bool_object(ObjTree *tree, char *name, bool value);
+static void append_float_object(ObjTree *tree, char *name, float8 value);
+static void append_null_object(ObjTree *tree, char *name);
+static void append_object_object(ObjTree *tree, char *name, ObjTree *value);
+static char *append_object_to_format_string(ObjTree *tree, char *sub_fmt);
+static void append_premade_object(ObjTree *tree, ObjElem *elem);
+static void append_string_object(ObjTree *tree, char *name, char *value);
+static void format_type_detailed(Oid type_oid, int32 typemod,
+ Oid *nspid, char **typname, char **typemodstr,
+ bool *typarray);
+static List *FunctionGetDefaults(text *proargdefaults);
+static ObjElem *new_object(ObjType type, char *name);
+static ObjTree *new_objtree_for_qualname_id(Oid classId, Oid objectId);
+static ObjTree *new_objtree_for_rolespec(RoleSpec *spec);
+static ObjElem *new_object_object(ObjTree *value);
+static ObjTree *new_objtree_VA(char *fmt, int numobjs,...);
+static ObjElem *new_string_object(char *value);
+static JsonbValue *objtree_to_jsonb_rec(ObjTree *tree, JsonbParseState *state);
+static void pg_get_indexdef_detailed(Oid indexrelid,
+ char **index_am,
+ char **definition,
+ char **reloptions,
+ char **tablespace,
+ char **whereClause);
+static char *RelationGetColumnDefault(Relation rel, AttrNumber attno,
+ List *dpcontext, List **exprs);
+static const char *stringify_objtype(ObjectType objtype);
+
+static ObjTree *deparse_ColumnDef(Relation relation, List *dpcontext, bool composite,
+ ColumnDef *coldef, bool is_alter, List **exprs);
+static ObjTree *deparse_ColumnIdentity(Oid seqrelid, char identity, bool alter_table);
+static ObjTree *deparse_ColumnSetOptions(AlterTableCmd *subcmd);
+static ObjTree *deparse_DefineStmt_Collation(Oid objectId, DefineStmt *define,
+ ObjectAddress fromCollid);
+static ObjTree *deparse_DefineStmt_Operator(Oid objectId, DefineStmt *define);
+static ObjTree *deparse_DefineStmt_Type(Oid objectId, DefineStmt *define);
+static ObjTree *deparse_DefineStmt_TSConfig(Oid objectId, DefineStmt *define, ObjectAddress copied);
+static ObjTree *deparse_DefineStmt_TSParser(Oid objectId, DefineStmt *define);
+static ObjTree *deparse_DefineStmt_TSDictionary(Oid objectId, DefineStmt *define);
+static ObjTree *deparse_DefineStmt_TSTemplate(Oid objectId, DefineStmt *define);
+
+static ObjTree *deparse_DefElem(DefElem *elem, bool is_reset);
+static ObjTree *deparse_FunctionSet(VariableSetKind kind, char *name, char *value);
+static ObjTree *deparse_OnCommitClause(OnCommitAction option);
+static ObjTree *deparse_RelSetOptions(AlterTableCmd *subcmd);
+
+static inline ObjElem *deparse_Seq_Cache(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table);
+static inline ObjElem *deparse_Seq_Cycle(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table);
+static inline ObjElem *deparse_Seq_IncrementBy(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table);
+static inline ObjElem *deparse_Seq_Minvalue(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table);
+static inline ObjElem *deparse_Seq_Maxvalue(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table);
+static ObjElem *deparse_Seq_OwnedBy(ObjTree *parent, Oid sequenceId, bool alter_table);
+static inline ObjElem *deparse_Seq_Restart(ObjTree *parent, Form_pg_sequence_data seqdata);
+static inline ObjElem *deparse_Seq_Startwith(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table);
+static inline ObjElem *deparse_Type_Storage(ObjTree *parent, Form_pg_type typForm);
+static inline ObjElem *deparse_Type_Receive(ObjTree *parent, Form_pg_type typForm);
+static inline ObjElem *deparse_Type_Send(ObjTree *parent, Form_pg_type typForm);
+static inline ObjElem *deparse_Type_Typmod_In(ObjTree *parent, Form_pg_type typForm);
+static inline ObjElem *deparse_Type_Typmod_Out(ObjTree *parent, Form_pg_type typForm);
+static inline ObjElem *deparse_Type_Analyze(ObjTree *parent, Form_pg_type typForm);
+static inline ObjElem *deparse_Type_Subscript(ObjTree *parent, Form_pg_type typForm);
+
+static List *deparse_InhRelations(Oid objectId);
+static List *deparse_TableElements(Relation relation, List *tableElements, List *dpcontext,
+ bool typed, bool composite);
+
+/*
+ * Add common clauses to CreatePolicy or AlterPolicy deparse objects
+ */
+static void
+add_policy_clauses(ObjTree *policyStmt, Oid policyOid, List *roles,
+ bool do_qual, bool do_with_check)
+{
+ Relation polRel = table_open(PolicyRelationId, AccessShareLock);
+ HeapTuple polTup = get_catalog_object_by_oid(polRel, Anum_pg_policy_oid, policyOid);
+ Form_pg_policy polForm;
+
+ if (!HeapTupleIsValid(polTup))
+ elog(ERROR, "cache lookup failed for policy %u", policyOid);
+
+ polForm = (Form_pg_policy) GETSTRUCT(polTup);
+
+ /* add the "ON table" clause */
+ append_object_object(policyStmt, "ON %{table}D",
+ new_objtree_for_qualname_id(RelationRelationId,
+ polForm->polrelid));
+
+ /*
+ * Add the "TO role" clause, if any. In the CREATE case, it always
+ * contains at least PUBLIC, but in the ALTER case it might be empty.
+ */
+ if (roles)
+ {
+ List *list = NIL;
+ ListCell *cell;
+
+ foreach(cell, roles)
+ {
+ RoleSpec *spec = (RoleSpec *) lfirst(cell);
+
+ list = lappend(list,
+ new_object_object(new_objtree_for_rolespec(spec)));
+ }
+ append_array_object(policyStmt, "TO %{role:, }R", list);
+ }
+ else
+ {
+ append_bool_object(policyStmt, "present", false);
+ }
+
+ /* add the USING clause, if any */
+ if (do_qual)
+ {
+ Datum deparsed;
+ Datum storedexpr;
+ bool isnull;
+
+ storedexpr = heap_getattr(polTup, Anum_pg_policy_polqual,
+ RelationGetDescr(polRel), &isnull);
+ if (isnull)
+ elog(ERROR, "invalid NULL polqual expression in policy %u", policyOid);
+ deparsed = DirectFunctionCall2(pg_get_expr, storedexpr, polForm->polrelid);
+ append_string_object(policyStmt, "USING (%{expression}s)",
+ TextDatumGetCString(deparsed));
+ }
+ else
+ append_bool_object(policyStmt, "present", false);
+
+ /* add the WITH CHECK clause, if any */
+ if (do_with_check)
+ {
+ Datum deparsed;
+ Datum storedexpr;
+ bool isnull;
+
+ storedexpr = heap_getattr(polTup, Anum_pg_policy_polwithcheck,
+ RelationGetDescr(polRel), &isnull);
+ if (isnull)
+ elog(ERROR, "invalid NULL polwithcheck expression in policy %u", policyOid);
+ deparsed = DirectFunctionCall2(pg_get_expr, storedexpr, polForm->polrelid);
+ append_string_object(policyStmt, "WITH CHECK (%{expression}s)",
+ TextDatumGetCString(deparsed));
+ }
+ else
+ append_bool_object(policyStmt, "present", false);
+
+ relation_close(polRel, AccessShareLock);
+}
+
+/* Append an array parameter to a tree */
+static void
+append_array_object(ObjTree *tree, char *sub_fmt, List *array)
+{
+ ObjElem *param;
+ char *object_name;
+
+ Assert(sub_fmt);
+
+ if (list_length(array) == 0)
+ return;
+
+ if (!verbose)
+ {
+ ListCell *lc;
+
+ /* Extract the ObjElems whose present flag is true */
+ foreach(lc, array)
+ {
+ ObjElem *elem = (ObjElem *) lfirst(lc);
+
+ Assert(elem->objtype == ObjTypeObject ||
+ elem->objtype == ObjTypeString);
+
+ if (!elem->value.object->present &&
+ elem->objtype == ObjTypeObject)
+ array = foreach_delete_current(array, lc);
+ }
+
+ }
+
+ object_name = append_object_to_format_string(tree, sub_fmt);
+
+ param = new_object(ObjTypeArray, object_name);
+ param->value.array = array;
+ append_premade_object(tree, param);
+}
+
+/* Append a boolean parameter to a tree */
+static void
+append_bool_object(ObjTree *tree, char *sub_fmt, bool value)
+{
+ ObjElem *param;
+ char *object_name = sub_fmt;
+ bool is_present_flag = false;
+
+ Assert(sub_fmt);
+
+ /*
+ * Check if the present is part of the format string and store the boolean
+ * value
+ */
+ if (strcmp(sub_fmt, "present") == 0)
+ {
+ is_present_flag = true;
+ tree->present = value;
+ }
+
+ if (!verbose && !tree->present)
+ return;
+
+ if (!is_present_flag)
+ object_name = append_object_to_format_string(tree, sub_fmt);
+
+ param = new_object(ObjTypeBool, object_name);
+ param->value.boolean = value;
+ append_premade_object(tree, param);
+}
+
+/*
+ * Append a float8 parameter to a tree.
+ */
+static void
+append_float_object(ObjTree *tree, char *sub_fmt, float8 value)
+{
+ ObjElem *param;
+ char *object_name;
+
+ Assert(sub_fmt);
+
+ object_name = append_object_to_format_string(tree, sub_fmt);
+
+ param = new_object(ObjTypeFloat, object_name);
+ param->value.flt = value;
+ append_premade_object(tree, param);
+}
+
+/*
+ * Append the input format string to the ObjTree.
+ */
+static void
+append_format_string(ObjTree *tree, char *sub_fmt)
+{
+ int len;
+ char *fmt;
+
+ if (tree->fmtinfo == NULL)
+ return;
+
+ fmt = tree->fmtinfo->data;
+ len = tree->fmtinfo->len;
+
+ /* Add a separator if necessary */
+ if (len > 0 && fmt[len - 1] != ' ')
+ appendStringInfoSpaces(tree->fmtinfo, 1);
+
+ appendStringInfoString(tree->fmtinfo, sub_fmt);
+}
+
+/* Append a NULL object to a tree */
+static void
+append_null_object(ObjTree *tree, char *sub_fmt)
+{
+ char *object_name;
+
+ Assert(sub_fmt);
+
+ if (!verbose)
+ return;
+
+ object_name = append_object_to_format_string(tree, sub_fmt);
+
+ append_premade_object(tree, new_object(ObjTypeNull, object_name));
+}
+
+/* Append an object parameter to a tree */
+static void
+append_object_object(ObjTree *tree, char *sub_fmt, ObjTree *value)
+{
+ ObjElem *param;
+ char *object_name;
+
+ Assert(sub_fmt);
+
+ if (!verbose && !value->present)
+ return;
+
+ object_name = append_object_to_format_string(tree, sub_fmt);
+
+ param = new_object(ObjTypeObject, object_name);
+ param->value.object = value;
+ append_premade_object(tree, param);
+}
+
+/*
+ * Return the object name which is extracted from the input "*%{name[:.]}*"
+ * style string. And append the input format string to the ObjTree.
+ */
+static char *
+append_object_to_format_string(ObjTree *tree, char *sub_fmt)
+{
+ StringInfoData object_name;
+ const char *end_ptr;
+ const char *cp;
+ bool start_copy = false;
+
+ if (sub_fmt == NULL || tree->fmtinfo == NULL)
+ return sub_fmt;
+
+ initStringInfo(&object_name);
+ end_ptr = sub_fmt + strlen(sub_fmt);
+
+ for (cp = sub_fmt; cp < end_ptr; cp++)
+ {
+ if (*cp == '{')
+ {
+ start_copy = true;
+ continue;
+ }
+
+ if (!start_copy)
+ continue;
+
+ if (*cp == ':' || *cp == '}')
+ break;
+
+ appendStringInfoCharMacro(&object_name, *cp);
+ }
+
+ if (object_name.len == 0)
+ elog(ERROR, "object name not found");
+
+ append_format_string(tree, sub_fmt);
+
+ return object_name.data;
+}
+
+/* Append a preallocated parameter to a tree */
+static inline void
+append_premade_object(ObjTree *tree, ObjElem *elem)
+{
+ slist_push_head(&tree->params, &elem->node);
+ tree->numParams++;
+}
+
+/* Append a string parameter to a tree */
+static void
+append_string_object(ObjTree *tree, char *sub_fmt, char *value)
+{
+ ObjElem *param;
+ char *object_name;
+
+ Assert(sub_fmt);
+
+ if (!verbose && (value == NULL || value[0] == '\0'))
+ return;
+
+ object_name = append_object_to_format_string(tree, sub_fmt);
+
+ param = new_object(ObjTypeString, object_name);
+ param->value.string = value;
+ append_premade_object(tree, param);
+}
+
+/*
+ * Similar to format_type_extended, except we return each bit of information
+ * separately:
+ *
+ * - nspid is the schema OID. For certain SQL-standard types which have weird
+ * typmod rules, we return InvalidOid; the caller is expected to not schema-
+ * qualify the name nor add quotes to the type name in this case.
+ *
+ * - typname is set to the type name, without quotes
+ *
+ * - typemodstr is set to the typemod, if any, as a string with parentheses
+ *
+ * - typarray indicates whether []s must be added
+ *
+ * We don't try to decode type names to their standard-mandated names, except
+ * in the cases of types with unusual typmod rules.
+ */
+static void
+format_type_detailed(Oid type_oid, int32 typemod,
+ Oid *nspid, char **typname, char **typemodstr,
+ bool *typarray)
+{
+ HeapTuple tuple;
+ Form_pg_type typeform;
+ Oid array_base_type;
+
+ tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for type %u", type_oid);
+
+ typeform = (Form_pg_type) GETSTRUCT(tuple);
+
+ /*
+ * Check if it's a regular (variable length) array type. As above,
+ * fixed-length array types such as "name" shouldn't get deconstructed.
+ */
+ array_base_type = typeform->typelem;
+
+ *typarray = (IsTrueArrayType(typeform) && typeform->typstorage != TYPSTORAGE_PLAIN);
+
+ if (*typarray)
+ {
+ /* Switch our attention to the array element type */
+ ReleaseSysCache(tuple);
+ tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for type %u", type_oid);
+
+ typeform = (Form_pg_type) GETSTRUCT(tuple);
+ type_oid = array_base_type;
+ }
+
+ /*
+ * Special-case crock for types with strange typmod rules where we put
+ * typmod at the middle of name(e.g. TIME(6) with time zone ). We cannot
+ * schema-qualify nor add quotes to the type name in these cases.
+ */
+ if (type_oid == INTERVALOID ||
+ type_oid == TIMESTAMPOID ||
+ type_oid == TIMESTAMPTZOID ||
+ type_oid == TIMEOID ||
+ type_oid == TIMETZOID)
+ {
+ switch (type_oid)
+ {
+ case INTERVALOID:
+ *typname = pstrdup("INTERVAL");
+ break;
+ case TIMESTAMPTZOID:
+ if (typemod < 0)
+ *typname = pstrdup("TIMESTAMP WITH TIME ZONE");
+ else
+ /* otherwise, WITH TZ is added by typmod. */
+ *typname = pstrdup("TIMESTAMP");
+ break;
+ case TIMESTAMPOID:
+ *typname = pstrdup("TIMESTAMP");
+ break;
+ case TIMETZOID:
+ if (typemod < 0)
+ *typname = pstrdup("TIME WITH TIME ZONE");
+ else
+ /* otherwise, WITH TZ is added by typmod. */
+ *typname = pstrdup("TIME");
+ break;
+ case TIMEOID:
+ *typname = pstrdup("TIME");
+ break;
+ }
+ *nspid = InvalidOid;
+ }
+ else
+ {
+ /*
+ * No additional processing is required for other types, so get the
+ * type name and schema directly from the catalog.
+ */
+ *nspid = typeform->typnamespace;
+ *typname = pstrdup(NameStr(typeform->typname));
+ }
+
+ if (typemod >= 0)
+ *typemodstr = printTypmod("", typemod, typeform->typmodout);
+ else
+ *typemodstr = pstrdup("");
+
+ ReleaseSysCache(tuple);
+}
+
+/*
+ * Return the defaults values of arguments to a function, as a list of
+ * deparsed expressions.
+ */
+static List *
+FunctionGetDefaults(text *proargdefaults)
+{
+ List *nodedefs;
+ List *strdefs = NIL;
+ ListCell *cell;
+
+ nodedefs = (List *) stringToNode(text_to_cstring(proargdefaults));
+ if (!IsA(nodedefs, List))
+ elog(ERROR, "proargdefaults is not a list");
+
+ foreach(cell, nodedefs)
+ {
+ Node *onedef = lfirst(cell);
+
+ strdefs = lappend(strdefs, deparse_expression(onedef, NIL, false, false));
+ }
+
+ return strdefs;
+}
+
+/*
+ * Return the string representation of the given RELPERSISTENCE value.
+ */
+static char *
+get_persistence_str(char persistence)
+{
+ switch (persistence)
+ {
+ case RELPERSISTENCE_TEMP:
+ return "TEMPORARY";
+ case RELPERSISTENCE_UNLOGGED:
+ return "UNLOGGED";
+ case RELPERSISTENCE_PERMANENT:
+ return "";
+ default:
+ elog(ERROR, "unexpected persistence marking %c", persistence);
+ return ""; /* make compiler happy */
+ }
+}
+
+/* Allocate a new parameter */
+static ObjElem *
+new_object(ObjType type, char *name)
+{
+ ObjElem *param;
+
+ param = palloc0(sizeof(ObjElem));
+ param->name = name;
+ param->objtype = type;
+
+ return param;
+}
+
+/* Allocate a new object parameter */
+static ObjElem *
+new_object_object(ObjTree *value)
+{
+ ObjElem *param;
+
+ param = new_object(ObjTypeObject, NULL);
+ param->value.object = value;
+
+ return param;
+}
+
+/*
+ * Allocate a new object tree to store parameter values.
+ */
+static ObjTree *
+new_objtree(char *fmt)
+{
+ ObjTree *params;
+
+ params = palloc0(sizeof(ObjTree));
+ params->present = true;
+ slist_init(¶ms->params);
+
+ if (fmt)
+ {
+ params->fmtinfo = makeStringInfo();
+ appendStringInfoString(params->fmtinfo, fmt);
+ }
+
+ return params;
+}
+
+/*
+ * A helper routine to set up %{}D and %{}O elements.
+ *
+ * Elements "schemaname" and "objname" are set. If the namespace OID
+ * corresponds to a temp schema, that's set to "pg_temp".
+ *
+ * The difference between those two element types is whether the objname will
+ * be quoted as an identifier or not, which is not something that this routine
+ * concerns itself with; that will be up to the expand function.
+ */
+static ObjTree *
+new_objtree_for_qualname(Oid nspid, char *name)
+{
+ ObjTree *qualified;
+ char *namespace;
+
+ if (isAnyTempNamespace(nspid))
+ namespace = pstrdup("pg_temp");
+ else
+ namespace = get_namespace_name(nspid);
+
+ qualified = new_objtree_VA(NULL, 2,
+ "schemaname", ObjTypeString, namespace,
+ "objname", ObjTypeString, pstrdup(name));
+
+ return qualified;
+}
+
+/*
+ * A helper routine to set up %{}D and %{}O elements, with the object specified
+ * by classId/objId.
+ *
+ */
+static ObjTree *
+new_objtree_for_qualname_id(Oid classId, Oid objectId)
+{
+ ObjTree *qualified;
+ Relation catalog;
+ HeapTuple catobj;
+ Datum objnsp;
+ Datum objname;
+ AttrNumber Anum_name;
+ AttrNumber Anum_namespace;
+ AttrNumber Anum_oid = get_object_attnum_oid(classId);
+ bool isnull;
+
+ catalog = table_open(classId, AccessShareLock);
+
+ catobj = get_catalog_object_by_oid(catalog, Anum_oid, objectId);
+ if (!catobj)
+ elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
+ objectId, RelationGetRelationName(catalog));
+ Anum_name = get_object_attnum_name(classId);
+ Anum_namespace = get_object_attnum_namespace(classId);
+
+ objnsp = heap_getattr(catobj, Anum_namespace, RelationGetDescr(catalog),
+ &isnull);
+ if (isnull)
+ elog(ERROR, "unexpected NULL namespace");
+
+ objname = heap_getattr(catobj, Anum_name, RelationGetDescr(catalog),
+ &isnull);
+ if (isnull)
+ elog(ERROR, "unexpected NULL name");
+
+ qualified = new_objtree_for_qualname(DatumGetObjectId(objnsp),
+ NameStr(*DatumGetName(objname)));
+ table_close(catalog, AccessShareLock);
+
+ return qualified;
+}
+
+/*
+ * Helper routine for %{}R objects, with role specified by RoleSpec node.
+ * Special values such as ROLESPEC_CURRENT_USER are expanded to their final
+ * names.
+ */
+static ObjTree *
+new_objtree_for_rolespec(RoleSpec *spec)
+{
+ ObjTree *role;
+ char *roletype;
+
+ if (spec->roletype != ROLESPEC_PUBLIC)
+ roletype = get_rolespec_name(spec);
+ else
+ roletype = pstrdup("");
+
+ role = new_objtree_VA(NULL, 2,
+ "is_public", ObjTypeBool, spec->roletype == ROLESPEC_PUBLIC,
+ "rolename", ObjTypeString, roletype);
+ return role;
+}
+
+/*
+ * Helper routine for %{}R objects, with role specified by OID. (ACL_ID_PUBLIC
+ * means to use "public").
+ */
+static ObjTree *
+new_objtree_for_role_id(Oid roleoid)
+{
+ ObjTree *role;
+
+ role = new_objtree("");
+
+ if (roleoid != ACL_ID_PUBLIC)
+ {
+ HeapTuple roltup;
+ char *rolename;
+
+ roltup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleoid));
+ if (!HeapTupleIsValid(roltup))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("role with OID %u does not exist", roleoid)));
+
+ rolename = NameStr(((Form_pg_authid) GETSTRUCT(roltup))->rolname);
+ append_string_object(role, "%{rolename}I", pstrdup(rolename));
+
+ ReleaseSysCache(roltup);
+ }
+ else
+ append_string_object(role, "%{rolename}I", "public");
+
+ return role;
+}
+
+/*
+ * A helper routine to setup %{}T elements.
+ */
+static ObjTree *
+new_objtree_for_type(Oid typeId, int32 typmod)
+{
+ ObjTree *typeParam;
+ Oid typnspid;
+ char *typnsp;
+ char *typename = NULL;
+ char *typmodstr;
+ bool typarray;
+
+ format_type_detailed(typeId, typmod,
+ &typnspid, &typename, &typmodstr, &typarray);
+
+ if (OidIsValid(typnspid))
+ typnsp = get_namespace_name_or_temp(typnspid);
+ else
+ typnsp = pstrdup("");
+
+ typeParam = new_objtree_VA(NULL, 4,
+ "schemaname", ObjTypeString, typnsp,
+ "typename", ObjTypeString, typename,
+ "typmod", ObjTypeString, typmodstr,
+ "typarray", ObjTypeBool, typarray);
+
+ return typeParam;
+}
+
+/*
+ * Allocate a new object tree to store parameter values -- varargs version.
+ *
+ * The "fmt" argument is used to append as a "fmt" element in the output blob.
+ * numobjs indicates the number of extra elements to append; for each one, a
+ * name (string), type (from the ObjType enum) and value must be supplied. The
+ * value must match the type given; for instance, ObjTypeInteger requires an
+ * int64, ObjTypeString requires a char *, ObjTypeArray requires a list (of
+ * ObjElem), ObjTypeObject requires an ObjTree, and so on. Each element type *
+ * must match the conversion specifier given in the format string, as described
+ * in ddl_deparse_expand_command, q.v.
+ *
+ * Note we don't have the luxury of sprintf-like compiler warnings for
+ * malformed argument lists.
+ */
+static ObjTree *
+new_objtree_VA(char *fmt, int numobjs,...)
+{
+ ObjTree *tree;
+ va_list args;
+ int i;
+
+ /* Set up the toplevel object and its "fmt" */
+ tree = new_objtree(fmt);
+
+ /* And process the given varargs */
+ va_start(args, numobjs);
+ for (i = 0; i < numobjs; i++)
+ {
+ char *name;
+ ObjType type;
+ ObjElem *elem;
+
+ name = va_arg(args, char *);
+ type = va_arg(args, ObjType);
+ elem = new_object(type, NULL);
+
+ /*
+ * For all other param types there must be a value in the varargs.
+ * Fetch it and add the fully formed subobject into the main object.
+ */
+ switch (type)
+ {
+ case ObjTypeNull:
+ /* Null params don't have a value (obviously) */
+ break;
+ case ObjTypeBool:
+ elem->value.boolean = va_arg(args, int);
+ break;
+ case ObjTypeString:
+ elem->value.string = va_arg(args, char *);
+ break;
+ case ObjTypeArray:
+ elem->value.array = va_arg(args, List *);
+ break;
+ case ObjTypeInteger:
+ elem->value.integer = va_arg(args, int64);
+ break;
+ case ObjTypeFloat:
+ elem->value.flt = va_arg(args, double);
+ break;
+ case ObjTypeObject:
+ elem->value.object = va_arg(args, ObjTree *);
+ break;
+ default:
+ elog(ERROR, "invalid ObjTree element type %d", type);
+ }
+
+ elem->name = name;
+ append_premade_object(tree, elem);
+ }
+
+ va_end(args);
+ return tree;
+}
+
+/* Allocate a new string object */
+static ObjElem *
+new_string_object(char *value)
+{
+ ObjElem *param;
+
+ Assert(value);
+
+ param = new_object(ObjTypeString, NULL);
+ param->value.string = value;
+
+ return param;
+}
+
+/*
+ * Process the pre-built format string from the ObjTree into the output parse
+ * state.
+ */
+static void
+objtree_fmt_to_jsonb_element(JsonbParseState *state, ObjTree *tree)
+{
+ JsonbValue key;
+ JsonbValue val;
+
+ if (tree->fmtinfo == NULL)
+ return;
+
+ /* Push the key first */
+ key.type = jbvString;
+ key.val.string.val = "fmt";
+ key.val.string.len = strlen(key.val.string.val);
+ pushJsonbValue(&state, WJB_KEY, &key);
+
+ /* Then process the pre-built format string */
+ val.type = jbvString;
+ val.val.string.len = tree->fmtinfo->len;
+ val.val.string.val = tree->fmtinfo->data;
+ pushJsonbValue(&state, WJB_VALUE, &val);
+}
+
+/*
+ * Create a JSONB representation from an ObjTree.
+ */
+static Jsonb *
+objtree_to_jsonb(ObjTree *tree)
+{
+ JsonbValue *value;
+
+ value = objtree_to_jsonb_rec(tree, NULL);
+ return JsonbValueToJsonb(value);
+}
+
+/*
+ * Helper for objtree_to_jsonb: process an individual element from an object or
+ * an array into the output parse state.
+ */
+static void
+objtree_to_jsonb_element(JsonbParseState *state, ObjElem *object,
+ JsonbIteratorToken elem_token)
+{
+ ListCell *cell;
+ JsonbValue val;
+
+ switch (object->objtype)
+ {
+ case ObjTypeNull:
+ val.type = jbvNull;
+ pushJsonbValue(&state, elem_token, &val);
+ break;
+
+ case ObjTypeString:
+ val.type = jbvString;
+ val.val.string.len = strlen(object->value.string);
+ val.val.string.val = object->value.string;
+ pushJsonbValue(&state, elem_token, &val);
+ break;
+
+ case ObjTypeInteger:
+ val.type = jbvNumeric;
+ val.val.numeric = (Numeric)
+ DatumGetNumeric(DirectFunctionCall1(int8_numeric,
+ object->value.integer));
+ pushJsonbValue(&state, elem_token, &val);
+ break;
+
+ case ObjTypeFloat:
+ val.type = jbvNumeric;
+ val.val.numeric = (Numeric)
+ DatumGetNumeric(DirectFunctionCall1(float8_numeric,
+ object->value.integer));
+ pushJsonbValue(&state, elem_token, &val);
+ break;
+
+ case ObjTypeBool:
+ val.type = jbvBool;
+ val.val.boolean = object->value.boolean;
+ pushJsonbValue(&state, elem_token, &val);
+ break;
+
+ case ObjTypeObject:
+ /* Recursively add the object into the existing parse state */
+ objtree_to_jsonb_rec(object->value.object, state);
+ break;
+
+ case ObjTypeArray:
+ pushJsonbValue(&state, WJB_BEGIN_ARRAY, NULL);
+ foreach(cell, object->value.array)
+ {
+ ObjElem *elem = lfirst(cell);
+
+ objtree_to_jsonb_element(state, elem, WJB_ELEM);
+ }
+ pushJsonbValue(&state, WJB_END_ARRAY, NULL);
+ break;
+
+ default:
+ elog(ERROR, "unrecognized object type %d", object->objtype);
+ break;
+ }
+}
+
+/*
+ * Recursive helper for objtree_to_jsonb.
+ */
+static JsonbValue *
+objtree_to_jsonb_rec(ObjTree *tree, JsonbParseState *state)
+{
+ slist_iter iter;
+
+ pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
+
+ objtree_fmt_to_jsonb_element(state, tree);
+
+ slist_foreach(iter, &tree->params)
+ {
+ ObjElem *object = slist_container(ObjElem, node, iter.cur);
+ JsonbValue key;
+
+ /* Push the key first */
+ key.type = jbvString;
+ key.val.string.len = strlen(object->name);
+ key.val.string.val = object->name;
+ pushJsonbValue(&state, WJB_KEY, &key);
+
+ /* Then process the value according to its type */
+ objtree_to_jsonb_element(state, object, WJB_VALUE);
+ }
+
+ return pushJsonbValue(&state, WJB_END_OBJECT, NULL);
+}
+
+/*
+ * Subroutine for CREATE TABLE/CREATE DOMAIN deparsing.
+ *
+ * Given a table OID or domain OID, obtain its constraints and append them to
+ * the given elements list. The updated list is returned.
+ *
+ * This works for typed tables, regular tables, and domains.
+ *
+ * Note that CONSTRAINT_FOREIGN constraints are always ignored.
+ */
+static List *
+obtainConstraints(List *elements, Oid relationId, Oid domainId)
+{
+ Relation conRel;
+ ScanKeyData key;
+ SysScanDesc scan;
+ HeapTuple tuple;
+ ObjTree *constr;
+
+ /* Only one may be valid */
+ Assert(OidIsValid(relationId) ^ OidIsValid(domainId));
+
+ /*
+ * Scan pg_constraint to fetch all constraints linked to the given
+ * relation.
+ */
+ conRel = table_open(ConstraintRelationId, AccessShareLock);
+ if (OidIsValid(relationId))
+ {
+ ScanKeyInit(&key,
+ Anum_pg_constraint_conrelid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(relationId));
+ scan = systable_beginscan(conRel, ConstraintRelidTypidNameIndexId,
+ true, NULL, 1, &key);
+ }
+ else
+ {
+ Assert(OidIsValid(domainId));
+ ScanKeyInit(&key,
+ Anum_pg_constraint_contypid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(domainId));
+ scan = systable_beginscan(conRel, ConstraintTypidIndexId,
+ true, NULL, 1, &key);
+ }
+
+ /*
+ * For each constraint, add a node to the list of table elements. In
+ * these nodes we include not only the printable information ("fmt"), but
+ * also separate attributes to indicate the type of constraint, for
+ * automatic processing.
+ */
+ while (HeapTupleIsValid(tuple = systable_getnext(scan)))
+ {
+ Form_pg_constraint constrForm;
+ char *contype;
+
+ constrForm = (Form_pg_constraint) GETSTRUCT(tuple);
+
+ switch (constrForm->contype)
+ {
+ case CONSTRAINT_CHECK:
+ contype = "check";
+ break;
+ case CONSTRAINT_FOREIGN:
+ continue; /* not here */
+ case CONSTRAINT_PRIMARY:
+ contype = "primary key";
+ break;
+ case CONSTRAINT_UNIQUE:
+ contype = "unique";
+ break;
+ case CONSTRAINT_TRIGGER:
+ contype = "trigger";
+ break;
+ case CONSTRAINT_EXCLUSION:
+ contype = "exclusion";
+ break;
+ default:
+ elog(ERROR, "unrecognized constraint type");
+ }
+
+ /*
+ * "type" and "contype" are not part of the printable output, but are
+ * useful to programmatically distinguish these from columns and among
+ * different constraint types.
+ *
+ * XXX it might be useful to also list the column names in a PK, etc.
+ */
+ constr = new_objtree_VA("CONSTRAINT %{name}I %{definition}s",
+ 4,
+ "type", ObjTypeString, "constraint",
+ "contype", ObjTypeString, contype,
+ "name", ObjTypeString, NameStr(constrForm->conname),
+ "definition", ObjTypeString,
+ pg_get_constraintdef_command_simple(constrForm->oid));
+ elements = lappend(elements, new_object_object(constr));
+ }
+
+ systable_endscan(scan);
+ table_close(conRel, AccessShareLock);
+
+ return elements;
+}
+
+/*
+ * Return an index definition, split into several pieces.
+ *
+ * A large amount of code is duplicated from pg_get_indexdef_worker, but
+ * control flow is different enough that it doesn't seem worth keeping them
+ * together.
+ */
+static void
+pg_get_indexdef_detailed(Oid indexrelid,
+ char **index_am,
+ char **definition,
+ char **reloptions,
+ char **tablespace,
+ char **whereClause)
+{
+ HeapTuple ht_idx;
+ HeapTuple ht_idxrel;
+ HeapTuple ht_am;
+ Form_pg_index idxrec;
+ Form_pg_class idxrelrec;
+ Form_pg_am amrec;
+ IndexAmRoutine *amroutine;
+ List *indexprs;
+ ListCell *indexpr_item;
+ List *context;
+ Oid indrelid;
+ int keyno;
+ Datum indcollDatum;
+ Datum indclassDatum;
+ Datum indoptionDatum;
+ bool isnull;
+ oidvector *indcollation;
+ oidvector *indclass;
+ int2vector *indoption;
+ StringInfoData definitionBuf;
+
+ /*
+ * Fetch the pg_index tuple by the Oid of the index
+ */
+ ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
+ if (!HeapTupleIsValid(ht_idx))
+ elog(ERROR, "cache lookup failed for index %u", indexrelid);
+ idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
+
+ indrelid = idxrec->indrelid;
+ Assert(indexrelid == idxrec->indexrelid);
+
+ /* Must get indcollation, indclass, and indoption the hard way */
+ indcollDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
+ Anum_pg_index_indcollation, &isnull);
+ Assert(!isnull);
+ indcollation = (oidvector *) DatumGetPointer(indcollDatum);
+
+ indclassDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
+ Anum_pg_index_indclass, &isnull);
+ Assert(!isnull);
+ indclass = (oidvector *) DatumGetPointer(indclassDatum);
+
+ indoptionDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
+ Anum_pg_index_indoption, &isnull);
+ Assert(!isnull);
+ indoption = (int2vector *) DatumGetPointer(indoptionDatum);
+
+ /*
+ * Fetch the pg_class tuple of the index relation
+ */
+ ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexrelid));
+ if (!HeapTupleIsValid(ht_idxrel))
+ elog(ERROR, "cache lookup failed for relation %u", indexrelid);
+ idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
+
+ /*
+ * Fetch the pg_am tuple of the index' access method
+ */
+ ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam));
+ if (!HeapTupleIsValid(ht_am))
+ elog(ERROR, "cache lookup failed for access method %u",
+ idxrelrec->relam);
+ amrec = (Form_pg_am) GETSTRUCT(ht_am);
+
+ /*
+ * Get the index expressions, if any. (NOTE: we do not use the relcache
+ * versions of the expressions and predicate, because we want to display
+ * non-const-folded expressions.)
+ */
+ if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs, NULL))
+ {
+ Datum exprsDatum;
+ char *exprsString;
+
+ exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
+ Anum_pg_index_indexprs, &isnull);
+ Assert(!isnull);
+ exprsString = TextDatumGetCString(exprsDatum);
+ indexprs = (List *) stringToNode(exprsString);
+ pfree(exprsString);
+ }
+ else
+ indexprs = NIL;
+
+ indexpr_item = list_head(indexprs);
+
+ context = deparse_context_for(get_rel_name(indrelid), indrelid);
+
+ initStringInfo(&definitionBuf);
+
+ /* Output index AM */
+ *index_am = pstrdup(quote_identifier(NameStr(amrec->amname)));
+
+ /* Fetch the index AM's API struct */
+ amroutine = GetIndexAmRoutine(amrec->amhandler);
+
+ /*
+ * Output index definition. Note the outer parens must be supplied by
+ * caller.
+ */
+ for (keyno = 0; keyno < idxrec->indnatts; keyno++)
+ {
+ AttrNumber attnum = idxrec->indkey.values[keyno];
+ int16 opt = indoption->values[keyno];
+ Oid keycoltype;
+ Oid keycolcollation;
+ Oid indcoll;
+
+ appendStringInfoString(&definitionBuf, keyno == 0 ? "" : ", ");
+
+ if (attnum != 0)
+ {
+ /* Simple index column */
+ char *attname;
+ int32 keycoltypmod;
+
+ attname = get_attname(indrelid, attnum, false);
+ appendStringInfoString(&definitionBuf, quote_identifier(attname));
+ get_atttypetypmodcoll(indrelid, attnum,
+ &keycoltype, &keycoltypmod,
+ &keycolcollation);
+ }
+ else
+ {
+ /* Expressional index */
+ Node *indexkey;
+ char *str;
+
+ if (indexpr_item == NULL)
+ elog(ERROR, "too few entries in indexprs list");
+ indexkey = (Node *) lfirst(indexpr_item);
+ indexpr_item = lnext(indexprs, indexpr_item);
+ /* Deparse */
+ str = deparse_expression(indexkey, context, false, false);
+
+ /* Need parens if it's not a bare function call */
+ if (indexkey && IsA(indexkey, FuncExpr) &&
+ ((FuncExpr *) indexkey)->funcformat == COERCE_EXPLICIT_CALL)
+ appendStringInfoString(&definitionBuf, str);
+ else
+ appendStringInfo(&definitionBuf, "(%s)", str);
+
+ keycoltype = exprType(indexkey);
+ keycolcollation = exprCollation(indexkey);
+ }
+
+ /* Add collation, even if default */
+ indcoll = indcollation->values[keyno];
+ if (OidIsValid(indcoll))
+ appendStringInfo(&definitionBuf, " COLLATE %s",
+ generate_collation_name((indcoll)));
+
+ /* Add the operator class name, even if default */
+ get_opclass_name(indclass->values[keyno], InvalidOid, &definitionBuf);
+
+ /* Add options if relevant */
+ if (amroutine->amcanorder)
+ {
+ /* If it supports sort ordering, report DESC and NULLS opts */
+ if (opt & INDOPTION_DESC)
+ {
+ appendStringInfoString(&definitionBuf, " DESC");
+ /* NULLS FIRST is the default in this case */
+ if (!(opt & INDOPTION_NULLS_FIRST))
+ appendStringInfoString(&definitionBuf, " NULLS LAST");
+ }
+ else
+ {
+ if (opt & INDOPTION_NULLS_FIRST)
+ appendStringInfoString(&definitionBuf, " NULLS FIRST");
+ }
+ }
+
+ /* XXX excludeOps thingy was here; do we need anything? */
+ }
+ *definition = definitionBuf.data;
+
+ /* Output reloptions */
+ *reloptions = flatten_reloptions(indexrelid);
+
+ /* Output tablespace */
+ {
+ Oid tblspc;
+
+ tblspc = get_rel_tablespace(indexrelid);
+ if (OidIsValid(tblspc))
+ *tablespace = pstrdup(quote_identifier(get_tablespace_name(tblspc)));
+ else
+ *tablespace = NULL;
+ }
+
+ /* Report index predicate, if any */
+ if (!heap_attisnull(ht_idx, Anum_pg_index_indpred, NULL))
+ {
+ Node *node;
+ Datum predDatum;
+ char *predString;
+
+ /* Convert text string to node tree */
+ predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
+ Anum_pg_index_indpred, &isnull);
+ Assert(!isnull);
+ predString = TextDatumGetCString(predDatum);
+ node = (Node *) stringToNode(predString);
+ pfree(predString);
+
+ /* Deparse */
+ *whereClause =
+ deparse_expression(node, context, false, false);
+ }
+ else
+ *whereClause = NULL;
+
+ /* Clean up */
+ ReleaseSysCache(ht_idx);
+ ReleaseSysCache(ht_idxrel);
+ ReleaseSysCache(ht_am);
+}
+
+/*
+ * Obtain the deparsed default value for the given column of the given table.
+ *
+ * Caller must have set a correct deparse context.
+ */
+static char *
+RelationGetColumnDefault(Relation rel, AttrNumber attno, List *dpcontext,
+ List **exprs)
+{
+ Node *defval;
+ char *defstr;
+
+ defval = build_column_default(rel, attno);
+ defstr = deparse_expression(defval, dpcontext, false, false);
+
+ /* Collect the expression for later replication safety checks */
+ if (exprs)
+ *exprs = lappend(*exprs, defval);
+
+ return defstr;
+}
+
+/*
+ * Obtain the deparsed partition bound expression for the given table.
+ */
+static char *
+RelationGetPartitionBound(Oid relid)
+{
+ Datum deparsed;
+ Datum boundDatum;
+ bool isnull;
+ HeapTuple tuple;
+
+ tuple = SearchSysCache1(RELOID, relid);
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for relation %u",
+ relid);
+
+ boundDatum = SysCacheGetAttr(RELOID, tuple,
+ Anum_pg_class_relpartbound,
+ &isnull);
+
+ deparsed = DirectFunctionCall2(pg_get_expr,
+ CStringGetTextDatum(TextDatumGetCString(boundDatum)),
+ relid);
+
+ ReleaseSysCache(tuple);
+
+ return TextDatumGetCString(deparsed);
+}
+
+/*
+ * Return the given object type as a string.
+ */
+static const char *
+stringify_objtype(ObjectType objtype)
+{
+ switch (objtype)
+ {
+ case OBJECT_AGGREGATE:
+ return "AGGREGATE";
+ case OBJECT_CAST:
+ return "CAST";
+ case OBJECT_COLUMN:
+ return "COLUMN";
+ case OBJECT_COLLATION:
+ return "COLLATION";
+ case OBJECT_CONVERSION:
+ return "CONVERSION";
+ case OBJECT_DATABASE:
+ return "DATABASE";
+ case OBJECT_DOMAIN:
+ return "DOMAIN";
+ case OBJECT_EVENT_TRIGGER:
+ return "EVENT TRIGGER";
+ case OBJECT_EXTENSION:
+ return "EXTENSION";
+ case OBJECT_FDW:
+ return "FOREIGN DATA WRAPPER";
+ case OBJECT_FOREIGN_SERVER:
+ return "SERVER";
+ case OBJECT_FOREIGN_TABLE:
+ return "FOREIGN TABLE";
+ case OBJECT_FUNCTION:
+ return "FUNCTION";
+ case OBJECT_INDEX:
+ return "INDEX";
+ case OBJECT_LANGUAGE:
+ return "LANGUAGE";
+ case OBJECT_LARGEOBJECT:
+ return "LARGE OBJECT";
+ case OBJECT_MATVIEW:
+ return "MATERIALIZED VIEW";
+ case OBJECT_OPCLASS:
+ return "OPERATOR CLASS";
+ case OBJECT_OPERATOR:
+ return "OPERATOR";
+ case OBJECT_OPFAMILY:
+ return "OPERATOR FAMILY";
+ case OBJECT_POLICY:
+ return "POLICY";
+ case OBJECT_ROLE:
+ return "ROLE";
+ case OBJECT_RULE:
+ return "RULE";
+ case OBJECT_SCHEMA:
+ return "SCHEMA";
+ case OBJECT_SEQUENCE:
+ return "SEQUENCE";
+ case OBJECT_STATISTIC_EXT:
+ return "STATISTICS";
+ case OBJECT_TABLE:
+ return "TABLE";
+ case OBJECT_TABLESPACE:
+ return "TABLESPACE";
+ case OBJECT_TRIGGER:
+ return "TRIGGER";
+ case OBJECT_TSCONFIGURATION:
+ return "TEXT SEARCH CONFIGURATION";
+
+ /*
+ * case OBJECT_TSCONFIG_MAPPING:
+ * return "TEXT SEARCH CONFIGURATION MAPPING";
+ */
+ case OBJECT_TSDICTIONARY:
+ return "TEXT SEARCH DICTIONARY";
+ case OBJECT_TSPARSER:
+ return "TEXT SEARCH PARSER";
+ case OBJECT_TSTEMPLATE:
+ return "TEXT SEARCH TEMPLATE";
+ case OBJECT_TYPE:
+ return "TYPE";
+ case OBJECT_USER_MAPPING:
+ return "USER MAPPING";
+ case OBJECT_VIEW:
+ return "VIEW";
+
+ default:
+ elog(ERROR, "unsupported object type %d", objtype);
+ }
+}
+
+/*
+ * Given a CollectedCommand, return a JSON representation of it.
+ *
+ * The command is expanded fully so that there are no ambiguities even in the
+ * face of search_path changes.
+ */
+Datum
+ddl_deparse_to_json(PG_FUNCTION_ARGS)
+{
+ CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
+ char *command;
+
+ command = deparse_utility_command(cmd, true);
+
+ if (command)
+ PG_RETURN_TEXT_P(cstring_to_text(command));
+
+ PG_RETURN_NULL();
+}
+
+/*
+ * Deparse an AlterCollationStmt (ALTER COLLATION)
+ *
+ * Given a collation OID and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterCollation(Oid objectId, Node *parsetree)
+{
+ ObjTree *stmt;
+ HeapTuple colTup;
+ Form_pg_collation colForm;
+
+ colTup = SearchSysCache1(COLLOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(colTup))
+ elog(ERROR, "cache lookup failed for collation with OID %u", objectId);
+ colForm = (Form_pg_collation) GETSTRUCT(colTup);
+
+ stmt = new_objtree_VA("ALTER COLLATION %{identity}O REFRESH VERSION", 1,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(colForm->collnamespace,
+ NameStr(colForm->collname)));
+
+ ReleaseSysCache(colTup);
+
+ return stmt;
+}
+
+/*
+ * Deparse an AlterFunctionStmt (ALTER FUNCTION)
+ *
+ * Given a function OID and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterFunction(Oid objectId, Node *parsetree)
+{
+ AlterFunctionStmt *node = (AlterFunctionStmt *) parsetree;
+ ObjTree *alterFunc;
+ ObjTree *sign;
+ HeapTuple procTup;
+ Form_pg_proc procForm;
+ List *params = NIL;
+ List *elems = NIL;
+ ListCell *cell;
+ int i;
+
+ /* Get the pg_proc tuple */
+ procTup = SearchSysCache1(PROCOID, objectId);
+ if (!HeapTupleIsValid(procTup))
+ elog(ERROR, "cache lookup failure for function with OID %u",
+ objectId);
+ procForm = (Form_pg_proc) GETSTRUCT(procTup);
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER FUNCTION %{signature}s %{definition: }s
+ */
+ alterFunc = new_objtree_VA("ALTER FUNCTION", 0);
+
+ /*
+ * ALTER FUNCTION does not change signature so we can use catalog to get
+ * input type Oids.
+ */
+ for (i = 0; i < procForm->pronargs; i++)
+ {
+ ObjTree *tmpobj = new_objtree_VA("%{type}T", 0);
+
+ append_object_object(tmpobj, "type",
+ new_objtree_for_type(procForm->proargtypes.values[i], -1));
+ params = lappend(params, new_object_object(tmpobj));
+ }
+
+ sign = new_objtree("");
+
+ append_object_object(sign, "%{identity}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ objectId));
+ append_array_object(sign, "(%{arguments:, }s)", params);
+
+ append_object_object(alterFunc, "%{signature}s", sign);
+
+ foreach(cell, node->actions)
+ {
+ DefElem *defel = (DefElem *) lfirst(cell);
+ ObjTree *tmpobj = NULL;
+
+ if (strcmp(defel->defname, "volatility") == 0)
+ {
+ tmpobj = new_objtree_VA(strVal(defel->arg), 0);
+ }
+ else if (strcmp(defel->defname, "strict") == 0)
+ {
+ tmpobj = new_objtree_VA(intVal(defel->arg) ?
+ "RETURNS NULL ON NULL INPUT" :
+ "CALLED ON NULL INPUT", 0);
+ }
+ else if (strcmp(defel->defname, "security") == 0)
+ {
+ tmpobj = new_objtree_VA(intVal(defel->arg) ?
+ "SECURITY DEFINER" : "SECURITY INVOKER", 0);
+ }
+ else if (strcmp(defel->defname, "leakproof") == 0)
+ {
+ tmpobj = new_objtree_VA(intVal(defel->arg) ?
+ "LEAKPROOF" : "NOT LEAKPROOF", 0);
+ }
+ else if (strcmp(defel->defname, "cost") == 0)
+ {
+ tmpobj = new_objtree_VA("COST %{cost}n", 1,
+ "cost", ObjTypeFloat,
+ defGetNumeric(defel));
+ }
+ else if (strcmp(defel->defname, "rows") == 0)
+ {
+ tmpobj = new_objtree_VA("ROWS", 0);
+ if (defGetNumeric(defel) == 0)
+ append_bool_object(tmpobj, "present", false);
+ else
+ append_float_object(tmpobj, "%{rows}n",
+ defGetNumeric(defel));
+ }
+ else if (strcmp(defel->defname, "set") == 0)
+ {
+ VariableSetStmt *sstmt = (VariableSetStmt *) defel->arg;
+ char *value = ExtractSetVariableArgs(sstmt);
+
+ tmpobj = deparse_FunctionSet(sstmt->kind, sstmt->name, value);
+ }
+ else if (strcmp(defel->defname, "support") == 0)
+ {
+ Oid argtypes[1];
+
+ tmpobj = new_objtree("SUPPORT");
+
+ Assert(procForm->prosupport);
+
+ /*
+ * We should qualify the support function's name if it wouldn't be
+ * resolved by lookup in the current search path.
+ */
+ argtypes[0] = INTERNALOID;
+ append_string_object(tmpobj, "%{name}s",
+ generate_function_name(procForm->prosupport, 1,
+ NIL, argtypes,
+ false, NULL,
+ EXPR_KIND_NONE));
+ }
+ else if (strcmp(defel->defname, "parallel") == 0)
+ {
+ char *fmt = psprintf("PARALLEL %s", strVal(defel->arg));
+
+ tmpobj = new_objtree(fmt);
+ }
+
+ elems = lappend(elems, new_object_object(tmpobj));
+ }
+
+ append_array_object(alterFunc, "%{definition: }s", elems);
+
+ ReleaseSysCache(procTup);
+
+ return alterFunc;
+}
+
+/*
+ * Deparse an AlterObjectSchemaStmt (ALTER ... SET SCHEMA command)
+ *
+ * Given the object address and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterObjectSchemaStmt(ObjectAddress address, Node *parsetree,
+ ObjectAddress oldschema)
+{
+ AlterObjectSchemaStmt *node = (AlterObjectSchemaStmt *) parsetree;
+ ObjTree *alterStmt;
+ char *fmt;
+ char *identity;
+ char *newschema;
+ char *oldschname;
+ char *ident;
+
+ newschema = node->newschema;
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER %s %{identity}s SET SCHEMA %{newschema}I
+ */
+ fmt = psprintf("ALTER %s", stringify_objtype(node->objectType));
+ alterStmt = new_objtree(fmt);
+
+ /*
+ * Since the command has already taken place from the point of view of
+ * catalogs, getObjectIdentity returns the object name with the already
+ * changed schema. The output of our deparsing must return the original
+ * schema name, however, so we chop the schema name off the identity
+ * string and then prepend the quoted schema name.
+ *
+ * XXX This is pretty clunky. Can we do better?
+ */
+ identity = getObjectIdentity(&address, false);
+ oldschname = get_namespace_name(oldschema.objectId);
+ if (!oldschname)
+ elog(ERROR, "cache lookup failed for schema with OID %u",
+ oldschema.objectId);
+
+ ident = psprintf("%s%s", quote_identifier(oldschname),
+ identity + strlen(quote_identifier(newschema)));
+ append_string_object(alterStmt, "%{identity}s", ident);
+
+ append_string_object(alterStmt, "SET SCHEMA %{newschema}I", newschema);
+
+ return alterStmt;
+}
+
+/*
+ * Deparse a GRANT/REVOKE command.
+ *
+ */
+static ObjTree *
+deparse_GrantStmt(CollectedCommand *cmd)
+{
+ InternalGrant *istmt;
+ ObjTree *grantStmt;
+ char *fmt;
+ char *objtype;
+ List *list;
+ ListCell *cell;
+ Oid classId;
+ ObjTree *tmp;
+
+ istmt = cmd->d.grant.istmt;
+
+ /*
+ * If there are no objects from "ALL ... IN SCHEMA" to be granted, then we
+ * need not do anything.
+ */
+ if (istmt->objects == NIL)
+ return NULL;
+
+ switch (istmt->objtype)
+ {
+ case OBJECT_COLUMN:
+ case OBJECT_TABLE:
+ objtype = "TABLE";
+ classId = RelationRelationId;
+ break;
+ case OBJECT_SEQUENCE:
+ objtype = "SEQUENCE";
+ classId = RelationRelationId;
+ break;
+ case OBJECT_DOMAIN:
+ objtype = "DOMAIN";
+ classId = TypeRelationId;
+ break;
+ case OBJECT_FDW:
+ objtype = "FOREIGN DATA WRAPPER";
+ classId = ForeignDataWrapperRelationId;
+ break;
+ case OBJECT_FOREIGN_SERVER:
+ objtype = "FOREIGN SERVER";
+ classId = ForeignServerRelationId;
+ break;
+ case OBJECT_FUNCTION:
+ objtype = "FUNCTION";
+ classId = ProcedureRelationId;
+ break;
+ case OBJECT_PROCEDURE:
+ objtype = "PROCEDURE";
+ classId = ProcedureRelationId;
+ break;
+ case OBJECT_ROUTINE:
+ objtype = "ROUTINE";
+ classId = ProcedureRelationId;
+ break;
+ case OBJECT_LANGUAGE:
+ objtype = "LANGUAGE";
+ classId = LanguageRelationId;
+ break;
+ case OBJECT_LARGEOBJECT:
+ objtype = "LARGE OBJECT";
+ classId = LargeObjectRelationId;
+ break;
+ case OBJECT_SCHEMA:
+ objtype = "SCHEMA";
+ classId = NamespaceRelationId;
+ break;
+ case OBJECT_TYPE:
+ objtype = "TYPE";
+ classId = TypeRelationId;
+ break;
+ case OBJECT_DATABASE:
+ case OBJECT_TABLESPACE:
+ objtype = "";
+ classId = InvalidOid;
+ elog(ERROR, "global objects not supported");
+ break;
+ default:
+ elog(ERROR, "invalid OBJECT value %d", istmt->objtype);
+ }
+
+ /* GRANT TO or REVOKE FROM */
+ if (istmt->is_grant)
+ fmt = psprintf("GRANT");
+ else
+ fmt = psprintf("REVOKE");
+
+ grantStmt = new_objtree_VA(fmt, 0);
+
+ /* build a list of privileges to grant/revoke */
+ if (istmt->all_privs)
+ {
+ tmp = new_objtree_VA("ALL PRIVILEGES", 0);
+ list = list_make1(new_object_object(tmp));
+ }
+ else
+ {
+ list = NIL;
+
+ if (istmt->privileges & ACL_INSERT)
+ list = lappend(list, new_string_object("INSERT"));
+ if (istmt->privileges & ACL_SELECT)
+ list = lappend(list, new_string_object("SELECT"));
+ if (istmt->privileges & ACL_UPDATE)
+ list = lappend(list, new_string_object("UPDATE"));
+ if (istmt->privileges & ACL_DELETE)
+ list = lappend(list, new_string_object("DELETE"));
+ if (istmt->privileges & ACL_TRUNCATE)
+ list = lappend(list, new_string_object("TRUNCATE"));
+ if (istmt->privileges & ACL_REFERENCES)
+ list = lappend(list, new_string_object("REFERENCES"));
+ if (istmt->privileges & ACL_TRIGGER)
+ list = lappend(list, new_string_object("TRIGGER"));
+ if (istmt->privileges & ACL_EXECUTE)
+ list = lappend(list, new_string_object("EXECUTE"));
+ if (istmt->privileges & ACL_USAGE)
+ list = lappend(list, new_string_object("USAGE"));
+ if (istmt->privileges & ACL_CREATE)
+ list = lappend(list, new_string_object("CREATE"));
+ if (istmt->privileges & ACL_CREATE_TEMP)
+ list = lappend(list, new_string_object("TEMPORARY"));
+ if (istmt->privileges & ACL_CONNECT)
+ list = lappend(list, new_string_object("CONNECT"));
+
+ if (!istmt->is_grant && istmt->grant_option)
+ append_string_object(grantStmt, "%{grant_option}s",
+ istmt->grant_option ? "GRANT OPTION FOR" : "");
+
+ if (istmt->col_privs != NIL)
+ {
+ ListCell *ocell;
+
+ foreach(ocell, istmt->col_privs)
+ {
+ AccessPriv *priv = lfirst(ocell);
+ List *cols = NIL;
+
+ tmp = new_objtree("");
+ foreach(cell, priv->cols)
+ {
+ String *colname = lfirst_node(String, cell);
+
+ cols = lappend(cols,
+ new_string_object(strVal(colname)));
+ }
+ append_array_object(tmp, "(%{cols:, }s)", cols);
+
+ if (priv->priv_name == NULL)
+ append_string_object(grantStmt, "%{priv}s", "ALL PRIVILEGES");
+ else
+ append_string_object(grantStmt, "%{priv}s", priv->priv_name);
+
+ list = lappend(list, new_object_object(tmp));
+ }
+ }
+ }
+ append_array_object(grantStmt, "%{privileges:, }s ON", list);
+
+ append_string_object(grantStmt, "%{objtype}s", objtype);
+
+ /* target objects. We use object identities here */
+ list = NIL;
+ foreach(cell, istmt->objects)
+ {
+ Oid objid = lfirst_oid(cell);
+ ObjectAddress addr;
+
+ addr.classId = classId;
+ addr.objectId = objid;
+ addr.objectSubId = 0;
+
+ tmp = new_objtree_VA("%{identity}s", 1,
+ "identity", ObjTypeString,
+ getObjectIdentity(&addr, false));
+
+ list = lappend(list, new_object_object(tmp));
+ }
+ append_array_object(grantStmt, "%{privtarget:, }s", list);
+
+ if (istmt->is_grant)
+ append_format_string(grantStmt, "TO");
+ else
+ append_format_string(grantStmt, "FROM");
+
+ /* list of grantees */
+ list = NIL;
+ foreach(cell, istmt->grantees)
+ {
+ Oid grantee = lfirst_oid(cell);
+
+ tmp = new_objtree_for_role_id(grantee);
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ append_array_object(grantStmt, "%{grantees:, }s", list);
+
+ /* the wording of the grant option is variable ... */
+ if (istmt->is_grant)
+ append_string_object(grantStmt, "%{grant_option}s",
+ istmt->grant_option ? "WITH GRANT OPTION" : "");
+
+ if (istmt->grantor_uid)
+ {
+ HeapTuple roltup;
+ char *rolename;
+
+ roltup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(istmt->grantor_uid));
+ if (!HeapTupleIsValid(roltup))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("role with OID %u does not exist", istmt->grantor_uid)));
+
+ rolename = NameStr(((Form_pg_authid) GETSTRUCT(roltup))->rolname);
+ append_string_object(grantStmt, "GRANTED BY %{rolename}s", rolename);
+ ReleaseSysCache(roltup);
+ }
+
+ if (!istmt->is_grant)
+ {
+ if (istmt->behavior == DROP_CASCADE)
+ append_string_object(grantStmt, "%{cascade}s", "CASCADE");
+ else
+ append_string_object(grantStmt, "%{cascade}s", "");
+ }
+
+ return grantStmt;
+}
+
+/*
+ * Deparse an AlterOperatorStmt (ALTER OPERATOR ... SET ...).
+ *
+ * Given an operator OID and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterOperatorStmt(Oid objectId, Node *parsetree)
+{
+ HeapTuple oprTup;
+ AlterOperatorStmt *node = (AlterOperatorStmt *) parsetree;
+ ObjTree *alterop;
+ Form_pg_operator oprForm;
+ ListCell *cell;
+ List *list = NIL;
+
+ oprTup = SearchSysCache1(OPEROID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(oprTup))
+ elog(ERROR, "cache lookup failed for operator with OID %u", objectId);
+ oprForm = (Form_pg_operator) GETSTRUCT(oprTup);
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER OPERATOR %{identity}O (%{left_type}T, %{right_type}T) SET
+ * (%{elems:, }s)
+ */
+ alterop = new_objtree_VA("ALTER OPERATOR %{identity}O", 1,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(oprForm->oprnamespace,
+ NameStr(oprForm->oprname)));
+
+ /* LEFTARG */
+ if (OidIsValid(oprForm->oprleft))
+ append_object_object(alterop, "(%{left_type}T",
+ new_objtree_for_type(oprForm->oprleft, -1));
+ else
+ append_string_object(alterop, "(%{left_type}s", "NONE");
+
+ /* RIGHTARG */
+ Assert(OidIsValid(oprForm->oprleft));
+ append_object_object(alterop, ", %{right_type}T)",
+ new_objtree_for_type(oprForm->oprright, -1));
+
+ foreach(cell, node->options)
+ {
+ DefElem *elem = (DefElem *) lfirst(cell);
+ ObjTree *tmpobj = NULL;
+
+ if (strcmp(elem->defname, "restrict") == 0)
+ {
+ tmpobj = new_objtree_VA("RESTRICT=", 1,
+ "clause", ObjTypeString, "restrict");
+ if (OidIsValid(oprForm->oprrest))
+ append_object_object(tmpobj, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ oprForm->oprrest));
+ else
+ append_string_object(tmpobj, "%{procedure}s", "NONE");
+ }
+ else if (strcmp(elem->defname, "join") == 0)
+ {
+ tmpobj = new_objtree_VA("JOIN=", 1,
+ "clause", ObjTypeString, "join");
+ if (OidIsValid(oprForm->oprjoin))
+ append_object_object(tmpobj, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ oprForm->oprjoin));
+ else
+ append_string_object(tmpobj, "%{procedure}s", "NONE");
+ }
+
+ Assert(tmpobj);
+ list = lappend(list, new_object_object(tmpobj));
+ }
+
+ append_array_object(alterop, "SET (%{elems:, }s)", list);
+
+ ReleaseSysCache(oprTup);
+
+ return alterop;
+}
+
+/*
+ * Deparse an ALTER OPERATOR FAMILY ADD/DROP command.
+ *
+ * Given the CollectedCommand, return the JSON
+ * blob representing the Alter Op command.
+ */
+static ObjTree *
+deparse_AlterOpFamily(CollectedCommand *cmd)
+{
+ ObjTree *alterOpFam;
+ AlterOpFamilyStmt *stmt = (AlterOpFamilyStmt *) cmd->parsetree;
+ HeapTuple ftp;
+ Form_pg_opfamily opfForm;
+ List *list;
+ ListCell *cell;
+
+ ftp = SearchSysCache1(OPFAMILYOID,
+ ObjectIdGetDatum(cmd->d.opfam.address.objectId));
+ if (!HeapTupleIsValid(ftp))
+ elog(ERROR, "cache lookup failed for operator family %u",
+ cmd->d.opfam.address.objectId);
+ opfForm = (Form_pg_opfamily) GETSTRUCT(ftp);
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER OPERATOR FAMILY %{identity}D USING %{amname}I ADD/DROP
+ * %{items:,}s
+ */
+ alterOpFam = new_objtree_VA("ALTER OPERATOR FAMILY %{identity}D "
+ "USING %{amname}I", 2,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(opfForm->opfnamespace,
+ NameStr(opfForm->opfname)),
+ "amname", ObjTypeString, stmt->amname);
+
+ list = NIL;
+ foreach(cell, cmd->d.opfam.operators)
+ {
+ OpFamilyMember *oper = lfirst(cell);
+ ObjTree *tmpobj;
+
+ /*
+ * Verbose syntax
+ *
+ * OPERATOR %{num}n %{operator}O(%{ltype}T, %{rtype}T) %{purpose}s
+ */
+ tmpobj = new_objtree_VA("OPERATOR %{num}n", 1,
+ "num", ObjTypeInteger, oper->number);
+
+ /* Add the operator name; the DROP case doesn't have this */
+ if (!stmt->isDrop)
+ {
+ append_object_object(tmpobj, "%{operator}O",
+ new_objtree_for_qualname_id(OperatorRelationId,
+ oper->object));
+ }
+
+ /* Add the types */
+ append_object_object(tmpobj, "(%{ltype}T,",
+ new_objtree_for_type(oper->lefttype, -1));
+ append_object_object(tmpobj, "%{rtype}T)",
+ new_objtree_for_type(oper->righttype, -1));
+
+ /* Add the FOR SEARCH / FOR ORDER BY clause; not in the DROP case */
+ if (!stmt->isDrop)
+ {
+ if (oper->sortfamily == InvalidOid)
+ append_string_object(tmpobj, "%{purpose}s", "FOR SEARCH");
+ else
+ {
+ ObjTree *tmpobj2;
+
+ tmpobj2 = new_objtree_VA("FOR ORDER BY", 0);
+ append_object_object(tmpobj2, "%{opfamily}D",
+ new_objtree_for_qualname_id(OperatorFamilyRelationId,
+ oper->sortfamily));
+ append_object_object(tmpobj, "%{purpose}s", tmpobj2);
+ }
+ }
+
+ list = lappend(list, new_object_object(tmpobj));
+ }
+
+ foreach(cell, cmd->d.opfam.procedures)
+ {
+ OpFamilyMember *proc = lfirst(cell);
+ ObjTree *tmpobj;
+
+ tmpobj = new_objtree_VA("FUNCTION %{num}n (%{ltype}T, %{rtype}T)", 3,
+ "num", ObjTypeInteger, proc->number,
+ "ltype", ObjTypeObject,
+ new_objtree_for_type(proc->lefttype, -1),
+ "rtype", ObjTypeObject,
+ new_objtree_for_type(proc->righttype, -1));
+
+ /*
+ * Add the function name and arg types; the DROP case doesn't have
+ * this
+ */
+ if (!stmt->isDrop)
+ {
+ HeapTuple procTup;
+ Form_pg_proc procForm;
+ Oid *proargtypes;
+ List *arglist = NIL;
+ int i;
+
+ procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(proc->object));
+ if (!HeapTupleIsValid(procTup))
+ elog(ERROR, "cache lookup failed for procedure %u", proc->object);
+ procForm = (Form_pg_proc) GETSTRUCT(procTup);
+
+ proargtypes = procForm->proargtypes.values;
+ for (i = 0; i < procForm->pronargs; i++)
+ {
+ ObjTree *arg;
+
+ arg = new_objtree_for_type(proargtypes[i], -1);
+ arglist = lappend(arglist, new_object_object(arg));
+ }
+
+ append_object_object(tmpobj, "%{function}D",
+ new_objtree_for_qualname(procForm->pronamespace,
+ NameStr(procForm->proname)));
+
+ append_format_string(tmpobj, "(");
+ append_array_object(tmpobj, "%{argtypes:, }T", arglist);
+ append_format_string(tmpobj, ")");
+
+ ReleaseSysCache(procTup);
+ }
+
+ list = lappend(list, new_object_object(tmpobj));
+ }
+
+ if (stmt->isDrop)
+ append_format_string(alterOpFam, "DROP");
+ else
+ append_format_string(alterOpFam, "ADD");
+
+ append_array_object(alterOpFam, "%{items:, }s", list);
+
+ ReleaseSysCache(ftp);
+
+ return alterOpFam;
+}
+
+/*
+ * Deparse an AlterOwnerStmt (ALTER ... OWNER TO ...).
+ *
+ * Given the object address and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterOwnerStmt(ObjectAddress address, Node *parsetree)
+{
+ AlterOwnerStmt *node = (AlterOwnerStmt *) parsetree;
+ ObjTree *ownerStmt;
+ char *fmt;
+
+ fmt = psprintf("ALTER %s %%{identity}s OWNER TO %%{newowner}I",
+ stringify_objtype(node->objectType));
+
+ ownerStmt = new_objtree_VA(fmt, 2,
+ "identity", ObjTypeString,
+ getObjectIdentity(&address, false),
+ "newowner", ObjTypeString,
+ get_rolespec_name(node->newowner));
+
+ return ownerStmt;
+}
+
+/*
+ * Deparse an AlterSeqStmt.
+ *
+ * Given a sequence OID and a parse tree that modified it, return an ObjTree
+ * representing the alter command.
+ */
+static ObjTree *
+deparse_AlterSeqStmt(Oid objectId, Node *parsetree)
+{
+ ObjTree *alterSeq;
+ ObjTree *tmpobj;
+ Relation relation;
+ Form_pg_sequence_data seqdata;
+ List *elems = NIL;
+ ListCell *cell;
+ Form_pg_sequence seqform;
+ Relation rel;
+ HeapTuple seqtuple;
+ AlterSeqStmt *alterSeqStmt = (AlterSeqStmt *) parsetree;
+
+ /*
+ * Sequence for IDENTITY COLUMN output separately(via CREATE TABLE or
+ * ALTER TABLE); return empty here.
+ */
+ if (alterSeqStmt->for_identity)
+ return NULL;
+
+ seqdata = get_sequence_values(objectId);
+
+ relation = relation_open(objectId, AccessShareLock);
+ rel = table_open(SequenceRelationId, RowExclusiveLock);
+ seqtuple = SearchSysCacheCopy1(SEQRELID,
+ ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(seqtuple))
+ elog(ERROR, "cache lookup failed for sequence %u",
+ objectId);
+
+ seqform = (Form_pg_sequence) GETSTRUCT(seqtuple);
+
+ alterSeq = new_objtree("ALTER SEQUENCE");
+
+ tmpobj = new_objtree_for_qualname(relation->rd_rel->relnamespace,
+ RelationGetRelationName(relation));
+ append_object_object(alterSeq, "%{identity}D", tmpobj);
+
+ foreach(cell, ((AlterSeqStmt *) parsetree)->options)
+ {
+ DefElem *elem = (DefElem *) lfirst(cell);
+ ObjElem *newelm;
+
+ if (strcmp(elem->defname, "cache") == 0)
+ newelm = deparse_Seq_Cache(alterSeq, seqform, false);
+ else if (strcmp(elem->defname, "cycle") == 0)
+ newelm = deparse_Seq_Cycle(alterSeq, seqform, false);
+ else if (strcmp(elem->defname, "increment") == 0)
+ newelm = deparse_Seq_IncrementBy(alterSeq, seqform, false);
+ else if (strcmp(elem->defname, "minvalue") == 0)
+ newelm = deparse_Seq_Minvalue(alterSeq, seqform, false);
+ else if (strcmp(elem->defname, "maxvalue") == 0)
+ newelm = deparse_Seq_Maxvalue(alterSeq, seqform, false);
+ else if (strcmp(elem->defname, "start") == 0)
+ newelm = deparse_Seq_Startwith(alterSeq, seqform, false);
+ else if (strcmp(elem->defname, "restart") == 0)
+ newelm = deparse_Seq_Restart(alterSeq, seqdata);
+ else if (strcmp(elem->defname, "owned_by") == 0)
+ newelm = deparse_Seq_OwnedBy(alterSeq, objectId, false);
+ else
+ elog(ERROR, "invalid sequence option %s", elem->defname);
+
+ elems = lappend(elems, newelm);
+ }
+
+ append_array_object(alterSeq, "%{definition: }s", elems);
+
+ table_close(rel, RowExclusiveLock);
+ relation_close(relation, AccessShareLock);
+
+ return alterSeq;
+}
+
+/*
+ * Deparse an AlterTypeStmt.
+ *
+ * Given a type OID and a parse tree that modified it, return an ObjTree
+ * representing the alter type.
+ */
+static ObjTree *
+deparse_AlterTypeSetStmt(Oid objectId, Node *cmd)
+{
+ AlterTypeStmt *alterTypeSetStmt = (AlterTypeStmt *) cmd;
+ ObjTree *alterType;
+ ListCell *pl;
+ List *elems = NIL;
+ HeapTuple typTup;
+ Form_pg_type typForm;
+
+ typTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(typTup))
+ elog(ERROR, "cache lookup failed for type with OID %u", objectId);
+ typForm = (Form_pg_type) GETSTRUCT(typTup);
+
+ alterType = new_objtree("ALTER TYPE");
+ append_object_object(alterType, "%{identity}D SET",
+ new_objtree_for_qualname_id(TypeRelationId,
+ objectId));
+
+ foreach(pl, alterTypeSetStmt->options)
+ {
+ DefElem *defel = (DefElem *) lfirst(pl);
+ ObjElem *newelm;
+
+ if (strcmp(defel->defname, "storage") == 0)
+ newelm = deparse_Type_Storage(alterType, typForm);
+ if (strcmp(defel->defname, "receive") == 0)
+ newelm = deparse_Type_Receive(alterType, typForm);
+ if (strcmp(defel->defname, "send") == 0)
+ newelm = deparse_Type_Send(alterType, typForm);
+ if (strcmp(defel->defname, "typmod_in") == 0)
+ newelm = deparse_Type_Typmod_In(alterType, typForm);
+ if (strcmp(defel->defname, "typmod_out") == 0)
+ newelm = deparse_Type_Typmod_Out(alterType, typForm);
+ if (strcmp(defel->defname, "analyze") == 0)
+ newelm = deparse_Type_Analyze(alterType, typForm);
+ if (strcmp(defel->defname, "subscript") == 0)
+ newelm = deparse_Type_Subscript(alterType, typForm);
+ else
+ elog(ERROR, "invalid type option %s", defel->defname);
+
+ elems = lappend(elems, newelm);
+ }
+
+ append_array_object(alterType, "(%{definition: }s)", elems);
+
+ ReleaseSysCache(typTup);
+
+ return alterType;
+}
+
+/*
+ * Deparse a CompositeTypeStmt (CREATE TYPE AS)
+ *
+ * Given a Composite type OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CompositeTypeStmt(Oid objectId, Node *parsetree)
+{
+ CompositeTypeStmt *node = (CompositeTypeStmt *) parsetree;
+ ObjTree *composite;
+ HeapTuple typtup;
+ Form_pg_type typform;
+ Relation typerel;
+ List *dpcontext;
+ List *tableelts = NIL;
+
+ /* Find the pg_type entry and open the corresponding relation */
+ typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(typtup))
+ elog(ERROR, "cache lookup failed for type %u", objectId);
+ typform = (Form_pg_type) GETSTRUCT(typtup);
+ typerel = relation_open(typform->typrelid, AccessShareLock);
+
+ dpcontext = deparse_context_for(RelationGetRelationName(typerel),
+ RelationGetRelid(typerel));
+
+ composite = new_objtree_VA("CREATE TYPE", 0);
+ append_object_object(composite, "%{identity}D",
+ new_objtree_for_qualname_id(TypeRelationId,
+ objectId));
+
+ tableelts = deparse_TableElements(typerel, node->coldeflist, dpcontext,
+ false, /* not typed */
+ true); /* composite type */
+
+ append_array_object(composite, "AS (%{columns:, }s)", tableelts);
+
+ table_close(typerel, AccessShareLock);
+ ReleaseSysCache(typtup);
+
+ return composite;
+}
+
+/*
+ * Deparse a CreateConversionStmt
+ *
+ * Given a conversion OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateConversion(Oid objectId, Node *parsetree)
+{
+ HeapTuple conTup;
+ Relation convrel;
+ Form_pg_conversion conForm;
+ ObjTree *ccStmt;
+ ObjTree *tmpObj;
+
+ convrel = table_open(ConversionRelationId, AccessShareLock);
+ conTup = get_catalog_object_by_oid(convrel, Anum_pg_conversion_oid, objectId);
+ if (!HeapTupleIsValid(conTup))
+ elog(ERROR, "cache lookup failed for conversion with OID %u", objectId);
+ conForm = (Form_pg_conversion) GETSTRUCT(conTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{default}s CONVERSION %{identity}D FOR %{source}L TO %{dest}L
+ * FROM %{function}D
+ */
+ ccStmt = new_objtree("CREATE");
+
+
+ /* Add the DEFAULT clause */
+ append_string_object(ccStmt, "default",
+ conForm->condefault ? "DEFAULT" : "");
+
+ tmpObj = new_objtree_for_qualname(conForm->connamespace, NameStr(conForm->conname));
+ append_object_object(ccStmt, "CONVERSION %{identity}D", tmpObj);
+ append_string_object(ccStmt, "FOR %{source}L", (char *)
+ pg_encoding_to_char(conForm->conforencoding));
+ append_string_object(ccStmt, "TO %{dest}L", (char *)
+ pg_encoding_to_char(conForm->contoencoding));
+ append_object_object(ccStmt, "FROM %{function}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ conForm->conproc));
+
+ table_close(convrel, AccessShareLock);
+
+ return ccStmt;
+}
+
+/*
+ * Deparse a CreateEnumStmt (CREATE TYPE AS ENUM)
+ *
+ * Given a Enum type OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateEnumStmt(Oid objectId, Node *parsetree)
+{
+ CreateEnumStmt *node = (CreateEnumStmt *) parsetree;
+ ObjTree *enumtype;
+ List *values;
+ ListCell *cell;
+
+ enumtype = new_objtree("CREATE TYPE");
+ append_object_object(enumtype, "%{identity}D",
+ new_objtree_for_qualname_id(TypeRelationId,
+ objectId));
+
+ values = NIL;
+ foreach(cell, node->vals)
+ {
+ String *val = lfirst_node(String, cell);
+
+ values = lappend(values, new_string_object(strVal(val)));
+ }
+
+ append_array_object(enumtype, "AS ENUM (%{values:, }L)", values);
+ return enumtype;
+}
+
+/*
+ * Deparse a CreateExtensionStmt
+ *
+ * Given an extension OID and the parse tree that created it, return the JSON
+ * blob representing the creation command.
+ *
+ */
+static ObjTree *
+deparse_CreateExtensionStmt(Oid objectId, Node *parsetree)
+{
+ CreateExtensionStmt *node = (CreateExtensionStmt *) parsetree;
+ Relation pg_extension;
+ HeapTuple extTup;
+ Form_pg_extension extForm;
+ ObjTree *extStmt;
+ ObjTree *tmp;
+ List *list;
+ ListCell *cell;
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE EXTENSION %{if_not_exists}s %{identity}I %{options: }s
+ */
+
+ pg_extension = table_open(ExtensionRelationId, AccessShareLock);
+ extTup = get_catalog_object_by_oid(pg_extension, Anum_pg_extension_oid, objectId);
+ if (!HeapTupleIsValid(extTup))
+ elog(ERROR, "cache lookup failed for extension with OID %u",
+ objectId);
+ extForm = (Form_pg_extension) GETSTRUCT(extTup);
+
+ extStmt = new_objtree("CREATE EXTENSION");
+
+ append_string_object(extStmt, "%{if_not_exists}s",
+ node->if_not_exists ? "IF NOT EXISTS" : "");
+
+ append_string_object(extStmt, "%{name}I", node->extname);
+
+ /* List of options */
+ list = NIL;
+ foreach(cell, node->options)
+ {
+ DefElem *opt = (DefElem *) lfirst(cell);
+
+ if (strcmp(opt->defname, "schema") == 0)
+ {
+ /* skip this one; we add one unconditionally below */
+ continue;
+ }
+ else if (strcmp(opt->defname, "new_version") == 0)
+ {
+ tmp = new_objtree_VA("VERSION %{version}L", 2,
+ "type", ObjTypeString, "version",
+ "version", ObjTypeString, defGetString(opt));
+ list = lappend(list, new_object_object(tmp));
+ }
+ else if (strcmp(opt->defname, "old_version") == 0)
+ {
+ tmp = new_objtree_VA("FROM %{version}L", 2,
+ "type", ObjTypeString, "from",
+ "version", ObjTypeString, defGetString(opt));
+ list = lappend(list, new_object_object(tmp));
+ }
+ else
+ elog(ERROR, "unsupported option %s", opt->defname);
+ }
+
+ /* Add the SCHEMA option */
+ tmp = new_objtree_VA("SCHEMA %{schema}I",
+ 2, "type", ObjTypeString, "schema",
+ "schema", ObjTypeString,
+ get_namespace_name(extForm->extnamespace));
+ list = lappend(list, new_object_object(tmp));
+
+ /* done */
+ append_array_object(extStmt, "%{options: }s", list);
+
+ table_close(pg_extension, AccessShareLock);
+
+ return extStmt;
+}
+
+/*
+ * If a column name is specified, add an element for it; otherwise it's a
+ * table-level option.
+ */
+static ObjTree *
+deparse_FdwOptions(List *options, char *colname)
+{
+ ObjTree *tmp;
+
+ if (colname)
+ tmp = new_objtree_VA("ALTER COLUMN %{column}I",
+ 1, "column", ObjTypeString, colname);
+ else
+ tmp = new_objtree_VA("OPTIONS", 0);
+
+ if (options != NIL)
+ {
+ List *optout = NIL;
+ ListCell *cell;
+
+ foreach(cell, options)
+ {
+ DefElem *elem;
+ ObjTree *opt;
+
+ elem = (DefElem *) lfirst(cell);
+
+ switch (elem->defaction)
+ {
+ case DEFELEM_UNSPEC:
+ opt = new_objtree_VA("%{label}I %{value}L", 2,
+ "label", ObjTypeString, elem->defname,
+ "value", ObjTypeString,
+ elem->arg ? defGetString(elem) :
+ defGetBoolean(elem) ? "TRUE" : "FALSE");
+ break;
+ case DEFELEM_SET:
+ opt = new_objtree_VA("SET %{label}I %{value}L", 2,
+ "label", ObjTypeString, elem->defname,
+ "value", ObjTypeString,
+ elem->arg ? defGetString(elem) :
+ defGetBoolean(elem) ? "TRUE" : "FALSE");
+ break;
+ case DEFELEM_ADD:
+ opt = new_objtree_VA("ADD %{label}I %{value}L", 2,
+ "label", ObjTypeString, elem->defname,
+ "value", ObjTypeString,
+ elem->arg ? defGetString(elem) :
+ defGetBoolean(elem) ? "TRUE" : "FALSE");
+ break;
+ case DEFELEM_DROP:
+ opt = new_objtree_VA("DROP %{label}I", 1,
+ "label", ObjTypeString, elem->defname);
+ break;
+ default:
+ elog(ERROR, "invalid def action %d", elem->defaction);
+ opt = NULL;
+ }
+
+ optout = lappend(optout, new_object_object(opt));
+ }
+
+ append_array_object(tmp, "(%{option: ,}s)", optout);
+ }
+ else
+ append_bool_object(tmp, "present", false);
+
+ return tmp;
+}
+
+/*
+ * Deparse a CreateFdwStmt (CREATE FOREIGN DATA WRAPPER)
+ *
+ * Given an FDW OID and the parse tree that created it,
+ * return an ObjTree representing the creation command.
+ */
+static ObjTree *
+deparse_CreateFdwStmt(Oid objectId, Node *parsetree)
+{
+ CreateFdwStmt *node = (CreateFdwStmt *) parsetree;
+ HeapTuple fdwTup;
+ Form_pg_foreign_data_wrapper fdwForm;
+ Relation rel;
+
+ ObjTree *createStmt;
+ ObjTree *tmp;
+
+ rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
+
+ fdwTup = SearchSysCache1(FOREIGNDATAWRAPPEROID,
+ ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(fdwTup))
+ elog(ERROR, "cache lookup failed for foreign-data wrapper %u", objectId);
+
+ fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(fdwTup);
+
+ createStmt = new_objtree_VA("CREATE FOREIGN DATA WRAPPER %{identity}I", 1,
+ "identity", ObjTypeString, NameStr(fdwForm->fdwname));
+
+ /* add HANDLER clause */
+ if (fdwForm->fdwhandler == InvalidOid)
+ tmp = new_objtree_VA("NO HANDLER", 0);
+ else
+ {
+ tmp = new_objtree_VA("HANDLER %{procedure}D", 1,
+ "handler", ObjTypeObject,
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ fdwForm->fdwhandler));
+ }
+ append_object_object(createStmt, "%{handler}s", tmp);
+
+ /* add VALIDATOR clause */
+ if (fdwForm->fdwvalidator == InvalidOid)
+ tmp = new_objtree_VA("NO VALIDATOR", 0);
+ else
+ {
+ tmp = new_objtree_VA("VALIDATOR %{procedure}D", 1,
+ "validator", ObjTypeObject,
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ fdwForm->fdwvalidator));
+ }
+ append_object_object(createStmt, "%{validator}s", tmp);
+
+ /* add an OPTIONS clause, if any */
+ append_object_object(createStmt, "%{generic_options}s",
+ deparse_FdwOptions(node->options, NULL));
+
+ ReleaseSysCache(fdwTup);
+ table_close(rel, RowExclusiveLock);
+
+ return createStmt;
+}
+
+/*
+ * Deparse an AlterFdwStmt (ALTER FOREIGN DATA WRAPPER)
+ *
+ * Given an FDW OID and the parse tree that created it, return the
+ * JSON blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterFdwStmt(Oid objectId, Node *parsetree)
+{
+ AlterFdwStmt *node = (AlterFdwStmt *) parsetree;
+ HeapTuple fdwTup;
+ Form_pg_foreign_data_wrapper fdwForm;
+ Relation rel;
+ ObjTree *alterStmt;
+ List *fdw_options = NIL;
+ ListCell *cell;
+
+ rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
+
+ fdwTup = SearchSysCache1(FOREIGNDATAWRAPPEROID,
+ ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(fdwTup))
+ elog(ERROR, "cache lookup failed for foreign-data wrapper %u", objectId);
+
+ fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(fdwTup);
+
+ alterStmt = new_objtree_VA("ALTER FOREIGN DATA WRAPPER %{identity}I", 1,
+ "identity", ObjTypeString, NameStr(fdwForm->fdwname));
+
+ /*
+ * Iterate through options, to see what changed, but use catalog as a
+ * basis for new values.
+ */
+ foreach(cell, node->func_options)
+ {
+ DefElem *elem;
+ ObjTree *tmp;
+
+ elem = lfirst(cell);
+
+ if (pg_strcasecmp(elem->defname, "handler") == 0)
+ {
+ /* add HANDLER clause */
+ if (fdwForm->fdwhandler == InvalidOid)
+ tmp = new_objtree_VA("NO HANDLER", 0);
+ else
+ {
+ tmp = new_objtree_VA("HANDLER %{procedure}D", 1,
+ "procedure",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ fdwForm->fdwhandler));
+ }
+ fdw_options = lappend(fdw_options, new_object_object(tmp));
+ }
+ else if (pg_strcasecmp(elem->defname, "validator") == 0)
+ {
+ /* add VALIDATOR clause */
+ if (fdwForm->fdwvalidator == InvalidOid)
+ tmp = new_objtree_VA("NO VALIDATOR", 0);
+ else
+ {
+ tmp = new_objtree_VA("VALIDATOR %{procedure}D", 1,
+ "procedure",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ fdwForm->fdwvalidator));
+ }
+ fdw_options = lappend(fdw_options, new_object_object(tmp));
+ }
+ }
+
+ /* Add HANDLER/VALIDATOR if specified */
+ append_array_object(alterStmt, "%{fdw_options: }s", fdw_options);
+
+
+ /* add an OPTIONS clause, if any */
+ append_object_object(alterStmt, "%{generic_options}D",
+ deparse_FdwOptions(node->options, NULL));
+
+ ReleaseSysCache(fdwTup);
+ table_close(rel, RowExclusiveLock);
+
+ return alterStmt;
+}
+
+/*
+ * Deparse a CREATE TYPE AS RANGE statement
+ *
+ * Given a Range type OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateRangeStmt(Oid objectId, Node *parsetree)
+{
+ ObjTree *range;
+ ObjTree *tmp;
+ List *definition = NIL;
+ Relation pg_range;
+ HeapTuple rangeTup;
+ Form_pg_range rangeForm;
+ ScanKeyData key[1];
+ SysScanDesc scan;
+
+ pg_range = table_open(RangeRelationId, RowExclusiveLock);
+
+ ScanKeyInit(&key[0],
+ Anum_pg_range_rngtypid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(objectId));
+
+ scan = systable_beginscan(pg_range, RangeTypidIndexId, true,
+ NULL, 1, key);
+
+ rangeTup = systable_getnext(scan);
+ if (!HeapTupleIsValid(rangeTup))
+ elog(ERROR, "cache lookup failed for range with type oid %u",
+ objectId);
+
+ rangeForm = (Form_pg_range) GETSTRUCT(rangeTup);
+
+ range = new_objtree_VA("CREATE TYPE", 0);
+ tmp = new_objtree_for_qualname_id(TypeRelationId, objectId);
+ append_object_object(range, "%{identity}D AS RANGE", tmp);
+
+ /* SUBTYPE */
+ tmp = new_objtree_for_qualname_id(TypeRelationId,
+ rangeForm->rngsubtype);
+ tmp = new_objtree_VA("SUBTYPE = %{type}D",
+ 2,
+ "clause", ObjTypeString, "subtype",
+ "type", ObjTypeObject, tmp);
+ definition = lappend(definition, new_object_object(tmp));
+
+ /* SUBTYPE_OPCLASS */
+ if (OidIsValid(rangeForm->rngsubopc))
+ {
+ tmp = new_objtree_for_qualname_id(OperatorClassRelationId,
+ rangeForm->rngsubopc);
+ tmp = new_objtree_VA("SUBTYPE_OPCLASS = %{opclass}D",
+ 2,
+ "clause", ObjTypeString, "opclass",
+ "opclass", ObjTypeObject, tmp);
+ definition = lappend(definition, new_object_object(tmp));
+ }
+
+ /* COLLATION */
+ if (OidIsValid(rangeForm->rngcollation))
+ {
+ tmp = new_objtree_for_qualname_id(CollationRelationId,
+ rangeForm->rngcollation);
+ tmp = new_objtree_VA("COLLATION = %{collation}D",
+ 2,
+ "clause", ObjTypeString, "collation",
+ "collation", ObjTypeObject, tmp);
+ definition = lappend(definition, new_object_object(tmp));
+ }
+
+ /* CANONICAL */
+ if (OidIsValid(rangeForm->rngcanonical))
+ {
+ tmp = new_objtree_for_qualname_id(ProcedureRelationId,
+ rangeForm->rngcanonical);
+ tmp = new_objtree_VA("CANONICAL = %{canonical}D",
+ 2,
+ "clause", ObjTypeString, "canonical",
+ "canonical", ObjTypeObject, tmp);
+ definition = lappend(definition, new_object_object(tmp));
+ }
+
+ /* SUBTYPE_DIFF */
+ if (OidIsValid(rangeForm->rngsubdiff))
+ {
+ tmp = new_objtree_for_qualname_id(ProcedureRelationId,
+ rangeForm->rngsubdiff);
+ tmp = new_objtree_VA("SUBTYPE_DIFF = %{subtype_diff}D",
+ 2,
+ "clause", ObjTypeString, "subtype_diff",
+ "subtype_diff", ObjTypeObject, tmp);
+ definition = lappend(definition, new_object_object(tmp));
+ }
+
+ append_array_object(range, "(%{definition:, }s)", definition);
+
+ systable_endscan(scan);
+ table_close(pg_range, RowExclusiveLock);
+
+ return range;
+}
+
+/*
+ * Deparse an AlterEnumStmt.
+ *
+ * Given an enum OID and a parse tree that modified it, return an ObjTree
+ * representing the alter type.
+ */
+static ObjTree *
+deparse_AlterEnumStmt(Oid objectId, Node *parsetree)
+{
+ AlterEnumStmt *node = (AlterEnumStmt *) parsetree;
+ ObjTree *alterEnum;
+
+ alterEnum = new_objtree_VA("ALTER TYPE", 0);
+ append_object_object(alterEnum, "%{identity}D",
+ new_objtree_for_qualname_id(TypeRelationId,
+ objectId));
+
+ if (!node->oldVal)
+ {
+ append_format_string(alterEnum, "ADD VALUE");
+ append_string_object(alterEnum, "%{if_not_exists}s",
+ node->skipIfNewValExists ? "IF NOT EXISTS" : "");
+
+ append_string_object(alterEnum, "%{value}L", node->newVal);
+
+ if (node->newValNeighbor)
+ {
+ append_string_object(alterEnum, "%{after_or_before}s",
+ node->newValIsAfter ? "AFTER" : "BEFORE");
+ append_string_object(alterEnum, "%{neighbour}L", node->newValNeighbor);
+ }
+ }
+ else
+ {
+ append_string_object(alterEnum, "RENAME VALUE %{value}L TO",
+ node->oldVal);
+ append_string_object(alterEnum, "%{newvalue}L",
+ node->newVal);
+ }
+
+ return alterEnum;
+}
+
+/*
+ * Deparse an AlterExtensionStmt (ALTER EXTENSION .. UPDATE TO VERSION)
+ *
+ * Given an extension OID and a parse tree that modified it, return an ObjTree
+ * representing the alter type.
+ */
+static ObjTree *
+deparse_AlterExtensionStmt(Oid objectId, Node *parsetree)
+{
+ AlterExtensionStmt *node = (AlterExtensionStmt *) parsetree;
+ Relation pg_extension;
+ HeapTuple extTup;
+ Form_pg_extension extForm;
+ ObjTree *stmt;
+ ObjTree *tmp;
+ List *list = NIL;
+ ListCell *cell;
+
+ pg_extension = table_open(ExtensionRelationId, AccessShareLock);
+ extTup = get_catalog_object_by_oid(pg_extension, Anum_pg_extension_oid, objectId);
+ if (!HeapTupleIsValid(extTup))
+ elog(ERROR, "cache lookup failed for extension with OID %u",
+ objectId);
+ extForm = (Form_pg_extension) GETSTRUCT(extTup);
+
+ stmt = new_objtree_VA("ALTER EXTENSION %{identity}D UPDATE", 1,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(extForm->extnamespace,
+ NameStr(extForm->extname)));
+
+ foreach(cell, node->options)
+ {
+ DefElem *opt = (DefElem *) lfirst(cell);
+
+ if (strcmp(opt->defname, "new_version") == 0)
+ {
+ tmp = new_objtree_VA("TO %{version}L", 2,
+ "type", ObjTypeString, "version",
+ "version", ObjTypeString, defGetString(opt));
+ list = lappend(list, new_object_object(tmp));
+ }
+ else
+ elog(ERROR, "unsupported option %s", opt->defname);
+ }
+
+ append_array_object(stmt, "%{options: }s", list);
+
+ table_close(pg_extension, AccessShareLock);
+
+ return stmt;
+}
+
+/*
+ * Deparse an AlterExtensionContentsStmt (ALTER EXTENSION ext ADD/DROP object)
+ *
+ */
+static ObjTree *
+deparse_AlterExtensionContentsStmt(Oid objectId, Node *parsetree,
+ ObjectAddress objectAddress)
+{
+ AlterExtensionContentsStmt *node = (AlterExtensionContentsStmt *) parsetree;
+ ObjTree *stmt;
+ char *fmt;
+
+ Assert(node->action == +1 || node->action == -1);
+
+ fmt = psprintf("ALTER EXTENSION %%{extidentity}I %s %s %%{objidentity}s",
+ node->action == +1 ? "ADD" : "DROP",
+ stringify_objtype(node->objtype));
+
+ stmt = new_objtree_VA(fmt, 2, "extidentity", ObjTypeString,
+ node->extname,
+ "objidentity", ObjTypeString,
+ getObjectIdentity(&objectAddress, false));
+
+ return stmt;
+}
+
+/*
+ * Deparse an CreateCastStmt.
+ *
+ * Given a sequence OID and a parse tree that modified it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateCastStmt(Oid objectId, Node *parsetree)
+{
+ CreateCastStmt *node = (CreateCastStmt *) parsetree;
+ Relation castrel;
+ HeapTuple castTup;
+ Form_pg_cast castForm;
+ ObjTree *createCast;
+ char *context;
+
+ castrel = table_open(CastRelationId, AccessShareLock);
+ castTup = get_catalog_object_by_oid(castrel, Anum_pg_cast_oid, objectId);
+ if (!HeapTupleIsValid(castTup))
+ elog(ERROR, "cache lookup failed for cast with OID %u", objectId);
+ castForm = (Form_pg_cast) GETSTRUCT(castTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE CAST (%{sourcetype}T AS %{targettype}T) %{mechanism}s
+ * %{context}s
+ */
+ createCast = new_objtree_VA("CREATE CAST (%{sourcetype}T AS %{targettype}T)",
+ 2, "sourcetype", ObjTypeObject,
+ new_objtree_for_type(castForm->castsource, -1),
+ "targettype", ObjTypeObject,
+ new_objtree_for_type(castForm->casttarget, -1));
+
+ if (node->inout)
+ append_string_object(createCast, "%{mechanism}s", "WITH INOUT");
+ else if (node->func == NULL)
+ append_string_object(createCast, "%{mechanism}s", "WITHOUT FUNCTION");
+ else
+ {
+ ObjTree *tmpobj;
+ StringInfoData func;
+ HeapTuple funcTup;
+ Form_pg_proc funcForm;
+ int i;
+
+ funcTup = SearchSysCache1(PROCOID, castForm->castfunc);
+ funcForm = (Form_pg_proc) GETSTRUCT(funcTup);
+
+ initStringInfo(&func);
+ appendStringInfo(&func, "%s(",
+ quote_qualified_identifier(get_namespace_name(funcForm->pronamespace),
+ NameStr(funcForm->proname)));
+ for (i = 0; i < funcForm->pronargs; i++)
+ appendStringInfoString(&func,
+ format_type_be_qualified(funcForm->proargtypes.values[i]));
+ appendStringInfoChar(&func, ')');
+
+ tmpobj = new_objtree_VA("WITH FUNCTION %{castfunction}s", 1,
+ "castfunction", ObjTypeString, func.data);
+ append_object_object(createCast, "%{mechanism}s", tmpobj);
+
+ ReleaseSysCache(funcTup);
+ }
+
+ switch (node->context)
+ {
+ case COERCION_IMPLICIT:
+ context = "AS IMPLICIT";
+ break;
+ case COERCION_ASSIGNMENT:
+ context = "AS ASSIGNMENT";
+ break;
+ case COERCION_EXPLICIT:
+ context = "";
+ break;
+ default:
+ elog(ERROR, "invalid coercion code %c", node->context);
+ return NULL; /* keep compiler quiet */
+ }
+ append_string_object(createCast, "%{context}s", context);
+
+ table_close(castrel, AccessShareLock);
+
+ return createCast;
+}
+
+/*
+ * Deparse all the collected subcommands and return an ObjTree representing the
+ * alter command.
+ */
+static ObjTree *
+deparse_AlterTableStmt(CollectedCommand *cmd)
+{
+ ObjTree *alterTableStmt;
+ ObjTree *tmpobj;
+ ObjTree *tmpobj2;
+ List *dpcontext;
+ Relation rel;
+ List *subcmds = NIL;
+ ListCell *cell;
+ char *fmtstr;
+ const char *reltype;
+ bool istype = false;
+ List *exprs = NIL;
+
+ Assert(cmd->type == SCT_AlterTable);
+
+ rel = relation_open(cmd->d.alterTable.objectId, AccessShareLock);
+ dpcontext = deparse_context_for(RelationGetRelationName(rel),
+ cmd->d.alterTable.objectId);
+
+ switch (rel->rd_rel->relkind)
+ {
+ case RELKIND_RELATION:
+ case RELKIND_PARTITIONED_TABLE:
+ reltype = "TABLE";
+ break;
+ case RELKIND_INDEX:
+ case RELKIND_PARTITIONED_INDEX:
+ reltype = "INDEX";
+ break;
+ case RELKIND_VIEW:
+ reltype = "VIEW";
+ break;
+ case RELKIND_COMPOSITE_TYPE:
+ reltype = "TYPE";
+ istype = true;
+ break;
+ case RELKIND_FOREIGN_TABLE:
+ reltype = "FOREIGN TABLE";
+ break;
+
+ /* TODO support for partitioned table */
+
+ default:
+ elog(ERROR, "unexpected relkind %d", rel->rd_rel->relkind);
+ }
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER reltype %{identity}D %{subcmds:, }s
+ */
+ fmtstr = psprintf("ALTER %s", reltype);
+ alterTableStmt = new_objtree(fmtstr);
+
+ tmpobj = new_objtree_for_qualname(rel->rd_rel->relnamespace,
+ RelationGetRelationName(rel));
+ append_object_object(alterTableStmt, "%{identity}D", tmpobj);
+
+ foreach(cell, cmd->d.alterTable.subcmds)
+ {
+ CollectedATSubcmd *sub = (CollectedATSubcmd *) lfirst(cell);
+ AlterTableCmd *subcmd = (AlterTableCmd *) sub->parsetree;
+ ObjTree *tree;
+
+ Assert(IsA(subcmd, AlterTableCmd));
+
+ switch (subcmd->subtype)
+ {
+ case AT_AddColumn:
+ case AT_AddColumnRecurse:
+ /* XXX need to set the "recurse" bit somewhere? */
+ Assert(IsA(subcmd->def, ColumnDef));
+ tree = deparse_ColumnDef(rel, dpcontext, false,
+ (ColumnDef *) subcmd->def, true, &exprs);
+ fmtstr = psprintf("ADD %s %%{definition}s",
+ istype ? "ATTRIBUTE" : "COLUMN");
+ tmpobj = new_objtree_VA(fmtstr, 2,
+ "type", ObjTypeString, "add column",
+ "definition", ObjTypeObject, tree);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_AddIndexConstraint:
+ {
+ IndexStmt *istmt;
+ Relation idx;
+ Oid constrOid = sub->address.objectId;
+
+ Assert(IsA(subcmd->def, IndexStmt));
+ istmt = (IndexStmt *) subcmd->def;
+
+ Assert(istmt->isconstraint && istmt->unique);
+
+ idx = relation_open(istmt->indexOid, AccessShareLock);
+
+ /*
+ * Verbose syntax
+ *
+ * ADD CONSTRAINT %{name}I %{constraint_type}s USING INDEX
+ * %index_name}I %{deferrable}s %{init_deferred}s
+ */
+ tmpobj = new_objtree_VA("ADD CONSTRAINT %{name}I", 2,
+ "type", ObjTypeString, "add constraint using index",
+ "name", ObjTypeString, get_constraint_name(constrOid));
+
+ append_string_object(tmpobj, "%{constraint_type}s", istmt->deferrable ?
+ "DEFERRABLE" : "NOT DEFERRABLE");
+ append_string_object(tmpobj, "USING INDEX %{index_name}I",
+ RelationGetRelationName(idx));
+ append_string_object(tmpobj, "%{deferrable}s", istmt->deferrable ?
+ "DEFERRABLE" : "NOT DEFERRABLE");
+ append_string_object(tmpobj, "%{init_deferred}s", istmt->initdeferred ?
+ "INITIALLY DEFERRED" : "INITIALLY IMMEDIATE");
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+
+ relation_close(idx, AccessShareLock);
+ }
+ break;
+
+ case AT_ReAddIndex:
+ case AT_ReAddConstraint:
+ case AT_ReAddComment:
+ case AT_ReplaceRelOptions:
+ /* Subtypes used for internal operations; nothing to do here */
+ break;
+
+ case AT_AddColumnToView:
+ /* CREATE OR REPLACE VIEW -- nothing to do here */
+ break;
+
+ case AT_ColumnDefault:
+ if (subcmd->def == NULL)
+ {
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I DROP DEFAULT", 2,
+ "type", ObjTypeString, "drop default",
+ "column", ObjTypeString, subcmd->name);
+ }
+ else
+ {
+ List *dpcontext_rel;
+ HeapTuple attrtup;
+ AttrNumber attno;
+
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I SET DEFAULT", 2,
+ "type", ObjTypeString, "set default",
+ "column", ObjTypeString, subcmd->name);
+
+ dpcontext_rel = deparse_context_for(RelationGetRelationName(rel),
+ RelationGetRelid(rel));
+ attrtup = SearchSysCacheAttName(RelationGetRelid(rel), subcmd->name);
+ attno = ((Form_pg_attribute) GETSTRUCT(attrtup))->attnum;
+ append_string_object(tmpobj, "%{definition}s",
+ RelationGetColumnDefault(rel, attno,
+ dpcontext_rel,
+ NULL));
+ ReleaseSysCache(attrtup);
+ }
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropNotNull:
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I DROP NOT NULL", 2,
+ "type", ObjTypeString, "drop not null",
+ "column", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_SetNotNull:
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I SET NOT NULL", 2,
+ "type", ObjTypeString, "set not null",
+ "column", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropExpression:
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I DROP EXPRESSION", 2,
+ "type", ObjTypeString, "drop expression",
+ "column", ObjTypeString, subcmd->name);
+ append_string_object(tmpobj, "%{if_not_exists}s",
+ subcmd->missing_ok ? "IF EXISTS" : "");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_SetStatistics:
+ {
+ Assert(IsA(subcmd->def, Integer));
+ if (subcmd->name)
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I SET STATISTICS %{statistics}n",
+ 3,
+ "type", ObjTypeString, "set statistics",
+ "column", ObjTypeString, subcmd->name,
+ "statistics", ObjTypeInteger,
+ intVal((Integer *) subcmd->def));
+ else
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}n SET STATISTICS %{statistics}n",
+ 3,
+ "type", ObjTypeString, "set statistics",
+ "column", ObjTypeInteger, subcmd->num,
+ "statistics", ObjTypeInteger,
+ intVal((Integer *) subcmd->def));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ }
+ break;
+
+ case AT_SetOptions:
+ case AT_ResetOptions:
+ subcmds = lappend(subcmds, new_object_object(
+ deparse_ColumnSetOptions(subcmd)));
+ break;
+
+ case AT_SetStorage:
+ Assert(IsA(subcmd->def, String));
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I SET STORAGE %{storage}s", 3,
+ "type", ObjTypeString, "set storage",
+ "column", ObjTypeString, subcmd->name,
+ "storage", ObjTypeString,
+ strVal((String *) subcmd->def));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_SetCompression:
+ Assert(IsA(subcmd->def, String));
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I SET COMPRESSION %{compression_method}s",
+ 3,
+ "type", ObjTypeString, "set compression",
+ "column", ObjTypeString, subcmd->name,
+ "compression_method", ObjTypeString,
+ strVal((String *) subcmd->def));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropColumnRecurse:
+ case AT_DropColumn:
+ fmtstr = psprintf("DROP %s %%{column}I",
+ istype ? "ATTRIBUTE" : "COLUMN");
+ tmpobj = new_objtree_VA(fmtstr, 2,
+ "type", ObjTypeString, "drop column",
+ "column", ObjTypeString, subcmd->name);
+ tmpobj2 = new_objtree_VA("CASCADE", 1,
+ "present", ObjTypeBool, subcmd->behavior);
+ append_object_object(tmpobj, "%{cascade}s", tmpobj2);
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_AddIndex:
+ {
+ Oid idxOid = sub->address.objectId;
+ IndexStmt *istmt;
+ Relation idx;
+ const char *idxname;
+ Oid constrOid;
+
+ Assert(IsA(subcmd->def, IndexStmt));
+ istmt = (IndexStmt *) subcmd->def;
+
+ if (!istmt->isconstraint)
+ break;
+
+ idx = relation_open(idxOid, AccessShareLock);
+ idxname = RelationGetRelationName(idx);
+
+ constrOid = get_relation_constraint_oid(
+ cmd->d.alterTable.objectId, idxname, false);
+
+ tmpobj = new_objtree_VA("ADD CONSTRAINT %{name}I %{definition}s", 3,
+ "type", ObjTypeString, "add constraint",
+ "name", ObjTypeString, idxname,
+ "definition", ObjTypeString,
+ pg_get_constraintdef_command_simple(constrOid));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+
+ relation_close(idx, AccessShareLock);
+ }
+ break;
+
+ case AT_AddConstraint:
+ case AT_AddConstraintRecurse:
+ {
+ /* XXX need to set the "recurse" bit somewhere? */
+ Oid constrOid = sub->address.objectId;
+ bool isnull;
+ HeapTuple tup;
+ Datum val;
+ Constraint *constr;
+
+ Assert(IsA(subcmd->def, Constraint));
+ constr = castNode(Constraint, subcmd->def);
+
+ if (!constr->skip_validation)
+ {
+ tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constrOid));
+
+ if (HeapTupleIsValid(tup))
+ {
+ char *conbin;
+
+ /* Fetch constraint expression in parsetree form */
+ val = SysCacheGetAttr(CONSTROID, tup,
+ Anum_pg_constraint_conbin, &isnull);
+
+ if (!isnull)
+ {
+ conbin = TextDatumGetCString(val);
+ exprs = lappend(exprs, stringToNode(conbin));
+ }
+
+ ReleaseSysCache(tup);
+ }
+ }
+
+ tmpobj = new_objtree_VA("ADD CONSTRAINT %{name}I %{definition}s", 3,
+ "type", ObjTypeString, "add constraint",
+ "name", ObjTypeString, get_constraint_name(constrOid),
+ "definition", ObjTypeString,
+ pg_get_constraintdef_command_simple(constrOid));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ }
+ break;
+
+ case AT_AlterConstraint:
+ {
+ Oid constrOid = sub->address.objectId;
+ Constraint *c = (Constraint *) subcmd->def;
+
+ /* If no constraint was altered, silently skip it */
+ if (!OidIsValid(constrOid))
+ break;
+
+ Assert(IsA(c, Constraint));
+ tmpobj = new_objtree_VA("ALTER CONSTRAINT %{name}I %{deferrable}s %{init_deferred}s",
+ 4,
+ "type", ObjTypeString, "alter constraint",
+ "name", ObjTypeString, get_constraint_name(constrOid),
+ "deferrable", ObjTypeString,
+ c->deferrable ? "DEFERRABLE" : "NOT DEFERRABLE",
+ "init_deferred", ObjTypeString,
+ c->initdeferred ? "INITIALLY DEFERRED" : "INITIALLY IMMEDIATE");
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ }
+ break;
+
+ case AT_ValidateConstraintRecurse:
+ case AT_ValidateConstraint:
+ tmpobj = new_objtree_VA("VALIDATE CONSTRAINT %{constraint}I", 2,
+ "type", ObjTypeString, "validate constraint",
+ "constraint", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropConstraintRecurse:
+ case AT_DropConstraint:
+ tmpobj = new_objtree_VA("DROP CONSTRAINT %{constraint}I", 2,
+ "type", ObjTypeString, "drop constraint",
+ "constraint", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_AlterColumnType:
+ {
+ TupleDesc tupdesc = RelationGetDescr(rel);
+ Form_pg_attribute att;
+ ColumnDef *def;
+
+ att = &(tupdesc->attrs[sub->address.objectSubId - 1]);
+ def = (ColumnDef *) subcmd->def;
+ Assert(IsA(def, ColumnDef));
+
+ /*
+ * Verbose syntax
+ *
+ * Composite types: ALTER reltype %{column}I SET DATA TYPE
+ * %{datatype}T %{collation}s ATTRIBUTE %{cascade}s
+ *
+ * Normal types: ALTER reltype %{column}I SET DATA TYPE
+ * %{datatype}T %{collation}s COLUMN %{using}s
+ */
+ fmtstr = psprintf("ALTER %s %%{column}I SET DATA TYPE",
+ istype ? "ATTRIBUTE" : "COLUMN");
+
+ tmpobj = new_objtree_VA(fmtstr, 2,
+ "type", ObjTypeString, "alter column type",
+ "column", ObjTypeString, subcmd->name);
+ /* Add the TYPE clause */
+ append_object_object(tmpobj, "%{datatype}T",
+ new_objtree_for_type(att->atttypid,
+ att->atttypmod));
+
+ /* Add a COLLATE clause, if needed */
+ tmpobj2 = new_objtree("COLLATE");
+ if (OidIsValid(att->attcollation))
+ {
+ ObjTree *collname;
+
+ collname = new_objtree_for_qualname_id(CollationRelationId,
+ att->attcollation);
+ append_object_object(tmpobj2, "%{name}D", collname);
+ }
+ else
+ append_bool_object(tmpobj2, "present", false);
+ append_object_object(tmpobj, "%{collation}s", tmpobj2);
+
+ /* If not a composite type, add the USING clause */
+ if (!istype)
+ {
+ /*
+ * If there's a USING clause, transformAlterTableStmt
+ * ran it through transformExpr and stored the
+ * resulting node in cooked_default, which we can use
+ * here.
+ */
+ tmpobj2 = new_objtree("USING");
+ if (def->raw_default)
+ {
+ Datum deparsed;
+ char *defexpr;
+
+ exprs = lappend(exprs, def->cooked_default);
+ defexpr = nodeToString(def->cooked_default);
+ deparsed = DirectFunctionCall2(pg_get_expr,
+ CStringGetTextDatum(defexpr),
+ RelationGetRelid(rel));
+ append_string_object(tmpobj2, "%{expression}s",
+ TextDatumGetCString(deparsed));
+ }
+ else
+ append_bool_object(tmpobj2, "present", false);
+ append_object_object(tmpobj, "%{using}s", tmpobj2);
+ }
+
+ /* If it's a composite type, add the CASCADE clause */
+ if (istype)
+ {
+ tmpobj2 = new_objtree("CASCADE");
+ if (subcmd->behavior != DROP_CASCADE)
+ append_bool_object(tmpobj2, "present", false);
+ append_object_object(tmpobj, "%{cascade}s", tmpobj2);
+ }
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ }
+ break;
+
+#ifdef TODOLIST
+ case AT_AlterColumnGenericOptions:
+ tmpobj = deparse_FdwOptions((List *) subcmd->def,
+ subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+#endif
+ case AT_ChangeOwner:
+ tmpobj = new_objtree_VA("OWNER TO %{owner}I", 2,
+ "type", ObjTypeString, "change owner",
+ "owner", ObjTypeString,
+ get_rolespec_name(subcmd->newowner));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_ClusterOn:
+ tmpobj = new_objtree_VA("CLUSTER ON %{index}I", 2,
+ "type", ObjTypeString, "cluster on",
+ "index", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropCluster:
+ tmpobj = new_objtree_VA("SET WITHOUT CLUSTER", 1,
+ "type", ObjTypeString, "set without cluster");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_SetLogged:
+ tmpobj = new_objtree_VA("SET LOGGED", 1,
+ "type", ObjTypeString, "set logged");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_SetUnLogged:
+ tmpobj = new_objtree_VA("SET UNLOGGED", 1,
+ "type", ObjTypeString, "set unlogged");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropOids:
+ tmpobj = new_objtree_VA("SET WITHOUT OIDS", 1,
+ "type", ObjTypeString, "set without oids");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ case AT_SetAccessMethod:
+ tmpobj = new_objtree_VA("SET ACCESS METHOD %{access_method}I", 2,
+ "type", ObjTypeString, "set access method",
+ "access_method", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ case AT_SetTableSpace:
+ tmpobj = new_objtree_VA("SET TABLESPACE %{tablespace}I", 2,
+ "type", ObjTypeString, "set tablespace",
+ "tablespace", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_SetRelOptions:
+ case AT_ResetRelOptions:
+ subcmds = lappend(subcmds, new_object_object(
+ deparse_RelSetOptions(subcmd)));
+ break;
+
+ case AT_EnableTrig:
+ tmpobj = new_objtree_VA("ENABLE TRIGGER %{trigger}I", 2,
+ "type", ObjTypeString, "enable trigger",
+ "trigger", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableAlwaysTrig:
+ tmpobj = new_objtree_VA("ENABLE ALWAYS TRIGGER %{trigger}I", 2,
+ "type", ObjTypeString, "enable always trigger",
+ "trigger", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableReplicaTrig:
+ tmpobj = new_objtree_VA("ENABLE REPLICA TRIGGER %{trigger}I", 2,
+ "type", ObjTypeString, "enable replica trigger",
+ "trigger", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DisableTrig:
+ tmpobj = new_objtree_VA("DISABLE TRIGGER %{trigger}I", 2,
+ "type", ObjTypeString, "disable trigger",
+ "trigger", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableTrigAll:
+ tmpobj = new_objtree_VA("ENABLE TRIGGER ALL", 1,
+ "type", ObjTypeString, "enable trigger all");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DisableTrigAll:
+ tmpobj = new_objtree_VA("DISABLE TRIGGER ALL", 1,
+ "type", ObjTypeString, "disable trigger all");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableTrigUser:
+ tmpobj = new_objtree_VA("ENABLE TRIGGER USER", 1,
+ "type", ObjTypeString, "enable trigger user");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DisableTrigUser:
+ tmpobj = new_objtree_VA("DISABLE TRIGGER USER", 1,
+ "type", ObjTypeString, "disable trigger user");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableRule:
+ tmpobj = new_objtree_VA("ENABLE RULE %{rule}I", 2,
+ "type", ObjTypeString, "enable rule",
+ "rule", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableAlwaysRule:
+ tmpobj = new_objtree_VA("ENABLE ALWAYS RULE %{rule}I", 2,
+ "type", ObjTypeString, "enable always rule",
+ "rule", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableReplicaRule:
+ tmpobj = new_objtree_VA("ENABLE REPLICA RULE %{rule}I", 2,
+ "type", ObjTypeString, "enable replica rule",
+ "rule", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DisableRule:
+ tmpobj = new_objtree_VA("DISABLE RULE %{rule}I", 2,
+ "type", ObjTypeString, "disable rule",
+ "rule", ObjTypeString, subcmd->name);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_AddInherit:
+ tmpobj = new_objtree_VA("INHERIT %{parent}D", 2,
+ "type", ObjTypeString, "inherit",
+ "parent", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ sub->address.objectId));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropInherit:
+ tmpobj = new_objtree_VA("NO INHERIT %{parent}D", 2,
+ "type", ObjTypeString, "drop inherit",
+ "parent", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ sub->address.objectId));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_AddOf:
+ tmpobj = new_objtree_VA("OF %{type_of}T", 2,
+ "type", ObjTypeString, "add of",
+ "type_of", ObjTypeObject,
+ new_objtree_for_type(sub->address.objectId, -1));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DropOf:
+ tmpobj = new_objtree_VA("NOT OF", 1,
+ "type", ObjTypeString, "not of");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_ReplicaIdentity:
+ tmpobj = new_objtree_VA("REPLICA IDENTITY", 1,
+ "type", ObjTypeString, "replica identity");
+ switch (((ReplicaIdentityStmt *) subcmd->def)->identity_type)
+ {
+ case REPLICA_IDENTITY_DEFAULT:
+ append_string_object(tmpobj, "%{ident}s", "DEFAULT");
+ break;
+ case REPLICA_IDENTITY_FULL:
+ append_string_object(tmpobj, "%{ident}s", "FULL");
+ break;
+ case REPLICA_IDENTITY_NOTHING:
+ append_string_object(tmpobj, "%{ident}s", "NOTHING");
+ break;
+ case REPLICA_IDENTITY_INDEX:
+ tmpobj2 = new_objtree_VA("USING INDEX %{index}I", 1,
+ "index", ObjTypeString,
+ ((ReplicaIdentityStmt *) subcmd->def)->name);
+ append_object_object(tmpobj, "%{ident}s", tmpobj2);
+ break;
+ }
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_EnableRowSecurity:
+ tmpobj = new_objtree_VA("ENABLE ROW LEVEL SECURITY", 1,
+ "type", ObjTypeString, "enable row security");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+
+ case AT_DisableRowSecurity:
+ tmpobj = new_objtree_VA("DISABLE ROW LEVEL SECURITY", 1,
+ "type", ObjTypeString, "disable row security");
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+#ifdef TODOLIST
+ case AT_GenericOptions:
+ tmpobj = deparse_FdwOptions((List *) subcmd->def, NULL);
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+#endif
+ case AT_AttachPartition:
+ tmpobj = new_objtree_VA("ATTACH PARTITION %{partition_identity}D", 2,
+ "type", ObjTypeString, "attach partition",
+ "partition_identity", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ sub->address.objectId));
+
+ if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+ append_string_object(tmpobj, "%{partition_bound}s",
+ RelationGetPartitionBound(sub->address.objectId));
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ case AT_DetachPartition:
+ tmpobj = new_objtree_VA("DETACH PARTITION %{partition_identity}D", 2,
+ "type", ObjTypeString, "detach partition",
+ "partition_identity", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ sub->address.objectId));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ case AT_DetachPartitionFinalize:
+ tmpobj = new_objtree_VA("DETACH PARTITION %{partition_identity}D FINALIZE", 2,
+ "type", ObjTypeString, "detach partition finalize",
+ "partition_identity", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ sub->address.objectId));
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ case AT_AddIdentity:
+ {
+ AttrNumber attnum;
+ Oid seq_relid;
+ ObjTree *seqdef;
+ ColumnDef *coldef = (ColumnDef *) subcmd->def;
+
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I ADD %{identity_column}s", 2,
+ "type", ObjTypeString, "add identity",
+ "column", ObjTypeString, subcmd->name);
+
+ attnum = get_attnum(RelationGetRelid(rel), subcmd->name);
+ seq_relid = getIdentitySequence(RelationGetRelid(rel), attnum, true);
+ seqdef = deparse_ColumnIdentity(seq_relid, coldef->identity, false);
+
+ append_object_object(tmpobj, "identity_column", seqdef);
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ }
+ break;
+ case AT_SetIdentity:
+ {
+ DefElem *defel;
+ char identity = 0;
+ ObjTree *seqdef;
+ AttrNumber attnum;
+ Oid seq_relid;
+
+
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I", 2,
+ "type", ObjTypeString, "set identity",
+ "column", ObjTypeString, subcmd->name);
+
+ if (subcmd->def)
+ {
+ List *def = (List *) subcmd->def;
+
+ Assert(IsA(subcmd->def, List));
+
+ defel = linitial_node(DefElem, def);
+ identity = defGetInt32(defel);
+ }
+
+ attnum = get_attnum(RelationGetRelid(rel), subcmd->name);
+ seq_relid = getIdentitySequence(RelationGetRelid(rel), attnum, true);
+ seqdef = deparse_ColumnIdentity(seq_relid, identity, true);
+
+ append_object_object(tmpobj, "%{definition}s", seqdef);
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ }
+ case AT_DropIdentity:
+ tmpobj = new_objtree_VA("ALTER COLUMN %{column}I DROP IDENTITY", 2,
+ "type", ObjTypeString, "drop identity",
+ "column", ObjTypeString, subcmd->name);
+
+ append_string_object(tmpobj, "%{if_not_exists}s",
+ subcmd->missing_ok ? "IF EXISTS" : "");
+
+ subcmds = lappend(subcmds, new_object_object(tmpobj));
+ break;
+ default:
+ elog(WARNING, "unsupported alter table subtype %d",
+ subcmd->subtype);
+ break;
+ }
+
+ /*
+ * We don't support replicating ALTER TABLE which contains volatile
+ * functions because It's possible the functions contain DDL/DML in
+ * which case these operations will be executed twice and cause
+ * duplicate data. In addition, we don't know whether the tables being
+ * accessed by these DDL/DML are published or not. So blindly allowing
+ * such functions can allow unintended clauses like the tables
+ * accessed in those functions may not even exist on the subscriber.
+ */
+ if (contain_volatile_functions((Node *) exprs))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("ALTER TABLE command using volatile function cannot be replicated")));
+
+ /*
+ * Clean the list as we already confirmed there is no volatile
+ * function.
+ */
+ list_free(exprs);
+ exprs = NIL;
+ }
+
+ table_close(rel, AccessShareLock);
+
+ if (list_length(subcmds) == 0)
+ return NULL;
+
+ append_array_object(alterTableStmt, "%{subcmds:, }s", subcmds);
+
+ return alterTableStmt;
+}
+
+/*
+ * Subroutine for CREATE TABLE deparsing.
+ *
+ * Deparse a ColumnDef node within a regular (non-typed) table creation.
+ *
+ * NOT NULL constraints in the column definition are emitted directly in the
+ * column definition by this routine; other constraints must be emitted
+ * elsewhere (the info in the parse node is incomplete anyway.).
+ */
+static ObjTree *
+deparse_ColumnDef(Relation relation, List *dpcontext, bool composite,
+ ColumnDef *coldef, bool is_alter, List **exprs)
+{
+ ObjTree *column;
+ ObjTree *tmpobj;
+ Oid relid = RelationGetRelid(relation);
+ HeapTuple attrTup;
+ Form_pg_attribute attrForm;
+ Oid typid;
+ int32 typmod;
+ Oid typcollation;
+ bool saw_notnull;
+ ListCell *cell;
+
+ /*
+ * Inherited columns without local definitions must not be emitted.
+ *
+ * XXX maybe it is useful to have them with "present = false" or some
+ * such?
+ */
+ if (!coldef->is_local)
+ return NULL;
+
+ attrTup = SearchSysCacheAttName(relid, coldef->colname);
+ if (!HeapTupleIsValid(attrTup))
+ elog(ERROR, "could not find cache entry for column \"%s\" of relation %u",
+ coldef->colname, relid);
+ attrForm = (Form_pg_attribute) GETSTRUCT(attrTup);
+
+ get_atttypetypmodcoll(relid, attrForm->attnum,
+ &typid, &typmod, &typcollation);
+
+ /*
+ * Verbose syntax
+ *
+ * %{name}I %{coltype}T %{compression}s %{default}s %{not_null}s
+ * %{collation}s
+ */
+ column = new_objtree_VA("%{name}I %{coltype}T", 3,
+ "type", ObjTypeString, "column",
+ "name", ObjTypeString, coldef->colname,
+ "coltype", ObjTypeObject,
+ new_objtree_for_type(typid, typmod));
+
+ /* USING clause */
+ tmpobj = new_objtree("COMPRESSION");
+ if (coldef->compression)
+ append_string_object(tmpobj, "%{compression_method}I", coldef->compression);
+ else
+ {
+ append_null_object(tmpobj, "%{compression_method}I");
+ append_bool_object(tmpobj, "present", false);
+ }
+ append_object_object(column, "%{compression}s", tmpobj);
+
+ tmpobj = new_objtree("COLLATE");
+ if (OidIsValid(typcollation))
+ {
+ ObjTree *collname;
+
+ collname = new_objtree_for_qualname_id(CollationRelationId,
+ typcollation);
+ append_object_object(tmpobj, "%{name}D", collname);
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(column, "%{collation}s", tmpobj);
+
+ if (!composite)
+ {
+ Oid seqrelid = InvalidOid;
+
+ /*
+ * Emit a NOT NULL declaration if necessary. Note that we cannot
+ * trust pg_attribute.attnotnull here, because that bit is also set
+ * when primary keys are specified; we must not emit a NOT NULL
+ * constraint in that case, unless explicitly specified. Therefore,
+ * we scan the list of constraints attached to this column to
+ * determine whether we need to emit anything. (Fortunately, NOT NULL
+ * constraints cannot be table constraints.)
+ *
+ * In the ALTER TABLE cases, we also add a NOT NULL if the colDef is
+ * marked is_not_null.
+ */
+ saw_notnull = false;
+ foreach(cell, coldef->constraints)
+ {
+ Constraint *constr = (Constraint *) lfirst(cell);
+
+ if (constr->contype == CONSTR_NOTNULL)
+ saw_notnull = true;
+ }
+ if (is_alter && coldef->is_not_null)
+ saw_notnull = true;
+
+ append_string_object(column, "%{not_null}s",
+ saw_notnull ? "NOT NULL" : "");
+
+ tmpobj = new_objtree("DEFAULT");
+ if (attrForm->atthasdef)
+ {
+ char *defstr;
+
+ defstr = RelationGetColumnDefault(relation, attrForm->attnum,
+ dpcontext, exprs);
+
+ append_string_object(tmpobj, "%{default}s", defstr);
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(column, "%{default}s", tmpobj);
+
+ /* IDENTITY COLUMN */
+ if (coldef->identity)
+ {
+ Oid attno = get_attnum(relid, coldef->colname);
+
+ seqrelid = getIdentitySequence(relid, attno, false);
+ }
+
+ tmpobj = deparse_ColumnIdentity(seqrelid, coldef->identity, is_alter);
+ append_object_object(column, "%{identity_column}s", tmpobj);
+
+ /* GENERATED COLUMN EXPRESSION */
+ tmpobj = new_objtree("GENERATED ALWAYS AS");
+ if (coldef->generated == ATTRIBUTE_GENERATED_STORED)
+ {
+ char *defstr;
+
+ defstr = RelationGetColumnDefault(relation, attrForm->attnum,
+ dpcontext, exprs);
+ append_string_object(tmpobj, "%{generation_expr}s STORED", defstr);
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+
+ append_object_object(column, "%{generated_column}s", tmpobj);
+ }
+
+ ReleaseSysCache(attrTup);
+
+ return column;
+}
+
+/*
+ * Subroutine for CREATE TABLE OF deparsing.
+ *
+ * Deparse a ColumnDef node within a typed table creation. This is simpler
+ * than the regular case, because we don't have to emit the type declaration,
+ * collation, or default. Here we only return something if the column is being
+ * declared NOT NULL.
+ *
+ * As in deparse_ColumnDef, any other constraint is processed elsewhere.
+ */
+static ObjTree *
+deparse_ColumnDef_typed(Relation relation, List *dpcontext, ColumnDef *coldef)
+{
+ ObjTree *column = NULL;
+ ObjTree *tmpobj;
+ Oid relid = RelationGetRelid(relation);
+ HeapTuple attrTup;
+ Form_pg_attribute attrForm;
+ Oid typid;
+ int32 typmod;
+ Oid typcollation;
+ bool saw_notnull;
+ ListCell *cell;
+
+ attrTup = SearchSysCacheAttName(relid, coldef->colname);
+ if (!HeapTupleIsValid(attrTup))
+ elog(ERROR, "could not find cache entry for column \"%s\" of relation %u",
+ coldef->colname, relid);
+ attrForm = (Form_pg_attribute) GETSTRUCT(attrTup);
+
+ get_atttypetypmodcoll(relid, attrForm->attnum,
+ &typid, &typmod, &typcollation);
+
+ /*
+ * Search for a NOT NULL declaration. As in deparse_ColumnDef, we rely on
+ * finding a constraint on the column rather than coldef->is_not_null.
+ * (This routine is never used for ALTER cases.)
+ */
+ saw_notnull = false;
+ foreach(cell, coldef->constraints)
+ {
+ Constraint *constr = (Constraint *) lfirst(cell);
+
+ if (constr->contype == CONSTR_NOTNULL)
+ {
+ saw_notnull = true;
+ break;
+ }
+ }
+
+ if (!saw_notnull && !attrForm->atthasdef)
+ {
+ ReleaseSysCache(attrTup);
+ return NULL;
+ }
+
+ /*
+ * Verbose syntax
+ *
+ * %{name}I WITH OPTIONS %{default}s %{not_null}s.
+ */
+ column = new_objtree_VA("%{name}I WITH OPTIONS", 2,
+ "type", ObjTypeString, "column",
+ "name", ObjTypeString, coldef->colname);
+
+ append_string_object(column, "%{not_null}s",
+ saw_notnull ? "NOT NULL" : "");
+
+ tmpobj = new_objtree("DEFAULT");
+ if (attrForm->atthasdef)
+ {
+ char *defstr;
+
+ defstr = RelationGetColumnDefault(relation, attrForm->attnum,
+ dpcontext, NULL);
+
+ append_string_object(tmpobj, "%{default}s", defstr);
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(column, "%{default}s", tmpobj);
+
+ /*
+ * Generated columns are not supported on typed tables, so we are done.
+ */
+
+ ReleaseSysCache(attrTup);
+
+ return column;
+}
+
+/*
+ * Deparse the definition of column identity.
+ */
+static ObjTree *
+deparse_ColumnIdentity(Oid seqrelid, char identity, bool alter_table)
+{
+ ObjTree *column;
+ ObjTree *identobj;
+ List *elems = NIL;
+ Relation rel;
+ HeapTuple seqtuple;
+ Form_pg_sequence seqform;
+ Form_pg_sequence_data seqdata;
+ char *identfmt;
+ char *objfmt;
+
+ column = new_objtree("");
+
+ if (!OidIsValid(seqrelid))
+ {
+ append_bool_object(column, "present", false);
+ return column;
+ }
+
+ if (alter_table)
+ {
+ identfmt = "SET GENERATED ";
+ objfmt = "%{option}s";
+ }
+ else
+ {
+ identfmt = "GENERATED ";
+ objfmt = "%{option}s AS IDENTITY";
+ }
+
+ identobj = new_objtree(identfmt);
+
+ if (identity == ATTRIBUTE_IDENTITY_ALWAYS)
+ append_string_object(identobj, objfmt, "ALWAYS");
+ else if (identity == ATTRIBUTE_IDENTITY_BY_DEFAULT)
+ append_string_object(identobj, objfmt, "BY DEFAULT");
+ else
+ append_bool_object(identobj, "present", false);
+
+ append_object_object(column, "%{identity_type}s", identobj);
+
+ rel = table_open(SequenceRelationId, RowExclusiveLock);
+ seqtuple = SearchSysCacheCopy1(SEQRELID, seqrelid);
+ if (!HeapTupleIsValid(seqtuple))
+ elog(ERROR, "cache lookup failed for sequence %u",
+ seqrelid);
+
+ seqform = (Form_pg_sequence) GETSTRUCT(seqtuple);
+ seqdata = get_sequence_values(seqrelid);
+
+ /* Definition elements */
+ elems = lappend(elems, deparse_Seq_Cache(NULL, seqform, alter_table));
+ elems = lappend(elems, deparse_Seq_Cycle(NULL, seqform, alter_table));
+ elems = lappend(elems, deparse_Seq_IncrementBy(NULL, seqform, alter_table));
+ elems = lappend(elems, deparse_Seq_Minvalue(NULL, seqform, alter_table));
+ elems = lappend(elems, deparse_Seq_Maxvalue(NULL, seqform, alter_table));
+ elems = lappend(elems, deparse_Seq_Startwith(NULL, seqform, alter_table));
+ elems = lappend(elems, deparse_Seq_Restart(NULL, seqdata));
+ /* We purposefully do not emit OWNED BY here */
+
+ if (alter_table)
+ append_array_object(column, "%{seq_definition: }s", elems);
+ else
+ append_array_object(column, "( %{seq_definition: }s )", elems);
+
+ table_close(rel, RowExclusiveLock);
+
+ return column;
+}
+
+/*
+ * ... ALTER COLUMN ... SET/RESET (...)
+ */
+static ObjTree *
+deparse_ColumnSetOptions(AlterTableCmd *subcmd)
+{
+ List *sets = NIL;
+ ListCell *cell;
+ ObjTree *colset;
+ char *fmt;
+ bool is_reset = subcmd->subtype == AT_ResetOptions;
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER COLUMN %{column}I RESET|SET (%{options:, }s)
+ */
+ if (is_reset)
+ fmt = "ALTER COLUMN %{column}I RESET ";
+ else
+ fmt = "ALTER COLUMN %{column}I SET ";
+
+ colset = new_objtree_VA(fmt, 1, "column", ObjTypeString, subcmd->name);
+
+ foreach(cell, (List *) subcmd->def)
+ {
+ DefElem *elem;
+ ObjTree *set;
+
+ elem = (DefElem *) lfirst(cell);
+ set = deparse_DefElem(elem, is_reset);
+ sets = lappend(sets, new_object_object(set));
+ }
+
+ Assert(sets);
+ append_array_object(colset, "(%{options:, }s)", sets);
+
+ return colset;
+}
+
+/*
+ * Deparse the CREATE DOMAIN
+ *
+ * Given a function OID and the parse tree that created it, return the JSON
+ * blob representing the creation command.
+ */
+static ObjTree *
+deparse_CreateDomain(Oid objectId, Node *parsetree)
+{
+ ObjTree *createDomain;
+ ObjTree *tmpobj;
+ HeapTuple typTup;
+ Form_pg_type typForm;
+ List *constraints;
+
+ typTup = SearchSysCache1(TYPEOID, objectId);
+ if (!HeapTupleIsValid(typTup))
+ elog(ERROR, "cache lookup failed for domain with OID %u", objectId);
+ typForm = (Form_pg_type) GETSTRUCT(typTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE DOMAIN %{identity}D AS %{type}T %{not_null}s %{constraints}s
+ * %{collation}s
+ */
+ createDomain = new_objtree("CREATE");
+
+ append_object_object(createDomain,
+ "DOMAIN %{identity}D AS",
+ new_objtree_for_qualname_id(TypeRelationId,
+ objectId));
+ append_object_object(createDomain,
+ "%{type}T",
+ new_objtree_for_type(typForm->typbasetype, typForm->typtypmod));
+
+ if (typForm->typnotnull)
+ append_string_object(createDomain, "%{not_null}s", "NOT NULL");
+ else
+ append_string_object(createDomain, "%{not_null}s", "");
+
+ constraints = obtainConstraints(NIL, InvalidOid, objectId);
+ if (constraints == NIL)
+ {
+ tmpobj = new_objtree("");
+ append_bool_object(tmpobj, "present", false);
+ }
+ else
+ tmpobj = new_objtree_VA("%{elements:, }s", 1,
+ "elements", ObjTypeArray, constraints);
+ append_object_object(createDomain, "%{constraints}s", tmpobj);
+
+ tmpobj = new_objtree("COLLATE");
+ if (OidIsValid(typForm->typcollation))
+ {
+ ObjTree *collname;
+
+ collname = new_objtree_for_qualname_id(CollationRelationId,
+ typForm->typcollation);
+ append_object_object(tmpobj, "%{name}D", collname);
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(createDomain, "%{collation}s", tmpobj);
+
+ ReleaseSysCache(typTup);
+
+ return createDomain;
+}
+
+/*
+ * Deparse a CreateFunctionStmt (CREATE FUNCTION)
+ *
+ * Given a function OID and the parse tree that created it, return the JSON
+ * blob representing the creation command.
+ */
+static ObjTree *
+deparse_CreateFunction(Oid objectId, Node *parsetree)
+{
+ CreateFunctionStmt *node = (CreateFunctionStmt *) parsetree;
+ ObjTree *createFunc;
+ ObjTree *tmpobj;
+ Datum tmpdatum;
+ char *source;
+ char *probin;
+ List *params;
+ List *defaults;
+ List *sets = NIL;
+ List *types = NIL;
+ ListCell *cell;
+ ListCell *curdef;
+ ListCell *table_params = NULL;
+ HeapTuple procTup;
+ Form_pg_proc procForm;
+ HeapTuple langTup;
+ Oid *typarray;
+ Oid *trftypes;
+ Form_pg_language langForm;
+ int i;
+ int typnum;
+ int ntypes;
+ bool isnull;
+ bool isfunction;
+
+ /* Get the pg_proc tuple */
+ procTup = SearchSysCache1(PROCOID, objectId);
+ if (!HeapTupleIsValid(procTup))
+ elog(ERROR, "cache lookup failure for function with OID %u",
+ objectId);
+ procForm = (Form_pg_proc) GETSTRUCT(procTup);
+
+ /* Get the corresponding pg_language tuple */
+ langTup = SearchSysCache1(LANGOID, procForm->prolang);
+ if (!HeapTupleIsValid(langTup))
+ elog(ERROR, "cache lookup failure for language with OID %u",
+ procForm->prolang);
+ langForm = (Form_pg_language) GETSTRUCT(langTup);
+
+ /*
+ * Determine useful values for prosrc and probin. We cope with probin
+ * being either NULL or "-", but prosrc must have a valid value.
+ */
+ tmpdatum = SysCacheGetAttr(PROCOID, procTup,
+ Anum_pg_proc_prosrc, &isnull);
+ if (isnull)
+ elog(ERROR, "null prosrc in function with OID %u", objectId);
+ source = TextDatumGetCString(tmpdatum);
+
+ /* Determine a useful value for probin */
+ tmpdatum = SysCacheGetAttr(PROCOID, procTup,
+ Anum_pg_proc_probin, &isnull);
+ if (isnull)
+ probin = NULL;
+ else
+ {
+ probin = TextDatumGetCString(tmpdatum);
+ if (probin[0] == '\0' || strcmp(probin, "-") == 0)
+ probin = NULL;
+ }
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{or_replace}s FUNCTION %{signature}s RETURNS %{return_type}s
+ * LANGUAGE %{transform_type}s %{language}I %{window}s %{volatility}s
+ * %{parallel_safety}s %{leakproof}s %{strict}s %{security_definer}s
+ * %{cost}s %{rows}s %{support}s %{set_options: }s AS %{objfile}L,
+ * %{symbol}L
+ */
+ createFunc = new_objtree("CREATE");
+
+ append_string_object(createFunc, "%{or_replace}s",
+ node->replace ? "OR REPLACE" : "");
+
+ /*
+ * To construct the arguments array, extract the type OIDs from the
+ * function's pg_proc entry. If pronargs equals the parameter list
+ * length, there are no OUT parameters and thus we can extract the type
+ * OID from proargtypes; otherwise we need to decode proallargtypes, which
+ * is a bit more involved.
+ */
+ typarray = palloc(list_length(node->parameters) * sizeof(Oid));
+ if (list_length(node->parameters) > procForm->pronargs)
+ {
+ Datum alltypes;
+ Datum *values;
+ bool *nulls;
+ int nelems;
+
+ alltypes = SysCacheGetAttr(PROCOID, procTup,
+ Anum_pg_proc_proallargtypes, &isnull);
+ if (isnull)
+ elog(ERROR, "NULL proallargtypes, but more parameters than args");
+ deconstruct_array(DatumGetArrayTypeP(alltypes),
+ OIDOID, 4, 't', 'i',
+ &values, &nulls, &nelems);
+ if (nelems != list_length(node->parameters))
+ elog(ERROR, "mismatched proallargatypes");
+ for (i = 0; i < list_length(node->parameters); i++)
+ typarray[i] = values[i];
+ }
+ else
+ {
+ for (i = 0; i < list_length(node->parameters); i++)
+ typarray[i] = procForm->proargtypes.values[i];
+ }
+
+ /*
+ * If there are any default expressions, we read the deparsed expression
+ * as a list so that we can attach them to each argument.
+ */
+ tmpdatum = SysCacheGetAttr(PROCOID, procTup,
+ Anum_pg_proc_proargdefaults, &isnull);
+ if (!isnull)
+ {
+ defaults = FunctionGetDefaults(DatumGetTextP(tmpdatum));
+ curdef = list_head(defaults);
+ }
+ else
+ {
+ defaults = NIL;
+ curdef = NULL;
+ }
+
+ /*
+ * Now iterate over each parameter in the parse tree to create the
+ * parameters array.
+ */
+ params = NIL;
+ typnum = 0;
+ foreach(cell, node->parameters)
+ {
+ FunctionParameter *param = (FunctionParameter *) lfirst(cell);
+ ObjTree *paramobj;
+ ObjTree *defaultval;
+ ObjTree *name;
+
+ /*
+ * A PARAM_TABLE parameter indicates the end of input arguments; the
+ * following parameters are part of the return type. We ignore them
+ * here, but keep track of the current position in the list so that we
+ * can easily produce the return type below.
+ */
+ if (param->mode == FUNC_PARAM_TABLE)
+ {
+ table_params = cell;
+ break;
+ }
+
+ /*
+ * Verbose syntax for paramater: %{mode}s %{name}s %{type}T
+ * %{default}s
+ *
+ * Note that %{name}s is a string here, not an identifier; the reason
+ * for this is that an absent parameter name must produce an empty
+ * string, not "", which is what would happen if we were to use
+ * %{name}I here. So we add another level of indirection to allow us
+ * to inject a "present" parameter.
+ */
+ paramobj = new_objtree("");
+ append_string_object(paramobj, "%{mode}s",
+ param->mode == FUNC_PARAM_IN ? "IN" :
+ param->mode == FUNC_PARAM_OUT ? "OUT" :
+ param->mode == FUNC_PARAM_INOUT ? "INOUT" :
+ param->mode == FUNC_PARAM_VARIADIC ? "VARIADIC" :
+ "IN");
+
+ /* Optional wholesale suppression of "name" occurs here */
+
+ name = new_objtree("");
+ append_string_object(name, "%{name}I",
+ param->name ? param->name : "NULL");
+
+ append_bool_object(name, "present",
+ param->name ? true : false);
+
+ append_object_object(paramobj, "%{name}s", name);
+
+ defaultval = new_objtree("DEFAULT");
+ if (PointerIsValid(param->defexpr))
+ {
+ char *expr;
+
+ if (curdef == NULL)
+ elog(ERROR, "proargdefaults list too short");
+ expr = lfirst(curdef);
+
+ append_string_object(defaultval, "%{value}s", expr);
+ curdef = lnext(defaults, curdef);
+ }
+ else
+ append_bool_object(defaultval, "present", false);
+
+ append_object_object(paramobj, "%{type}T",
+ new_objtree_for_type(typarray[typnum++], -1));
+
+ append_object_object(paramobj, "%{default}s", defaultval);
+
+ params = lappend(params, new_object_object(paramobj));
+ }
+
+ tmpobj = new_objtree_VA("%{identity}D", 1,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ objectId));
+
+ append_format_string(tmpobj, "(");
+ append_array_object(tmpobj, "%{arguments:, }s", params);
+ append_format_string(tmpobj, ")");
+
+ isfunction = (procForm->prokind != PROKIND_PROCEDURE);
+
+ if (isfunction)
+ append_object_object(createFunc, "FUNCTION %{signature}s", tmpobj);
+ else
+ append_object_object(createFunc, "PROCEDURE %{signature}s", tmpobj);
+
+ /*
+ * A return type can adopt one of two forms: either a [SETOF] some_type,
+ * or a TABLE(list-of-types). We can tell the second form because we saw
+ * a table param above while scanning the argument list.
+ */
+ if (table_params == NULL)
+ {
+ tmpobj = new_objtree_VA("", 1,
+ "return_form", ObjTypeString, "plain");
+ append_string_object(tmpobj, "%{setof}s",
+ procForm->proretset ? "SETOF" : "");
+ append_object_object(tmpobj, "%{rettype}T",
+ new_objtree_for_type(procForm->prorettype, -1));
+ }
+ else
+ {
+ List *rettypes = NIL;
+ ObjTree *paramobj;
+
+ tmpobj = new_objtree_VA("TABLE", 1,
+ "return_form", ObjTypeString, "table");
+ for (; table_params != NULL; table_params = lnext(node->parameters, table_params))
+ {
+ FunctionParameter *param = lfirst(table_params);
+
+ paramobj = new_objtree("");
+ append_string_object(paramobj, "%{name}I", param->name);
+ append_object_object(paramobj, "%{type}T",
+ new_objtree_for_type(typarray[typnum++], -1));
+ rettypes = lappend(rettypes, new_object_object(paramobj));
+ }
+
+ append_array_object(tmpobj, "(%{rettypes:, }s)", rettypes);
+ }
+
+ if (isfunction)
+ append_object_object(createFunc, "RETURNS %{return_type}s", tmpobj);
+
+ /* TRANSFORM FOR TYPE */
+ tmpobj = new_objtree("TRANSFORM");
+
+ ntypes = get_func_trftypes(procTup, &trftypes);
+ for (i = 0; i < ntypes; i++)
+ {
+ tmpobj = new_objtree_VA("FOR TYPE %{type}T", 1,
+ "type", ObjTypeObject,
+ new_objtree_for_type(trftypes[i], -1));
+ types = lappend(types, tmpobj);
+ }
+
+ if (types)
+ append_array_object(tmpobj, "%{for_type:, }s", types);
+ else
+ append_bool_object(tmpobj, "present", false);
+
+ append_object_object(createFunc, "%{transform_type}s", tmpobj);
+
+ append_string_object(createFunc, "LANGUAGE %{language}I",
+ NameStr(langForm->lanname));
+
+ if (isfunction)
+ {
+ append_string_object(createFunc, "%{window}s",
+ procForm->prokind == PROKIND_WINDOW ? "WINDOW" : "");
+ append_string_object(createFunc, "%{volatility}s",
+ procForm->provolatile == PROVOLATILE_VOLATILE ?
+ "VOLATILE" :
+ procForm->provolatile == PROVOLATILE_STABLE ?
+ "STABLE" : "IMMUTABLE");
+
+ append_string_object(createFunc, "%{parallel_safety}s",
+ procForm->proparallel == PROPARALLEL_SAFE ?
+ "PARALLEL SAFE" :
+ procForm->proparallel == PROPARALLEL_RESTRICTED ?
+ "PARALLEL RESTRICTED" : "PARALLEL UNSAFE");
+
+ append_string_object(createFunc, "%{leakproof}s",
+ procForm->proleakproof ? "LEAKPROOF" : "");
+ append_string_object(createFunc, "%{strict}s",
+ procForm->proisstrict ?
+ "RETURNS NULL ON NULL INPUT" :
+ "CALLED ON NULL INPUT");
+
+ append_string_object(createFunc, "%{security_definer}s",
+ procForm->prosecdef ?
+ "SECURITY DEFINER" : "SECURITY INVOKER");
+
+ append_object_object(createFunc, "%{cost}s",
+ new_objtree_VA("COST %{cost}n", 1,
+ "cost", ObjTypeFloat,
+ procForm->procost));
+
+ tmpobj = new_objtree("ROWS");
+ if (procForm->prorows == 0)
+ append_bool_object(tmpobj, "present", false);
+ else
+ append_float_object(tmpobj, "%{rows}n", procForm->prorows);
+ append_object_object(createFunc, "%{rows}s", tmpobj);
+
+ tmpobj = new_objtree("SUPPORT %{name}s");
+ if (procForm->prosupport)
+ {
+ Oid argtypes[1];
+
+ /*
+ * We should qualify the support function's name if it wouldn't be
+ * resolved by lookup in the current search path.
+ */
+ argtypes[0] = INTERNALOID;
+ append_string_object(tmpobj, "%{name}s",
+ generate_function_name(procForm->prosupport, 1,
+ NIL, argtypes,
+ false, NULL,
+ EXPR_KIND_NONE));
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+
+ append_object_object(createFunc, "%{support}s", tmpobj);
+ }
+
+ foreach(cell, node->options)
+ {
+ DefElem *defel = (DefElem *) lfirst(cell);
+
+ if (strcmp(defel->defname, "set") == 0)
+ {
+ VariableSetStmt *sstmt = (VariableSetStmt *) defel->arg;
+ char *value = ExtractSetVariableArgs(sstmt);
+
+ tmpobj = deparse_FunctionSet(sstmt->kind, sstmt->name, value);
+ sets = lappend(sets, new_object_object(tmpobj));
+ }
+ }
+ append_array_object(createFunc, "%{set_options: }s", sets);
+
+ /* Add the function definition */
+ (void) SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_prosqlbody, &isnull);
+ if (procForm->prolang == SQLlanguageId && !isnull)
+ {
+ StringInfoData buf;
+
+ initStringInfo(&buf);
+ print_function_sqlbody(&buf, procTup);
+
+ append_string_object(createFunc, "%{definition}s", buf.data);
+ }
+ else if (probin == NULL)
+ {
+ append_string_object(createFunc, "AS %{definition}L",
+ source);
+ }
+ else
+ {
+ append_string_object(createFunc, "AS %{objfile}L", probin);
+ append_string_object(createFunc, ", %{symbol}L", source);
+ }
+
+ ReleaseSysCache(langTup);
+ ReleaseSysCache(procTup);
+
+ return createFunc;
+}
+
+/*
+ * Deparse a CREATE OPERATOR CLASS command.
+ */
+static ObjTree *
+deparse_CreateOpClassStmt(CollectedCommand *cmd)
+{
+ Oid opcoid = cmd->d.createopc.address.objectId;
+ HeapTuple opcTup;
+ HeapTuple opfTup;
+ Form_pg_opfamily opfForm;
+ Form_pg_opclass opcForm;
+ ObjTree *stmt;
+ ObjTree *tmpobj;
+ List *list;
+ ListCell *cell;
+
+ /* Don't deparse SQL commands generated while creating extension */
+ if (cmd->in_extension)
+ return NULL;
+
+ opcTup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opcoid));
+ if (!HeapTupleIsValid(opcTup))
+ elog(ERROR, "cache lookup failed for opclass with OID %u", opcoid);
+ opcForm = (Form_pg_opclass) GETSTRUCT(opcTup);
+
+ opfTup = SearchSysCache1(OPFAMILYOID, opcForm->opcfamily);
+ if (!HeapTupleIsValid(opfTup))
+ elog(ERROR, "cache lookup failed for operator family with OID %u", opcForm->opcfamily);
+ opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE OPERATOR CLASS %{identity}D %{default}s FOR TYPE %{type}T USING
+ * %{amname}I %{opfamily}s AS %{items:, }s
+ */
+
+ stmt = new_objtree_VA("CREATE OPERATOR CLASS %{identity}D", 1,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(opcForm->opcnamespace,
+ NameStr(opcForm->opcname)));
+
+ /* Add the DEFAULT clause */
+ append_string_object(stmt, "%{default}s",
+ opcForm->opcdefault ? "DEFAULT" : "");
+
+ /* Add the FOR TYPE clause */
+ append_object_object(stmt, "FOR TYPE %{type}T",
+ new_objtree_for_type(opcForm->opcintype, -1));
+
+ /* Add the USING clause */
+ append_string_object(stmt, "USING %{amname}I", get_am_name(opcForm->opcmethod));
+
+ /*
+ * Add the FAMILY clause, but if it has the same name and namespace as the
+ * opclass, then have it expand to empty because it would cause a failure
+ * if the opfamily was created internally.
+ */
+ tmpobj = new_objtree_VA("FAMILY %{opfamily}D", 1,
+ "opfamily", ObjTypeObject,
+ new_objtree_for_qualname(opfForm->opfnamespace,
+ NameStr(opfForm->opfname)));
+
+ if (strcmp(NameStr(opfForm->opfname), NameStr(opcForm->opcname)) == 0 &&
+ opfForm->opfnamespace == opcForm->opcnamespace)
+ append_bool_object(tmpobj, "present", false);
+
+ append_object_object(stmt, "%{opfamily}s", tmpobj);
+
+ /*
+ * Add the initial item list. Note we always add the STORAGE clause, even
+ * when it is implicit in the original command.
+ */
+ tmpobj = new_objtree("STORAGE");
+ append_object_object(tmpobj, "%{type}T",
+ new_objtree_for_type(opcForm->opckeytype != InvalidOid ?
+ opcForm->opckeytype : opcForm->opcintype,
+ -1));
+ list = list_make1(new_object_object(tmpobj));
+
+ /* Add the declared operators */
+ foreach(cell, cmd->d.createopc.operators)
+ {
+ OpFamilyMember *oper = lfirst(cell);
+
+ tmpobj = new_objtree_VA("OPERATOR %{num}n %{operator}O(%{ltype}T, %{rtype}T)",
+ 4,
+ "num", ObjTypeInteger, oper->number,
+ "operator", ObjTypeObject,
+ new_objtree_for_qualname_id(OperatorRelationId,
+ oper->object),
+ "ltype", ObjTypeObject,
+ new_objtree_for_type(oper->lefttype, -1),
+ "rtype", ObjTypeObject,
+ new_objtree_for_type(oper->righttype, -1));
+
+ /* Add the FOR SEARCH / FOR ORDER BY clause */
+ if (oper->sortfamily == InvalidOid)
+ append_string_object(tmpobj, "%{purpose}s", "FOR SEARCH");
+ else
+ {
+ ObjTree *tmpobj2;
+
+ tmpobj2 = new_objtree_VA("FOR ORDER BY %{opfamily}D", 0);
+ append_object_object(tmpobj2, "opfamily",
+ new_objtree_for_qualname_id(OperatorFamilyRelationId,
+ oper->sortfamily));
+ append_object_object(tmpobj, "%{purpose}s", tmpobj2);
+ }
+
+ list = lappend(list, new_object_object(tmpobj));
+ }
+
+ /* Add the declared support functions */
+ foreach(cell, cmd->d.createopc.procedures)
+ {
+ OpFamilyMember *proc = lfirst(cell);
+ HeapTuple procTup;
+ Form_pg_proc procForm;
+ Oid *proargtypes;
+ List *arglist = NIL;
+ int i;
+
+ procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(proc->object));
+ if (!HeapTupleIsValid(procTup))
+ elog(ERROR, "cache lookup failed for procedure %u", proc->object);
+ procForm = (Form_pg_proc) GETSTRUCT(procTup);
+
+ tmpobj = new_objtree_VA("FUNCTION %{num}n (%{ltype}T, %{rtype}T) %{function}D",
+ 4,
+ "num", ObjTypeInteger, proc->number,
+ "ltype", ObjTypeObject,
+ new_objtree_for_type(proc->lefttype, -1),
+ "rtype", ObjTypeObject,
+ new_objtree_for_type(proc->righttype, -1),
+ "function", ObjTypeObject,
+ new_objtree_for_qualname(procForm->pronamespace,
+ NameStr(procForm->proname)));
+
+ proargtypes = procForm->proargtypes.values;
+ for (i = 0; i < procForm->pronargs; i++)
+ {
+ ObjTree *arg;
+
+ arg = new_objtree_for_type(proargtypes[i], -1);
+ arglist = lappend(arglist, new_object_object(arg));
+ }
+
+ append_format_string(tmpobj, "(");
+ append_array_object(tmpobj, "%{argtypes:, }T", arglist);
+ append_format_string(tmpobj, ")");
+
+ ReleaseSysCache(procTup);
+
+ list = lappend(list, new_object_object(tmpobj));
+ }
+
+ append_array_object(stmt, "AS %{items:, }s", list);
+
+ ReleaseSysCache(opfTup);
+ ReleaseSysCache(opcTup);
+
+ return stmt;
+}
+
+/*
+ * Deparse a CreateTrigStmt (CREATE OPERATOR FAMILY)
+ *
+ * Given a trigger OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateOpFamily(Oid objectId, Node *parsetree)
+{
+ HeapTuple opfTup;
+ HeapTuple amTup;
+ Form_pg_opfamily opfForm;
+ Form_pg_am amForm;
+ ObjTree *copfStmt;
+
+ opfTup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(opfTup))
+ elog(ERROR, "cache lookup failed for operator family with OID %u", objectId);
+ opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
+
+ amTup = SearchSysCache1(AMOID, ObjectIdGetDatum(opfForm->opfmethod));
+ if (!HeapTupleIsValid(amTup))
+ elog(ERROR, "cache lookup failed for access method %u",
+ opfForm->opfmethod);
+ amForm = (Form_pg_am) GETSTRUCT(amTup);
+
+ copfStmt = new_objtree_VA("CREATE OPERATOR FAMILY %{identity}D USING %{amname}I",
+ 2,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(opfForm->opfnamespace,
+ NameStr(opfForm->opfname)),
+ "amname", ObjTypeString, NameStr(amForm->amname));
+
+ ReleaseSysCache(amTup);
+ ReleaseSysCache(opfTup);
+
+ return copfStmt;
+}
+
+static ObjTree *
+deparse_CreatePolicyStmt(Oid objectId, Node *parsetree)
+{
+ CreatePolicyStmt *node = (CreatePolicyStmt *) parsetree;
+ ObjTree *policy;
+
+ policy = new_objtree_VA("CREATE POLICY %{identity}I", 1,
+ "identity", ObjTypeString, node->policy_name);
+
+ /* Add the rest of the stuff */
+ add_policy_clauses(policy, objectId, node->roles, !!node->qual,
+ !!node->with_check);
+
+ return policy;
+}
+
+static ObjTree *
+deparse_AlterPolicyStmt(Oid objectId, Node *parsetree)
+{
+ AlterPolicyStmt *node = (AlterPolicyStmt *) parsetree;
+ ObjTree *policy;
+
+ policy = new_objtree_VA("ALTER POLICY %{identity}I", 1,
+ "identity", ObjTypeString, node->policy_name);
+
+ /* Add the rest of the stuff */
+ add_policy_clauses(policy, objectId, node->roles, !!node->qual,
+ !!node->with_check);
+
+ return policy;
+}
+
+/*
+ * Deparse a CreateSchemaStmt.
+ *
+ * Given a schema OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ *
+ */
+static ObjTree *
+deparse_CreateSchemaStmt(Oid objectId, Node *parsetree)
+{
+ CreateSchemaStmt *node = (CreateSchemaStmt *) parsetree;
+ ObjTree *createSchema;
+ ObjTree *auth;
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE SCHEMA %{if_not_exists}s %{name}I %{authorization}s
+ */
+ createSchema = new_objtree("CREATE SCHEMA");
+
+ append_string_object(createSchema, "%{if_not_exists}s",
+ node->if_not_exists ? "IF NOT EXISTS" : "");
+
+ append_string_object(createSchema, "%{name}I", node->schemaname);
+
+ auth = new_objtree("AUTHORIZATION");
+ if (node->authrole)
+ append_string_object(auth, "%{authorization_role}I",
+ get_rolespec_name(node->authrole));
+ else
+ {
+ append_null_object(auth, "%{authorization_role}I ");
+ append_bool_object(auth, "present", false);
+ }
+ append_object_object(createSchema, "%{authorization}s", auth);
+
+ return createSchema;
+}
+
+/*
+ * Return the default value of a domain.
+ */
+static char *
+DomainGetDefault(HeapTuple domTup)
+{
+ Datum def;
+ Node *defval;
+ char *defstr;
+ bool isnull;
+
+ def = SysCacheGetAttr(TYPEOID, domTup, Anum_pg_type_typdefaultbin,
+ &isnull);
+ if (isnull)
+ elog(ERROR, "domain \"%s\" does not have a default value",
+ NameStr(((Form_pg_type) GETSTRUCT(domTup))->typname));
+ defval = stringToNode(TextDatumGetCString(def));
+ defstr = deparse_expression(defval, NULL /* dpcontext? */ ,
+ false, false);
+
+ return defstr;
+}
+
+/*
+ * Deparse a AlterDomainStmt.
+ */
+static ObjTree *
+deparse_AlterDomainStmt(Oid objectId, Node *parsetree,
+ ObjectAddress constraintAddr)
+{
+ AlterDomainStmt *node = (AlterDomainStmt *) parsetree;
+ HeapTuple domTup;
+ Form_pg_type domForm;
+ ObjTree *alterDom;
+ char *fmt;
+ const char *type;
+
+ /* ALTER DOMAIN DROP CONSTRAINT is handled by the DROP support code */
+ if (node->subtype == 'X')
+ return NULL;
+
+ domTup = SearchSysCache1(TYPEOID, objectId);
+ if (!HeapTupleIsValid(domTup))
+ elog(ERROR, "cache lookup failed for domain with OID %u",
+ objectId);
+ domForm = (Form_pg_type) GETSTRUCT(domTup);
+
+ switch (node->subtype)
+ {
+ case 'T':
+ /* SET DEFAULT / DROP DEFAULT */
+ if (node->def == NULL)
+ {
+ fmt = "ALTER DOMAIN";
+ type = "drop default";
+ alterDom = new_objtree_VA(fmt, 1, "type", ObjTypeString, type);
+ append_object_object(alterDom, "%{identity}D DROP DEFAULT",
+ new_objtree_for_qualname(domForm->typnamespace,
+ NameStr(domForm->typname)));
+ }
+ else
+ {
+ fmt = "ALTER DOMAIN";
+ type = "set default";
+ alterDom = new_objtree_VA(fmt, 1, "type", ObjTypeString, type);
+ append_object_object(alterDom, "%{identity}D SET DEFAULT",
+ new_objtree_for_qualname(domForm->typnamespace,
+ NameStr(domForm->typname)));
+ append_string_object(alterDom, "%{default}s", DomainGetDefault(domTup));
+ }
+
+ break;
+ case 'N':
+ /* DROP NOT NULL */
+ fmt = "ALTER DOMAIN";
+ type = "drop not null";
+ alterDom = new_objtree_VA(fmt, 1, "type", ObjTypeString, type);
+ append_object_object(alterDom, "%{identity}D DROP NOT NULL",
+ new_objtree_for_qualname(domForm->typnamespace,
+ NameStr(domForm->typname)));
+ break;
+ case 'O':
+ /* SET NOT NULL */
+ fmt = "ALTER DOMAIN";
+ type = "set not null";
+ alterDom = new_objtree_VA(fmt, 1, "type", ObjTypeString, type);
+ append_object_object(alterDom, "%{identity}D SET NOT NULL",
+ new_objtree_for_qualname(domForm->typnamespace,
+ NameStr(domForm->typname)));
+ break;
+ case 'C':
+
+ /*
+ * ADD CONSTRAINT. Only CHECK constraints are supported by
+ * domains
+ */
+ fmt = "ALTER DOMAIN";
+ type = "add constraint";
+ alterDom = new_objtree_VA(fmt, 1, "type", ObjTypeString, type);
+ append_object_object(alterDom, "%{identity}D",
+ new_objtree_for_qualname(domForm->typnamespace,
+ NameStr(domForm->typname)));
+ /* A new constraint has a name and definition */
+ append_string_object(alterDom, "ADD CONSTRAINT %{constraint_name}s",
+ get_constraint_name(constraintAddr.objectId));
+ append_string_object(alterDom, "%{definition}s",
+ pg_get_constraintdef_command_simple(constraintAddr.objectId));
+ break;
+ case 'V':
+ /* VALIDATE CONSTRAINT */
+ fmt = "ALTER DOMAIN";
+ type = "validate constraint";
+
+ /*
+ * Process subtype=specific options. Validation a constraint
+ * requires its name.
+ */
+ alterDom = new_objtree_VA(fmt, 1, "type", ObjTypeString, type);
+ append_object_object(alterDom, "%{identity}D",
+ new_objtree_for_qualname(domForm->typnamespace,
+ NameStr(domForm->typname)));
+ append_string_object(alterDom, "VALIDATE CONSTRAINT %{constraint_name}s", node->name);
+
+ break;
+ default:
+ elog(ERROR, "invalid subtype %c", node->subtype);
+ }
+
+ /* Done */
+ ReleaseSysCache(domTup);
+
+ return alterDom;
+}
+
+/*
+ * Deparse a CreateStatsStmt.
+ *
+ * Given a statistics OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateStatisticsStmt(Oid objectId, Node *parsetree)
+{
+ CreateStatsStmt *node = (CreateStatsStmt *) parsetree;
+ Form_pg_statistic_ext statform;
+ ObjTree *createStat;
+ HeapTuple tup;
+ Datum datum;
+ bool isnull;
+ List *statexprs = NIL;
+
+ createStat = new_objtree("CREATE STATISTICS");
+ append_string_object(createStat, "%{if_not_exists}s",
+ node->if_not_exists ? "IF NOT EXISTS" : "");
+
+ tup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tup))
+ elog(ERROR, "cache lookup failed for statistic %u", objectId);
+
+ statform = (Form_pg_statistic_ext) GETSTRUCT(tup);
+
+ append_object_object(createStat, "%{identity}D",
+ new_objtree_for_qualname(statform->stxnamespace,
+ NameStr(statform->stxname)));
+
+ datum = SysCacheGetAttr(STATEXTOID, tup, Anum_pg_statistic_ext_stxexprs,
+ &isnull);
+ if (!isnull)
+ {
+ ListCell *lc;
+ Relation statsrel;
+ List *context;
+ List *exprs = NIL;
+ char *exprsString;
+
+ statsrel = relation_open(statform->stxrelid, AccessShareLock);
+ context = deparse_context_for(RelationGetRelationName(statsrel),
+ RelationGetRelid(statsrel));
+
+ exprsString = TextDatumGetCString(datum);
+ exprs = (List *) stringToNode(exprsString);
+
+ foreach(lc, exprs)
+ {
+ Node *expr = (Node *) lfirst(lc);
+ char *statexpr;
+
+ statexpr = deparse_expression(expr, context, false, false);
+ statexprs = lappend(statexprs, new_string_object(statexpr));
+ }
+
+ append_array_object(createStat, "ON %{expr:, }s", statexprs);
+ pfree(exprsString);
+ relation_close(statsrel, AccessShareLock);
+ }
+
+ datum = SysCacheGetAttr(STATEXTOID, tup, Anum_pg_statistic_ext_stxkeys,
+ &isnull);
+ if (!isnull)
+ {
+ int keyno;
+ char *attname;
+ List *statcols = NIL;
+ int2vector *indoption;
+
+ indoption = (int2vector *) DatumGetPointer(datum);
+
+ for (keyno = 0; keyno < indoption->dim1; keyno++)
+ {
+ attname = get_attname(statform->stxrelid, indoption->values[keyno],
+ false);
+ statcols = lappend(statcols, new_string_object(attname));
+ }
+
+ if (indoption->dim1)
+ {
+ /* Append a ',' if statexprs is present else append 'ON' */
+ append_string_object(createStat, "%{comma}s", statexprs ? "," : "ON");
+ append_array_object(createStat, "%{cols:, }s", statcols);
+ }
+ }
+
+ append_format_string(createStat, "FROM");
+
+ append_object_object(createStat, "%{stat_table_identity}D",
+ new_objtree_for_qualname(get_rel_namespace(statform->stxrelid),
+ get_rel_name(statform->stxrelid)));
+
+ ReleaseSysCache(tup);
+
+ return createStat;
+}
+
+/*
+ * Deparse an CreateForeignServerStmt (CREATE SERVER)
+ *
+ * Given a server OID and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_CreateForeignServerStmt(Oid objectId, Node *parsetree)
+{
+ CreateForeignServerStmt *node = (CreateForeignServerStmt *) parsetree;
+ ObjTree *createServer;
+ ObjTree *tmp;
+
+ createServer = new_objtree_VA("CREATE SERVER %{identity}I", 1,
+ "identity", ObjTypeString, node->servername);
+
+ /* Add a TYPE clause, if any */
+ tmp = new_objtree_VA("TYPE", 0);
+ if (node->servertype)
+ append_string_object(tmp, "%{type}L", node->servertype);
+ else
+ append_bool_object(tmp, "present", false);
+ append_object_object(createServer, "%{type}s", tmp);
+
+ /* Add a VERSION clause, if any */
+ tmp = new_objtree_VA("VERSION", 0);
+ if (node->version)
+ append_string_object(tmp, "%{version}L", node->version);
+ else
+ append_bool_object(tmp, "present", false);
+ append_object_object(createServer, "%{version}s", tmp);
+
+ append_string_object(createServer, "FOREIGN DATA WRAPPER %{fdw}I", node->fdwname);
+ /* add an OPTIONS clause, if any */
+ append_object_object(createServer, "%{generic_options}s",
+ deparse_FdwOptions(node->options, NULL));
+
+ return createServer;
+}
+
+/*
+ * Deparse an AlterForeignServerStmt (ALTER SERVER)
+ *
+ * Given a server OID and the parse tree that created it, return the JSON
+ * blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterForeignServerStmt(Oid objectId, Node *parsetree)
+{
+ AlterForeignServerStmt *node = (AlterForeignServerStmt *) parsetree;
+ ObjTree *alterServer;
+ ObjTree *tmp;
+
+ alterServer = new_objtree_VA("ALTER SERVER %{identity}I", 1,
+ "identity", ObjTypeString, node->servername);
+
+ /* Add a VERSION clause, if any */
+ tmp = new_objtree_VA("VERSION", 0);
+ if (node->has_version && node->version)
+ append_string_object(tmp, "%{version}L", node->version);
+ else if (node->has_version)
+ append_string_object(tmp, "version", "NULL");
+ else
+ append_bool_object(tmp, "present", false);
+ append_object_object(alterServer, "%{version}s", tmp);
+
+ /* Add a VERSION clause, if any */
+ tmp = new_objtree_VA("VERSION", 0);
+ if (node->has_version && node->version)
+ append_string_object(tmp, "%{version}L", node->version);
+ else if (node->has_version)
+ append_string_object(tmp, "version", "NULL");
+ else
+ append_bool_object(tmp, "present", false);
+ append_object_object(alterServer, "%{version}s", tmp);
+
+ /* add an OPTIONS clause, if any */
+ append_object_object(alterServer, "%{generic_options}s",
+ deparse_FdwOptions(node->options, NULL));
+
+ return alterServer;
+}
+
+/*
+ * Deparse a CreateSeqStmt.
+ *
+ * Given a sequence OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateSeqStmt(Oid objectId, Node *parsetree)
+{
+ ObjTree *createSeq;
+ ObjTree *tmpobj;
+ Relation relation;
+ Form_pg_sequence_data seqdata;
+ List *elems = NIL;
+ Form_pg_sequence seqform;
+ Relation rel;
+ HeapTuple seqtuple;
+ CreateSeqStmt *createSeqStmt = (CreateSeqStmt *) parsetree;
+
+ /*
+ * Sequence for IDENTITY COLUMN output separately(via CREATE TABLE or
+ * ALTER TABLE); return empty here.
+ */
+ if (createSeqStmt->for_identity)
+ return NULL;
+
+ seqdata = get_sequence_values(objectId);
+
+ relation = relation_open(objectId, AccessShareLock);
+ rel = table_open(SequenceRelationId, RowExclusiveLock);
+ seqtuple = SearchSysCacheCopy1(SEQRELID,
+ ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(seqtuple))
+ elog(ERROR, "cache lookup failed for sequence %u",
+ objectId);
+
+ seqform = (Form_pg_sequence) GETSTRUCT(seqtuple);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{persistence}s SEQUENCE %{identity}D
+ */
+ createSeq = new_objtree("CREATE");
+
+ append_string_object(createSeq, "%{persistence}s",
+ get_persistence_str(relation->rd_rel->relpersistence));
+
+ tmpobj = new_objtree_for_qualname(relation->rd_rel->relnamespace,
+ RelationGetRelationName(relation));
+ append_object_object(createSeq, "SEQUENCE %{identity}D", tmpobj);
+
+ /* Definition elements */
+ elems = lappend(elems, deparse_Seq_Cache(createSeq, seqform, false));
+ elems = lappend(elems, deparse_Seq_Cycle(createSeq, seqform, false));
+ elems = lappend(elems, deparse_Seq_IncrementBy(createSeq, seqform, false));
+ elems = lappend(elems, deparse_Seq_Minvalue(createSeq, seqform, false));
+ elems = lappend(elems, deparse_Seq_Maxvalue(createSeq, seqform, false));
+ elems = lappend(elems, deparse_Seq_Startwith(createSeq, seqform, false));
+ elems = lappend(elems, deparse_Seq_Restart(createSeq, seqdata));
+
+ /* We purposefully do not emit OWNED BY here */
+ append_array_object(createSeq, "%{definition: }s", elems);
+
+ table_close(rel, RowExclusiveLock);
+ relation_close(relation, AccessShareLock);
+
+ return createSeq;
+}
+
+/*
+ * Deparse a CreateStmt (CREATE TABLE).
+ *
+ * Given a table OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateStmt(Oid objectId, Node *parsetree)
+{
+ CreateStmt *node = (CreateStmt *) parsetree;
+ Relation relation = relation_open(objectId, AccessShareLock);
+ List *dpcontext;
+ ObjTree *createStmt;
+ ObjTree *tmpobj;
+ List *list = NIL;
+ ListCell *cell;
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{persistence}s TABLE %{if_not_exists}s %{identity}D [OF
+ * %{of_type}T | PARTITION OF %{parent_identity}D] %{table_elements}s
+ * %{inherits}s %{partition_by}s %{access_method}s %{with_clause}s
+ * %{on_commit}s %{tablespace}s
+ */
+ createStmt = new_objtree("CREATE");
+
+ append_string_object(createStmt, "%{persistence}s",
+ get_persistence_str(relation->rd_rel->relpersistence));
+
+ append_format_string(createStmt, "TABLE");
+
+ append_string_object(createStmt, "%{if_not_exists}s",
+ node->if_not_exists ? "IF NOT EXISTS" : "");
+
+ tmpobj = new_objtree_for_qualname(relation->rd_rel->relnamespace,
+ RelationGetRelationName(relation));
+ append_object_object(createStmt, "%{identity}D", tmpobj);
+
+ dpcontext = deparse_context_for(RelationGetRelationName(relation),
+ objectId);
+
+ /*
+ * Typed tables and partitions use a slightly different format string: we
+ * must not put table_elements with parents directly in the fmt string,
+ * because if there are no options the parentheses must not be emitted;
+ * and also, typed tables do not allow for inheritance.
+ */
+ if (node->ofTypename || node->partbound)
+ {
+ List *tableelts = NIL;
+
+ /*
+ * We can't put table elements directly in the fmt string as an array
+ * surrounded by parentheses here, because an empty clause would cause
+ * a syntax error. Therefore, we use an indirection element and set
+ * present=false when there are no elements.
+ */
+ if (node->ofTypename)
+ {
+ tmpobj = new_objtree_for_type(relation->rd_rel->reloftype, -1);
+ append_object_object(createStmt, "OF %{of_type}T", tmpobj);
+ }
+ else
+ {
+ List *parents;
+ ObjElem *elem;
+
+ parents = deparse_InhRelations(objectId);
+ elem = (ObjElem *) linitial(parents);
+
+ Assert(list_length(parents) == 1);
+
+ append_format_string(createStmt, "PARTITION OF");
+
+ append_object_object(createStmt, "%{parent_identity}D",
+ elem->value.object);
+ }
+
+ tableelts = deparse_TableElements(relation, node->tableElts, dpcontext,
+ true, /* typed table */
+ false); /* not composite */
+ tableelts = obtainConstraints(tableelts, objectId, InvalidOid);
+
+ if (tableelts == NIL)
+ {
+ tmpobj = new_objtree("");
+ append_bool_object(tmpobj, "present", false);
+ }
+ else
+ tmpobj = new_objtree_VA("(%{elements:, }s)", 1,
+ "elements", ObjTypeArray, tableelts);
+
+ append_object_object(createStmt, "%{table_elements}s", tmpobj);
+ }
+ else
+ {
+ List *tableelts = NIL;
+
+ /*
+ * There is no need to process LIKE clauses separately; they have
+ * already been transformed into columns and constraints.
+ */
+
+ /*
+ * Process table elements: column definitions and constraints. Only
+ * the column definitions are obtained from the parse node itself. To
+ * get constraints we rely on pg_constraint, because the parse node
+ * might be missing some things such as the name of the constraints.
+ */
+ tableelts = deparse_TableElements(relation, node->tableElts, dpcontext,
+ false, /* not typed table */
+ false); /* not composite */
+ tableelts = obtainConstraints(tableelts, objectId, InvalidOid);
+
+ append_array_object(createStmt, "(%{table_elements:, }s)", tableelts);
+
+ /*
+ * Add inheritance specification. We cannot simply scan the list of
+ * parents from the parser node, because that may lack the actual
+ * qualified names of the parent relations. Rather than trying to
+ * re-resolve them from the information in the parse node, it seems
+ * more accurate and convenient to grab it from pg_inherits.
+ */
+ tmpobj = new_objtree("INHERITS");
+ if (list_length(node->inhRelations) > 0)
+ append_array_object(tmpobj, "(%{parents:, }D)", deparse_InhRelations(objectId));
+ else
+ {
+ append_null_object(tmpobj, "(%{parents:, }D)");
+ append_bool_object(tmpobj, "present", false);
+ }
+ append_object_object(createStmt, "%{inherits}s", tmpobj);
+ }
+
+ tmpobj = new_objtree("TABLESPACE");
+ if (node->tablespacename)
+ append_string_object(tmpobj, "%{tablespace}I", node->tablespacename);
+ else
+ {
+ append_null_object(tmpobj, "%{tablespace}I");
+ append_bool_object(tmpobj, "present", false);
+ }
+ append_object_object(createStmt, "%{tablespace}s", tmpobj);
+ append_object_object(createStmt, "%{on_commit}s",
+ deparse_OnCommitClause(node->oncommit));
+
+ /* FOR VALUES clause */
+ if (node->partbound)
+ {
+ /*
+ * Get pg_class.relpartbound. We cannot use partbound in the parsetree
+ * directly as it's the original partbound expression which haven't
+ * been transformed.
+ */
+ append_string_object(createStmt, "%{partition_bound}s",
+ RelationGetPartitionBound(objectId));
+ }
+
+ /* PARTITION BY clause */
+ tmpobj = new_objtree("PARTITION BY");
+ if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+ append_string_object(tmpobj, "%{definition}s", pg_get_partkeydef_simple(objectId));
+ else
+ {
+ append_null_object(tmpobj, "%{definition}s");
+ append_bool_object(tmpobj, "present", false);
+ }
+ append_object_object(createStmt, "%{partition_by}s", tmpobj);
+
+ /* USING clause */
+ tmpobj = new_objtree("USING");
+ if (node->accessMethod)
+ append_string_object(tmpobj, "%{access_method}I", node->accessMethod);
+ else
+ {
+ append_null_object(tmpobj, "%{access_method}I");
+ append_bool_object(tmpobj, "present", false);
+ }
+ append_object_object(createStmt, "%{access_method}s", tmpobj);
+
+ /* WITH clause */
+ tmpobj = new_objtree("WITH");
+
+ foreach(cell, node->options)
+ {
+ ObjTree *tmpobj2;
+ DefElem *opt = (DefElem *) lfirst(cell);
+
+ tmpobj2 = deparse_DefElem(opt, false);
+ list = lappend(list, new_object_object(tmpobj2));
+ }
+
+ if (list)
+ append_array_object(tmpobj, "(%{with:, }s)", list);
+ else
+ append_bool_object(tmpobj, "present", false);
+
+ append_object_object(createStmt, "%{with_clause}s", tmpobj);
+
+ relation_close(relation, AccessShareLock);
+
+ return createStmt;
+}
+
+/*
+ * Deparse a DefineStmt.
+ */
+static ObjTree *
+deparse_DefineStmt(Oid objectId, Node *parsetree, ObjectAddress secondaryObj)
+{
+ DefineStmt *define = (DefineStmt *) parsetree;
+ ObjTree *defStmt = NULL;
+
+ switch (define->kind)
+ {
+ case OBJECT_COLLATION:
+ defStmt = deparse_DefineStmt_Collation(objectId, define, secondaryObj);
+ break;
+
+ case OBJECT_OPERATOR:
+ defStmt = deparse_DefineStmt_Operator(objectId, define);
+ break;
+
+ case OBJECT_TYPE:
+ defStmt = deparse_DefineStmt_Type(objectId, define);
+ break;
+
+ case OBJECT_TSCONFIGURATION:
+ defStmt = deparse_DefineStmt_TSConfig(objectId, define, secondaryObj);
+ break;
+
+ case OBJECT_TSPARSER:
+ defStmt = deparse_DefineStmt_TSParser(objectId, define);
+ break;
+
+ case OBJECT_TSDICTIONARY:
+ defStmt = deparse_DefineStmt_TSDictionary(objectId, define);
+ break;
+
+ case OBJECT_TSTEMPLATE:
+ defStmt = deparse_DefineStmt_TSTemplate(objectId, define);
+ break;
+
+ default:
+ elog(ERROR, "unsupported object kind");
+ }
+
+ return defStmt;
+}
+
+/*
+ * Deparse a DefineStmt (CREATE COLLATION)
+ *
+ * Given a collation OID, return the JSON blob representing the alter command.
+ */
+static ObjTree *
+deparse_DefineStmt_Collation(Oid objectId, DefineStmt *define,
+ ObjectAddress fromCollid)
+{
+ ObjTree *stmt;
+ HeapTuple colTup;
+ Form_pg_collation colForm;
+ Datum datum;
+ bool isnull;
+ ObjTree *tmp;
+ List *list = NIL;
+
+ colTup = SearchSysCache1(COLLOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(colTup))
+ elog(ERROR, "cache lookup failed for collation with OID %u", objectId);
+ colForm = (Form_pg_collation) GETSTRUCT(colTup);
+
+ stmt = new_objtree_VA("CREATE COLLATION", 0);
+
+ append_object_object(stmt, "%{identity}D",
+ new_objtree_for_qualname(colForm->collnamespace,
+ NameStr(colForm->collname)));
+
+ if (fromCollid.objectId != InvalidOid)
+ {
+ Oid collid = fromCollid.objectId;
+ HeapTuple tp;
+ Form_pg_collation fromColForm;
+
+ /*
+ * CREATE COLLATION %{identity}D FROM existing_collation;
+ */
+ tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
+ if (!HeapTupleIsValid(tp))
+ elog(ERROR, "cache lookup failed for collation %u", collid);
+
+ fromColForm = (Form_pg_collation) GETSTRUCT(tp);
+
+ append_object_object(stmt, "FROM %{from_identity}D",
+ new_objtree_for_qualname(fromColForm->collnamespace,
+ NameStr(fromColForm->collname)));
+
+
+ ReleaseSysCache(tp);
+ ReleaseSysCache(colTup);
+ return stmt;
+ }
+
+ /* LOCALE */
+ datum = SysCacheGetAttr(COLLOID, colTup, Anum_pg_collation_colliculocale, &isnull);
+ if (!isnull)
+ {
+ tmp = new_objtree_VA("LOCALE=", 1,
+ "clause", ObjTypeString, "locale");
+ append_string_object(tmp, "%{locale}L",
+ psprintf("%s", TextDatumGetCString(datum)));
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ /* LC_COLLATE */
+ datum = SysCacheGetAttr(COLLOID, colTup, Anum_pg_collation_collcollate, &isnull);
+ if (!isnull)
+ {
+ tmp = new_objtree_VA("LC_COLLATE=", 1,
+ "clause", ObjTypeString, "collate");
+ append_string_object(tmp, "%{collate}L",
+ psprintf("%s", TextDatumGetCString(datum)));
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ /* LC_CTYPE */
+ datum = SysCacheGetAttr(COLLOID, colTup, Anum_pg_collation_collctype, &isnull);
+ if (!isnull)
+ {
+ tmp = new_objtree_VA("LC_CTYPE=", 1,
+ "clause", ObjTypeString, "ctype");
+ append_string_object(tmp, "%{ctype}L",
+ psprintf("%s", TextDatumGetCString(datum)));
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ /* PROVIDER */
+ if (colForm->collprovider == COLLPROVIDER_ICU)
+ {
+ tmp = new_objtree_VA("PROVIDER=", 1,
+ "clause", ObjTypeString, "provider");
+ append_string_object(tmp, "%{provider}L",
+ psprintf("%s", "icu"));
+ list = lappend(list, new_object_object(tmp));
+ }
+ else if (colForm->collprovider == COLLPROVIDER_LIBC)
+ {
+ tmp = new_objtree_VA("PROVIDER=", 1,
+ "clause", ObjTypeString, "provider");
+ append_string_object(tmp, "%{provider}L",
+ psprintf("%s", "libc"));
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ /* DETERMINISTIC */
+ if (colForm->collisdeterministic)
+ {
+ tmp = new_objtree_VA("DETERMINISTIC=", 1,
+ "clause", ObjTypeString, "deterministic");
+ append_string_object(tmp, "%{deterministic}L",
+ psprintf("%s", "true"));
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ /* VERSION */
+ datum = SysCacheGetAttr(COLLOID, colTup, Anum_pg_collation_collversion, &isnull);
+ if (!isnull)
+ {
+ tmp = new_objtree_VA("VERSION=", 1,
+ "clause", ObjTypeString, "version");
+ append_string_object(tmp, "%{version}L",
+ psprintf("%s", TextDatumGetCString(datum)));
+ list = lappend(list, new_object_object(tmp));
+ }
+
+ append_array_object(stmt, "(%{elems:, }s)", list);
+ ReleaseSysCache(colTup);
+
+ return stmt;
+}
+
+/*
+ * Deparse a DefineStmt (CREATE OPERATOR)
+ *
+ * Given a trigger OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_DefineStmt_Operator(Oid objectId, DefineStmt *define)
+{
+ HeapTuple oprTup;
+ ObjTree *stmt;
+ ObjTree *tmpobj;
+ List *list = NIL;
+ Form_pg_operator oprForm;
+
+ oprTup = SearchSysCache1(OPEROID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(oprTup))
+ elog(ERROR, "cache lookup failed for operator with OID %u", objectId);
+ oprForm = (Form_pg_operator) GETSTRUCT(oprTup);
+
+ stmt = new_objtree_VA("CREATE OPERATOR %{identity}O", 1,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(oprForm->oprnamespace,
+ NameStr(oprForm->oprname)));
+
+ /* PROCEDURE */
+ tmpobj = new_objtree_VA("PROCEDURE=%{procedure}D", 2,
+ "clause", ObjTypeString, "procedure",
+ "procedure", ObjTypeObject,
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ oprForm->oprcode));
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* LEFTARG */
+ tmpobj = new_objtree_VA("LEFTARG=", 1,
+ "clause", ObjTypeString, "leftarg");
+ if (OidIsValid(oprForm->oprleft))
+ append_object_object(tmpobj, "%{type}T",
+ new_objtree_for_type(oprForm->oprleft, -1));
+ else
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* RIGHTARG */
+ tmpobj = new_objtree_VA("RIGHTARG=", 1,
+ "clause", ObjTypeString, "rightarg");
+ if (OidIsValid(oprForm->oprright))
+ append_object_object(tmpobj, "%{type}T",
+ new_objtree_for_type(oprForm->oprright, -1));
+ else
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* COMMUTATOR */
+ tmpobj = new_objtree_VA("COMMUTATOR=", 1,
+ "clause", ObjTypeString, "commutator");
+ if (OidIsValid(oprForm->oprcom))
+ append_object_object(tmpobj, "%{oper}D",
+ new_objtree_for_qualname_id(OperatorRelationId,
+ oprForm->oprcom));
+ else
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* NEGATOR */
+ tmpobj = new_objtree_VA("NEGATOR=", 1,
+ "clause", ObjTypeString, "negator");
+ if (OidIsValid(oprForm->oprnegate))
+ append_object_object(tmpobj, "%{oper}D",
+ new_objtree_for_qualname_id(OperatorRelationId,
+ oprForm->oprnegate));
+ else
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* RESTRICT */
+ tmpobj = new_objtree_VA("RESTRICT=", 1,
+ "clause", ObjTypeString, "restrict");
+ if (OidIsValid(oprForm->oprrest))
+ append_object_object(tmpobj, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ oprForm->oprrest));
+ else
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* JOIN */
+ tmpobj = new_objtree_VA("JOIN=", 1,
+ "clause", ObjTypeString, "join");
+ if (OidIsValid(oprForm->oprjoin))
+ append_object_object(tmpobj, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ oprForm->oprjoin));
+ else
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* MERGES */
+ tmpobj = new_objtree_VA("MERGES", 1,
+ "clause", ObjTypeString, "merges");
+ if (!oprForm->oprcanmerge)
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ /* HASHES */
+ tmpobj = new_objtree_VA("HASHES", 1,
+ "clause", ObjTypeString, "hashes");
+ if (!oprForm->oprcanhash)
+ append_bool_object(tmpobj, "present", false);
+ list = lappend(list, new_object_object(tmpobj));
+
+ append_array_object(stmt, "(%{elems:, }s)", list);
+
+ ReleaseSysCache(oprTup);
+
+ return stmt;
+}
+
+/*
+ * Deparse a CREATE TYPE statement.
+ */
+static ObjTree *
+deparse_DefineStmt_Type(Oid objectId, DefineStmt *define)
+{
+ HeapTuple typTup;
+ ObjTree *stmt;
+ ObjTree *tmp;
+ List *list;
+ char *str;
+ Datum dflt;
+ bool isnull;
+ Form_pg_type typForm;
+
+ typTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(typTup))
+ elog(ERROR, "cache lookup failed for type with OID %u", objectId);
+ typForm = (Form_pg_type) GETSTRUCT(typTup);
+
+ /* Shortcut processing for shell types. */
+ if (!typForm->typisdefined)
+ {
+ stmt = new_objtree_VA("CREATE TYPE", 0);
+ append_object_object(stmt, "%{identity}D",
+ new_objtree_for_qualname(typForm->typnamespace,
+ NameStr(typForm->typname)));
+ append_bool_object(stmt, "present", true);
+ ReleaseSysCache(typTup);
+ return stmt;
+ }
+
+ stmt = new_objtree_VA("CREATE TYPE", 0);
+ append_object_object(stmt, "%{identity}D",
+ new_objtree_for_qualname(typForm->typnamespace,
+ NameStr(typForm->typname)));
+ append_bool_object(stmt, "present", true);
+
+ /* Add the definition clause */
+ list = NIL;
+
+ /* INPUT */
+ tmp = new_objtree_VA("(INPUT=", 1,
+ "clause", ObjTypeString, "input");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typinput));
+ list = lappend(list, new_object_object(tmp));
+
+ /* OUTPUT */
+ tmp = new_objtree_VA("OUTPUT=", 1,
+ "clause", ObjTypeString, "output");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typoutput));
+ list = lappend(list, new_object_object(tmp));
+
+ /* RECEIVE */
+ tmp = new_objtree_VA("RECEIVE=", 1,
+ "clause", ObjTypeString, "receive");
+ if (OidIsValid(typForm->typreceive))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typreceive));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* SEND */
+ tmp = new_objtree_VA("SEND=", 1,
+ "clause", ObjTypeString, "send");
+ if (OidIsValid(typForm->typsend))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typsend));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* TYPMOD_IN */
+ tmp = new_objtree_VA("TYPMOD_IN=", 1,
+ "clause", ObjTypeString, "typmod_in");
+ if (OidIsValid(typForm->typmodin))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typmodin));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* TYPMOD_OUT */
+ tmp = new_objtree_VA("TYPMOD_OUT=", 1,
+ "clause", ObjTypeString, "typmod_out");
+ if (OidIsValid(typForm->typmodout))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typmodout));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* ANALYZE */
+ tmp = new_objtree_VA("ANALYZE=", 1,
+ "clause", ObjTypeString, "analyze");
+ if (OidIsValid(typForm->typanalyze))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typanalyze));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* INTERNALLENGTH */
+ if (typForm->typlen == -1)
+ {
+ tmp = new_objtree_VA("INTERNALLENGTH=VARIABLE", 0);
+ }
+ else
+ {
+ tmp = new_objtree_VA("INTERNALLENGTH=%{typlen}n", 1,
+ "typlen", ObjTypeInteger, typForm->typlen);
+ }
+
+ list = lappend(list, new_object_object(tmp));
+
+ /* PASSEDBYVALUE */
+ tmp = new_objtree_VA("PASSEDBYVALUE", 1,
+ "clause", ObjTypeString, "passedbyvalue");
+ if (!typForm->typbyval)
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* ALIGNMENT */
+ tmp = new_objtree_VA("ALIGNMENT=", 1,
+ "clause", ObjTypeString, "alignment");
+ /* XXX it's odd to represent alignment with schema-qualified type names */
+ switch (typForm->typalign)
+ {
+ case 'd':
+ str = "pg_catalog.float8";
+ break;
+ case 'i':
+ str = "pg_catalog.int4";
+ break;
+ case 's':
+ str = "pg_catalog.int2";
+ break;
+ case 'c':
+ str = "pg_catalog.bpchar";
+ break;
+ default:
+ elog(ERROR, "invalid alignment %c", typForm->typalign);
+ }
+ append_string_object(tmp, "%{align}s", str);
+ list = lappend(list, new_object_object(tmp));
+
+ tmp = new_objtree_VA("STORAGE=", 1,
+ "clause", ObjTypeString, "storage");
+ switch (typForm->typstorage)
+ {
+ case 'p':
+ str = "plain";
+ break;
+ case 'e':
+ str = "external";
+ break;
+ case 'x':
+ str = "extended";
+ break;
+ case 'm':
+ str = "main";
+ break;
+ default:
+ elog(ERROR, "invalid storage specifier %c", typForm->typstorage);
+ }
+ append_string_object(tmp, "%{storage}s", str);
+ list = lappend(list, new_object_object(tmp));
+
+ /* CATEGORY */
+ tmp = new_objtree_VA("CATEGORY=", 1,
+ "clause", ObjTypeString, "category");
+ append_string_object(tmp, "%{category}s",
+ psprintf("%c", typForm->typcategory));
+ list = lappend(list, new_object_object(tmp));
+
+ /* PREFERRED */
+ tmp = new_objtree_VA("PREFERRED=", 1,
+ "clause", ObjTypeString, "preferred");
+ if (!typForm->typispreferred)
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* DEFAULT */
+ dflt = SysCacheGetAttr(TYPEOID, typTup,
+ Anum_pg_type_typdefault,
+ &isnull);
+ tmp = new_objtree_VA("DEFAULT=", 1,
+ "clause", ObjTypeString, "default");
+ if (!isnull)
+ append_string_object(tmp, "%{default}s", TextDatumGetCString(dflt));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* ELEMENT */
+ tmp = new_objtree_VA("ELEMENT=", 1,
+ "clause", ObjTypeString, "element");
+ if (OidIsValid(typForm->typelem))
+ append_object_object(tmp, "elem",
+ new_objtree_for_type(typForm->typelem, -1));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* DELIMITER */
+ tmp = new_objtree_VA("DELIMITER=", 1,
+ "clause", ObjTypeString, "delimiter");
+ append_string_object(tmp, "%{delim}L",
+ psprintf("%c", typForm->typdelim));
+ list = lappend(list, new_object_object(tmp));
+
+ /* COLLATABLE */
+ tmp = new_objtree_VA("COLLATABLE=", 1,
+ "clause", ObjTypeString, "collatable");
+ if (!OidIsValid(typForm->typcollation))
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ append_array_object(stmt, "%{elems:, }s)", list);
+
+ ReleaseSysCache(typTup);
+
+ return stmt;
+}
+
+static ObjTree *
+deparse_DefineStmt_TSConfig(Oid objectId, DefineStmt *define,
+ ObjectAddress copied)
+{
+ HeapTuple tscTup;
+ HeapTuple tspTup;
+ ObjTree *stmt;
+ ObjTree *tmp;
+ Form_pg_ts_config tscForm;
+ Form_pg_ts_parser tspForm;
+ List *list;
+
+ tscTup = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tscTup))
+ elog(ERROR, "cache lookup failed for text search configuration %u",
+ objectId);
+ tscForm = (Form_pg_ts_config) GETSTRUCT(tscTup);
+
+ tspTup = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(tscForm->cfgparser));
+ if (!HeapTupleIsValid(tspTup))
+ elog(ERROR, "cache lookup failed for text search parser %u",
+ tscForm->cfgparser);
+ tspForm = (Form_pg_ts_parser) GETSTRUCT(tspTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE TEXT SEARCH CONFIGURATION %{identity}D (%{elems:, }s)
+ */
+ stmt = new_objtree("CREATE");
+
+ append_object_object(stmt, "TEXT SEARCH CONFIGURATION %{identity}D",
+ new_objtree_for_qualname(tscForm->cfgnamespace,
+ NameStr(tscForm->cfgname)));
+
+ /*
+ * Add the definition clause. If we have COPY'ed an existing config, add
+ * a COPY clause; otherwise add a PARSER clause.
+ */
+ list = NIL;
+ /* COPY */
+ tmp = new_objtree_VA("COPY=", 1,
+ "clause", ObjTypeString, "copy");
+ if (copied.objectId != InvalidOid)
+ append_object_object(tmp, "%{tsconfig}D",
+ new_objtree_for_qualname_id(TSConfigRelationId,
+ copied.objectId));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* PARSER */
+ tmp = new_objtree_VA("PARSER=", 1,
+ "clause", ObjTypeString, "parser");
+ if (copied.objectId == InvalidOid)
+ append_object_object(tmp, "%{parser}D",
+ new_objtree_for_qualname(tspForm->prsnamespace,
+ NameStr(tspForm->prsname)));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ append_array_object(stmt, "(%{elems:, }s)", list);
+
+ ReleaseSysCache(tspTup);
+ ReleaseSysCache(tscTup);
+
+ return stmt;
+}
+
+static ObjTree *
+deparse_DefineStmt_TSParser(Oid objectId, DefineStmt *define)
+{
+ HeapTuple tspTup;
+ ObjTree *stmt;
+ ObjTree *tmp;
+ List *list;
+ Form_pg_ts_parser tspForm;
+
+ tspTup = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tspTup))
+ elog(ERROR, "cache lookup failed for text search parser with OID %u",
+ objectId);
+ tspForm = (Form_pg_ts_parser) GETSTRUCT(tspTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE TEXT SEARCH PARSER %{identity}D (%{elems:, }s)
+ */
+ stmt = new_objtree("CREATE");
+
+ append_object_object(stmt, "TEXT SEARCH PARSER %{identity}D",
+ new_objtree_for_qualname(tspForm->prsnamespace,
+ NameStr(tspForm->prsname)));
+
+ /* Add the definition clause */
+ list = NIL;
+
+ /* START */
+ tmp = new_objtree_VA("START=", 1,
+ "clause", ObjTypeString, "start");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tspForm->prsstart));
+ list = lappend(list, new_object_object(tmp));
+
+ /* GETTOKEN */
+ tmp = new_objtree_VA("GETTOKEN=", 1,
+ "clause", ObjTypeString, "gettoken");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tspForm->prstoken));
+ list = lappend(list, new_object_object(tmp));
+
+ /* END */
+ tmp = new_objtree_VA("END=", 1,
+ "clause", ObjTypeString, "end");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tspForm->prsend));
+ list = lappend(list, new_object_object(tmp));
+
+ /* LEXTYPES */
+ tmp = new_objtree_VA("LEXTYPES=", 1,
+ "clause", ObjTypeString, "lextypes");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tspForm->prslextype));
+ list = lappend(list, new_object_object(tmp));
+
+ /* HEADLINE */
+ tmp = new_objtree_VA("HEADLINE=", 1,
+ "clause", ObjTypeString, "headline");
+ if (OidIsValid(tspForm->prsheadline))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tspForm->prsheadline));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ append_array_object(stmt, "(%{elems:, }s)", list);
+
+ ReleaseSysCache(tspTup);
+
+ return stmt;
+}
+
+static ObjTree *
+deparse_DefineStmt_TSDictionary(Oid objectId, DefineStmt *define)
+{
+ HeapTuple tsdTup;
+ ObjTree *stmt;
+ ObjTree *tmp;
+ List *list;
+ Datum options;
+ bool isnull;
+ Form_pg_ts_dict tsdForm;
+
+ tsdTup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tsdTup))
+ elog(ERROR, "cache lookup failed for text search dictionary "
+ "with OID %u", objectId);
+ tsdForm = (Form_pg_ts_dict) GETSTRUCT(tsdTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE TEXT SEARCH DICTIONARY %{identity}D (%{elems:, }s)
+ */
+ stmt = new_objtree("CREATE");
+
+ append_object_object(stmt, "TEXT SEARCH DICTIONARY %{identity}D",
+ new_objtree_for_qualname(tsdForm->dictnamespace,
+ NameStr(tsdForm->dictname)));
+
+ /* Add the definition clause */
+ list = NIL;
+
+ /* TEMPLATE */
+ tmp = new_objtree_VA("TEMPLATE=", 1,
+ "clause", ObjTypeString, "template");
+ append_object_object(tmp, "%{template}D",
+ new_objtree_for_qualname_id(TSTemplateRelationId,
+ tsdForm->dicttemplate));
+ list = lappend(list, new_object_object(tmp));
+
+ /*
+ * options. XXX this is a pretty useless representation, but we can't do
+ * better without more ts_cache.c cooperation ...
+ */
+ options = SysCacheGetAttr(TSDICTOID, tsdTup,
+ Anum_pg_ts_dict_dictinitoption,
+ &isnull);
+ tmp = new_objtree_VA("", 0);
+ if (!isnull)
+ append_string_object(tmp, "%{options}s", TextDatumGetCString(options));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ append_array_object(stmt, "(%{elems:, }s)", list);
+
+ ReleaseSysCache(tsdTup);
+
+ return stmt;
+}
+
+static ObjTree *
+deparse_DefineStmt_TSTemplate(Oid objectId, DefineStmt *define)
+{
+ HeapTuple tstTup;
+ ObjTree *stmt;
+ ObjTree *tmp;
+ List *list;
+ Form_pg_ts_template tstForm;
+
+ tstTup = SearchSysCache1(TSTEMPLATEOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tstTup))
+ elog(ERROR, "cache lookup failed for text search template with OID %u",
+ objectId);
+ tstForm = (Form_pg_ts_template) GETSTRUCT(tstTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE TEXT SEARCH TEMPLATE %{identity}D (%{elems:, }s)
+ */
+ stmt = new_objtree("CREATE");
+
+ append_object_object(stmt, "TEXT SEARCH TEMPLATE %{identity}D",
+ new_objtree_for_qualname(tstForm->tmplnamespace,
+ NameStr(tstForm->tmplname)));
+
+ /* Add the definition clause */
+ list = NIL;
+
+ /* INIT */
+ tmp = new_objtree_VA("INIT=", 1,
+ "clause", ObjTypeString, "init");
+ if (OidIsValid(tstForm->tmplinit))
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tstForm->tmplinit));
+ else
+ append_bool_object(tmp, "present", false);
+ list = lappend(list, new_object_object(tmp));
+
+ /* LEXIZE */
+ tmp = new_objtree_VA("LEXIZE=", 1,
+ "clause", ObjTypeString, "lexize");
+ append_object_object(tmp, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ tstForm->tmpllexize));
+ list = lappend(list, new_object_object(tmp));
+
+ append_array_object(stmt, "(%{elems:, }s)", list);
+
+ ReleaseSysCache(tstTup);
+
+ return stmt;
+}
+
+static ObjTree *
+deparse_AlterTSConfigurationStmt(CollectedCommand *cmd)
+{
+ AlterTSConfigurationStmt *node = (AlterTSConfigurationStmt *) cmd->parsetree;
+ ObjTree *config;
+ ObjTree *tmp;
+ List *list;
+ ListCell *cell;
+ int i;
+
+ /*
+ * Verbose syntax
+ * case ALTER_TSCONFIG_ADD_MAPPING:
+ * ALTER TEXT SEARCH CONFIGURATION %{identity}D ADD MAPPING
+ * FOR %{tokentype:, }I WITH %{dictionaries:, }D
+ *
+ * case ALTER_TSCONFIG_DROP_MAPPING:
+ * ALTER TEXT SEARCH CONFIGURATION %{identity}D DROP MAPPING %{if_exists}s
+ * FOR %{tokentype}I
+ *
+ * case ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN:
+ * ALTER TEXT SEARCH CONFIGURATION %{identity}D ALTER MAPPING
+ * FOR %{tokentype:, }I WITH %{dictionaries:, }D
+ *
+ * case ALTER_TSCONFIG_REPLACE_DICT:
+ * ALTER TEXT SEARCH CONFIGURATION %{identity}D ALTER MAPPING
+ * REPLACE %{old_dictionary}D WITH %{new_dictionary}D
+ *
+ * case ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN:
+ * ALTER TEXT SEARCH CONFIGURATION %{identity}D ALTER MAPPING
+ * FOR %{tokentype:, }I REPLACE %{old_dictionary}D
+ * WITH %{new_dictionary}D
+ */
+
+ config = new_objtree("ALTER TEXT SEARCH CONFIGURATION");
+
+ /* determine the format string appropriate to each subcommand */
+ switch (node->kind)
+ {
+ case ALTER_TSCONFIG_ADD_MAPPING:
+ append_object_object(config, "%{identity}D ADD MAPPING",
+ new_objtree_for_qualname_id(cmd->d.atscfg.address.classId,
+ cmd->d.atscfg.address.objectId));
+ break;
+
+ case ALTER_TSCONFIG_DROP_MAPPING:
+ append_object_object(config, "%{identity}D DROP MAPPING",
+ new_objtree_for_qualname_id(cmd->d.atscfg.address.classId,
+ cmd->d.atscfg.address.objectId));
+ tmp = new_objtree_VA("IF EXISTS", 0);
+ append_bool_object(tmp, "present", node->missing_ok);
+ append_object_object(config, "%{if_exists}s", tmp);
+ break;
+
+ case ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN:
+ append_object_object(config, "%{identity}D ALTER MAPPING",
+ new_objtree_for_qualname_id(cmd->d.atscfg.address.classId,
+ cmd->d.atscfg.address.objectId));
+ break;
+
+ case ALTER_TSCONFIG_REPLACE_DICT:
+ append_object_object(config, "%{identity}D ALTER MAPPING",
+ new_objtree_for_qualname_id(cmd->d.atscfg.address.classId,
+ cmd->d.atscfg.address.objectId));
+ break;
+
+ case ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN:
+ append_object_object(config, "%{identity}D ALTER MAPPING",
+ new_objtree_for_qualname_id(cmd->d.atscfg.address.classId,
+ cmd->d.atscfg.address.objectId));
+ break;
+ }
+
+ /* Add the affected token list, for subcommands that have one */
+ if (node->kind == ALTER_TSCONFIG_ADD_MAPPING ||
+ node->kind == ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN ||
+ node->kind == ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN ||
+ node->kind == ALTER_TSCONFIG_DROP_MAPPING)
+ {
+ list = NIL;
+ foreach(cell, node->tokentype)
+ list = lappend(list, new_string_object(strVal(lfirst(cell))));
+ append_array_object(config, "FOR %{tokentype:, }I", list);
+ }
+
+ /* add further subcommand-specific elements */
+ if (node->kind == ALTER_TSCONFIG_ADD_MAPPING ||
+ node->kind == ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN)
+ {
+ /* ADD MAPPING and ALTER MAPPING FOR need a list of dictionaries */
+ list = NIL;
+ for (i = 0; i < cmd->d.atscfg.ndicts; i++)
+ {
+ ObjTree *dictobj;
+
+ dictobj = new_objtree_for_qualname_id(TSDictionaryRelationId,
+ cmd->d.atscfg.dictIds[i]);
+ list = lappend(list,
+ new_object_object(dictobj));
+ }
+ append_array_object(config, "WITH %{dictionaries:, }D", list);
+ }
+ else if (node->kind == ALTER_TSCONFIG_REPLACE_DICT ||
+ node->kind == ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN)
+ {
+ /* the REPLACE forms want old and new dictionaries */
+ Assert(cmd->d.atscfg.ndicts == 2);
+ append_object_object(config, "REPLACE %{old_dictionary}D",
+ new_objtree_for_qualname_id(TSDictionaryRelationId,
+ cmd->d.atscfg.dictIds[0]));
+ append_object_object(config, "WITH %{new_dictionary}D",
+ new_objtree_for_qualname_id(TSDictionaryRelationId,
+ cmd->d.atscfg.dictIds[1]));
+ }
+
+ return config;
+}
+
+static ObjTree *
+deparse_AlterTSDictionaryStmt(Oid objectId, Node *parsetree)
+{
+ ObjTree *alterTSD;
+ ObjTree *tmp;
+ Datum options;
+ List *definition = NIL;
+ bool isnull;
+ HeapTuple tsdTup;
+ Form_pg_ts_dict tsdForm;
+
+ tsdTup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tsdTup))
+ elog(ERROR, "cache lookup failed for text search dictionary "
+ "with OID %u", objectId);
+ tsdForm = (Form_pg_ts_dict) GETSTRUCT(tsdTup);
+
+ /*
+ * Verbose syntax
+ * ALTER TEXT SEARCH DICTIONARY %{identity}D (%{definition:, }s)
+ */
+
+ alterTSD = new_objtree("ALTER TEXT SEARCH DICTIONARY");
+
+ append_object_object(alterTSD, "%{identity}D",
+ new_objtree_for_qualname(tsdForm->dictnamespace,
+ NameStr(tsdForm->dictname)));
+
+ /*
+ * Add the definition list according to the pg_ts_dict dictinitoption
+ * column
+ */
+ options = SysCacheGetAttr(TSDICTOID, tsdTup,
+ Anum_pg_ts_dict_dictinitoption,
+ &isnull);
+ tmp = new_objtree_VA("", 0);
+ if (!isnull)
+ append_string_object(tmp, "%{options}s", TextDatumGetCString(options));
+ else
+ append_bool_object(tmp, "present", false);
+
+ definition = lappend(definition, new_object_object(tmp));
+ append_array_object(alterTSD, "(%{definition:, }s)", definition);
+ ReleaseSysCache(tsdTup);
+
+ return alterTSD;
+}
+
+/*
+ * ... ALTER COLUMN ... SET/RESET (...)
+ */
+static ObjTree *
+deparse_RelSetOptions(AlterTableCmd *subcmd)
+{
+ List *sets = NIL;
+ ListCell *cell;
+ ObjTree *relset;
+ char *fmt;
+ bool is_reset = subcmd->subtype == AT_ResetRelOptions;
+
+ /*
+ * Verbose syntax
+ *
+ * RESET|SET (%{options:, }s)
+ */
+ if (is_reset)
+ fmt = "RESET ";
+ else
+ fmt = "SET ";
+
+ relset = new_objtree(fmt);
+
+ foreach(cell, (List *) subcmd->def)
+ {
+ DefElem *elem;
+ ObjTree *set;
+
+ elem = (DefElem *) lfirst(cell);
+ set = deparse_DefElem(elem, is_reset);
+ sets = lappend(sets, new_object_object(set));
+ }
+
+ Assert(sets);
+ append_array_object(relset, "(%{options:, }s)", sets);
+
+ return relset;
+}
+
+/*
+ * deparse_ViewStmt
+ * deparse a ViewStmt
+ *
+ * Given a view OID and the parsetree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_ViewStmt(Oid objectId, Node *parsetree)
+{
+ ViewStmt *node = (ViewStmt *) parsetree;
+ ObjTree *viewStmt;
+ ObjTree *tmp;
+ Relation relation;
+
+ relation = relation_open(objectId, AccessShareLock);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{or_replace}s %{persistence}s VIEW %{identity}D AS %{query}s
+ */
+ viewStmt = new_objtree("CREATE");
+
+ append_string_object(viewStmt, "%{or_replace}s",
+ node->replace ? "OR REPLACE" : "");
+
+ append_string_object(viewStmt, "%{persistence}s",
+ get_persistence_str(relation->rd_rel->relpersistence));
+
+ tmp = new_objtree_for_qualname(relation->rd_rel->relnamespace,
+ RelationGetRelationName(relation));
+
+ append_object_object(viewStmt, "VIEW %{identity}D", tmp);
+
+ append_string_object(viewStmt, "AS %{query}s",
+ pg_get_viewdef_internal(objectId));
+
+ relation_close(relation, AccessShareLock);
+
+ return viewStmt;
+}
+
+/*
+ * Deparse CREATE Materialized View statement, it is a variant of CreateTableAsStmt
+ *
+ * Note that CREATE TABLE AS SELECT INTO can also be deparsed by
+ * deparse_CreateTableAsStmt to remove the SELECT INTO clause.
+ */
+static ObjTree *
+deparse_CreateTableAsStmt_vanilla(Oid objectId, Node *parsetree)
+{
+ CreateTableAsStmt *node = (CreateTableAsStmt *) parsetree;
+ Relation relation = relation_open(objectId, AccessShareLock);
+ ObjTree *createStmt;
+ ObjTree *tmp;
+ ObjTree *tmp2;
+ char *fmt;
+ List *list;
+ ListCell *cell;
+
+ /*
+ * Reject unsupported case right away.
+ */
+ if (((Query *) (node->query))->commandType == CMD_UTILITY)
+ elog(ERROR, "unimplemented deparse of CREATE TABLE AS EXECUTE");
+
+ /*
+ * Note that INSERT INTO is deparsed as CREATE TABLE AS. They are
+ * functionally equivalent synonyms so there is no harm from this.
+ *
+ * Verbose syntax
+ * CREATE %{persistence}s [MATERIALIZED VIEW | TABLE] %{if_not_exists}s
+ * %{identity}D %{columns}s [%{on_commit}s] %{tablespace}s
+ * AS %{query}s %{with_no_data}s"
+ */
+ if (node->objtype == OBJECT_MATVIEW)
+ fmt = "CREATE %{persistence}s MATERIALIZED VIEW %{if_not_exists}s ";
+ else
+ fmt = "CREATE %{persistence}s TABLE %{if_not_exists}s ";
+
+ createStmt =
+ new_objtree_VA(fmt, 2,
+ "persistence", ObjTypeString,
+ get_persistence_str(node->into->rel->relpersistence),
+ "if_not_exists", ObjTypeString,
+ node->if_not_exists ? "IF NOT EXISTS" : "");
+
+ /* Add identity */
+ append_object_object(createStmt, "%{identity}D",
+ new_objtree_for_qualname_id(RelationRelationId,
+ objectId));
+
+ /* COLLUMNS clause */
+ if (node->into->colNames == NIL)
+ tmp = new_objtree_VA("", 1,
+ "present", ObjTypeBool, false);
+ else
+ {
+ list = NIL;
+ foreach(cell, node->into->colNames)
+ list = lappend(list, new_string_object(strVal(lfirst(cell))));
+
+ tmp = new_objtree_VA("(%{columns:, }I)", 1,
+ "columns", ObjTypeArray, list);
+ }
+ append_object_object(createStmt, "%{columns}s", tmp);
+
+ /* USING clause */
+ tmp = new_objtree("USING");
+ if (node->into->accessMethod)
+ append_string_object(tmp, "%{access_method}I", node->into->accessMethod);
+ else
+ {
+ append_null_object(tmp, "%{access_method}I");
+ append_bool_object(tmp, "present", false);
+ }
+ append_object_object(createStmt, "%{access_method}s", tmp);
+
+ /* WITH clause */
+ tmp = new_objtree_VA("WITH", 0);
+ list = NIL;
+
+ foreach(cell, node->into->options)
+ {
+ DefElem *opt = (DefElem *) lfirst(cell);
+
+ tmp2 = deparse_DefElem(opt, false);
+ list = lappend(list, new_object_object(tmp2));
+ }
+
+ if (list)
+ append_array_object(tmp, "(%{with:, }s)", list);
+ else
+ append_bool_object(tmp, "present", false);
+
+ append_object_object(createStmt, "%{with_clause}s", tmp);
+
+ /* ON COMMIT clause. CREATE MATERIALIZED VIEW doesn't have one */
+ if (node->objtype == OBJECT_TABLE)
+ {
+ append_object_object(createStmt, "%{on_commit}s",
+ deparse_OnCommitClause(node->into->onCommit));
+ }
+
+ /* TABLESPACE clause */
+ tmp = new_objtree_VA("TABLESPACE %{tablespace}I", 0);
+ if (node->into->tableSpaceName)
+ append_string_object(tmp, "%{tablespace}s", node->into->tableSpaceName);
+ else
+ {
+ append_null_object(tmp, "%{tablespace}I");
+ append_bool_object(tmp, "present", false);
+ }
+ append_object_object(createStmt, "%{tablespace}s", tmp);
+
+ /* add the query */
+ Assert(IsA(node->query, Query));
+ append_string_object(createStmt, "AS %{query}s",
+ pg_get_querydef((Query *) node->query, false));
+
+ /* add a WITH NO DATA clause */
+ tmp = new_objtree_VA("WITH NO DATA", 1,
+ "present", ObjTypeBool,
+ node->into->skipData ? true : false);
+ append_object_object(createStmt, "%{with_no_data}s", tmp);
+
+ relation_close(relation, AccessShareLock);
+
+ return createStmt;
+}
+
+/*
+ * Deparse a CreateTrigStmt (CREATE TRIGGER)
+ *
+ * Given a trigger OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ */
+static ObjTree *
+deparse_CreateTrigStmt(Oid objectId, Node *parsetree)
+{
+ CreateTrigStmt *node = (CreateTrigStmt *) parsetree;
+ Relation pg_trigger;
+ HeapTuple trigTup;
+ Form_pg_trigger trigForm;
+ ObjTree *trigger;
+ ObjTree *tmpobj;
+ int tgnargs;
+ List *list;
+ List *events;
+
+ pg_trigger = table_open(TriggerRelationId, AccessShareLock);
+
+ trigTup = get_catalog_object_by_oid(pg_trigger, Anum_pg_trigger_oid, objectId);
+ trigForm = (Form_pg_trigger) GETSTRUCT(trigTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{constraint}s TRIGGER %{name}I %{time}s %{events: OR }s ON
+ * %{relation}D %{from_table}s %{constraint_attrs: }s FOR EACH
+ * %{for_each}s %{when}s EXECUTE PROCEDURE %{function}s
+ */
+ trigger = new_objtree("CREATE");
+
+ append_string_object(trigger, "%{constraint}s",
+ node->isconstraint ? "CONSTRAINT" : "");
+
+ append_string_object(trigger, "TRIGGER %{name}I", node->trigname);
+
+ if (node->timing == TRIGGER_TYPE_BEFORE)
+ append_string_object(trigger, "%{time}s", "BEFORE");
+ else if (node->timing == TRIGGER_TYPE_AFTER)
+ append_string_object(trigger, "%{time}s", "AFTER");
+ else if (node->timing == TRIGGER_TYPE_INSTEAD)
+ append_string_object(trigger, "%{time}s", "INSTEAD OF");
+ else
+ elog(ERROR, "unrecognized trigger timing type %d", node->timing);
+
+ /*
+ * Decode the events that the trigger fires for. The output is a list; in
+ * most cases it will just be a string with the event name, but when
+ * there's an UPDATE with a list of columns, we return a JSON object.
+ */
+ events = NIL;
+ if (node->events & TRIGGER_TYPE_INSERT)
+ events = lappend(events, new_string_object("INSERT"));
+ if (node->events & TRIGGER_TYPE_DELETE)
+ events = lappend(events, new_string_object("DELETE"));
+ if (node->events & TRIGGER_TYPE_TRUNCATE)
+ events = lappend(events, new_string_object("TRUNCATE"));
+ if (node->events & TRIGGER_TYPE_UPDATE)
+ {
+ if (node->columns == NIL)
+ {
+ events = lappend(events, new_string_object("UPDATE"));
+ }
+ else
+ {
+ ObjTree *update;
+ ListCell *cell;
+ List *cols = NIL;
+
+ /*
+ * Currently only UPDATE OF can be objects in the output JSON, but
+ * we add a "kind" element so that user code can distinguish
+ * possible future new event types.
+ */
+ update = new_objtree_VA("UPDATE OF", 1,
+ "kind", ObjTypeString, "update_of");
+
+ foreach(cell, node->columns)
+ {
+ char *colname = strVal(lfirst(cell));
+
+ cols = lappend(cols, new_string_object(colname));
+ }
+
+ append_array_object(update, "%{columns:, }I", cols);
+
+ events = lappend(events, new_object_object(update));
+ }
+ }
+ append_array_object(trigger, "%{events: OR }s", events);
+
+ tmpobj = new_objtree_for_qualname_id(RelationRelationId,
+ trigForm->tgrelid);
+ append_object_object(trigger, "ON %{relation}D", tmpobj);
+
+ tmpobj = new_objtree_VA("FROM", 0);
+ if (trigForm->tgconstrrelid)
+ {
+ ObjTree *rel;
+
+ rel = new_objtree_for_qualname_id(RelationRelationId,
+ trigForm->tgconstrrelid);
+ append_object_object(tmpobj, "%{relation}D", rel);
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(trigger, "%{from_table}s", tmpobj);
+
+ list = NIL;
+ if (node->deferrable)
+ list = lappend(list,
+ new_string_object("DEFERRABLE"));
+ if (node->initdeferred)
+ list = lappend(list,
+ new_string_object("INITIALLY DEFERRED"));
+ append_array_object(trigger, "%{constraint_attrs: }s", list);
+
+ append_string_object(trigger, "FOR EACH %{for_each}s",
+ node->row ? "ROW" : "STATEMENT");
+
+ tmpobj = new_objtree_VA("WHEN", 0);
+ if (node->whenClause)
+ {
+ Node *whenClause;
+ Datum value;
+ bool isnull;
+
+ value = fastgetattr(trigTup, Anum_pg_trigger_tgqual,
+ RelationGetDescr(pg_trigger), &isnull);
+ if (isnull)
+ elog(ERROR, "bogus NULL tgqual");
+
+ whenClause = stringToNode(TextDatumGetCString(value));
+ append_string_object(tmpobj, "(%{clause}s)",
+ pg_get_trigger_whenclause(trigForm,
+ whenClause,
+ false));
+ }
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(trigger, "%{when}s", tmpobj);
+
+ tmpobj = new_objtree_VA("%{funcname}D", 1,
+ "funcname", ObjTypeObject,
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ trigForm->tgfoid));
+ list = NIL;
+ tgnargs = trigForm->tgnargs;
+ if (tgnargs > 0)
+ {
+ bytea *tgargs;
+ char *argstr;
+ bool isnull;
+ int findx;
+ int lentgargs;
+ char *p;
+
+ tgargs = DatumGetByteaP(fastgetattr(trigTup,
+ Anum_pg_trigger_tgargs,
+ RelationGetDescr(pg_trigger),
+ &isnull));
+ if (isnull)
+ elog(ERROR, "invalid NULL tgargs");
+ argstr = (char *) VARDATA(tgargs);
+ lentgargs = VARSIZE_ANY_EXHDR(tgargs);
+
+ p = argstr;
+ for (findx = 0; findx < tgnargs; findx++)
+ {
+ size_t tlen;
+
+ /* Verify that the argument encoding is correct */
+ tlen = strlen(p);
+ if (p + tlen >= argstr + lentgargs)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("invalid argument string (%s) for trigger \"%s\"",
+ argstr, NameStr(trigForm->tgname))));
+
+ list = lappend(list, new_string_object(p));
+
+ p += tlen + 1;
+ }
+ }
+
+ append_format_string(tmpobj, "(");
+ append_array_object(tmpobj, "%{args:, }L", list); /* might be NIL */
+ append_format_string(tmpobj, ")");
+
+ append_object_object(trigger, "EXECUTE PROCEDURE %{function}s", tmpobj);
+
+ table_close(pg_trigger, AccessShareLock);
+
+ return trigger;
+}
+
+/*
+ * Deparse a CreateUserMappingStmt (CREATE USER MAPPING)
+ *
+ * Given a User Mapping OID and the parse tree that created it,
+ * return an ObjTree representing the CREATE USER MAPPING command.
+ */
+static ObjTree *
+deparse_CreateUserMappingStmt(Oid objectId, Node *parsetree)
+{
+ CreateUserMappingStmt *node = (CreateUserMappingStmt *) parsetree;
+ ObjTree *createStmt;
+ Relation rel;
+ HeapTuple tp;
+ Form_pg_user_mapping form;
+ ForeignServer *server;
+
+ rel = table_open(UserMappingRelationId, RowExclusiveLock);
+
+ /*
+ * Lookup up object in the catalog, so we don't have to deal with
+ * current_user and such.
+ */
+ tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tp))
+ elog(ERROR, "cache lookup failed for user mapping %u", objectId);
+
+ form = (Form_pg_user_mapping) GETSTRUCT(tp);
+
+ server = GetForeignServer(form->umserver);
+
+ createStmt = new_objtree("CREATE USER MAPPING ");
+
+ append_object_object(createStmt, "FOR %{role}R", new_objtree_for_role_id(form->umuser));
+
+ append_string_object(createStmt, "SERVER %{server}I", server->servername);
+
+ /* add an OPTIONS clause, if any */
+ append_object_object(createStmt, "%{generic_options}s",
+ deparse_FdwOptions(node->options, NULL));
+
+ ReleaseSysCache(tp);
+ table_close(rel, RowExclusiveLock);
+ return createStmt;
+}
+
+/*
+ * deparse_AlterUserMapping
+ *
+ * Given a User Mapping OID and the parse tree that created it,
+ * return the JSON blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterUserMappingStmt(Oid objectId, Node *parsetree)
+{
+ AlterUserMappingStmt *node = (AlterUserMappingStmt *) parsetree;
+ ObjTree *alterStmt;
+ Relation rel;
+ HeapTuple tp;
+ Form_pg_user_mapping form;
+ ForeignServer *server;
+
+ rel = table_open(UserMappingRelationId, RowExclusiveLock);
+
+ /*
+ * Lookup up object in the catalog, so we don't have to deal with
+ * current_user and such.
+ */
+
+ tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tp))
+ elog(ERROR, "cache lookup failed for user mapping %u", objectId);
+
+ form = (Form_pg_user_mapping) GETSTRUCT(tp);
+
+ /*
+ * Lookup up object in the catalog, so we don't have to deal with
+ * current_user and such.
+ */
+
+ tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tp))
+ elog(ERROR, "cache lookup failed for user mapping %u", objectId);
+
+ form = (Form_pg_user_mapping) GETSTRUCT(tp);
+
+
+ server = GetForeignServer(form->umserver);
+
+ alterStmt = new_objtree("ALTER USER MAPPING");
+
+ append_object_object(alterStmt, "FOR %{role}R", new_objtree_for_role_id(form->umuser));
+
+ append_string_object(alterStmt, "SERVER %{server}I", server->servername);
+
+ /* add an OPTIONS clause, if any */
+ append_object_object(alterStmt, "%{generic_options}s",
+ deparse_FdwOptions(node->options, NULL));
+
+ ReleaseSysCache(tp);
+ table_close(rel, RowExclusiveLock);
+ return alterStmt;
+}
+
+/*
+ * Deparse an AlterStatsStmt (ALTER STATISTICS)
+ *
+ * Given a alter statistics OID and the parse tree that created it,
+ * return the JSON blob representing the alter command.
+ */
+static ObjTree *
+deparse_AlterStatsStmt(Oid objectId, Node *parsetree)
+{
+ AlterStatsStmt *node = (AlterStatsStmt *) parsetree;
+ ObjTree *alterStat;
+ HeapTuple tp;
+ Form_pg_statistic_ext statform;
+
+ if (!node->stxstattarget)
+ return NULL;
+
+ alterStat = new_objtree("ALTER STATISTICS");
+
+ /* Lookup up object in the catalog */
+ tp = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(tp))
+ elog(ERROR, "cache lookup failed for statistic %u", objectId);
+
+ statform = (Form_pg_statistic_ext) GETSTRUCT(tp);
+
+ append_object_object(alterStat, "%{identity}D",
+ new_objtree_for_qualname(statform->stxnamespace,
+ NameStr(statform->stxname)));
+
+ append_float_object(alterStat, "SET STATISTICS %{target}n", node->stxstattarget);
+
+ ReleaseSysCache(tp);
+ return alterStat;
+}
+
+/*
+ * Deparse a RefreshMatViewStmt (REFRESH MATERIALIZED VIEW)
+ *
+ * Given a materialized view OID and the parse tree that created it, return an
+ * ObjTree representing the refresh command.
+ */
+static ObjTree *
+deparse_RefreshMatViewStmt(Oid objectId, Node *parsetree)
+{
+ RefreshMatViewStmt *node = (RefreshMatViewStmt *) parsetree;
+ ObjTree *refreshStmt;
+ ObjTree *tmp;
+
+ refreshStmt = new_objtree_VA("REFRESH MATERIALIZED VIEW", 0);
+
+ /* Add a CONCURRENTLY clause */
+ append_string_object(refreshStmt, "%{concurrently}s",
+ node->concurrent ? "CONCURRENTLY" : "");
+ /* Add the matview name */
+ append_object_object(refreshStmt, "%{identity}D",
+ new_objtree_for_qualname_id(RelationRelationId,
+ objectId));
+ /* Add a WITH NO DATA clause */
+ tmp = new_objtree_VA("WITH NO DATA", 1,
+ "present", ObjTypeBool,
+ node->skipData ? true : false);
+ append_object_object(refreshStmt, "%{with_no_data}s", tmp);
+
+ return refreshStmt;
+}
+
+/*
+ * Deparse DefElems, as used e.g. by ALTER COLUMN ... SET, into a list of SET
+ * (...) or RESET (...) contents.
+ */
+static ObjTree *
+deparse_DefElem(DefElem *elem, bool is_reset)
+{
+ ObjTree *set;
+ ObjTree *optname;
+
+ set = new_objtree("");
+ optname = new_objtree("");
+
+ if (elem->defnamespace != NULL)
+ append_string_object(optname, "%{schema}I.", elem->defnamespace);
+
+ append_string_object(optname, "%{label}I", elem->defname);
+
+ append_object_object(set, "%{label}s", optname);
+
+ if (!is_reset)
+ append_string_object(set, " = %{value}L",
+ elem->arg ? defGetString(elem) :
+ defGetBoolean(elem) ? "TRUE" : "FALSE");
+
+ return set;
+}
+
+/*
+ * Handle deparsing of DROP commands.
+ */
+char *
+deparse_drop_command(const char *objidentity, const char *objecttype,
+ DropBehavior behavior)
+{
+ StringInfoData str;
+ char *command;
+ char *identity = (char *) objidentity;
+ char *fmt;
+ ObjTree *stmt;
+ ObjTree *stmt2;
+ Jsonb *jsonb;
+
+ initStringInfo(&str);
+
+ fmt = psprintf("DROP %s IF EXISTS %%{objidentity}s", objecttype);
+
+ stmt = new_objtree_VA(fmt, 1, "objidentity", ObjTypeString, identity);
+ stmt2 = new_objtree_VA("CASCADE", 1,
+ "present", ObjTypeBool, behavior == DROP_CASCADE);
+
+ append_object_object(stmt, "%{cascade}s", stmt2);
+
+ jsonb = objtree_to_jsonb(stmt);
+ command = JsonbToCString(&str, &jsonb->root, JSONB_ESTIMATED_LEN);
+
+ return command;
+}
+
+/*
+ * Handle deparsing setting of Function
+ */
+static ObjTree *
+deparse_FunctionSet(VariableSetKind kind, char *name, char *value)
+{
+ ObjTree *obj;
+
+ if (kind == VAR_RESET_ALL)
+ {
+ obj = new_objtree("RESET ALL");
+ }
+ else if (value != NULL)
+ {
+ obj = new_objtree_VA("SET %{set_name}I", 1,
+ "set_name", ObjTypeString, name);
+
+ /*
+ * Some GUC variable names are 'LIST' type and hence must not be
+ * quoted.
+ */
+ if (GetConfigOptionFlags(name, true) & GUC_LIST_QUOTE)
+ append_string_object(obj, "TO %{set_value}s", value);
+ else
+ append_string_object(obj, "TO %{set_value}L", value);
+ }
+ else
+ {
+ obj = new_objtree("RESET");
+ append_string_object(obj, "%{set_name}I", name);
+ }
+
+ return obj;
+}
+
+/*
+ * Deparse an IndexStmt.
+ *
+ * Given an index OID and the parse tree that created it, return an ObjTree
+ * representing the creation command.
+ *
+ * If the index corresponds to a constraint, NULL is returned.
+ */
+static ObjTree *
+deparse_IndexStmt(Oid objectId, Node *parsetree)
+{
+ IndexStmt *node = (IndexStmt *) parsetree;
+ ObjTree *indexStmt;
+ ObjTree *tmpobj;
+ Relation idxrel;
+ Relation heaprel;
+ char *index_am;
+ char *definition;
+ char *reloptions;
+ char *tablespace;
+ char *whereClause;
+
+ if (node->primary || node->isconstraint)
+ {
+ /*
+ * Indexes for PRIMARY KEY and other constraints are output
+ * separately; return empty here.
+ */
+ return NULL;
+ }
+
+ idxrel = relation_open(objectId, AccessShareLock);
+ heaprel = relation_open(idxrel->rd_index->indrelid, AccessShareLock);
+
+ pg_get_indexdef_detailed(objectId,
+ &index_am, &definition, &reloptions,
+ &tablespace, &whereClause);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{unique}s INDEX %{concurrently}s %{if_not_exists}s %{name}I ON
+ * %{table}D USING %{index_am}s (%{definition}s) %{with}s %{tablespace}s
+ * %{where_clause}s
+ */
+ indexStmt = new_objtree("CREATE");
+
+ append_string_object(indexStmt, "%{unique}s",
+ node->unique ? "UNIQUE" : "");
+
+ append_format_string(indexStmt, "INDEX");
+
+ append_string_object(indexStmt, "%{concurrently}s",
+ node->concurrent ? "CONCURRENTLY" : "");
+
+ append_string_object(indexStmt, "%{if_not_exists}s",
+ node->if_not_exists ? "IF NOT EXISTS" : "");
+
+ append_string_object(indexStmt, "%{name}I",
+ RelationGetRelationName(idxrel));
+
+ append_object_object(indexStmt, "ON %{table}D",
+ new_objtree_for_qualname(heaprel->rd_rel->relnamespace,
+ RelationGetRelationName(heaprel)));
+
+ append_string_object(indexStmt, "USING %{index_am}s", index_am);
+
+ append_string_object(indexStmt, "(%{definition}s)", definition);
+
+ /* reloptions */
+ tmpobj = new_objtree("WITH");
+ if (reloptions)
+ append_string_object(tmpobj, "(%{opts}s)", reloptions);
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(indexStmt, "%{with}s", tmpobj);
+
+ /* tablespace */
+ tmpobj = new_objtree("TABLESPACE");
+ if (tablespace)
+ append_string_object(tmpobj, "%{tablespace}s", tablespace);
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(indexStmt, "%{tablespace}s", tmpobj);
+
+ /* WHERE clause */
+ tmpobj = new_objtree("WHERE");
+ if (whereClause)
+ append_string_object(tmpobj, "%{where}s", whereClause);
+ else
+ append_bool_object(tmpobj, "present", false);
+ append_object_object(indexStmt, "%{where_clause}s", tmpobj);
+
+ table_close(idxrel, AccessShareLock);
+ table_close(heaprel, AccessShareLock);
+
+ return indexStmt;
+}
+
+/*
+ * Deparse the INHERITS relations.
+ *
+ * Given a table OID, return a schema-qualified table list representing
+ * the parent tables.
+ */
+static List *
+deparse_InhRelations(Oid objectId)
+{
+ List *parents = NIL;
+ Relation inhRel;
+ SysScanDesc scan;
+ ScanKeyData key;
+ HeapTuple tuple;
+
+ inhRel = table_open(InheritsRelationId, RowExclusiveLock);
+
+ ScanKeyInit(&key,
+ Anum_pg_inherits_inhrelid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(objectId));
+
+ scan = systable_beginscan(inhRel, InheritsRelidSeqnoIndexId,
+ true, NULL, 1, &key);
+
+ while (HeapTupleIsValid(tuple = systable_getnext(scan)))
+ {
+ ObjTree *parent;
+ Form_pg_inherits formInh = (Form_pg_inherits) GETSTRUCT(tuple);
+
+ parent = new_objtree_for_qualname_id(RelationRelationId,
+ formInh->inhparent);
+ parents = lappend(parents, new_object_object(parent));
+ }
+
+ systable_endscan(scan);
+ table_close(inhRel, RowExclusiveLock);
+
+ return parents;
+}
+
+/*
+ * Deparse the ON COMMIT ... clause for CREATE ... TEMPORARY ...
+ */
+static ObjTree *
+deparse_OnCommitClause(OnCommitAction option)
+{
+ ObjTree *oncommit;
+
+ oncommit = new_objtree("ON COMMIT");
+ switch (option)
+ {
+ case ONCOMMIT_DROP:
+ append_string_object(oncommit, "%{on_commit_value}s", "DROP");
+ break;
+
+ case ONCOMMIT_DELETE_ROWS:
+ append_string_object(oncommit, "%{on_commit_value}s", "DELETE ROWS");
+ break;
+
+ case ONCOMMIT_PRESERVE_ROWS:
+ append_string_object(oncommit, "%{on_commit_value}s", "PRESERVE ROWS");
+ break;
+
+ case ONCOMMIT_NOOP:
+ append_null_object(oncommit, "%{on_commit_value}s");
+ append_bool_object(oncommit, "present", false);
+ break;
+ }
+
+ return oncommit;
+}
+
+/*
+ * Deparse a RenameStmt.
+ */
+static ObjTree *
+deparse_RenameStmt(ObjectAddress address, Node *parsetree)
+{
+ RenameStmt *node = (RenameStmt *) parsetree;
+ ObjTree *renameStmt;
+ char *fmtstr;
+ const char *objtype;
+ Relation relation;
+ Oid schemaId;
+
+ /*
+ * In an ALTER .. RENAME command, we don't have the original name of the
+ * object in system catalogs: since we inspect them after the command has
+ * executed, the old name is already gone. Therefore, we extract it from
+ * the parse node. Note we still extract the schema name from the catalog
+ * (it might not be present in the parse node); it cannot possibly have
+ * changed anyway.
+ */
+ switch (node->renameType)
+ {
+ case OBJECT_TABLE:
+ case OBJECT_INDEX:
+ case OBJECT_SEQUENCE:
+ case OBJECT_VIEW:
+ case OBJECT_MATVIEW:
+
+ /*
+ * Verbose syntax
+ *
+ * ALTER %s %{if_exists}s %{identity}D RENAME TO %{newname}I
+ */
+ objtype = stringify_objtype(node->renameType);
+ fmtstr = psprintf("ALTER %s", objtype);
+ relation = relation_open(address.objectId, AccessShareLock);
+ schemaId = RelationGetNamespace(relation);
+ renameStmt = new_objtree_VA(fmtstr, 0);
+ append_string_object(renameStmt, "%{if_exists}s",
+ node->missing_ok ? "IF EXISTS" : "");
+ append_object_object(renameStmt, "%{identity}D",
+ new_objtree_for_qualname(schemaId,
+ node->relation->relname));
+ append_string_object(renameStmt, "RENAME TO %{newname}I",
+ node->newname);
+ relation_close(relation, AccessShareLock);
+ break;
+
+ case OBJECT_POLICY:
+ {
+ HeapTuple polTup;
+ Form_pg_policy polForm;
+ Relation pg_policy;
+ ScanKeyData key;
+ SysScanDesc scan;
+
+ pg_policy = relation_open(PolicyRelationId, AccessShareLock);
+ ScanKeyInit(&key, Anum_pg_policy_oid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(address.objectId));
+ scan = systable_beginscan(pg_policy, PolicyOidIndexId, true,
+ NULL, 1, &key);
+ polTup = systable_getnext(scan);
+ if (!HeapTupleIsValid(polTup))
+ elog(ERROR, "cache lookup failed for policy %u", address.objectId);
+ polForm = (Form_pg_policy) GETSTRUCT(polTup);
+
+ renameStmt = new_objtree_VA("ALTER POLICY", 0);
+ append_string_object(renameStmt, "%{if_exists}s",
+ node->missing_ok ? "IF EXISTS" : "");
+ append_string_object(renameStmt, "%{policyname}I", node->subname);
+ append_object_object(renameStmt, "ON %{identity}D",
+ new_objtree_for_qualname_id(RelationRelationId,
+ polForm->polrelid));
+ append_string_object(renameStmt, "RENAME TO %{newname}I",
+ node->newname);
+ systable_endscan(scan);
+ relation_close(pg_policy, AccessShareLock);
+
+ }
+ break;
+
+ case OBJECT_ATTRIBUTE:
+ case OBJECT_COLUMN:
+ relation = relation_open(address.objectId, AccessShareLock);
+ schemaId = RelationGetNamespace(relation);
+
+ /*
+ * Verbose syntax
+ *
+ * Composite types: ALTER objtype %{identity}D RENAME ATTRIBUTE
+ * %{colname}I TO %{newname}I %{cascade}s
+ *
+ * Normal types: ALTER objtype %{if_exists}s %%{identity}D RENAME
+ * COLUMN %{colname}I TO %{newname}I
+ */
+
+ if (node->renameType == OBJECT_ATTRIBUTE)
+ {
+ renameStmt = new_objtree("ALTER TYPE");
+ append_object_object(renameStmt, "%{identity}D",
+ new_objtree_for_qualname(schemaId,
+ node->relation->relname));
+ fmtstr = psprintf("RENAME ATTRIBUTE %%{colname}I");
+ }
+ else
+ {
+ objtype = stringify_objtype(node->relationType);
+ fmtstr = psprintf("ALTER %s", objtype);
+ renameStmt = new_objtree(fmtstr);
+
+ /* Composite types do not support IF EXISTS */
+ if (node->renameType == OBJECT_COLUMN)
+ append_string_object(renameStmt, "%{if_exists}s",
+ node->missing_ok ? "IF EXISTS" : "");
+
+ append_object_object(renameStmt, "%{identity}D",
+ new_objtree_for_qualname(schemaId,
+ node->relation->relname));
+ fmtstr = psprintf("RENAME COLUMN %%{colname}I");
+ }
+
+ append_string_object(renameStmt, fmtstr, node->subname);
+ append_string_object(renameStmt, "TO %{newname}I", node->newname);
+
+ if (node->renameType == OBJECT_ATTRIBUTE)
+ append_object_object(renameStmt, "%{cascade}s",
+ new_objtree_VA("CASCADE", 1,
+ "present", ObjTypeBool,
+ node->behavior == DROP_CASCADE));
+
+ relation_close(relation, AccessShareLock);
+
+ break;
+ case OBJECT_FUNCTION:
+ {
+ char *ident;
+ HeapTuple proctup;
+ Form_pg_proc procform;
+ List *identity;
+
+ Assert(IsA(node->object, ObjectWithArgs));
+ identity = ((ObjectWithArgs *) node->object)->objname;
+
+ proctup = SearchSysCache1(PROCOID,
+ ObjectIdGetDatum(address.objectId));
+ if (!HeapTupleIsValid(proctup))
+ elog(ERROR, "cache lookup failed for procedure %u",
+ address.objectId);
+ procform = (Form_pg_proc) GETSTRUCT(proctup);
+
+ /* XXX does this work for ordered-set aggregates? */
+ ident = psprintf("%s%s",
+ quote_qualified_identifier(get_namespace_name(procform->pronamespace),
+ strVal(llast(identity))),
+ format_procedure_args(address.objectId, true));
+
+ fmtstr = psprintf("ALTER %s %%{identity}s RENAME TO %%{newname}I",
+ stringify_objtype(node->renameType));
+ renameStmt = new_objtree_VA(fmtstr, 2,
+ "identity", ObjTypeString, ident,
+ "newname", ObjTypeString, node->newname);
+
+ ReleaseSysCache(proctup);
+ }
+ break;
+
+ case OBJECT_OPCLASS:
+ {
+ HeapTuple opcTup;
+ Form_pg_opclass opcForm;
+ List *oldnames;
+ char *schemaname;
+ char *opcname;
+ char *fmt;
+
+ fmt = psprintf("ALTER %s %%{identity}D USING %%{index_method}s RENAME TO %%{newname}I",
+ stringify_objtype(node->renameType));
+
+ opcTup = SearchSysCache1(CLAOID, ObjectIdGetDatum(address.objectId));
+ if (!HeapTupleIsValid(opcTup))
+ elog(ERROR, "cache lookup failed for opclass with OID %u",
+ address.objectId);
+
+ opcForm = (Form_pg_opclass) GETSTRUCT(opcTup);
+
+ oldnames = list_copy_tail((List *) node->object, 1);
+
+ /* Deconstruct the name list */
+ DeconstructQualifiedName(oldnames, &schemaname, &opcname);
+
+ renameStmt = new_objtree_VA(fmt, 3,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(opcForm->opcnamespace,
+ opcname),
+ "index_method", ObjTypeString,
+ get_am_name(opcForm->opcmethod),
+ "newname", ObjTypeString, node->newname);
+
+ ReleaseSysCache(opcTup);
+ }
+ break;
+
+ case OBJECT_OPFAMILY:
+ {
+ HeapTuple opfTup;
+ HeapTuple amTup;
+ Form_pg_opfamily opfForm;
+ Form_pg_am amForm;
+ List *oldnames;
+ char *schemaname;
+ char *opfname;
+ char *fmt;
+
+ fmt = psprintf("ALTER %s %%{identity}D USING %%{index_method}s RENAME TO %%{newname}I",
+ stringify_objtype(node->renameType));
+
+ opfTup = SearchSysCache1(OPFAMILYOID, address.objectId);
+ if (!HeapTupleIsValid(opfTup))
+ elog(ERROR, "cache lookup failed for operator family with OID %u",
+ address.objectId);
+ opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
+
+ amTup = SearchSysCache1(AMOID, ObjectIdGetDatum(opfForm->opfmethod));
+ if (!HeapTupleIsValid(amTup))
+ elog(ERROR, "cache lookup failed for access method %u",
+ opfForm->opfmethod);
+
+ amForm = (Form_pg_am) GETSTRUCT(amTup);
+
+ oldnames = list_copy_tail((List *) node->object, 1);
+
+ /* Deconstruct the name list */
+ DeconstructQualifiedName(oldnames, &schemaname, &opfname);
+
+ renameStmt = new_objtree_VA(fmt, 3,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname(opfForm->opfnamespace,
+ opfname),
+ "index_method", ObjTypeString,
+ NameStr(amForm->amname),
+ "newname", ObjTypeString, node->newname);
+
+ ReleaseSysCache(amTup);
+ ReleaseSysCache(opfTup);
+ }
+ break;
+
+ case OBJECT_SCHEMA:
+ renameStmt =
+ new_objtree_VA("ALTER SCHEMA %{identity}I RENAME TO %{newname}I", 2,
+ "identity", ObjTypeString, node->subname,
+ "newname", ObjTypeString, node->newname);
+ break;
+
+ case OBJECT_FDW:
+ case OBJECT_FOREIGN_SERVER:
+ {
+ String *identity = castNode(String, node->object);
+
+ fmtstr = psprintf("ALTER %s %%{identity}s RENAME TO %%{newname}I",
+ stringify_objtype(node->renameType));
+ renameStmt =
+ new_objtree_VA(fmtstr, 2,
+ "identity", ObjTypeString, strVal(identity),
+ "newname", ObjTypeString, node->newname);
+ }
+ break;
+
+ case OBJECT_RULE:
+ {
+ HeapTuple rewrTup;
+ Form_pg_rewrite rewrForm;
+ Relation pg_rewrite;
+
+ pg_rewrite = relation_open(RewriteRelationId, AccessShareLock);
+ rewrTup = get_catalog_object_by_oid(pg_rewrite, Anum_pg_rewrite_oid, address.objectId);
+ rewrForm = (Form_pg_rewrite) GETSTRUCT(rewrTup);
+
+ renameStmt = new_objtree_VA("ALTER RULE %{rulename}I ON %{identity}D RENAME TO %{newname}I",
+ 3,
+ "rulename", ObjTypeString, node->subname,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ rewrForm->ev_class),
+ "newname", ObjTypeString, node->newname);
+ relation_close(pg_rewrite, AccessShareLock);
+ }
+ break;
+
+ case OBJECT_TRIGGER:
+ {
+ HeapTuple trigTup;
+ Form_pg_trigger trigForm;
+ Relation pg_trigger;
+
+ pg_trigger = relation_open(TriggerRelationId, AccessShareLock);
+ trigTup = get_catalog_object_by_oid(pg_trigger, get_object_attnum_oid(address.classId), address.objectId);
+ trigForm = (Form_pg_trigger) GETSTRUCT(trigTup);
+
+ renameStmt = new_objtree_VA("ALTER TRIGGER %{triggername}I ON %{identity}D RENAME TO %{newname}I",
+ 3,
+ "triggername", ObjTypeString, node->subname,
+ "identity", ObjTypeObject,
+ new_objtree_for_qualname_id(RelationRelationId,
+ trigForm->tgrelid),
+ "newname", ObjTypeString, node->newname);
+
+ relation_close(pg_trigger, AccessShareLock);
+ }
+ break;
+
+ case OBJECT_COLLATION:
+ case OBJECT_STATISTIC_EXT:
+ case OBJECT_TYPE:
+ case OBJECT_CONVERSION:
+ case OBJECT_DOMAIN:
+ case OBJECT_TSDICTIONARY:
+ case OBJECT_TSPARSER:
+ case OBJECT_TSTEMPLATE:
+ case OBJECT_TSCONFIGURATION:
+ {
+ HeapTuple objTup;
+ Relation catalog;
+ Datum objnsp;
+ bool isnull;
+ AttrNumber Anum_namespace;
+ List *identity = castNode(List, node->object);
+ char *fmtstring;
+
+ /* Obtain object tuple */
+ catalog = relation_open(address.classId, AccessShareLock);
+ objTup = get_catalog_object_by_oid(catalog, get_object_attnum_oid(address.classId), address.objectId);
+
+ /* Obtain namespace */
+ Anum_namespace = get_object_attnum_namespace(address.classId);
+ objnsp = heap_getattr(objTup, Anum_namespace,
+ RelationGetDescr(catalog), &isnull);
+ if (isnull)
+ elog(ERROR, "invalid NULL namespace");
+
+ objtype = stringify_objtype(node->renameType);
+ fmtstring = psprintf("ALTER %s", objtype);
+
+ renameStmt = new_objtree_VA(fmtstring,
+ 0);
+ append_object_object(renameStmt, "%{identity}D",
+ new_objtree_for_qualname(DatumGetObjectId(objnsp),
+ strVal(llast(identity))));
+
+ append_string_object(renameStmt, "RENAME TO %{newname}I",
+ node->newname);
+ relation_close(catalog, AccessShareLock);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unsupported object type %d", node->renameType);
+ }
+
+ return renameStmt;
+}
+
+/*
+ * Deparse a AlterObjectDependsStmt (ALTER ... DEPENDS ON ...).
+ */
+static ObjTree *
+deparse_AlterDependStmt(Oid objectId, Node *parsetree)
+{
+ AlterObjectDependsStmt *node = (AlterObjectDependsStmt *) parsetree;
+ ObjTree *alterDependeStmt = NULL;
+
+
+ if (node->objectType == OBJECT_INDEX)
+ {
+ Relation relation = relation_open(objectId, AccessShareLock);
+ ObjTree *qualified;
+
+ alterDependeStmt = new_objtree("ALTER INDEX");
+
+ qualified = new_objtree_for_qualname(relation->rd_rel->relnamespace,
+ node->relation->relname);
+ append_object_object(alterDependeStmt, "%{identity}D", qualified);
+ relation_close(relation, AccessShareLock);
+
+ append_string_object(alterDependeStmt, "%{NO}s",
+ node->remove ? "NO" : "");
+
+ append_format_string(alterDependeStmt, "DEPENDS ON EXTENSION");
+ append_string_object(alterDependeStmt, "%{newname}I", strVal(node->extname));
+ }
+ else
+ elog(LOG, "unrecognized node type in deparse command: %d",
+ (int) nodeTag(parsetree));
+
+ return alterDependeStmt;
+}
+
+/*
+ * Deparse the sequence CACHE option.
+ */
+static inline ObjElem *
+deparse_Seq_Cache(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table)
+{
+ ObjTree *cache;
+ char *tmpstr;
+ char *fmt;
+
+ tmpstr = psprintf(INT64_FORMAT, seqdata->seqcache);
+
+ if (alter_table)
+ fmt = "SET CACHE %{value}s";
+ else
+ fmt = "CACHE %{value}s";
+
+ cache = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "cache",
+ "value", ObjTypeString, tmpstr);
+
+ return new_object_object(cache);
+}
+
+/*
+ * Deparse the sequence CYCLE option.
+ */
+static inline ObjElem *
+deparse_Seq_Cycle(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table)
+{
+ ObjTree *cycle;
+ char *fmt;
+
+ if (alter_table)
+ fmt = "SET %{no}s CYCLE";
+ else
+ fmt = "%{no}s CYCLE";
+
+ cycle = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "cycle",
+ "no", ObjTypeString,
+ seqdata->seqcycle ? "" : "NO");
+ return new_object_object(cycle);
+}
+
+/*
+ * Deparse the sequence INCREMENT BY option.
+ */
+static inline ObjElem *
+deparse_Seq_IncrementBy(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table)
+{
+ ObjTree *incrementalby;
+ char *tmpstr;
+ char *fmt;
+
+ if (alter_table)
+ fmt = "SET INCREMENT BY %{value}s";
+ else
+ fmt = "INCREMENT BY %{value}s";
+
+ tmpstr = psprintf(INT64_FORMAT, seqdata->seqincrement);
+ incrementalby = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "seqincrement",
+ "value", ObjTypeString, tmpstr);
+ return new_object_object(incrementalby);
+}
+
+/*
+ * Deparse the sequence MAXVALUE option.
+ */
+static inline ObjElem *
+deparse_Seq_Maxvalue(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table)
+{
+ ObjTree *maxvalue;
+ char *tmpstr;
+ char *fmt;
+
+ if (alter_table)
+ fmt = "SET MAXVALUE %{value}s";
+ else
+ fmt = "MAXVALUE %{value}s";
+
+ tmpstr = psprintf(INT64_FORMAT, seqdata->seqmax);
+ maxvalue = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "maxvalue",
+ "value", ObjTypeString, tmpstr);
+ return new_object_object(maxvalue);
+}
+
+/*
+ * Deparse the sequence MINVALUE option.
+ */
+static inline ObjElem *
+deparse_Seq_Minvalue(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table)
+{
+ ObjTree *minvalue;
+ char *tmpstr;
+ char *fmt;
+
+ if (alter_table)
+ fmt = "SET MINVALUE %{value}s";
+ else
+ fmt = "MINVALUE %{value}s";
+
+ tmpstr = psprintf(INT64_FORMAT, seqdata->seqmin);
+ minvalue = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "minvalue",
+ "value", ObjTypeString, tmpstr);
+ return new_object_object(minvalue);
+}
+
+/*
+ * Deparse the sequence OWNED BY command.
+ */
+static ObjElem *
+deparse_Seq_OwnedBy(ObjTree *parent, Oid sequenceId, bool alter_table)
+{
+ ObjTree *ownedby = NULL;
+ Relation depRel;
+ SysScanDesc scan;
+ ScanKeyData keys[3];
+ HeapTuple tuple;
+
+ depRel = table_open(DependRelationId, AccessShareLock);
+ ScanKeyInit(&keys[0],
+ Anum_pg_depend_classid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(RelationRelationId));
+ ScanKeyInit(&keys[1],
+ Anum_pg_depend_objid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(sequenceId));
+ ScanKeyInit(&keys[2],
+ Anum_pg_depend_objsubid,
+ BTEqualStrategyNumber, F_INT4EQ,
+ Int32GetDatum(0));
+
+ scan = systable_beginscan(depRel, DependDependerIndexId, true,
+ NULL, 3, keys);
+ while (HeapTupleIsValid(tuple = systable_getnext(scan)))
+ {
+ Oid ownerId;
+ Form_pg_depend depform;
+ ObjTree *tmpobj;
+ char *colname;
+
+ depform = (Form_pg_depend) GETSTRUCT(tuple);
+
+ /* Only consider AUTO dependencies on pg_class */
+ if (depform->deptype != DEPENDENCY_AUTO)
+ continue;
+ if (depform->refclassid != RelationRelationId)
+ continue;
+ if (depform->refobjsubid <= 0)
+ continue;
+
+ ownerId = depform->refobjid;
+ colname = get_attname(ownerId, depform->refobjsubid, false);
+ if (colname == NULL)
+ continue;
+
+ tmpobj = new_objtree_for_qualname_id(RelationRelationId, ownerId);
+ append_string_object(tmpobj, "attrname", colname);
+ ownedby = new_objtree_VA("OWNED BY %{owner}D",
+ 2,
+ "clause", ObjTypeString, "owned",
+ "owner", ObjTypeObject, tmpobj);
+ }
+
+ systable_endscan(scan);
+ relation_close(depRel, AccessShareLock);
+
+ /*
+ * If there's no owner column, emit an empty OWNED BY element, set up so
+ * that it won't print anything.
+ */
+ if (!ownedby)
+ /* XXX this shouldn't happen ... */
+ ownedby = new_objtree_VA("OWNED BY %{owner}D",
+ 3,
+ "clause", ObjTypeString, "owned",
+ "owner", ObjTypeNull,
+ "present", ObjTypeBool, false);
+
+ return new_object_object(ownedby);
+}
+
+/*
+ * Deparse the sequence RESTART option.
+ */
+static inline ObjElem *
+deparse_Seq_Restart(ObjTree *parent, Form_pg_sequence_data seqdata)
+{
+ ObjTree *restart;
+ char *tmpstr;
+
+ tmpstr = psprintf(INT64_FORMAT, seqdata->last_value);
+ restart = new_objtree_VA("RESTART %{value}s", 2,
+ "clause", ObjTypeString, "restart",
+ "value", ObjTypeString, tmpstr);
+ return new_object_object(restart);
+}
+
+/*
+ * Deparse the sequence START WITH option.
+ */
+static inline ObjElem *
+deparse_Seq_Startwith(ObjTree *parent, Form_pg_sequence seqdata, bool alter_table)
+{
+ ObjTree *startwith;
+ char *tmpstr;
+ char *fmt;
+
+ if (alter_table)
+ fmt = "SET START WITH %{value}s";
+ else
+ fmt = "START WITH %{value}s";
+
+ tmpstr = psprintf(INT64_FORMAT, seqdata->seqstart);
+ startwith = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "start",
+ "value", ObjTypeString, tmpstr);
+ return new_object_object(startwith);
+}
+
+/*
+ * Deparse the type STORAGE option.
+ */
+static inline ObjElem *
+deparse_Type_Storage(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *storage;
+ char *tmpstr;
+ char *fmt;
+ char *str;
+
+ switch (typForm->typstorage)
+ {
+ case 'p':
+ str = "plain";
+ break;
+ case 'e':
+ str = "external";
+ break;
+ case 'x':
+ str = "extended";
+ break;
+ case 'm':
+ str = "main";
+ break;
+ default:
+ elog(ERROR, "invalid storage specifier %c", typForm->typstorage);
+ }
+
+ tmpstr = psprintf("%s", str);
+
+ fmt = "STORAGE = %{value}s";
+
+ storage = new_objtree_VA(fmt, 2,
+ "clause", ObjTypeString, "storage",
+ "value", ObjTypeString, tmpstr);
+
+ return new_object_object(storage);
+}
+
+/*
+ * Deparse the type RECEIVE option.
+ */
+static inline ObjElem *
+deparse_Type_Receive(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *receive;
+
+ receive = new_objtree_VA("RECEIVE=", 1,
+ "clause", ObjTypeString, "receive");
+ if (OidIsValid(typForm->typreceive))
+ append_object_object(receive, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typreceive));
+ else
+ append_bool_object(receive, "present", false);
+
+ return new_object_object(receive);
+}
+
+/*
+ * Deparse the type SEND option.
+ */
+static inline ObjElem *
+deparse_Type_Send(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *send;
+
+ send = new_objtree_VA("SEND=", 1,
+ "clause", ObjTypeString, "send");
+ if (OidIsValid(typForm->typsend))
+ append_object_object(send, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typsend));
+ else
+ append_bool_object(send, "present", false);
+
+ return new_object_object(send);
+}
+
+/*
+ * Deparse the type typmod_in option.
+ */
+static inline ObjElem *
+deparse_Type_Typmod_In(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *typmodin;
+
+ typmodin = new_objtree_VA("TYPMOD_IN=", 1,
+ "clause", ObjTypeString, "typmod_in");
+ if (OidIsValid(typForm->typmodin))
+ append_object_object(typmodin, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typmodin));
+ else
+ append_bool_object(typmodin, "present", false);
+
+ return new_object_object(typmodin);
+}
+
+
+/*
+ * Deparse the type typmod_out option.
+ */
+static inline ObjElem *
+deparse_Type_Typmod_Out(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *typmodout;
+
+ typmodout = new_objtree_VA("TYPMOD_OUT=", 1,
+ "clause", ObjTypeString, "typmod_out");
+ if (OidIsValid(typForm->typmodout))
+ append_object_object(typmodout, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typmodout));
+ else
+ append_bool_object(typmodout, "present", false);
+
+ return new_object_object(typmodout);
+}
+
+
+/*
+ * Deparse the type analyze option.
+ */
+static inline ObjElem *
+deparse_Type_Analyze(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *typanalyze;
+
+ typanalyze = new_objtree_VA("ANALYZE=", 1,
+ "clause", ObjTypeString, "analyze");
+ if (OidIsValid(typForm->typanalyze))
+ append_object_object(typanalyze, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typanalyze));
+ else
+ append_bool_object(typanalyze, "present", false);
+
+ return new_object_object(typanalyze);
+}
+
+/*
+ * Deparse the type subscript option.
+ */
+static inline ObjElem *
+deparse_Type_Subscript(ObjTree *parent, Form_pg_type typForm)
+{
+ ObjTree *typsubscript;
+
+ typsubscript = new_objtree_VA("SUBSCRIPT=", 1,
+ "clause", ObjTypeString, "subscript");
+ if (OidIsValid(typForm->typsubscript))
+ append_object_object(typsubscript, "%{procedure}D",
+ new_objtree_for_qualname_id(ProcedureRelationId,
+ typForm->typsubscript));
+ else
+ append_bool_object(typsubscript, "present", false);
+
+ return new_object_object(typsubscript);
+}
+
+/*
+ * Deparse a RuleStmt (CREATE RULE).
+ *
+ * Given a rule OID and the parse tree that created it, return an ObjTree
+ * representing the rule command.
+ */
+static ObjTree *
+deparse_RuleStmt(Oid objectId, Node *parsetree)
+{
+ RuleStmt *node = (RuleStmt *) parsetree;
+ ObjTree *ruleStmt;
+ ObjTree *tmp;
+ Relation pg_rewrite;
+ Form_pg_rewrite rewrForm;
+ HeapTuple rewrTup;
+ SysScanDesc scan;
+ ScanKeyData key;
+ Datum ev_qual;
+ Datum ev_actions;
+ bool isnull;
+ char *qual;
+ List *actions;
+ List *list;
+ ListCell *cell;
+
+ pg_rewrite = table_open(RewriteRelationId, AccessShareLock);
+ ScanKeyInit(&key,
+ Anum_pg_rewrite_oid,
+ BTEqualStrategyNumber,
+ F_OIDEQ, ObjectIdGetDatum(objectId));
+
+ scan = systable_beginscan(pg_rewrite, RewriteOidIndexId, true,
+ NULL, 1, &key);
+ rewrTup = systable_getnext(scan);
+ if (!HeapTupleIsValid(rewrTup))
+ elog(ERROR, "cache lookup failed for rewrite rule with oid %u",
+ objectId);
+
+ rewrForm = (Form_pg_rewrite) GETSTRUCT(rewrTup);
+
+ ruleStmt = new_objtree("CREATE RULE");
+
+ append_string_object(ruleStmt, "%{or_replace}s",
+ node->replace ? "OR REPLACE" : "");
+
+ append_string_object(ruleStmt, "%{identity}I",
+ node->rulename);
+
+ append_string_object(ruleStmt, "AS ON %{event}s",
+ node->event == CMD_SELECT ? "SELECT" :
+ node->event == CMD_UPDATE ? "UPDATE" :
+ node->event == CMD_DELETE ? "DELETE" :
+ node->event == CMD_INSERT ? "INSERT" : "XXX");
+ append_object_object(ruleStmt, "TO %{table}D",
+ new_objtree_for_qualname_id(RelationRelationId,
+ rewrForm->ev_class));
+
+ append_string_object(ruleStmt, "DO %{instead}s",
+ node->instead ? "INSTEAD" : "ALSO");
+
+ ev_qual = heap_getattr(rewrTup, Anum_pg_rewrite_ev_qual,
+ RelationGetDescr(pg_rewrite), &isnull);
+ ev_actions = heap_getattr(rewrTup, Anum_pg_rewrite_ev_action,
+ RelationGetDescr(pg_rewrite), &isnull);
+
+ pg_get_ruledef_detailed(ev_qual, ev_actions, &qual, &actions);
+
+ tmp = new_objtree_VA("WHERE %{clause}s", 0);
+
+ if (qual)
+ append_string_object(tmp, "clause", qual);
+ else
+ {
+ append_null_object(tmp, "clause");
+ append_bool_object(tmp, "present", false);
+ }
+
+ append_object_object(ruleStmt, "where_clause", tmp);
+
+ list = NIL;
+ foreach(cell, actions)
+ {
+ char *action = lfirst(cell);
+
+ list = lappend(list, new_string_object(action));
+ }
+ append_array_object(ruleStmt, "%{actions:, }s", list);
+
+ systable_endscan(scan);
+ table_close(pg_rewrite, AccessShareLock);
+
+ return ruleStmt;
+}
+
+/*
+ * Deparse a CreateTransformStmt (CREATE TRANSFORM).
+ *
+ * Given a transform OID and the parse tree that created it, return an ObjTree
+ * representing the CREATE TRANSFORM command.
+ */
+static ObjTree *
+deparse_CreateTransformStmt(Oid objectId, Node *parsetree)
+{
+ CreateTransformStmt *node = (CreateTransformStmt *) parsetree;
+ ObjTree *createTransform;
+ ObjTree *sign;
+ HeapTuple trfTup;
+ HeapTuple langTup;
+ HeapTuple procTup;
+ Form_pg_transform trfForm;
+ Form_pg_language langForm;
+ Form_pg_proc procForm;
+ int i;
+
+ /* Get the pg_transform tuple */
+ trfTup = SearchSysCache1(TRFOID, ObjectIdGetDatum(objectId));
+ if (!HeapTupleIsValid(trfTup))
+ elog(ERROR, "cache lookup failure for transform with OID %u",
+ objectId);
+ trfForm = (Form_pg_transform) GETSTRUCT(trfTup);
+
+ /* Get the corresponding pg_language tuple */
+ langTup = SearchSysCache1(LANGOID, trfForm->trflang);
+ if (!HeapTupleIsValid(langTup))
+ elog(ERROR, "cache lookup failure for language with OID %u",
+ trfForm->trflang);
+ langForm = (Form_pg_language) GETSTRUCT(langTup);
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{or_replace}s TRANSFORM FOR %{typename}D LANGUAGE %{language}I
+ * ( FROM SQL WITH FUNCTION %{signature}s, TO SQL WITH FUNCTION
+ * %{signature_tof}s )
+ */
+ createTransform = new_objtree("CREATE");
+
+ append_string_object(createTransform, "%{or_replace}s",
+ node->replace ? "OR REPLACE" : "");
+ append_object_object(createTransform, "TRANSFORM FOR %{typename}D",
+ new_objtree_for_qualname_id(TypeRelationId,
+ trfForm->trftype));
+ append_string_object(createTransform, "LANGUAGE %{language}I",
+ NameStr(langForm->lanname));
+
+ /* deparse the transform_element_list */
+ if (trfForm->trffromsql != InvalidOid)
+ {
+ List *params = NIL;
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{or_replace}s TRANSFORM FOR %{typename}D LANGUAGE
+ * %{language}I ( FROM SQL WITH FUNCTION %{signature}s )
+ */
+
+ /* Get the pg_proc tuple for the FROM FUNCTION */
+ procTup = SearchSysCache1(PROCOID, trfForm->trffromsql);
+ if (!HeapTupleIsValid(procTup))
+ elog(ERROR, "cache lookup failure for function with OID %u",
+ trfForm->trffromsql);
+ procForm = (Form_pg_proc) GETSTRUCT(procTup);
+
+ /*
+ * CREATE TRANSFORM does not change function signature so we can use
+ * catalog to get input type Oids.
+ */
+ for (i = 0; i < procForm->pronargs; i++)
+ {
+ ObjTree *paramobj = new_objtree("");
+
+ append_object_object(paramobj, "%{type}T",
+ new_objtree_for_type(procForm->proargtypes.values[i], -1));
+ params = lappend(params, new_object_object(paramobj));
+ }
+
+ sign = new_objtree("");
+
+ append_object_object(sign, "%{identity}D",
+ new_objtree_for_qualname(procForm->pronamespace,
+ NameStr(procForm->proname)));
+ append_array_object(sign, "(%{arguments:, }s)", params);
+
+ append_object_object(createTransform, "(FROM SQL WITH FUNCTION %{signature}s", sign);
+ ReleaseSysCache(procTup);
+ }
+ if (trfForm->trftosql != InvalidOid)
+ {
+ List *params = NIL;
+
+ /*
+ * Verbose syntax
+ *
+ * CREATE %{or_replace}s TRANSFORM FOR %{typename}D LANGUAGE
+ * %{language}I ( FROM SQL WITH FUNCTION %{signature}s, TO SQL WITH
+ * FUNCTION %{signature_tof}s )
+ *
+ * OR
+ *
+ * CREATE %{or_replace}s TRANSFORM FOR %{typename}D LANGUAGE
+ * %{language}I ( TO SQL WITH FUNCTION %{signature_tof}s )
+ */
+
+ /* Append a ',' if trffromsql is present, else append '(' */
+ append_string_object(createTransform, "%{comma}s",
+ trfForm->trffromsql != InvalidOid ? "," : "(");
+
+ /* Get the pg_proc tuple for the TO FUNCTION */
+ procTup = SearchSysCache1(PROCOID, trfForm->trftosql);
+ if (!HeapTupleIsValid(procTup))
+ elog(ERROR, "cache lookup failure for function with OID %u",
+ trfForm->trftosql);
+ procForm = (Form_pg_proc) GETSTRUCT(procTup);
+
+ /*
+ * CREATE TRANSFORM does not change function signature so we can use
+ * catalog to get input type Oids.
+ */
+ for (i = 0; i < procForm->pronargs; i++)
+ {
+ ObjTree *paramobj = new_objtree("");
+
+ append_object_object(paramobj, "%{type}T",
+ new_objtree_for_type(procForm->proargtypes.values[i], -1));
+ params = lappend(params, new_object_object(paramobj));
+ }
+
+ sign = new_objtree("");
+
+ append_object_object(sign, "%{identity}D",
+ new_objtree_for_qualname(procForm->pronamespace,
+ NameStr(procForm->proname)));
+ append_array_object(sign, "(%{arguments:, }s)", params);
+
+ append_object_object(createTransform, "TO SQL WITH FUNCTION %{signature_tof}s", sign);
+ ReleaseSysCache(procTup);
+ }
+ append_string_object(createTransform, "%{close_bracket}s", ")");
+
+ ReleaseSysCache(langTup);
+ ReleaseSysCache(trfTup);
+ return createTransform;
+}
+
+/*
+ * Handle deparsing of simple commands.
+ *
+ * This function should cover all cases handled in ProcessUtilitySlow.
+ */
+static ObjTree *
+deparse_simple_command(CollectedCommand *cmd)
+{
+ Oid objectId;
+ Node *parsetree;
+ ObjTree *command;
+
+ Assert(cmd->type == SCT_Simple);
+
+ parsetree = cmd->parsetree;
+ objectId = cmd->d.simple.address.objectId;
+
+ if (cmd->in_extension && (nodeTag(parsetree) != T_CreateExtensionStmt))
+ return NULL;
+
+
+ /* This switch needs to handle everything that ProcessUtilitySlow does */
+ switch (nodeTag(parsetree))
+ {
+ case T_CreateSchemaStmt:
+ command = deparse_CreateSchemaStmt(objectId, parsetree);
+ break;
+
+ case T_AlterDomainStmt:
+ command = deparse_AlterDomainStmt(objectId, parsetree,
+ cmd->d.simple.secondaryObject);
+ break;
+
+ case T_CreateStmt:
+ command = deparse_CreateStmt(objectId, parsetree);
+ break;
+
+ case T_RefreshMatViewStmt:
+ command = deparse_RefreshMatViewStmt(objectId, parsetree);
+ break;
+
+ case T_CreateTrigStmt:
+ command = deparse_CreateTrigStmt(objectId, parsetree);
+ break;
+
+ case T_RuleStmt:
+ command = deparse_RuleStmt(objectId, parsetree);
+ break;
+
+ case T_CreateSeqStmt:
+ command = deparse_CreateSeqStmt(objectId, parsetree);
+ break;
+
+ case T_CreateFdwStmt:
+ command = deparse_CreateFdwStmt(objectId, parsetree);
+ break;
+
+ case T_CreateUserMappingStmt:
+ command = deparse_CreateUserMappingStmt(objectId, parsetree);
+ break;
+
+ case T_AlterUserMappingStmt:
+ command = deparse_AlterUserMappingStmt(objectId, parsetree);
+ break;
+
+ case T_AlterStatsStmt:
+ command = deparse_AlterStatsStmt(objectId, parsetree);
+ break;
+
+ case T_AlterFdwStmt:
+ command = deparse_AlterFdwStmt(objectId, parsetree);
+ break;
+
+ case T_AlterSeqStmt:
+ command = deparse_AlterSeqStmt(objectId, parsetree);
+ break;
+
+ case T_DefineStmt:
+ command = deparse_DefineStmt(objectId, parsetree,
+ cmd->d.simple.secondaryObject);
+ break;
+
+ case T_CreateConversionStmt:
+ command = deparse_CreateConversion(objectId, parsetree);
+ break;
+
+ case T_CreateDomainStmt:
+ command = deparse_CreateDomain(objectId, parsetree);
+ break;
+
+ case T_CreateExtensionStmt:
+ command = deparse_CreateExtensionStmt(objectId, parsetree);
+ break;
+
+ case T_AlterExtensionStmt:
+ command = deparse_AlterExtensionStmt(objectId, parsetree);
+ break;
+
+ case T_AlterExtensionContentsStmt:
+ command = deparse_AlterExtensionContentsStmt(objectId, parsetree,
+ cmd->d.simple.secondaryObject);
+ break;
+
+ case T_CreateOpFamilyStmt:
+ command = deparse_CreateOpFamily(objectId, parsetree);
+ break;
+
+ case T_CreatePolicyStmt:
+ command = deparse_CreatePolicyStmt(objectId, parsetree);
+ break;
+
+ case T_IndexStmt:
+ command = deparse_IndexStmt(objectId, parsetree);
+ break;
+
+ case T_CreateFunctionStmt:
+ command = deparse_CreateFunction(objectId, parsetree);
+ break;
+
+ case T_AlterFunctionStmt:
+ command = deparse_AlterFunction(objectId, parsetree);
+ break;
+
+ case T_AlterCollationStmt:
+ command = deparse_AlterCollation(objectId, parsetree);
+ break;
+
+ case T_RenameStmt:
+ command = deparse_RenameStmt(cmd->d.simple.address, parsetree);
+ break;
+
+ case T_AlterObjectDependsStmt:
+ command = deparse_AlterDependStmt(objectId, parsetree);
+ break;
+
+ case T_AlterObjectSchemaStmt:
+ command = deparse_AlterObjectSchemaStmt(cmd->d.simple.address,
+ parsetree,
+ cmd->d.simple.secondaryObject);
+ break;
+
+ case T_AlterOwnerStmt:
+ command = deparse_AlterOwnerStmt(cmd->d.simple.address, parsetree);
+ break;
+
+ case T_AlterOperatorStmt:
+ command = deparse_AlterOperatorStmt(objectId, parsetree);
+ break;
+
+ case T_AlterPolicyStmt:
+ command = deparse_AlterPolicyStmt(objectId, parsetree);
+ break;
+
+ case T_AlterTypeStmt:
+ command = deparse_AlterTypeSetStmt(objectId, parsetree);
+ break;
+
+ case T_CreateStatsStmt:
+ command = deparse_CreateStatisticsStmt(objectId, parsetree);
+ break;
+
+ case T_CreateForeignServerStmt:
+ command = deparse_CreateForeignServerStmt(objectId, parsetree);
+ break;
+
+ case T_AlterForeignServerStmt:
+ command = deparse_AlterForeignServerStmt(objectId, parsetree);
+ break;
+
+ case T_CompositeTypeStmt:
+ command = deparse_CompositeTypeStmt(objectId, parsetree);
+ break;
+
+ case T_CreateEnumStmt: /* CREATE TYPE AS ENUM */
+ command = deparse_CreateEnumStmt(objectId, parsetree);
+ break;
+
+ case T_CreateRangeStmt: /* CREATE TYPE AS RANGE */
+ command = deparse_CreateRangeStmt(objectId, parsetree);
+ break;
+
+ case T_AlterEnumStmt:
+ command = deparse_AlterEnumStmt(objectId, parsetree);
+ break;
+
+ case T_CreateCastStmt:
+ command = deparse_CreateCastStmt(objectId, parsetree);
+ break;
+
+ case T_AlterTSDictionaryStmt:
+ command = deparse_AlterTSDictionaryStmt(objectId, parsetree);
+ break;
+
+ case T_CreateTransformStmt:
+ command = deparse_CreateTransformStmt(objectId, parsetree);
+ break;
+
+ case T_ViewStmt: /* CREATE VIEW */
+ command = deparse_ViewStmt(objectId, parsetree);
+ break;
+
+ case T_CreateTableAsStmt: /* CREATE MATERIALIZED VIEW */
+ command = deparse_CreateTableAsStmt_vanilla(objectId, parsetree);
+ break;
+
+ default:
+ command = NULL;
+ elog(LOG, "unrecognized node type in deparse command: %d",
+ (int) nodeTag(parsetree));
+ }
+
+ return command;
+}
+
+/*
+ * Subroutine for CREATE TABLE deparsing.
+ *
+ * Deal with all the table elements (columns and constraints).
+ *
+ * Note we ignore constraints in the parse node here; they are extracted from
+ * system catalogs instead.
+ */
+static List *
+deparse_TableElements(Relation relation, List *tableElements, List *dpcontext,
+ bool typed, bool composite)
+{
+ List *elements = NIL;
+ ListCell *lc;
+
+ foreach(lc, tableElements)
+ {
+ Node *elt = (Node *) lfirst(lc);
+
+ switch (nodeTag(elt))
+ {
+ case T_ColumnDef:
+ {
+ ObjTree *tree;
+
+ tree = typed ?
+ deparse_ColumnDef_typed(relation, dpcontext,
+ (ColumnDef *) elt) :
+ deparse_ColumnDef(relation, dpcontext,
+ composite, (ColumnDef *) elt,
+ false, NULL);
+ if (tree != NULL)
+ {
+ ObjElem *column;
+
+ column = new_object_object(tree);
+ elements = lappend(elements, column);
+ }
+ }
+ break;
+ case T_Constraint:
+ break;
+ default:
+ elog(ERROR, "invalid node type %d", nodeTag(elt));
+ }
+ }
+
+ return elements;
+}
+
+/*
+ * Workhorse to deparse a CollectedCommand.
+ */
+char *
+deparse_utility_command(CollectedCommand *cmd, bool verbose_mode)
+{
+ OverrideSearchPath *overridePath;
+ MemoryContext oldcxt;
+ MemoryContext tmpcxt;
+ ObjTree *tree;
+ char *command;
+ StringInfoData str;
+
+ /*
+ * Allocate everything done by the deparsing routines into a temp context,
+ * to avoid having to sprinkle them with memory handling code, but
+ * allocate the output StringInfo before switching.
+ */
+ initStringInfo(&str);
+ tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
+ "deparse ctx",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+ oldcxt = MemoryContextSwitchTo(tmpcxt);
+
+ /*
+ * Many routines underlying this one will invoke ruleutils.c functionality
+ * to obtain deparsed versions of expressions. In such results, we want
+ * all object names to be qualified, so that results are "portable" to
+ * environments with different search_path settings. Rather than inject
+ * what would be repetitive calls to override search path all over the
+ * place, we do it centrally here.
+ */
+ overridePath = GetOverrideSearchPath(CurrentMemoryContext);
+ overridePath->schemas = NIL;
+ overridePath->addCatalog = false;
+ overridePath->addTemp = true;
+ PushOverrideSearchPath(overridePath);
+
+ verbose = verbose_mode;
+
+ switch (cmd->type)
+ {
+ case SCT_Simple:
+ tree = deparse_simple_command(cmd);
+ break;
+ case SCT_AlterTable:
+ tree = deparse_AlterTableStmt(cmd);
+ break;
+ case SCT_Grant:
+ tree = deparse_GrantStmt(cmd);
+ break;
+ case SCT_AlterOpFamily:
+ tree = deparse_AlterOpFamily(cmd);
+ break;
+ case SCT_CreateOpClass:
+ tree = deparse_CreateOpClassStmt(cmd);
+ break;
+ case SCT_AlterTSConfig:
+ tree = deparse_AlterTSConfigurationStmt(cmd);
+ break;
+ default:
+ elog(ERROR, "unexpected deparse node type %d", cmd->type);
+ }
+
+ PopOverrideSearchPath();
+
+ if (tree)
+ {
+ Jsonb *jsonb;
+
+ jsonb = objtree_to_jsonb(tree);
+ command = JsonbToCString(&str, &jsonb->root, JSONB_ESTIMATED_LEN);
+ }
+ else
+ command = NULL;
+
+ /*
+ * Clean up. Note that since we created the StringInfo in the caller's
+ * context, the output string is not deleted here.
+ */
+ MemoryContextSwitchTo(oldcxt);
+ MemoryContextDelete(tmpcxt);
+
+ return command;
+}
diff --git a/src/backend/commands/ddl_json.c b/src/backend/commands/ddl_json.c
new file mode 100644
index 0000000000..e16751915f
--- /dev/null
+++ b/src/backend/commands/ddl_json.c
@@ -0,0 +1,764 @@
+/*-------------------------------------------------------------------------
+ *
+ * ddl_json.c
+ * JSON code related to DDL command deparsing
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/backend/commands/ddl_json.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "lib/stringinfo.h"
+#include "utils/builtins.h"
+#include "utils/jsonb.h"
+
+
+/*
+ * Conversion specifier which determines how we expand the JSON element into
+ * string.
+ */
+typedef enum
+{
+ SpecTypename,
+ SpecOperatorname,
+ SpecDottedName,
+ SpecString,
+ SpecNumber,
+ SpecStringLiteral,
+ SpecIdentifier,
+ SpecRole
+} convSpecifier;
+
+/*
+ * A ternary value that represents a boolean type JsonbValue.
+ */
+typedef enum
+{
+ tv_absent,
+ tv_true,
+ tv_false
+} json_trivalue;
+
+static bool expand_one_jsonb_element(StringInfo buf, char *param,
+ JsonbValue *jsonval, convSpecifier specifier,
+ const char *fmt);
+static void expand_jsonb_array(StringInfo buf, char *param,
+ JsonbValue *jsonarr, char *arraysep,
+ convSpecifier specifier, const char *fmt);
+static void fmtstr_error_callback(void *arg);
+char *ddl_deparse_json_to_string(char *jsonb);
+
+/*
+ * Given a JsonbContainer, find the JsonbValue with the given key name in it.
+ * If it's of a type other than jbvBool, an error is raised. If it doesn't
+ * exist, tv_absent is returned; otherwise return the actual json_trivalue.
+ */
+static json_trivalue
+find_bool_in_jsonbcontainer(JsonbContainer *container, char *keyname)
+{
+ JsonbValue key;
+ JsonbValue *value;
+ json_trivalue result;
+
+ key.type = jbvString;
+ key.val.string.val = keyname;
+ key.val.string.len = strlen(keyname);
+ value = findJsonbValueFromContainer(container,
+ JB_FOBJECT, &key);
+ if (value == NULL)
+ return tv_absent;
+ if (value->type != jbvBool)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("element \"%s\" is not of type boolean",
+ keyname)));
+ result = value->val.boolean ? tv_true : tv_false;
+ pfree(value);
+
+ return result;
+}
+
+/*
+ * Given a JsonbContainer, find the JsonbValue with the given key name in it.
+ * If it's of a type other than jbvString, an error is raised. If it doesn't
+ * exist, an error is raised unless missing_ok; otherwise return NULL.
+ *
+ * If it exists and is a string, a freshly palloc'ed copy is returned.
+ *
+ * If *length is not NULL, it is set to the length of the string.
+ */
+static char *
+find_string_in_jsonbcontainer(JsonbContainer *container, char *keyname,
+ bool missing_ok, int *length)
+{
+ JsonbValue key;
+ JsonbValue *value;
+ char *str;
+
+ /* XXX verify that this is an object, not an array */
+
+ key.type = jbvString;
+ key.val.string.val = keyname;
+ key.val.string.len = strlen(keyname);
+ value = findJsonbValueFromContainer(container,
+ JB_FOBJECT, &key);
+ if (value == NULL)
+ {
+ if (missing_ok)
+ return NULL;
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("missing element \"%s\" in JSON object", keyname)));
+ }
+
+ str = pnstrdup(value->val.string.val, value->val.string.len);
+ if (length)
+ *length = value->val.string.len;
+ pfree(value);
+ return str;
+}
+
+#define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
+ do { \
+ if (++(ptr) >= (end_ptr)) \
+ ereport(ERROR, \
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
+ errmsg("unterminated format specifier"))); \
+ } while (0)
+
+/*
+ * Recursive helper for pg_event_trigger_expand_command
+ *
+ * Find the "fmt" element in the given container, and expand it into the
+ * provided StringInfo.
+ */
+static void
+expand_fmt_recursive(JsonbContainer *container, StringInfo buf)
+{
+ JsonbValue key;
+ JsonbValue *value;
+ const char *cp;
+ const char *start_ptr;
+ const char *end_ptr;
+ int len;
+
+ start_ptr = find_string_in_jsonbcontainer(container, "fmt", false, &len);
+ end_ptr = start_ptr + len;
+
+ for (cp = start_ptr; cp < end_ptr; cp++)
+ {
+ convSpecifier specifier;
+ bool is_array = false;
+ char *param = NULL;
+ char *arraysep = NULL;
+
+ if (*cp != '%')
+ {
+ appendStringInfoCharMacro(buf, *cp);
+ continue;
+ }
+
+
+ ADVANCE_PARSE_POINTER(cp, end_ptr);
+
+ /* Easy case: %% outputs a single % */
+ if (*cp == '%')
+ {
+ appendStringInfoCharMacro(buf, *cp);
+ continue;
+ }
+
+ /*
+ * Scan the mandatory element name. Allow for an array separator
+ * (which may be the empty string) to be specified after a colon.
+ */
+ if (*cp == '{')
+ {
+ StringInfoData parbuf;
+ StringInfoData arraysepbuf;
+ StringInfo appendTo;
+
+ initStringInfo(&parbuf);
+ appendTo = &parbuf;
+
+ ADVANCE_PARSE_POINTER(cp, end_ptr);
+ for (; cp < end_ptr;)
+ {
+ if (*cp == ':')
+ {
+ /*
+ * Found array separator delimiter; element name is now
+ * complete, start filling the separator.
+ */
+ initStringInfo(&arraysepbuf);
+ appendTo = &arraysepbuf;
+ is_array = true;
+ ADVANCE_PARSE_POINTER(cp, end_ptr);
+ continue;
+ }
+
+ if (*cp == '}')
+ {
+ ADVANCE_PARSE_POINTER(cp, end_ptr);
+ break;
+ }
+ appendStringInfoCharMacro(appendTo, *cp);
+ ADVANCE_PARSE_POINTER(cp, end_ptr);
+ }
+ param = parbuf.data;
+ if (is_array)
+ arraysep = arraysepbuf.data;
+ }
+ if (param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("missing conversion name in conversion specifier")));
+
+ switch (*cp)
+ {
+ case 'I':
+ specifier = SpecIdentifier;
+ break;
+ case 'D':
+ specifier = SpecDottedName;
+ break;
+ case 's':
+ specifier = SpecString;
+ break;
+ case 'L':
+ specifier = SpecStringLiteral;
+ break;
+ case 'T':
+ specifier = SpecTypename;
+ break;
+ case 'O':
+ specifier = SpecOperatorname;
+ break;
+ case 'n':
+ specifier = SpecNumber;
+ break;
+ case 'R':
+ specifier = SpecRole;
+ break;
+ default:
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("invalid conversion specifier \"%c\"", *cp)));
+ }
+
+ /*
+ * Obtain the element to be expanded.
+ */
+ key.type = jbvString;
+ key.val.string.val = param;
+ key.val.string.len = strlen(param);
+
+ value = findJsonbValueFromContainer(container, JB_FOBJECT, &key);
+
+ /*
+ * Expand the data (possibly an array) into the output StringInfo.
+ */
+ if (is_array)
+ expand_jsonb_array(buf, param, value, arraysep, specifier, start_ptr);
+ else
+ expand_one_jsonb_element(buf, param, value, specifier, start_ptr);
+ }
+}
+
+/*
+ * Expand a json value as a quoted identifier. The value must be of type string.
+ */
+static void
+expand_jsonval_identifier(StringInfo buf, JsonbValue *jsonval)
+{
+ char *str;
+
+ Assert(jsonval->type == jbvString);
+
+ str = pnstrdup(jsonval->val.string.val, jsonval->val.string.len);
+ appendStringInfoString(buf, quote_identifier(str));
+ pfree(str);
+}
+
+/*
+ * Expand a json value as a dot-separated-name. The value must be of type
+ * object and may contain elements "schemaname" (optional), "objname"
+ * (mandatory), "attrname" (optional). Double quotes are added to each element
+ * as necessary, and dot separators where needed.
+ *
+ * One day we might need a "catalog" element as well, but no current use case
+ * needs that.
+ */
+static void
+expand_jsonval_dottedname(StringInfo buf, JsonbValue *jsonval)
+{
+ char *str;
+
+ str = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "schemaname", true, NULL);
+ if (str)
+ {
+ appendStringInfo(buf, "%s.", quote_identifier(str));
+ pfree(str);
+ }
+
+ str = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "objname", false, NULL);
+ appendStringInfo(buf, "%s", quote_identifier(str));
+ pfree(str);
+
+ str = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "attrname", true, NULL);
+ if (str)
+ {
+ appendStringInfo(buf, ".%s", quote_identifier(str));
+ pfree(str);
+ }
+}
+
+/*
+ * Expand a JSON value as a type name.
+ */
+static void
+expand_jsonval_typename(StringInfo buf, JsonbValue *jsonval)
+{
+ char *schema = NULL;
+ char *typename;
+ char *typmodstr;
+ json_trivalue is_array;
+ char *array_decor;
+
+ /*
+ * We omit schema-qualifying the output name if the schema element is
+ * either the empty string or NULL; the difference between those two cases
+ * is that in the latter we quote the type name, in the former we don't.
+ * This allows for types with special typmod needs, such as interval and
+ * timestamp (see format_type_detailed), while at the same time allowing
+ * for the schema name to be omitted from type names that require quotes
+ * but are to be obtained from a user schema.
+ */
+
+ schema = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "schemaname", true, NULL);
+ typename = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "typename", false, NULL);
+ typmodstr = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "typmod", true, NULL);
+ is_array = find_bool_in_jsonbcontainer(jsonval->val.binary.data,
+ "typarray");
+ switch (is_array)
+ {
+ case tv_true:
+ array_decor = "[]";
+ break;
+
+ case tv_false:
+ array_decor = "";
+ break;
+
+ case tv_absent:
+ default:
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("missing typarray element")));
+ }
+
+ if (schema == NULL)
+ appendStringInfo(buf, "%s", quote_identifier(typename));
+ else if (schema[0] == '\0')
+ appendStringInfo(buf, "%s", typename); /* Special typmod needs */
+ else
+ appendStringInfo(buf, "%s.%s", quote_identifier(schema),
+ quote_identifier(typename));
+
+ appendStringInfo(buf, "%s%s", typmodstr ? typmodstr : "", array_decor);
+}
+
+/*
+ * Expand a JSON value as an operator name
+ */
+static void
+expand_jsonval_operator(StringInfo buf, JsonbValue *jsonval)
+{
+ char *str;
+
+ str = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "schemaname", true, NULL);
+ /* Schema might be NULL or empty */
+ if (str != NULL && str[0] != '\0')
+ {
+ appendStringInfo(buf, "%s.", quote_identifier(str));
+ pfree(str);
+ }
+
+ str = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "objname", false, NULL);
+
+ if (str)
+ {
+ appendStringInfoString(buf, str);
+ pfree(str);
+ }
+}
+
+/*
+ * Expand a JSON value as a string. The value must be of type string or of
+ * type object. In the latter case, it must contain a "fmt" element which will
+ * be recursively expanded; also, if the object contains an element "present"
+ * and it is set to false, the expansion is the empty string.
+ *
+ * Returns false if no actual expansion was made due to the "present" flag
+ * being set to "false".
+ */
+static bool
+expand_jsonval_string(StringInfo buf, JsonbValue *jsonval)
+{
+ if (jsonval->type == jbvString)
+ {
+ appendBinaryStringInfo(buf, jsonval->val.string.val,
+ jsonval->val.string.len);
+ }
+ else if (jsonval->type == jbvBinary)
+ {
+ json_trivalue present;
+
+ present = find_bool_in_jsonbcontainer(jsonval->val.binary.data,
+ "present");
+
+ /*
+ * If "present" is set to false, this element expands to empty;
+ * otherwise (either true or absent), fall through to expand "fmt".
+ */
+ if (present == tv_false)
+ return false;
+
+ expand_fmt_recursive(jsonval->val.binary.data, buf);
+ }
+ else
+ return false;
+
+ return true;
+}
+
+/*
+ * Expand a JSON value as a string literal.
+ */
+static void
+expand_jsonval_strlit(StringInfo buf, JsonbValue *jsonval)
+{
+ char *str;
+ StringInfoData dqdelim;
+ static const char dqsuffixes[] = "_XYZZYX_";
+ int dqnextchar = 0;
+
+ str = pnstrdup(jsonval->val.string.val, jsonval->val.string.len);
+
+ /* Easy case: if there are no ' and no \, just use a single quote */
+ if (strchr(str, '\'') == NULL &&
+ strchr(str, '\\') == NULL)
+ {
+ appendStringInfo(buf, "'%s'", str);
+ pfree(str);
+ return;
+ }
+
+ /* Otherwise need to find a useful dollar-quote delimiter */
+ initStringInfo(&dqdelim);
+ appendStringInfoString(&dqdelim, "$");
+ while (strstr(str, dqdelim.data) != NULL)
+ {
+ appendStringInfoChar(&dqdelim, dqsuffixes[dqnextchar++]);
+ dqnextchar = dqnextchar % (sizeof(dqsuffixes) - 1);
+ }
+ /* Add trailing $ */
+ appendStringInfoChar(&dqdelim, '$');
+
+ /* And finally produce the quoted literal into the output StringInfo */
+ appendStringInfo(buf, "%s%s%s", dqdelim.data, str, dqdelim.data);
+ pfree(dqdelim.data);
+ pfree(str);
+}
+
+/*
+ * Expand a JSON value as an integer quantity.
+ */
+static void
+expand_jsonval_number(StringInfo buf, JsonbValue *jsonval)
+{
+ char *strdatum;
+
+ strdatum = DatumGetCString(DirectFunctionCall1(numeric_out,
+ NumericGetDatum(jsonval->val.numeric)));
+ appendStringInfoString(buf, strdatum);
+}
+
+/*
+ * Expand a JSON value as a role name. If the is_public element is set to
+ * true, PUBLIC is expanded (no quotes); otherwise, expand the given role name,
+ * quoting as an identifier.
+ */
+static void
+expand_jsonval_role(StringInfo buf, JsonbValue *jsonval)
+{
+ json_trivalue is_public;
+
+ is_public = find_bool_in_jsonbcontainer(jsonval->val.binary.data,
+ "is_public");
+ if (is_public == tv_true)
+ appendStringInfoString(buf, "PUBLIC");
+ else
+ {
+ char *rolename;
+
+ rolename = find_string_in_jsonbcontainer(jsonval->val.binary.data,
+ "rolename", false, NULL);
+ if (rolename)
+ {
+ appendStringInfoString(buf, quote_identifier(rolename));
+ pfree(rolename);
+ }
+ }
+}
+
+/*
+ * Expand one JSON element into the output StringInfo according to the
+ * conversion specifier. The element type is validated, and an error is raised
+ * if it doesn't match what we expect for the conversion specifier.
+ *
+ * Returns false if no actual expansion was made (due to the "present" flag
+ * being set to "false" in formatted string expansion).
+ */
+static bool
+expand_one_jsonb_element(StringInfo buf, char *param, JsonbValue *jsonval,
+ convSpecifier specifier, const char *fmt)
+{
+ bool result = true;
+ ErrorContextCallback sqlerrcontext;
+
+ /* If we were given a format string, setup an ereport() context callback */
+ if (fmt)
+ {
+ sqlerrcontext.callback = fmtstr_error_callback;
+ sqlerrcontext.arg = (void *) fmt;
+ sqlerrcontext.previous = error_context_stack;
+ error_context_stack = &sqlerrcontext;
+ }
+
+ if (!jsonval)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("element \"%s\" not found", param)));
+
+ switch (specifier)
+ {
+ case SpecIdentifier:
+ if (jsonval->type != jbvString)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON string for %%I element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_identifier(buf, jsonval);
+ break;
+
+ case SpecDottedName:
+ if (jsonval->type != jbvBinary)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON object for %%D element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_dottedname(buf, jsonval);
+ break;
+
+ case SpecString:
+ if (jsonval->type != jbvString &&
+ jsonval->type != jbvBinary)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON string or object for %%s element \"%s\", got %d",
+ param, jsonval->type)));
+ result = expand_jsonval_string(buf, jsonval);
+ break;
+
+ case SpecStringLiteral:
+ if (jsonval->type != jbvString)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON string for %%L element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_strlit(buf, jsonval);
+ break;
+
+ case SpecTypename:
+ if (jsonval->type != jbvBinary)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON object for %%T element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_typename(buf, jsonval);
+ break;
+
+ case SpecOperatorname:
+ if (jsonval->type != jbvBinary)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON object for %%O element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_operator(buf, jsonval);
+ break;
+
+ case SpecNumber:
+ if (jsonval->type != jbvNumeric)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON numeric for %%n element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_number(buf, jsonval);
+ break;
+
+ case SpecRole:
+ if (jsonval->type != jbvBinary)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("expected JSON object for %%R element \"%s\", got %d",
+ param, jsonval->type)));
+ expand_jsonval_role(buf, jsonval);
+ break;
+ }
+
+ if (fmt)
+ error_context_stack = sqlerrcontext.previous;
+
+ return result;
+}
+
+/*
+ * Iterate on the elements of a JSON array, expanding each one into the output
+ * StringInfo per the given conversion specifier, separated by the given
+ * separator.
+ */
+static void
+expand_jsonb_array(StringInfo buf, char *param,
+ JsonbValue *jsonarr, char *arraysep, convSpecifier specifier,
+ const char *fmt)
+{
+ ErrorContextCallback sqlerrcontext;
+ JsonbContainer *container;
+ JsonbIterator *it;
+ JsonbValue v;
+ int type;
+ bool first = true;
+ StringInfoData arrayelem;
+
+ /* If we were given a format string, setup an ereport() context callback */
+ if (fmt)
+ {
+ sqlerrcontext.callback = fmtstr_error_callback;
+ sqlerrcontext.arg = (void *) fmt;
+ sqlerrcontext.previous = error_context_stack;
+ error_context_stack = &sqlerrcontext;
+ }
+
+ if (jsonarr->type != jbvBinary)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("element \"%s\" is not a JSON array", param)));
+
+ container = jsonarr->val.binary.data;
+ if (!JsonContainerIsArray(container))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("element \"%s\" is not a JSON array", param)));
+
+ initStringInfo(&arrayelem);
+
+ it = JsonbIteratorInit(container);
+ while ((type = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
+ {
+ if (type == WJB_ELEM)
+ {
+ resetStringInfo(&arrayelem);
+
+ if (expand_one_jsonb_element(&arrayelem, param, &v, specifier, NULL))
+ {
+ if (!first)
+ appendStringInfoString(buf, arraysep);
+
+ appendBinaryStringInfo(buf, arrayelem.data, arrayelem.len);
+ first = false;
+ }
+ }
+ }
+
+ if (fmt)
+ error_context_stack = sqlerrcontext.previous;
+}
+
+/*
+ * Workhorse for ddl_deparse_expand_command.
+ */
+char *
+ddl_deparse_json_to_string(char *json_str)
+{
+ Datum d;
+ Jsonb *jsonb;
+ StringInfo buf = (StringInfo) palloc0(sizeof(StringInfoData));
+
+ initStringInfo(buf);
+
+ d = DirectFunctionCall1(jsonb_in, PointerGetDatum(json_str));
+ jsonb = (Jsonb *) DatumGetPointer(d);
+
+ expand_fmt_recursive(&jsonb->root, buf);
+
+ return buf->data;
+}
+
+/*------
+ * Returns a formatted string from a JSON object.
+ *
+ * The starting point is the element named "fmt" (which must be a string).
+ * This format string may contain zero or more %-escapes, which consist of an
+ * element name enclosed in { }, possibly followed by a conversion modifier,
+ * followed by a conversion specifier. Possible conversion specifiers are:
+ *
+ * % expand to a literal %.
+ * I expand as a single, non-qualified identifier
+ * D expand as a possibly-qualified identifier
+ * T expand as a type name
+ * O expand as an operator name
+ * L expand as a string literal (quote using single quotes)
+ * s expand as a simple string (no quoting)
+ * n expand as a simple number (no quoting)
+ * R expand as a role name (possibly quoted name, or PUBLIC)
+ *
+ * The element name may have an optional separator specification preceded
+ * by a colon. Its presence indicates that the element is expected to be
+ * an array; the specified separator is used to join the array elements.
+ *------
+ */
+Datum
+ddl_deparse_expand_command(PG_FUNCTION_ARGS)
+{
+ text *json = PG_GETARG_TEXT_P(0);
+ char *json_str;
+
+ json_str = text_to_cstring(json);
+
+ PG_RETURN_TEXT_P(cstring_to_text(ddl_deparse_json_to_string(json_str)));
+}
+
+/*
+ * Error context callback for JSON format string expansion.
+ *
+ * XXX: indicate which element we're expanding, if applicable.
+ */
+static void
+fmtstr_error_callback(void *arg)
+{
+ errcontext("while expanding format string \"%s\"", (char *) arg);
+}
diff --git a/src/backend/commands/meson.build b/src/backend/commands/meson.build
index 9b350d025f..c1e1a2c18e 100644
--- a/src/backend/commands/meson.build
+++ b/src/backend/commands/meson.build
@@ -15,6 +15,8 @@ backend_sources += files(
'copyto.c',
'createas.c',
'dbcommands.c',
+ 'ddl_deparse.c',
+ 'ddl_json.c',
'define.c',
'discard.c',
'dropcmds.c',
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 99c9f91cba..4205dda222 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -1708,6 +1708,39 @@ process_owned_by(Relation seqrel, List *owned_by, bool for_identity)
relation_close(tablerel, NoLock);
}
+/*
+ * Return sequence parameters, detailed
+ */
+Form_pg_sequence_data
+get_sequence_values(Oid sequenceId)
+{
+ Buffer buf;
+ SeqTable elm;
+ Relation seqrel;
+ HeapTupleData seqtuple;
+ Form_pg_sequence_data seq;
+ Form_pg_sequence_data retSeq;
+
+ /* Open and AccessShareLock sequence */
+ init_sequence(sequenceId, &elm, &seqrel);
+
+ if (pg_class_aclcheck(sequenceId, GetUserId(),
+ ACL_SELECT | ACL_UPDATE | ACL_USAGE) != ACLCHECK_OK)
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ errmsg("permission denied for sequence %s",
+ RelationGetRelationName(seqrel))));
+
+ seq = read_seq_tuple(seqrel, &buf, &seqtuple);
+ retSeq = palloc(sizeof(FormData_pg_sequence_data));
+
+ memcpy(retSeq, seq, sizeof(FormData_pg_sequence_data));
+
+ UnlockReleaseBuffer(buf);
+ relation_close(seqrel, NoLock);
+
+ return retSeq;
+}
/*
* Return sequence parameters in a list of the form created by the parser.
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 247d0816ad..bea35a6281 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1446,7 +1446,8 @@ ProcessUtilitySlow(ParseState *pstate,
address = DefineCollation(pstate,
stmt->defnames,
stmt->definition,
- stmt->if_not_exists);
+ stmt->if_not_exists,
+ &secondaryObject);
break;
default:
elog(ERROR, "unrecognized define stmt type: %d",
diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c
index 2918fdbfb6..f64a7213b7 100644
--- a/src/backend/utils/adt/format_type.c
+++ b/src/backend/utils/adt/format_type.c
@@ -27,7 +27,6 @@
#include "utils/numeric.h"
#include "utils/syscache.h"
-static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
/*
@@ -363,7 +362,7 @@ format_type_with_typemod(Oid type_oid, int32 typemod)
/*
* Add typmod decoration to the basic type name
*/
-static char *
+char *
printTypmod(const char *typname, int32 typmod, Oid typmodout)
{
char *res;
diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c
index 6d4c1c27a5..b3a426858f 100644
--- a/src/backend/utils/adt/regproc.c
+++ b/src/backend/utils/adt/regproc.c
@@ -44,6 +44,8 @@
static void parseNameAndArgTypes(const char *string, bool allowNone,
List **names, int *nargs, Oid *argtypes);
+static void format_procedure_args_internal(Form_pg_proc procform,
+ StringInfo buf, bool force_qualify);
/*****************************************************************************
* USER I/O ROUTINES *
@@ -331,6 +333,29 @@ format_procedure_qualified(Oid procedure_oid)
return format_procedure_extended(procedure_oid, FORMAT_PROC_FORCE_QUALIFY);
}
+/*
+ * format_procedure_args - converts proc OID to "(args)"
+ */
+char *
+format_procedure_args(Oid procedure_oid, bool force_qualify)
+{
+ StringInfoData buf;
+ HeapTuple proctup;
+ Form_pg_proc procform;
+
+ proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procedure_oid));
+ if (!HeapTupleIsValid(proctup))
+ elog(ERROR, "cache lookup failed for procedure %u", procedure_oid);
+ procform = (Form_pg_proc) GETSTRUCT(proctup);
+
+ initStringInfo(&buf);
+ format_procedure_args_internal(procform, &buf, force_qualify);
+
+ ReleaseSysCache(proctup);
+
+ return buf.data;
+}
+
/*
* format_procedure_extended - converts procedure OID to "pro_name(args)"
*
@@ -2060,3 +2085,30 @@ parseNameAndArgTypes(const char *string, bool allowNone, List **names,
pfree(rawname);
}
+
+/*
+ * Append the parenthesized arguments of the given pg_proc row into the output
+ * buffer. force_qualify indicates whether to schema-qualify type names
+ * regardless of visibility.
+ */
+static void
+format_procedure_args_internal(Form_pg_proc procform, StringInfo buf,
+ bool force_qualify)
+{
+ int i;
+ int nargs = procform->pronargs;
+
+ appendStringInfoChar(buf, '(');
+ for (i = 0; i < nargs; i++)
+ {
+ Oid thisargtype = procform->proargtypes.values[i];
+
+ if (i > 0)
+ appendStringInfoChar(buf, ',');
+ appendStringInfoString(buf,
+ force_qualify ?
+ format_type_be_qualified(thisargtype) :
+ format_type_be(thisargtype));
+ }
+ appendStringInfoChar(buf, ')');
+}
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 70d723e80c..a5de597e3b 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -35,6 +35,7 @@
#include "catalog/pg_operator.h"
#include "catalog/pg_partitioned_table.h"
#include "catalog/pg_proc.h"
+#include "catalog/pg_rewrite.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
@@ -358,7 +359,6 @@ static int print_function_arguments(StringInfo buf, HeapTuple proctup,
bool print_table_args, bool print_defaults);
static void print_function_rettype(StringInfo buf, HeapTuple proctup);
static void print_function_trftypes(StringInfo buf, HeapTuple proctup);
-static void print_function_sqlbody(StringInfo buf, HeapTuple proctup);
static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
Bitmapset *rels_used);
static void set_deparse_for_query(deparse_namespace *dpns, Query *query,
@@ -482,22 +482,15 @@ static void get_from_clause_coldeflist(RangeTblFunction *rtfunc,
deparse_context *context);
static void get_tablesample_def(TableSampleClause *tablesample,
deparse_context *context);
-static void get_opclass_name(Oid opclass, Oid actual_datatype,
- StringInfo buf);
static Node *processIndirection(Node *node, deparse_context *context);
static void printSubscripts(SubscriptingRef *sbsref, deparse_context *context);
static char *get_relation_name(Oid relid);
static char *generate_relation_name(Oid relid, List *namespaces);
static char *generate_qualified_relation_name(Oid relid);
-static char *generate_function_name(Oid funcid, int nargs,
- List *argnames, Oid *argtypes,
- bool has_variadic, bool *use_variadic_p,
- ParseExprKind special_exprkind);
static char *generate_operator_name(Oid operid, Oid arg1, Oid arg2);
static void add_cast_to(StringInfo buf, Oid typid);
static char *generate_qualified_type_name(Oid typid);
static text *string_to_text(char *str);
-static char *flatten_reloptions(Oid relid);
static void get_reloptions(StringInfo buf, Datum reloptions);
#define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
@@ -545,6 +538,108 @@ pg_get_ruledef_ext(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(string_to_text(res));
}
+/*
+ * Given a pair of Datum corresponding to a rule's pg_rewrite.ev_qual and
+ * ev_action columns, return their text representation; ev_qual as a single
+ * string in whereClause and ev_action as a List of strings (which might be
+ * NIL, signalling NOTHING) in actions.
+ */
+void
+pg_get_ruledef_detailed(Datum ev_qual, Datum ev_action,
+ char **whereClause, List **actions)
+{
+ int prettyFlags = 0;
+ char *qualstr = TextDatumGetCString(ev_qual);
+ char *actionstr = TextDatumGetCString(ev_action);
+ List *actionNodeList = (List *) stringToNode(actionstr);
+ StringInfoData buf;
+
+ initStringInfo(&buf);
+ if (strlen(qualstr) > 0 && strcmp(qualstr, "<>") != 0)
+ {
+ Node *qual;
+ Query *query;
+ deparse_context context;
+ deparse_namespace dpns;
+
+ qual = stringToNode(qualstr);
+
+ query = (Query *) linitial(actionNodeList);
+ query = getInsertSelectQuery(query, NULL);
+
+ AcquireRewriteLocks(query, false, false);
+
+ context.buf = &buf;
+ context.namespaces = list_make1(&dpns);
+ context.windowClause = NIL;
+ context.windowTList = NIL;
+ context.varprefix = (list_length(query->rtable) != 1);
+ context.prettyFlags = prettyFlags;
+ context.wrapColumn = WRAP_COLUMN_DEFAULT;
+ context.indentLevel = PRETTYINDENT_STD;
+
+ set_deparse_for_query(&dpns, query, NIL);
+
+ get_rule_expr(qual, &context, false);
+
+ *whereClause = pstrdup(buf.data);
+ }
+ else
+ *whereClause = NULL;
+
+ if (list_length(actionNodeList) == 0)
+ *actions = NIL;
+ else
+ {
+ ListCell *cell;
+ List *output = NIL;
+
+ foreach(cell, actionNodeList)
+ {
+ Query *query = (Query *) lfirst(cell);
+
+ if (query->commandType == CMD_NOTHING)
+ continue;
+
+ resetStringInfo(&buf);
+ get_query_def(query, &buf, NIL, NULL, true,
+ prettyFlags, WRAP_COLUMN_DEFAULT, 0);
+ output = lappend(output, pstrdup(buf.data));
+ }
+ *actions = output;
+ }
+}
+
+/*
+ * In the case that the CREATE VIEW command execution is still in progress,
+ * we would need to search the system cache RULERELNAME to get the rewrite
+ * rule of the view as oppose to querying pg_rewrite as in pg_get_viewdef_worker(),
+ * the latter will return empty result.
+ */
+char *
+pg_get_viewdef_internal(Oid viewoid)
+{
+ StringInfoData buf;
+ Relation pg_rewrite;
+ HeapTuple ruletup;
+ TupleDesc rulettc;
+
+ initStringInfo(&buf);
+ pg_rewrite = table_open(RewriteRelationId, AccessShareLock);
+
+ ruletup = SearchSysCache2(RULERELNAME,
+ ObjectIdGetDatum(viewoid),
+ PointerGetDatum(ViewSelectRuleName));
+ if (!HeapTupleIsValid(ruletup))
+ elog(ERROR, "cache lookup failed for rewrite rule for view with OID %u", viewoid);
+
+ rulettc = pg_rewrite->rd_att;
+ make_viewdef(&buf, ruletup, rulettc, 0, WRAP_COLUMN_DEFAULT);
+ ReleaseSysCache(ruletup);
+ table_close(pg_rewrite, AccessShareLock);
+
+ return buf.data;
+}
static char *
pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
@@ -1015,65 +1110,12 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
if (!isnull)
{
Node *qual;
- char relkind;
- deparse_context context;
- deparse_namespace dpns;
- RangeTblEntry *oldrte;
- RangeTblEntry *newrte;
-
- appendStringInfoString(&buf, "WHEN (");
+ char *qualstr;
qual = stringToNode(TextDatumGetCString(value));
+ qualstr = pg_get_trigger_whenclause(trigrec, qual, pretty);
- relkind = get_rel_relkind(trigrec->tgrelid);
-
- /* Build minimal OLD and NEW RTEs for the rel */
- oldrte = makeNode(RangeTblEntry);
- oldrte->rtekind = RTE_RELATION;
- oldrte->relid = trigrec->tgrelid;
- oldrte->relkind = relkind;
- oldrte->rellockmode = AccessShareLock;
- oldrte->alias = makeAlias("old", NIL);
- oldrte->eref = oldrte->alias;
- oldrte->lateral = false;
- oldrte->inh = false;
- oldrte->inFromCl = true;
-
- newrte = makeNode(RangeTblEntry);
- newrte->rtekind = RTE_RELATION;
- newrte->relid = trigrec->tgrelid;
- newrte->relkind = relkind;
- newrte->rellockmode = AccessShareLock;
- newrte->alias = makeAlias("new", NIL);
- newrte->eref = newrte->alias;
- newrte->lateral = false;
- newrte->inh = false;
- newrte->inFromCl = true;
-
- /* Build two-element rtable */
- memset(&dpns, 0, sizeof(dpns));
- dpns.rtable = list_make2(oldrte, newrte);
- dpns.subplans = NIL;
- dpns.ctes = NIL;
- dpns.appendrels = NULL;
- set_rtable_names(&dpns, NIL, NULL);
- set_simple_column_names(&dpns);
-
- /* Set up context with one-deep namespace stack */
- context.buf = &buf;
- context.namespaces = list_make1(&dpns);
- context.windowClause = NIL;
- context.windowTList = NIL;
- context.varprefix = true;
- context.prettyFlags = GET_PRETTY_FLAGS(pretty);
- context.wrapColumn = WRAP_COLUMN_DEFAULT;
- context.indentLevel = PRETTYINDENT_STD;
- context.special_exprkind = EXPR_KIND_NONE;
- context.appendparents = NULL;
-
- get_rule_expr(qual, &context, false);
-
- appendStringInfoString(&buf, ") ");
+ appendStringInfo(&buf, "WHEN (%s) ", qualstr);
}
appendStringInfo(&buf, "EXECUTE FUNCTION %s(",
@@ -1114,6 +1156,64 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
return buf.data;
}
+char *
+pg_get_trigger_whenclause(Form_pg_trigger trigrec, Node *whenClause, bool pretty)
+{
+ StringInfoData buf;
+ char relkind;
+ deparse_context context;
+ deparse_namespace dpns;
+ RangeTblEntry *oldrte;
+ RangeTblEntry *newrte;
+
+ initStringInfo(&buf);
+
+ relkind = get_rel_relkind(trigrec->tgrelid);
+
+ /* Build minimal OLD and NEW RTEs for the rel */
+ oldrte = makeNode(RangeTblEntry);
+ oldrte->rtekind = RTE_RELATION;
+ oldrte->relid = trigrec->tgrelid;
+ oldrte->relkind = relkind;
+ oldrte->alias = makeAlias("old", NIL);
+ oldrte->eref = oldrte->alias;
+ oldrte->lateral = false;
+ oldrte->inh = false;
+ oldrte->inFromCl = true;
+
+ newrte = makeNode(RangeTblEntry);
+ newrte->rtekind = RTE_RELATION;
+ newrte->relid = trigrec->tgrelid;
+ newrte->relkind = relkind;
+ newrte->alias = makeAlias("new", NIL);
+ newrte->eref = newrte->alias;
+ newrte->lateral = false;
+ newrte->inh = false;
+ newrte->inFromCl = true;
+
+ /* Build two-element rtable */
+ memset(&dpns, 0, sizeof(dpns));
+ dpns.rtable = list_make2(oldrte, newrte);
+ dpns.ctes = NIL;
+ set_rtable_names(&dpns, NIL, NULL);
+ set_simple_column_names(&dpns);
+
+ /* Set up context with one-deep namespace stack */
+ context.buf = &buf;
+ context.namespaces = list_make1(&dpns);
+ context.windowClause = NIL;
+ context.windowTList = NIL;
+ context.varprefix = true;
+ context.prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
+ context.wrapColumn = WRAP_COLUMN_DEFAULT;
+ context.indentLevel = PRETTYINDENT_STD;
+ context.special_exprkind = EXPR_KIND_NONE;
+
+ get_rule_expr(whenClause, &context, false);
+
+ return buf.data;
+}
+
/* ----------
* pg_get_indexdef - Get the definition of an index
*
@@ -1880,6 +1980,13 @@ pg_get_partkeydef_columns(Oid relid, bool pretty)
return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
}
+/* Internal version that reports the full partition key definition */
+char *
+pg_get_partkeydef_simple(Oid relid)
+{
+ return pg_get_partkeydef_worker(relid, 0, false, false);
+}
+
/*
* Internal workhorse to decompile a partition key definition.
*/
@@ -2131,6 +2238,15 @@ pg_get_constraintdef_ext(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(string_to_text(res));
}
+/*
+ * Internal version that returns the definition of a CONSTRAINT command
+ */
+char *
+pg_get_constraintdef_command_simple(Oid constraintId)
+{
+ return pg_get_constraintdef_worker(constraintId, false, 0, false);
+}
+
/*
* Internal version that returns a full ALTER TABLE ... ADD CONSTRAINT command
*/
@@ -3501,7 +3617,7 @@ pg_get_function_arg_default(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(string_to_text(str));
}
-static void
+void
print_function_sqlbody(StringInfo buf, HeapTuple proctup)
{
int numargs;
@@ -11385,7 +11501,7 @@ get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
* actual_datatype. (If you don't want this behavior, just pass
* InvalidOid for actual_datatype.)
*/
-static void
+void
get_opclass_name(Oid opclass, Oid actual_datatype,
StringInfo buf)
{
@@ -11779,7 +11895,7 @@ generate_qualified_relation_name(Oid relid)
*
* The result includes all necessary quoting and schema-prefixing.
*/
-static char *
+char *
generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes,
bool has_variadic, bool *use_variadic_p,
ParseExprKind special_exprkind)
@@ -12165,7 +12281,7 @@ get_reloptions(StringInfo buf, Datum reloptions)
/*
* Generate a C string representing a relation's reloptions, or NULL if none.
*/
-static char *
+char *
flatten_reloptions(Oid relid)
{
char *result = NULL;
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 62a5b8e655..c2f1de2801 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11827,4 +11827,10 @@
prorettype => 'bytea', proargtypes => 'pg_brin_minmax_multi_summary',
prosrc => 'brin_minmax_multi_summary_send' },
+{ oid => '4642', descr => 'deparse the DDL command into JSON format string',
+ proname => 'ddl_deparse_to_json', prorettype => 'text',
+ proargtypes => 'pg_ddl_command', prosrc => 'ddl_deparse_to_json' },
+{ oid => '4643', descr => 'expand JSON format DDL to a plain DDL command',
+ proname => 'ddl_deparse_expand_command', prorettype => 'text',
+ proargtypes => 'text', prosrc => 'ddl_deparse_expand_command' },
]
diff --git a/src/include/commands/collationcmds.h b/src/include/commands/collationcmds.h
index 201b19f8b7..46d183760a 100644
--- a/src/include/commands/collationcmds.h
+++ b/src/include/commands/collationcmds.h
@@ -18,7 +18,8 @@
#include "catalog/objectaddress.h"
#include "parser/parse_node.h"
-extern ObjectAddress DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_exists);
+extern ObjectAddress DefineCollation(ParseState *pstate, List *names, List *parameters,
+ bool if_not_exists, ObjectAddress *from_collid);
extern void IsThereCollationInNamespace(const char *collname, Oid nspOid);
extern ObjectAddress AlterCollation(AlterCollationStmt *stmt);
diff --git a/src/include/commands/sequence.h b/src/include/commands/sequence.h
index b3b04ccfa9..c2b1ca32f6 100644
--- a/src/include/commands/sequence.h
+++ b/src/include/commands/sequence.h
@@ -54,6 +54,7 @@ typedef struct xl_seq_rec
extern int64 nextval_internal(Oid relid, bool check_permissions);
extern Datum nextval(PG_FUNCTION_ARGS);
extern List *sequence_options(Oid relid);
+extern Form_pg_sequence_data get_sequence_values(Oid sequenceId);
extern ObjectAddress DefineSequence(ParseState *pstate, CreateSeqStmt *seq);
extern ObjectAddress AlterSequence(ParseState *pstate, AlterSeqStmt *stmt);
diff --git a/src/include/tcop/ddl_deparse.h b/src/include/tcop/ddl_deparse.h
new file mode 100644
index 0000000000..12aae734ca
--- /dev/null
+++ b/src/include/tcop/ddl_deparse.h
@@ -0,0 +1,23 @@
+/*-------------------------------------------------------------------------
+ *
+ * ddl_deparse.h
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/tcop/ddl_deparse.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef DDL_DEPARSE_H
+#define DDL_DEPARSE_H
+
+#include "tcop/deparse_utility.h"
+
+extern char *deparse_utility_command(CollectedCommand *cmd, bool verbose_mode);
+extern char *ddl_deparse_json_to_string(char *jsonb);
+extern char *deparse_drop_command(const char *objidentity, const char *objecttype,
+ DropBehavior behavior);
+
+
+#endif /* DDL_DEPARSE_H */
diff --git a/src/include/tcop/deparse_utility.h b/src/include/tcop/deparse_utility.h
index 94de13d990..b53294bf57 100644
--- a/src/include/tcop/deparse_utility.h
+++ b/src/include/tcop/deparse_utility.h
@@ -62,6 +62,7 @@ typedef struct CollectedCommand
{
Oid objectId;
Oid classId;
+ bool rewrite;
List *subcmds;
} alterTable;
diff --git a/src/include/utils/aclchk_internal.h b/src/include/utils/aclchk_internal.h
index 361284d8eb..4e020ef4ae 100644
--- a/src/include/utils/aclchk_internal.h
+++ b/src/include/utils/aclchk_internal.h
@@ -39,6 +39,7 @@ typedef struct
List *grantees;
bool grant_option;
DropBehavior behavior;
+ Oid grantor_uid;
} InternalGrant;
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 81631f1645..7150fabd24 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -114,10 +114,12 @@ extern Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS);
#define FORMAT_TYPE_FORCE_QUALIFY 0x04 /* force qualification of type */
#define FORMAT_TYPE_INVALID_AS_NULL 0x08 /* NULL if undefined */
extern char *format_type_extended(Oid type_oid, int32 typemod, bits16 flags);
+extern char *format_procedure_args(Oid procedure_oid, bool force_qualify);
extern char *format_type_be(Oid type_oid);
extern char *format_type_be_qualified(Oid type_oid);
extern char *format_type_with_typemod(Oid type_oid, int32 typemod);
+extern char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
extern int32 type_maximum_size(Oid type_oid, int32 typemod);
diff --git a/src/include/utils/ruleutils.h b/src/include/utils/ruleutils.h
index 7d489718a3..ec6ef80164 100644
--- a/src/include/utils/ruleutils.h
+++ b/src/include/utils/ruleutils.h
@@ -13,9 +13,12 @@
#ifndef RULEUTILS_H
#define RULEUTILS_H
+#include "access/htup.h"
+#include "catalog/pg_trigger.h"
#include "nodes/nodes.h"
#include "nodes/parsenodes.h"
#include "nodes/pg_list.h"
+#include "parser/parse_node.h"
struct Plan; /* avoid including plannodes.h here */
struct PlannedStmt;
@@ -23,12 +26,20 @@ struct PlannedStmt;
extern char *pg_get_indexdef_string(Oid indexrelid);
extern char *pg_get_indexdef_columns(Oid indexrelid, bool pretty);
+extern char *pg_get_trigger_whenclause(Form_pg_trigger trigrec,
+ Node *whenClause, bool pretty);
extern char *pg_get_querydef(Query *query, bool pretty);
+extern char *pg_get_viewdef_internal(Oid viewoid);
extern char *pg_get_partkeydef_columns(Oid relid, bool pretty);
+extern char *pg_get_partkeydef_simple(Oid relid);
extern char *pg_get_partconstrdef_string(Oid partitionId, char *aliasname);
extern char *pg_get_constraintdef_command(Oid constraintId);
+extern char *pg_get_constraintdef_command_simple(Oid constraintId);
+extern void pg_get_ruledef_detailed(Datum ev_qual, Datum ev_action,
+ char **whereClause, List **actions);
+
extern char *deparse_expression(Node *expr, List *dpcontext,
bool forceprefix, bool showimplicit);
extern List *deparse_context_for(const char *aliasname, Oid relid);
@@ -40,8 +51,16 @@ extern List *select_rtable_names_for_explain(List *rtable,
Bitmapset *rels_used);
extern char *generate_collation_name(Oid collid);
extern char *generate_opclass_name(Oid opclass);
+extern char *generate_function_name(Oid funcid, int nargs, List *argnames,
+ Oid *argtypes, bool has_variadic,
+ bool *use_variadic_p,
+ ParseExprKind special_exprkind);
extern char *get_range_partbound_string(List *bound_datums);
+extern void get_opclass_name(Oid opclass, Oid actual_datatype,
+ StringInfo buf);
+extern char *flatten_reloptions(Oid relid);
extern char *pg_get_statisticsobjdef_string(Oid statextid);
+extern void print_function_sqlbody(StringInfo buf, HeapTuple proctup);
#endif /* RULEUTILS_H */
diff --git a/src/test/regress/expected/object_address.out b/src/test/regress/expected/object_address.out
index dadd58e8b9..3bf9e14f19 100644
--- a/src/test/regress/expected/object_address.out
+++ b/src/test/regress/expected/object_address.out
@@ -491,7 +491,7 @@ SELECT (pg_identify_object(addr1.classid, addr1.objid, addr1.objsubid)).*,
policy | | | genpol on addr_nsp.gentable | t
statistics object | addr_nsp | gentable_stat | addr_nsp.gentable_stat | t
collation | pg_catalog | "default" | pg_catalog."default" | t
- transform | | | for integer on language sql | t
+ transform | | | for integer language sql | t
text search dictionary | addr_nsp | addr_ts_dict | addr_nsp.addr_ts_dict | t
text search parser | addr_nsp | addr_ts_prs | addr_nsp.addr_ts_prs | t
text search configuration | addr_nsp | addr_ts_conf | addr_nsp.addr_ts_conf | t
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index d9b839c979..240d3ae41b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1610,6 +1610,9 @@ OSInfo
OSSLCipher
OSSLDigest
OVERLAPPED
+ObjElem
+ObjTree
+ObjType
ObjectAccessDrop
ObjectAccessNamespaceSearch
ObjectAccessPostAlter
@@ -3177,6 +3180,7 @@ compare_context
config_var_value
contain_aggs_of_level_context
convert_testexpr_context
+convSpecifier
copy_data_dest_cb
copy_data_source_cb
core_YYSTYPE
--
2.32.0
[text/x-patch] v30-0002-Support-DDL-replication.patch (132.7K, 3-v30-0002-Support-DDL-replication.patch)
download | inline diff:
From a0abf18629478b2125a148abb7f11301318dd13e Mon Sep 17 00:00:00 2001
From: Ajin Cherian <ajinc@fast.au.fujitsu.com>
Date: Thu, 13 Oct 2022 08:24:11 -0400
Subject: [PATCH v30 2/4] Support DDL replication.
To support DDL replication, we use event trigger and DDL deparsing
facilities. While creating a publication, we register a command end
trigger that deparses the DDL as a JSON blob, and WAL logs it. The event
trigger is automatically removed at the time of drop publication. The
WALSender decodes the WAL and sends it downstream similar to other DML
commands. The subscriber then converts JSON back to the DDL command string
and executes it. In the subscriber, we also add the newly added rel to
pg_subscription_rel so that the DML changes on the new table can be
replicated without having to manually run
"ALTER SUBSCRIPTION ... REFRESH PUBLICATION".
This is a POC patch to show how using event triggers and DDL deparsing
facilities we can implement DDL replication. So, the implementation is
restricted to CREATE TABLE/ALTER TABLE/DROP TABLE commands.
- For non-rewrite ALTER object command and
- CREATE object command:
we deparse the command at ddl_command_end event trigger and WAL log the
deparsed json string. The WALSender decodes the WAL and sends it to
subscriber if the created/altered table is published. It supports most of
ALTER TABLE command except some commands(DDL related to PARTITIONED TABLE
...) that introduced recently which haven't been supported by the current
ddl_deparser, we will support that later.
Note that the replication for ALTER INDEX command is still under
progress.
- For DROP object:
The 'command start' event handler logs a ddl message with the relids of
the tables that are dropped which the output plugin (pgoutput) stores in
its internal data structure after verifying that it is for a table that is
part of the publication. Later the 'command end' event handler sends the
actual drop message. Pgoutput on receiving the command end, only sends out
the drop command only if it is for one of the relids marked for deleting.
The reason we have to do this is because, once the logical decoder
receives the 'command end' message, the relid of the table is no longer
valid as it has been deleted as part of invalidations received for the
drop table command. It is no longer possible to verify if the table is
part of the publication list or not. To make this possible, I have added
two more elements to the ddl xlog and ddl message, (relid and cmdtype).
We could have also handled all this on the subscriber side as well, but
that would mean sending spurious ddl messages for tables that are not part
of the publication.
- For table_rewrite ALTER TABLE command:
(ALTER COLUMN TYPE, ADD COLUMN DEFAULT, SET LOGGED, SET ACCESS METHOD)
we deparse the command and WAL log the deparsed json string at
table_rewrite event trigger. The WALSender decodes the WAL and sends it to
subscriber if the altered table is published. Then, the WALSender will
convert the upcoming rewrite INSERTs to UPDATEs and send them to
subscriber so that the data between publisher and subscriber can always be
consistent. Note that the tables that publish rewrite ddl must have a
replica identity configured in order to be able to replicate the upcoming
rewrite UPDATEs.
We do this way because of two reason:
(1) The data before the rewrite ddl could already be different among
publisher and subscriber. To make sure the extra data in subscriber which
doesn't exist in publisher also get rewritten, we need to let the
subscriber execute the original rewrite ddl to rewrite all the data at
first.
(2) the data after executing rewrite ddl could be different among
publisher and subscriber(due to different functions/operators used during
rewrite), so we need to replicate the rewrite UPDATEs to keep the data
consistent.
TO IMPROVE:
This approach could be improved by letting the subscriber try to update
the extra data itself instead of doing fully rewrite ddl and use the
upcoming rewrite UPDATEs to rewrite the rest data. To achieve this, we
could modify the deparsed json string to temporarily remove the rewrite
part and add some logic in subscriber to update the extra data.
Besides, we may not need to send rewrite changes for all type of rewrite
ddl, for example, it seems fine to skip sending rewrite changes for ALTER
TABLE SET LOGGED as the data in the table doesn't actually be changed. We
could use the deparser and event trigger to filter these ddls and skip
sending rewrite changes for them.
---
src/backend/access/rmgrdesc/Makefile | 1 +
.../access/rmgrdesc/logicalddlmsgdesc.c | 52 +++
src/backend/access/rmgrdesc/meson.build | 1 +
src/backend/catalog/pg_publication.c | 1 +
src/backend/commands/event_trigger.c | 284 +++++++++++-
src/backend/commands/publicationcmds.c | 174 ++++++++
src/backend/commands/tablecmds.c | 2 +-
src/backend/replication/logical/Makefile | 1 +
src/backend/replication/logical/ddlmessage.c | 86 ++++
src/backend/replication/logical/decode.c | 41 ++
src/backend/replication/logical/logical.c | 93 ++++
src/backend/replication/logical/meson.build | 1 +
src/backend/replication/logical/proto.c | 48 ++
.../replication/logical/reorderbuffer.c | 136 ++++++
src/backend/replication/logical/worker.c | 234 ++++++++++
src/backend/replication/pgoutput/pgoutput.c | 194 +++++++-
src/backend/utils/cache/relcache.c | 1 +
src/bin/pg_dump/pg_dump.c | 21 +-
src/bin/pg_dump/pg_dump.h | 1 +
src/bin/pg_waldump/rmgrdesc.c | 1 +
src/bin/psql/describe.c | 17 +-
src/include/access/rmgrlist.h | 1 +
src/include/catalog/pg_proc.dat | 9 +
src/include/catalog/pg_publication.h | 4 +
src/include/commands/event_trigger.h | 3 +-
src/include/replication/ddlmessage.h | 60 +++
src/include/replication/decode.h | 1 +
src/include/replication/logicalproto.h | 7 +-
src/include/replication/output_plugin.h | 27 ++
src/include/replication/pgoutput.h | 1 +
src/include/replication/reorderbuffer.h | 39 ++
src/test/regress/expected/psql.out | 6 +-
src/test/regress/expected/publication.out | 420 +++++++++---------
33 files changed, 1739 insertions(+), 229 deletions(-)
create mode 100644 src/backend/access/rmgrdesc/logicalddlmsgdesc.c
create mode 100644 src/backend/replication/logical/ddlmessage.c
create mode 100644 src/include/replication/ddlmessage.h
diff --git a/src/backend/access/rmgrdesc/Makefile b/src/backend/access/rmgrdesc/Makefile
index f88d72fd86..b8e29e8df3 100644
--- a/src/backend/access/rmgrdesc/Makefile
+++ b/src/backend/access/rmgrdesc/Makefile
@@ -19,6 +19,7 @@ OBJS = \
hashdesc.o \
heapdesc.o \
logicalmsgdesc.o \
+ logicalddlmsgdesc.o \
mxactdesc.o \
nbtdesc.o \
relmapdesc.o \
diff --git a/src/backend/access/rmgrdesc/logicalddlmsgdesc.c b/src/backend/access/rmgrdesc/logicalddlmsgdesc.c
new file mode 100644
index 0000000000..81dee529d0
--- /dev/null
+++ b/src/backend/access/rmgrdesc/logicalddlmsgdesc.c
@@ -0,0 +1,52 @@
+/*-------------------------------------------------------------------------
+ *
+ * logicalddlmsgdesc.c
+ * rmgr descriptor routines for replication/logical/ddlmessage.c
+ *
+ * Portions Copyright (c) 2015-2022, PostgreSQL Global Development Group
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/access/rmgrdesc/logicalddlmsgdesc.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "replication/ddlmessage.h"
+
+void
+logicalddlmsg_desc(StringInfo buf, XLogReaderState *record)
+{
+ char *rec = XLogRecGetData(record);
+ uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
+
+ if (info == XLOG_LOGICAL_DDL_MESSAGE)
+ {
+ xl_logical_ddl_message *xlrec = (xl_logical_ddl_message *) rec;
+ char *prefix = xlrec->message;
+ char *message = xlrec->message + xlrec->prefix_size;
+ char *sep = "";
+
+ Assert(prefix[xlrec->prefix_size] != '\0');
+
+ appendStringInfo(buf, "prefix \"%s\"; payload (%zu bytes): ",
+ prefix, xlrec->message_size);
+ appendStringInfo(buf, "relid %u cmdtype %u", xlrec->relid, xlrec->cmdtype);
+ /* Write message payload as a series of hex bytes */
+ for (int cnt = 0; cnt < xlrec->message_size; cnt++)
+ {
+ appendStringInfo(buf, "%s%02X", sep, (unsigned char) message[cnt]);
+ sep = " ";
+ }
+ }
+}
+
+const char *
+logicalddlmsg_identify(uint8 info)
+{
+ if ((info & ~XLR_INFO_MASK) == XLOG_LOGICAL_DDL_MESSAGE)
+ return "DDL MESSAGE";
+
+ return NULL;
+}
diff --git a/src/backend/access/rmgrdesc/meson.build b/src/backend/access/rmgrdesc/meson.build
index f3a6e0a571..3a70c974de 100644
--- a/src/backend/access/rmgrdesc/meson.build
+++ b/src/backend/access/rmgrdesc/meson.build
@@ -9,6 +9,7 @@ rmgr_desc_sources = files(
'gistdesc.c',
'hashdesc.c',
'heapdesc.c',
+ 'logicalddlmsgdesc.c',
'logicalmsgdesc.c',
'mxactdesc.c',
'nbtdesc.c',
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 59967098b3..721d023aa5 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -1005,6 +1005,7 @@ GetPublication(Oid pubid)
pub->pubactions.pubupdate = pubform->pubupdate;
pub->pubactions.pubdelete = pubform->pubdelete;
pub->pubactions.pubtruncate = pubform->pubtruncate;
+ pub->pubactions.pubddl = pubform->pubddl;
pub->pubviaroot = pubform->pubviaroot;
ReleaseSysCache(tup);
diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c
index 8d36b66488..0a54f30126 100644
--- a/src/backend/commands/event_trigger.c
+++ b/src/backend/commands/event_trigger.c
@@ -37,8 +37,11 @@
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "pgstat.h"
+#include "replication/ddlmessage.h"
+#include "replication/message.h"
#include "tcop/deparse_utility.h"
#include "tcop/utility.h"
+#include "tcop/ddl_deparse.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/evtcache.h"
@@ -1537,6 +1540,7 @@ EventTriggerAlterTableStart(Node *parsetree)
command->d.alterTable.classId = RelationRelationId;
command->d.alterTable.objectId = InvalidOid;
+ command->d.alterTable.rewrite = false;
command->d.alterTable.subcmds = NIL;
command->parsetree = copyObject(parsetree);
@@ -1570,7 +1574,7 @@ EventTriggerAlterTableRelid(Oid objectId)
* internally, so that's all that this code needs to handle at the moment.
*/
void
-EventTriggerCollectAlterTableSubcmd(Node *subcmd, ObjectAddress address)
+EventTriggerCollectAlterTableSubcmd(Node *subcmd, ObjectAddress address, bool rewrite)
{
MemoryContext oldcxt;
CollectedATSubcmd *newsub;
@@ -1590,6 +1594,7 @@ EventTriggerCollectAlterTableSubcmd(Node *subcmd, ObjectAddress address)
newsub->address = address;
newsub->parsetree = copyObject(subcmd);
+ currentEventTriggerState->currentCommand->d.alterTable.rewrite |= rewrite;
currentEventTriggerState->currentCommand->d.alterTable.subcmds =
lappend(currentEventTriggerState->currentCommand->d.alterTable.subcmds, newsub);
@@ -2175,3 +2180,280 @@ stringify_adefprivs_objtype(ObjectType objtype)
return "???"; /* keep compiler quiet */
}
+
+/*
+ * publication_deparse_ddl_command_start
+ *
+ * Deparse the ddl command and log it.
+ */
+Datum
+publication_deparse_ddl_command_start(PG_FUNCTION_ARGS)
+{
+ EventTriggerData *trigdata;
+ char *command = psprintf("Drop table command start");
+ DropStmt *stmt;
+ ListCell *cell1;
+
+ if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+ elog(ERROR, "not fired by event trigger manager");
+
+ trigdata = (EventTriggerData *) fcinfo->context;
+ stmt = (DropStmt *) trigdata->parsetree;
+
+ /* extract the relid from the parse tree */
+ foreach(cell1, stmt->objects)
+ {
+ char relpersist;
+ Node *object = lfirst(cell1);
+ ObjectAddress address;
+ Relation relation = NULL;
+
+ address = get_object_address(stmt->removeType,
+ object,
+ &relation,
+ AccessExclusiveLock,
+ true);
+
+ relpersist = get_rel_persistence(address.objectId);
+
+ /*
+ * Do not generate wal log for commands whose target table is a
+ * temporary table.
+ *
+ * We will generate wal logs for unlogged tables so that unlogged
+ * tables can also be created and altered on the subscriber side. This
+ * makes it possible to directly replay the SET LOGGED command and the
+ * incoming rewrite message without creating a new table.
+ */
+ if (relpersist == RELPERSISTENCE_TEMP)
+ {
+ table_close(relation, NoLock);
+ continue;
+ }
+
+ LogLogicalDDLMessage("deparse", address.objectId, DCT_TableDropStart,
+ command, strlen(command) + 1);
+
+ if (relation)
+ table_close(relation, NoLock);
+ }
+ return PointerGetDatum(NULL);
+}
+
+/*
+ * publication_deparse_table_rewrite
+ *
+ * Deparse the ddl table rewrite command and log it.
+ */
+Datum
+publication_deparse_table_rewrite(PG_FUNCTION_ARGS)
+{
+ char relpersist;
+ CollectedCommand *cmd;
+ char *json_string;
+
+ if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+ elog(ERROR, "not fired by event trigger manager");
+
+ cmd = currentEventTriggerState->currentCommand;
+
+ Assert(cmd && cmd->d.alterTable.rewrite);
+
+ relpersist = get_rel_persistence(cmd->d.alterTable.objectId);
+
+ /*
+ * Do not generate wal log for commands whose target table is a temporary
+ * table.
+ *
+ * We will generate wal logs for unlogged tables so that unlogged tables
+ * can also be created and altered on the subscriber side. This makes it
+ * possible to directly replay the SET LOGGED command and the incoming
+ * rewrite message without creating a new table.
+ */
+ if (relpersist == RELPERSISTENCE_TEMP)
+ return PointerGetDatum(NULL);
+
+ /* Deparse the DDL command and WAL log it to allow decoding of the same. */
+ json_string = deparse_utility_command(cmd, false);
+
+ if (json_string != NULL)
+ LogLogicalDDLMessage("deparse", cmd->d.alterTable.objectId, DCT_TableAlter,
+ json_string, strlen(json_string) + 1);
+
+ return PointerGetDatum(NULL);
+}
+
+/*
+ * publication_deparse_ddl_command_end
+ *
+ * Deparse the ddl command and log it.
+ */
+Datum
+publication_deparse_ddl_command_end(PG_FUNCTION_ARGS)
+{
+ ListCell *lc;
+ slist_iter iter;
+ DeparsedCommandType type;
+ Oid relid;
+
+ if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+ elog(ERROR, "not fired by event trigger manager");
+
+ foreach(lc, currentEventTriggerState->commandList)
+ {
+ char relpersist = RELPERSISTENCE_PERMANENT;
+ CollectedCommand *cmd = lfirst(lc);
+ char *json_string;
+
+ /* Rewrite DDL has been handled in table_rewrite trigger */
+ if (cmd->d.alterTable.rewrite)
+ {
+ RenameStmt *renameStmt = (RenameStmt *) cmd->parsetree;
+
+ if (renameStmt && renameStmt->relationType != OBJECT_TYPE &&
+ renameStmt->relationType != OBJECT_TABLE)
+ continue;
+ }
+
+ if (cmd->type == SCT_Simple &&
+ !OidIsValid(cmd->d.simple.address.objectId))
+ continue;
+
+ if (cmd->type == SCT_AlterTable)
+ {
+ relid = cmd->d.alterTable.objectId;
+ type = DCT_TableAlter;
+ }
+ else
+ {
+ /* Only SCT_Simple for now */
+ relid = cmd->d.simple.address.objectId;
+ type = DCT_SimpleCmd;
+ }
+
+ if (get_rel_relkind(relid))
+ relpersist = get_rel_persistence(relid);
+
+ /*
+ * Do not generate wal log for commands whose target table is a
+ * temporary table.
+ *
+ * We will generate wal logs for unlogged tables so that unlogged
+ * tables can also be created and altered on the subscriber side. This
+ * makes it possible to directly replay the SET LOGGED command and the
+ * incoming rewrite message without creating a new table.
+ */
+ if (relpersist == RELPERSISTENCE_TEMP)
+ continue;
+
+ /*
+ * Deparse the DDL command and WAL log it to allow decoding of the
+ * same.
+ */
+ json_string = deparse_utility_command(cmd, false);
+
+ if (json_string == NULL)
+ continue;
+
+ LogLogicalDDLMessage("deparse", relid, type, json_string,
+ strlen(json_string) + 1);
+ }
+
+ slist_foreach(iter, &(currentEventTriggerState->SQLDropList))
+ {
+ volatile SQLDropObject *obj;
+ DropStmt *stmt;
+ EventTriggerData *trigdata;
+ char *command;
+ DeparsedCommandType cmdtype;
+ const char *tmptype;
+
+ trigdata = (EventTriggerData *) fcinfo->context;
+ stmt = (DropStmt *) trigdata->parsetree;
+
+ obj = slist_container(SQLDropObject, next, iter.cur);
+
+ if (strcmp(obj->objecttype, "table") == 0)
+ cmdtype = DCT_TableDropEnd;
+ else if (strcmp(obj->objecttype, "sequence") == 0 ||
+ strcmp(obj->objecttype, "schema") == 0 ||
+ strcmp(obj->objecttype, "index") == 0 ||
+ strcmp(obj->objecttype, "function") == 0 ||
+ strcmp(obj->objecttype, "procedure") == 0 ||
+ strcmp(obj->objecttype, "operator") == 0 ||
+ strcmp(obj->objecttype, "operator class") == 0 ||
+ strcmp(obj->objecttype, "operator family") == 0 ||
+ strcmp(obj->objecttype, "cast") == 0 ||
+ strcmp(obj->objecttype, "type") == 0 ||
+ strcmp(obj->objecttype, "domain") == 0 ||
+ strcmp(obj->objecttype, "trigger") == 0 ||
+ strcmp(obj->objecttype, "conversion") == 0 ||
+ strcmp(obj->objecttype, "policy") == 0 ||
+ strcmp(obj->objecttype, "rule") == 0 ||
+ strcmp(obj->objecttype, "extension") == 0 ||
+ strcmp(obj->objecttype, "foreign-data wrapper") == 0 ||
+ strcmp(obj->objecttype, "text search configuration") == 0 ||
+ strcmp(obj->objecttype, "text search dictionary") == 0 ||
+ strcmp(obj->objecttype, "text search parser") == 0 ||
+ strcmp(obj->objecttype, "text search template") == 0 ||
+ strcmp(obj->objecttype, "transform") == 0 ||
+ strcmp(obj->objecttype, "server") == 0 ||
+ strcmp(obj->objecttype, "collation") == 0 ||
+ strcmp(obj->objecttype, "user mapping") == 0 ||
+ strcmp(obj->objecttype, "view") == 0 ||
+ strcmp(obj->objecttype, "materialized view") == 0 ||
+ strcmp(obj->objecttype, "statistics object") == 0)
+ cmdtype = DCT_ObjectDrop;
+ else
+ continue;
+
+ /* Change foreign-data wrapper to foreign data wrapper */
+ if (strncmp(obj->objecttype, "foreign-data wrapper", 20) == 0)
+ {
+ tmptype = pstrdup("foreign data wrapper");
+ command = deparse_drop_command(obj->objidentity, tmptype,
+ stmt->behavior);
+ }
+
+ /* Change statistics object to statistics */
+ else if (strncmp(obj->objecttype, "statistics object",
+ strlen("statistics object")) == 0)
+ {
+ tmptype = pstrdup("statistics");
+ command = deparse_drop_command(obj->objidentity, tmptype,
+ stmt->behavior);
+ }
+
+ /*
+ * object identity needs to be modified to make the drop work
+ *
+ * FROM: <role> on server <servername> TO : for >role> server
+ * <servername>
+ *
+ */
+ else if (strncmp(obj->objecttype, "user mapping", 12) == 0)
+ {
+ char *on_server;
+
+ tmptype = palloc(strlen(obj->objidentity) + 2);
+ on_server = strstr(obj->objidentity, "on server");
+
+ sprintf((char *) tmptype, "for ");
+ strncat((char *) tmptype, obj->objidentity, on_server - obj->objidentity);
+ strcat((char *) tmptype, on_server + 3);
+ command = deparse_drop_command(tmptype, obj->objecttype,
+ stmt->behavior);
+ }
+ else
+ command = deparse_drop_command(obj->objidentity, obj->objecttype,
+ stmt->behavior);
+
+ if (command == NULL)
+ continue;
+
+ LogLogicalDDLMessage("deparse", obj->address.objectId, cmdtype,
+ command, strlen(command) + 1);
+ }
+
+ return PointerGetDatum(NULL);
+}
diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c
index a8b75eb1be..13992551f2 100644
--- a/src/backend/commands/publicationcmds.c
+++ b/src/backend/commands/publicationcmds.c
@@ -37,10 +37,12 @@
#include "commands/publicationcmds.h"
#include "funcapi.h"
#include "miscadmin.h"
+#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_clause.h"
#include "parser/parse_collate.h"
#include "parser/parse_relation.h"
+#include "parser/parser.h"
#include "storage/lmgr.h"
#include "utils/acl.h"
#include "utils/array.h"
@@ -96,6 +98,7 @@ parse_publication_options(ParseState *pstate,
pubactions->pubupdate = true;
pubactions->pubdelete = true;
pubactions->pubtruncate = true;
+ pubactions->pubddl = false;
*publish_via_partition_root = false;
/* Parse options */
@@ -143,6 +146,8 @@ parse_publication_options(ParseState *pstate,
pubactions->pubdelete = true;
else if (strcmp(publish_opt, "truncate") == 0)
pubactions->pubtruncate = true;
+ else if (strcmp(publish_opt, "ddl") == 0)
+ pubactions->pubddl = true;
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -727,6 +732,53 @@ CheckPubRelationColumnList(char *pubname, List *tables,
}
}
+/*
+ * Create event trigger which is used for DDL replication.
+ */
+static void
+CreateDDLReplicaEventTrigger(char *eventname, CommandTag *commands,
+ int ncommands, ObjectAddress pubaddress,
+ Oid puboid)
+{
+ int i;
+ List *tags = NIL;
+ Oid trigger_id;
+ ObjectAddress referenced;
+ CreateEventTrigStmt *ddl_trigger;
+ char trigger_name[NAMEDATALEN];
+ char trigger_func_name[NAMEDATALEN];
+ static const char *trigger_name_prefix = "pg_deparse_trig_%s_%u";
+ static const char *trigger_func_prefix = "publication_deparse_%s";
+
+ ddl_trigger = makeNode(CreateEventTrigStmt);
+
+ snprintf(trigger_name, sizeof(trigger_name), trigger_name_prefix,
+ eventname, puboid);
+ snprintf(trigger_func_name, sizeof(trigger_func_name), trigger_func_prefix,
+ eventname);
+
+ ddl_trigger->trigname = pstrdup(trigger_name);
+ ddl_trigger->eventname = eventname;
+ ddl_trigger->funcname = SystemFuncName(trigger_func_name);
+
+ for (i = 0; i < ncommands; i++)
+ {
+ String *tag = makeString(pstrdup(GetCommandTagName(commands[i])));
+
+ tags = lappend(tags, tag);
+ }
+
+ ddl_trigger->whenclause = list_make1(makeDefElem("tag", (Node *) tags, -1));
+
+ trigger_id = CreateEventTrigger(ddl_trigger);
+
+ /*
+ * Register the event triggers as internally dependent on the publication.
+ */
+ ObjectAddressSet(referenced, EventTriggerRelationId, trigger_id);
+ recordDependencyOn(&referenced, &pubaddress, DEPENDENCY_INTERNAL);
+}
+
/*
* Create new publication.
*/
@@ -797,6 +849,8 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
BoolGetDatum(pubactions.pubdelete);
values[Anum_pg_publication_pubtruncate - 1] =
BoolGetDatum(pubactions.pubtruncate);
+ values[Anum_pg_publication_pubddl - 1] =
+ BoolGetDatum(pubactions.pubddl);
values[Anum_pg_publication_pubviaroot - 1] =
BoolGetDatum(publish_via_partition_root);
@@ -857,6 +911,123 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
}
}
+ /*
+ * Create an event trigger to allow logging of DDL statements.
+ *
+ * TODO: We need to find a better syntax to allow replication of DDL
+ * statements.
+ *
+ * XXX: This code is just to show the replication of CREATE/ALTER/DROP
+ * TABLE works. We need to enhance this once the approach for DDL
+ * replication is finalized.
+ */
+ if (pubactions.pubddl)
+ {
+ CommandTag start_commands[] = {CMDTAG_DROP_TABLE};
+ CommandTag rewrite_commands[] = {CMDTAG_ALTER_TABLE};
+
+ CommandTag end_commands[] = {
+ CMDTAG_CREATE_VIEW,
+ CMDTAG_ALTER_VIEW,
+ CMDTAG_DROP_VIEW,
+ CMDTAG_CREATE_MATERIALIZED_VIEW,
+ CMDTAG_ALTER_MATERIALIZED_VIEW,
+ CMDTAG_DROP_MATERIALIZED_VIEW,
+ CMDTAG_CREATE_USER_MAPPING,
+ CMDTAG_ALTER_USER_MAPPING,
+ CMDTAG_CREATE_SERVER,
+ CMDTAG_ALTER_SERVER,
+ CMDTAG_DROP_SERVER,
+ CMDTAG_CREATE_COLLATION,
+ CMDTAG_ALTER_COLLATION,
+ CMDTAG_DROP_COLLATION,
+ CMDTAG_CREATE_TRANSFORM,
+ CMDTAG_DROP_TRANSFORM,
+ CMDTAG_CREATE_FOREIGN_DATA_WRAPPER,
+ CMDTAG_ALTER_FOREIGN_DATA_WRAPPER,
+ CMDTAG_DROP_FOREIGN_DATA_WRAPPER,
+ CMDTAG_CREATE_EXTENSION,
+ CMDTAG_ALTER_EXTENSION,
+ CMDTAG_DROP_EXTENSION,
+ CMDTAG_CREATE_TEXT_SEARCH_CONFIGURATION,
+ CMDTAG_ALTER_TEXT_SEARCH_CONFIGURATION,
+ CMDTAG_DROP_TEXT_SEARCH_CONFIGURATION,
+ CMDTAG_CREATE_TEXT_SEARCH_DICTIONARY,
+ CMDTAG_ALTER_TEXT_SEARCH_DICTIONARY,
+ CMDTAG_DROP_TEXT_SEARCH_DICTIONARY,
+ CMDTAG_CREATE_TEXT_SEARCH_PARSER,
+ CMDTAG_DROP_TEXT_SEARCH_PARSER,
+ CMDTAG_CREATE_TEXT_SEARCH_TEMPLATE,
+ CMDTAG_DROP_TEXT_SEARCH_TEMPLATE,
+ CMDTAG_CREATE_POLICY,
+ CMDTAG_ALTER_POLICY,
+ CMDTAG_DROP_POLICY,
+ CMDTAG_CREATE_CONVERSION,
+ CMDTAG_ALTER_CONVERSION,
+ CMDTAG_DROP_CONVERSION,
+ CMDTAG_CREATE_DOMAIN,
+ CMDTAG_ALTER_DOMAIN,
+ CMDTAG_DROP_DOMAIN,
+ CMDTAG_ALTER_INDEX,
+ CMDTAG_CREATE_TYPE,
+ CMDTAG_DROP_TYPE,
+ CMDTAG_ALTER_TYPE,
+ CMDTAG_CREATE_CAST,
+ CMDTAG_ALTER_CAST,
+ CMDTAG_DROP_CAST,
+ CMDTAG_CREATE_OPERATOR,
+ CMDTAG_CREATE_OPERATOR_CLASS,
+ CMDTAG_CREATE_OPERATOR_FAMILY,
+ CMDTAG_ALTER_OPERATOR_FAMILY,
+ CMDTAG_ALTER_OPERATOR_CLASS,
+ CMDTAG_ALTER_OPERATOR,
+ CMDTAG_DROP_OPERATOR,
+ CMDTAG_DROP_OPERATOR_CLASS,
+ CMDTAG_DROP_OPERATOR_FAMILY,
+ CMDTAG_CREATE_PROCEDURE,
+ CMDTAG_ALTER_PROCEDURE,
+ CMDTAG_DROP_PROCEDURE,
+ CMDTAG_CREATE_FUNCTION,
+ CMDTAG_ALTER_FUNCTION,
+ CMDTAG_DROP_FUNCTION,
+ CMDTAG_CREATE_TRIGGER,
+ CMDTAG_ALTER_TRIGGER,
+ CMDTAG_DROP_TABLE,
+ CMDTAG_CREATE_TABLE,
+ CMDTAG_ALTER_TABLE,
+ CMDTAG_CREATE_SEQUENCE,
+ CMDTAG_ALTER_SEQUENCE,
+ CMDTAG_DROP_SEQUENCE,
+ CMDTAG_CREATE_SCHEMA,
+ CMDTAG_ALTER_SCHEMA,
+ CMDTAG_DROP_SCHEMA,
+ CMDTAG_CREATE_INDEX,
+ CMDTAG_DROP_INDEX,
+ CMDTAG_ALTER_INDEX,
+ CMDTAG_GRANT,
+ CMDTAG_REVOKE,
+ CMDTAG_CREATE_RULE,
+ CMDTAG_ALTER_RULE,
+ CMDTAG_DROP_RULE,
+ CMDTAG_REFRESH_MATERIALIZED_VIEW,
+ CMDTAG_CREATE_STATISTICS,
+ CMDTAG_ALTER_STATISTICS,
+ CMDTAG_DROP_STATISTICS
+ };
+
+ /* Create the ddl_command_end event trigger */
+ CreateDDLReplicaEventTrigger("ddl_command_end", end_commands,
+ lengthof(end_commands), myself, puboid);
+
+ /* Create the ddl_command_start event trigger */
+ CreateDDLReplicaEventTrigger("ddl_command_start", start_commands,
+ lengthof(start_commands), myself, puboid);
+
+ /* Create the table_rewrite event trigger */
+ CreateDDLReplicaEventTrigger("table_rewrite", rewrite_commands,
+ lengthof(rewrite_commands), myself, puboid);
+ }
+
table_close(rel, RowExclusiveLock);
InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
@@ -995,6 +1166,9 @@ AlterPublicationOptions(ParseState *pstate, AlterPublicationStmt *stmt,
values[Anum_pg_publication_pubtruncate - 1] = BoolGetDatum(pubactions.pubtruncate);
replaces[Anum_pg_publication_pubtruncate - 1] = true;
+
+ values[Anum_pg_publication_pubddl - 1] = BoolGetDatum(pubactions.pubddl);
+ replaces[Anum_pg_publication_pubddl - 1] = true;
}
if (publish_via_partition_root_given)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 20135ef1b0..e7cc0ad343 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5253,7 +5253,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
* Report the subcommand to interested event triggers.
*/
if (cmd)
- EventTriggerCollectAlterTableSubcmd((Node *) cmd, address);
+ EventTriggerCollectAlterTableSubcmd((Node *) cmd, address, tab->rewrite);
/*
* Bump the command counter to ensure the next subcommand in the sequence
diff --git a/src/backend/replication/logical/Makefile b/src/backend/replication/logical/Makefile
index c4e2fdeb71..f3eeb67312 100644
--- a/src/backend/replication/logical/Makefile
+++ b/src/backend/replication/logical/Makefile
@@ -16,6 +16,7 @@ override CPPFLAGS := -I$(srcdir) $(CPPFLAGS)
OBJS = \
decode.o \
+ ddlmessage.o\
launcher.o \
logical.o \
logicalfuncs.o \
diff --git a/src/backend/replication/logical/ddlmessage.c b/src/backend/replication/logical/ddlmessage.c
new file mode 100644
index 0000000000..5093523e9a
--- /dev/null
+++ b/src/backend/replication/logical/ddlmessage.c
@@ -0,0 +1,86 @@
+/*-------------------------------------------------------------------------
+ *
+ * ddlmessage.c
+ * Logical DDL messages.
+ *
+ * Copyright (c) 2022, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * src/backend/replication/logical/ddlmessage.c
+ *
+ * NOTES
+ *
+ * Logical DDL messages allow XLOG logging of DDL command strings that
+ * get passed to the logical decoding plugin. In normal XLOG processing they
+ * are same as NOOP.
+ *
+ * Unlike generic logical messages, these DDL messages have only transactional
+ * mode.Note by default DDLs in PostgreSQL are transactional.
+ *
+ * These messages are part of current transaction and will be sent to
+ * decoding plugin using in a same way as DML operations.
+ *
+ * Every message carries prefix to avoid conflicts between different decoding
+ * plugins. The plugin authors must take extra care to use unique prefix,
+ * good options seems to be for example to use the name of the extension.
+ *
+ * ---------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "access/xact.h"
+#include "access/xloginsert.h"
+#include "catalog/namespace.h"
+#include "miscadmin.h"
+#include "nodes/execnodes.h"
+#include "replication/logical.h"
+#include "replication/ddlmessage.h"
+#include "utils/memutils.h"
+
+/*
+ * Write logical decoding DDL message into XLog.
+ */
+XLogRecPtr
+LogLogicalDDLMessage(const char *prefix, Oid relid, DeparsedCommandType cmdtype,
+ const char *message, size_t size)
+{
+ xl_logical_ddl_message xlrec;
+
+ /*
+ * Ensure we have a valid transaction id.
+ */
+ Assert(IsTransactionState());
+ GetCurrentTransactionId();
+
+ xlrec.dbId = MyDatabaseId;
+ /* trailing zero is critical; see logicalddlmsg_desc */
+ xlrec.prefix_size = strlen(prefix) + 1;
+ xlrec.message_size = size;
+ xlrec.relid = relid;
+ xlrec.cmdtype = cmdtype;
+
+ XLogBeginInsert();
+ XLogRegisterData((char *) &xlrec, SizeOfLogicalDDLMessage);
+ XLogRegisterData(unconstify(char *, prefix), xlrec.prefix_size);
+ XLogRegisterData(unconstify(char *, message), size);
+
+ /* allow origin filtering */
+ XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
+
+ return XLogInsert(RM_LOGICALDDLMSG_ID, XLOG_LOGICAL_DDL_MESSAGE);
+}
+
+/*
+ * Redo is basically just noop for logical decoding ddl messages.
+ */
+void
+logicalddlmsg_redo(XLogReaderState *record)
+{
+ uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
+
+ if (info != XLOG_LOGICAL_DDL_MESSAGE)
+ elog(PANIC, "logicalddlmsg_redo: unknown op code %u", info);
+
+ /* This is only interesting for logical decoding, see decode.c. */
+}
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 2cc0ac9eb0..1f2c751759 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -36,6 +36,7 @@
#include "access/xlogutils.h"
#include "catalog/pg_control.h"
#include "replication/decode.h"
+#include "replication/ddlmessage.h"
#include "replication/logical.h"
#include "replication/message.h"
#include "replication/origin.h"
@@ -603,6 +604,46 @@ logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
message->message + message->prefix_size);
}
+/*
+ * Handle rmgr LOGICALDDLMSG_ID records for DecodeRecordIntoReorderBuffer().
+ */
+void
+logicalddlmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
+{
+ SnapBuild *builder = ctx->snapshot_builder;
+ XLogReaderState *r = buf->record;
+ TransactionId xid = XLogRecGetXid(r);
+ uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
+ RepOriginId origin_id = XLogRecGetOrigin(r);
+ xl_logical_ddl_message *message;
+
+ if (info != XLOG_LOGICAL_DDL_MESSAGE)
+ elog(ERROR, "unexpected RM_LOGICALDDLMSG_ID record type: %u", info);
+
+ ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
+
+ /*
+ * If we don't have snapshot or we are just fast-forwarding, there is no
+ * point in decoding ddl messages.
+ */
+ if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT ||
+ ctx->fast_forward)
+ return;
+
+ message = (xl_logical_ddl_message *) XLogRecGetData(r);
+
+ if (message->dbId != ctx->slot->data.database ||
+ FilterByOrigin(ctx, origin_id))
+ return;
+
+ if (SnapBuildProcessChange(builder, xid, buf->origptr))
+ ReorderBufferQueueDDLMessage(ctx->reorder, xid, buf->endptr,
+ message->message, /* first part of message is prefix */
+ message->message_size,
+ message->message + message->prefix_size,
+ message->relid, message->cmdtype);
+}
+
/*
* Consolidated commit record handling between the different form of commit
* records.
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 625a7f4273..98969c7aec 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -33,6 +33,7 @@
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
+#include "replication/ddlmessage.h"
#include "replication/decode.h"
#include "replication/logical.h"
#include "replication/origin.h"
@@ -73,6 +74,10 @@ static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
XLogRecPtr message_lsn, bool transactional,
const char *prefix, Size message_size, const char *message);
+static void ddlmessage_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn, const char *prefix,
+ Oid relid, DeparsedCommandType cmdtype,
+ Size message_size, const char *message);
/* streaming callbacks */
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
@@ -90,6 +95,11 @@ static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn
static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
XLogRecPtr message_lsn, bool transactional,
const char *prefix, Size message_size, const char *message);
+static void stream_ddlmessage_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix,
+ Oid relid, DeparsedCommandType cmdtype,
+ Size message_size, const char *message);
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
int nrelations, Relation relations[], ReorderBufferChange *change);
@@ -218,6 +228,7 @@ StartupDecodingContext(List *output_plugin_options,
ctx->reorder->apply_truncate = truncate_cb_wrapper;
ctx->reorder->commit = commit_cb_wrapper;
ctx->reorder->message = message_cb_wrapper;
+ ctx->reorder->ddlmessage = ddlmessage_cb_wrapper;
/*
* To support streaming, we require start/stop/abort/commit/change
@@ -234,6 +245,7 @@ StartupDecodingContext(List *output_plugin_options,
(ctx->callbacks.stream_commit_cb != NULL) ||
(ctx->callbacks.stream_change_cb != NULL) ||
(ctx->callbacks.stream_message_cb != NULL) ||
+ (ctx->callbacks.stream_ddlmessage_cb != NULL) ||
(ctx->callbacks.stream_truncate_cb != NULL);
/*
@@ -251,6 +263,7 @@ StartupDecodingContext(List *output_plugin_options,
ctx->reorder->stream_commit = stream_commit_cb_wrapper;
ctx->reorder->stream_change = stream_change_cb_wrapper;
ctx->reorder->stream_message = stream_message_cb_wrapper;
+ ctx->reorder->stream_ddlmessage = stream_ddlmessage_cb_wrapper;
ctx->reorder->stream_truncate = stream_truncate_cb_wrapper;
@@ -1220,6 +1233,44 @@ message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
error_context_stack = errcallback.previous;
}
+static void
+ddlmessage_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix, Oid relid, DeparsedCommandType cmdtype,
+ Size message_size,
+ const char *message)
+{
+ LogicalDecodingContext *ctx = cache->private_data;
+ LogicalErrorCallbackState state;
+ ErrorContextCallback errcallback;
+
+ Assert(!ctx->fast_forward);
+
+ if (ctx->callbacks.ddlmessage_cb == NULL)
+ return;
+
+ /* Push callback + info on the error context stack */
+ state.ctx = ctx;
+ state.callback_name = "ddlmessage";
+ state.report_location = message_lsn;
+ errcallback.callback = output_plugin_error_callback;
+ errcallback.arg = (void *) &state;
+ errcallback.previous = error_context_stack;
+ error_context_stack = &errcallback;
+
+ /* set output state */
+ ctx->accept_writes = true;
+ ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
+ ctx->write_location = message_lsn;
+
+ /* do the actual work: call callback */
+ ctx->callbacks.ddlmessage_cb(ctx, txn, message_lsn, prefix, relid, cmdtype,
+ message_size, message);
+
+ /* Pop the error context stack */
+ error_context_stack = errcallback.previous;
+}
+
static void
stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
XLogRecPtr first_lsn)
@@ -1535,6 +1586,48 @@ stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
error_context_stack = errcallback.previous;
}
+static void
+stream_ddlmessage_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix, Oid relid, DeparsedCommandType cmdtype,
+ Size message_size,
+ const char *message)
+{
+ LogicalDecodingContext *ctx = cache->private_data;
+ LogicalErrorCallbackState state;
+ ErrorContextCallback errcallback;
+
+ Assert(!ctx->fast_forward);
+
+ /* We're only supposed to call this when streaming is supported. */
+ Assert(ctx->streaming);
+
+ /* this callback is optional */
+ if (ctx->callbacks.stream_ddlmessage_cb == NULL)
+ return;
+
+ /* Push callback + info on the error context stack */
+ state.ctx = ctx;
+ state.callback_name = "stream_ddlmessage";
+ state.report_location = message_lsn;
+ errcallback.callback = output_plugin_error_callback;
+ errcallback.arg = (void *) &state;
+ errcallback.previous = error_context_stack;
+ error_context_stack = &errcallback;
+
+ /* set output state */
+ ctx->accept_writes = true;
+ ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
+ ctx->write_location = message_lsn;
+
+ /* do the actual work: call callback */
+ ctx->callbacks.stream_ddlmessage_cb(ctx, txn, message_lsn, prefix, relid,
+ cmdtype, message_size, message);
+
+ /* Pop the error context stack */
+ error_context_stack = errcallback.previous;
+}
+
static void
stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
int nrelations, Relation relations[],
diff --git a/src/backend/replication/logical/meson.build b/src/backend/replication/logical/meson.build
index 773583a12b..e7c70aa261 100644
--- a/src/backend/replication/logical/meson.build
+++ b/src/backend/replication/logical/meson.build
@@ -1,4 +1,5 @@
backend_sources += files(
+ 'ddlmessage.c',
'decode.c',
'launcher.c',
'logical.c',
diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c
index ff8513e2d2..f35406275b 100644
--- a/src/backend/replication/logical/proto.c
+++ b/src/backend/replication/logical/proto.c
@@ -662,6 +662,52 @@ logicalrep_write_message(StringInfo out, TransactionId xid, XLogRecPtr lsn,
pq_sendbytes(out, message, sz);
}
+/*
+ * Read DDL MESSAGE from stream
+ */
+char *
+logicalrep_read_ddlmessage(StringInfo in, XLogRecPtr *lsn,
+ const char **prefix,
+ Size *sz)
+{
+ uint8 flags;
+ char *msg;
+
+ //TODO double check when do we need to get TransactionId.
+
+ flags = pq_getmsgint(in, 1);
+ if (flags != 0)
+ elog(ERROR, "unrecognized flags %u in ddl message", flags);
+ *lsn = pq_getmsgint64(in);
+ *prefix = pq_getmsgstring(in);
+ *sz = pq_getmsgint(in, 4);
+ msg = (char *) pq_getmsgbytes(in, *sz);
+
+ return msg;
+}
+
+/*
+ * Write DDL MESSAGE to stream
+ */
+void
+logicalrep_write_ddlmessage(StringInfo out, TransactionId xid, XLogRecPtr lsn,
+ const char *prefix, Size sz, const char *message)
+{
+ uint8 flags = 0;
+
+ pq_sendbyte(out, LOGICAL_REP_MSG_DDLMESSAGE);
+
+ /* transaction ID (if not valid, we're not streaming) */
+ if (TransactionIdIsValid(xid))
+ pq_sendint32(out, xid);
+
+ pq_sendint8(out, flags);
+ pq_sendint64(out, lsn);
+ pq_sendstring(out, prefix);
+ pq_sendint32(out, sz);
+ pq_sendbytes(out, message, sz);
+}
+
/*
* Write relation description to the output stream.
*/
@@ -1218,6 +1264,8 @@ logicalrep_message_type(LogicalRepMsgType action)
return "TYPE";
case LOGICAL_REP_MSG_MESSAGE:
return "MESSAGE";
+ case LOGICAL_REP_MSG_DDLMESSAGE:
+ return "DDL";
case LOGICAL_REP_MSG_BEGIN_PREPARE:
return "BEGIN PREPARE";
case LOGICAL_REP_MSG_PREPARE:
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 6dff9915a5..3c37690c32 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -94,6 +94,7 @@
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
+#include "replication/ddlmessage.h"
#include "replication/logical.h"
#include "replication/reorderbuffer.h"
#include "replication/slot.h"
@@ -515,6 +516,14 @@ ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBufferChange *change,
pfree(change->data.msg.message);
change->data.msg.message = NULL;
break;
+ case REORDER_BUFFER_CHANGE_DDLMESSAGE:
+ if (change->data.ddlmsg.prefix != NULL)
+ pfree(change->data.ddlmsg.prefix);
+ change->data.ddlmsg.prefix = NULL;
+ if (change->data.ddlmsg.message != NULL)
+ pfree(change->data.ddlmsg.message);
+ change->data.ddlmsg.message = NULL;
+ break;
case REORDER_BUFFER_CHANGE_INVALIDATION:
if (change->data.inval.invalidations)
pfree(change->data.inval.invalidations);
@@ -869,6 +878,36 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
}
}
+/*
+ * A transactional DDL message is queued to be processed upon commit.
+ */
+void
+ReorderBufferQueueDDLMessage(ReorderBuffer *rb, TransactionId xid,
+ XLogRecPtr lsn, const char *prefix,
+ Size message_size, const char *message,
+ Oid relid, DeparsedCommandType cmdtype)
+{
+ MemoryContext oldcontext;
+ ReorderBufferChange *change;
+
+ Assert(xid != InvalidTransactionId);
+
+ oldcontext = MemoryContextSwitchTo(rb->context);
+
+ change = ReorderBufferGetChange(rb);
+ change->action = REORDER_BUFFER_CHANGE_DDLMESSAGE;
+ change->data.ddlmsg.prefix = pstrdup(prefix);
+ change->data.ddlmsg.relid = relid;
+ change->data.ddlmsg.cmdtype = cmdtype;
+ change->data.ddlmsg.message_size = message_size;
+ change->data.ddlmsg.message = palloc(message_size);
+ memcpy(change->data.ddlmsg.message, message, message_size);
+
+ ReorderBufferQueueChange(rb, xid, lsn, change, false);
+
+ MemoryContextSwitchTo(oldcontext);
+}
+
/*
* AssertTXNLsnOrder
* Verify LSN ordering of transaction lists in the reorderbuffer
@@ -1968,6 +2007,29 @@ ReorderBufferApplyMessage(ReorderBuffer *rb, ReorderBufferTXN *txn,
change->data.msg.message);
}
+/*
+ * Helper function for ReorderBufferProcessTXN for applying the DDL message.
+ */
+static inline void
+ReorderBufferApplyDDLMessage(ReorderBuffer *rb, ReorderBufferTXN *txn,
+ ReorderBufferChange *change, bool streaming)
+{
+ if (streaming)
+ rb->stream_ddlmessage(rb, txn, change->lsn,
+ change->data.ddlmsg.prefix,
+ change->data.ddlmsg.relid,
+ change->data.ddlmsg.cmdtype,
+ change->data.ddlmsg.message_size,
+ change->data.ddlmsg.message);
+ else
+ rb->ddlmessage(rb, txn, change->lsn,
+ change->data.ddlmsg.prefix,
+ change->data.ddlmsg.relid,
+ change->data.ddlmsg.cmdtype,
+ change->data.ddlmsg.message_size,
+ change->data.ddlmsg.message);
+}
+
/*
* Function to store the command id and snapshot at the end of the current
* stream so that we can reuse the same while sending the next stream.
@@ -2348,6 +2410,10 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
ReorderBufferApplyMessage(rb, txn, change, streaming);
break;
+ case REORDER_BUFFER_CHANGE_DDLMESSAGE:
+ ReorderBufferApplyDDLMessage(rb, txn, change, streaming);
+ break;
+
case REORDER_BUFFER_CHANGE_INVALIDATION:
/* Execute the invalidation messages locally */
ReorderBufferExecuteInvalidations(change->data.inval.ninvalidations,
@@ -3771,6 +3837,40 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
change->data.msg.message_size);
data += change->data.msg.message_size;
+ break;
+ }
+ case REORDER_BUFFER_CHANGE_DDLMESSAGE:
+ {
+ char *data;
+ Size prefix_size = strlen(change->data.ddlmsg.prefix) + 1;
+
+ sz += prefix_size + change->data.ddlmsg.message_size +
+ sizeof(Size) + sizeof(Oid) + sizeof(int) + sizeof(Size);
+ ReorderBufferSerializeReserve(rb, sz);
+
+ data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
+
+ /* might have been reallocated above */
+ ondisk = (ReorderBufferDiskChange *) rb->outbuf;
+
+ /* write the prefix, relid and cmdtype including the size */
+ memcpy(data, &prefix_size, sizeof(Size));
+ data += sizeof(Size);
+ memcpy(data, &change->data.ddlmsg.relid, sizeof(Oid));
+ data += sizeof(Oid);
+ memcpy(data, &change->data.ddlmsg.cmdtype, sizeof(int));
+ data += sizeof(int);
+ memcpy(data, change->data.ddlmsg.prefix,
+ prefix_size);
+ data += prefix_size;
+
+ /* write the message including the size */
+ memcpy(data, &change->data.ddlmsg.message_size, sizeof(Size));
+ data += sizeof(Size);
+ memcpy(data, change->data.ddlmsg.message,
+ change->data.ddlmsg.message_size);
+ data += change->data.ddlmsg.message_size;
+
break;
}
case REORDER_BUFFER_CHANGE_INVALIDATION:
@@ -4085,6 +4185,15 @@ ReorderBufferChangeSize(ReorderBufferChange *change)
sz += prefix_size + change->data.msg.message_size +
sizeof(Size) + sizeof(Size);
+ break;
+ }
+ case REORDER_BUFFER_CHANGE_DDLMESSAGE:
+ {
+ Size prefix_size = strlen(change->data.ddlmsg.prefix) + 1;
+
+ sz += prefix_size + change->data.ddlmsg.message_size +
+ sizeof(Size) + sizeof(Size) + sizeof(Oid) + sizeof(int);
+
break;
}
case REORDER_BUFFER_CHANGE_INVALIDATION:
@@ -4360,6 +4469,33 @@ ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
change->data.msg.message_size);
data += change->data.msg.message_size;
+ break;
+ }
+ case REORDER_BUFFER_CHANGE_DDLMESSAGE:
+ {
+ Size prefix_size;
+
+ /* read prefix */
+ memcpy(&prefix_size, data, sizeof(Size));
+ data += sizeof(Size);
+ memcpy(&change->data.ddlmsg.relid, data, sizeof(Oid));
+ data += sizeof(Oid);
+ memcpy(&change->data.ddlmsg.cmdtype, data, sizeof(int));
+ data += sizeof(int);
+ change->data.ddlmsg.prefix = MemoryContextAlloc(rb->context, prefix_size);
+ memcpy(change->data.ddlmsg.prefix, data, prefix_size);
+ Assert(change->data.ddlmsg.prefix[prefix_size - 1] == '\0');
+ data += prefix_size;
+
+ /* read the message */
+ memcpy(&change->data.msg.message_size, data, sizeof(Size));
+ data += sizeof(Size);
+ change->data.msg.message = MemoryContextAlloc(rb->context,
+ change->data.msg.message_size);
+ memcpy(change->data.msg.message, data,
+ change->data.msg.message_size);
+ data += change->data.msg.message_size;
+
break;
}
case REORDER_BUFFER_CHANGE_INVALIDATION:
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 5250ae7f54..be762b93bc 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -156,6 +156,7 @@
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "optimizer/optimizer.h"
+#include "parser/analyze.h"
#include "pgstat.h"
#include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
@@ -179,7 +180,10 @@
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
+#include "tcop/ddl_deparse.h"
+#include "tcop/pquery.h"
#include "tcop/tcopprot.h"
+#include "tcop/utility.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
@@ -2488,6 +2492,232 @@ apply_handle_truncate(StringInfo s)
end_replication_step();
}
+/* Remove the data population from the command */
+static void
+preprocess_create_table(RawStmt *command)
+{
+ CommandTag commandTag;
+
+ commandTag = CreateCommandTag((Node *) command);
+
+ switch (commandTag)
+ {
+ case CMDTAG_CREATE_TABLE_AS:
+ case CMDTAG_SELECT_INTO:
+ {
+ CreateTableAsStmt *castmt = (CreateTableAsStmt *) command->stmt;
+
+ if (castmt->objtype == OBJECT_TABLE)
+ {
+ /*
+ * Force skipping data population to avoid data
+ * inconsistency. Data should be replicated from the
+ * publisher instead.
+ */
+ castmt->into->skipData = true;
+ }
+ }
+ break;
+ case CMDTAG_SELECT:
+ {
+ SelectStmt *sstmt = (SelectStmt *) command->stmt;
+
+ if (sstmt->intoClause != NULL)
+ {
+ /*
+ * Force skipping data population to avoid data
+ * inconsistency. Data should be replicated from the
+ * publisher instead.
+ */
+ sstmt->intoClause->skipData = true;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+/*
+ * Handle CREATE TABLE command
+ *
+ * Call AddSubscriptionRelState for CREATE TABEL command to set the relstate to
+ * SUBREL_STATE_READY so DML changes on this new table can be replicated without
+ * having to manually run "alter subscription ... refresh publication"
+ */
+static void
+handle_create_table(RawStmt *command)
+{
+ CommandTag commandTag;
+ RangeVar *rv = NULL;
+ Oid relid;
+ Oid relnamespace = InvalidOid;
+ char *schemaname = NULL;
+ char *relname = NULL;
+
+ commandTag = CreateCommandTag((Node *) command);
+
+ switch (commandTag)
+ {
+ case CMDTAG_CREATE_TABLE:
+ {
+ CreateStmt *cstmt = (CreateStmt *) command->stmt;
+
+ rv = cstmt->relation;
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (!rv)
+ return;
+
+ schemaname = rv->schemaname;
+ relname = rv->relname;
+
+ if (schemaname != NULL)
+ relnamespace = get_namespace_oid(schemaname, false);
+
+ if (relnamespace != InvalidOid)
+ relid = get_relname_relid(relname, relnamespace);
+ else
+ relid = RelnameGetRelid(relname);
+
+ if (relid != InvalidOid)
+ {
+ AddSubscriptionRelState(MySubscription->oid, relid,
+ SUBREL_STATE_READY,
+ InvalidXLogRecPtr);
+ ereport(DEBUG1,
+ (errmsg_internal("table \"%s\" added to subscription \"%s\"",
+ relname, MySubscription->name)));
+ }
+}
+
+static void
+apply_handle_ddl(StringInfo s)
+{
+ XLogRecPtr lsn;
+ const char *prefix = NULL;
+ char *message = NULL;
+ char *ddl_command;
+ Size sz;
+ List *parsetree_list;
+ ListCell *parsetree_item;
+ DestReceiver *receiver;
+ MemoryContext oldcontext;
+ const char *save_debug_query_string = debug_query_string;
+
+ message = logicalrep_read_ddlmessage(s, &lsn, &prefix, &sz);
+
+ /* Make sure we are in a transaction command */
+ begin_replication_step();
+
+ ddl_command = ddl_deparse_json_to_string(message);
+ debug_query_string = ddl_command;
+
+ /* DestNone for logical replication */
+ receiver = CreateDestReceiver(DestNone);
+ parsetree_list = pg_parse_query(ddl_command);
+
+ foreach(parsetree_item, parsetree_list)
+ {
+ List *plantree_list;
+ List *querytree_list;
+ RawStmt *command = (RawStmt *) lfirst(parsetree_item);
+ CommandTag commandTag;
+ MemoryContext per_parsetree_context = NULL;
+ Portal portal;
+ bool snapshot_set = false;
+
+ commandTag = CreateCommandTag((Node *) command);
+
+ /* If we got a cancel signal in parsing or prior command, quit */
+ CHECK_FOR_INTERRUPTS();
+
+ /* Remove data population from the command */
+ preprocess_create_table(command);
+
+ /*
+ * Set up a snapshot if parse analysis/planning will need one.
+ */
+ if (analyze_requires_snapshot(command))
+ {
+ PushActiveSnapshot(GetTransactionSnapshot());
+ snapshot_set = true;
+ }
+
+ /*
+ * We do the work for each parsetree in a short-lived context, to
+ * limit the memory used when there are many commands in the string.
+ */
+ per_parsetree_context =
+ AllocSetContextCreate(CurrentMemoryContext,
+ "execute_sql_string per-statement context",
+ ALLOCSET_DEFAULT_SIZES);
+ oldcontext = MemoryContextSwitchTo(per_parsetree_context);
+
+ querytree_list = pg_analyze_and_rewrite_fixedparams(command,
+ ddl_command,
+ NULL, 0, NULL);
+
+ plantree_list = pg_plan_queries(querytree_list, ddl_command, 0, NULL);
+
+ /* Done with the snapshot used for parsing/planning */
+ if (snapshot_set)
+ PopActiveSnapshot();
+
+ portal = CreatePortal("logical replication", true, true);
+
+ /*
+ * We don't have to copy anything into the portal, because everything
+ * we are passing here is in ApplyMessageContext or the
+ * per_parsetree_context, and so will outlive the portal anyway.
+ */
+ PortalDefineQuery(portal,
+ NULL,
+ ddl_command,
+ commandTag,
+ plantree_list,
+ NULL);
+
+ /*
+ * Start the portal. No parameters here.
+ */
+ PortalStart(portal, NULL, 0, InvalidSnapshot);
+
+ /*
+ * Switch back to transaction context for execution.
+ */
+ MemoryContextSwitchTo(oldcontext);
+
+ (void) PortalRun(portal,
+ FETCH_ALL,
+ true,
+ true,
+ receiver,
+ receiver,
+ NULL);
+
+ PortalDrop(portal, false);
+
+ CommandCounterIncrement();
+
+ /*
+ * Table created by DDL replication (database level) is automatically
+ * added to the subscription here.
+ */
+ handle_create_table(command);
+
+ /* Now we may drop the per-parsetree context, if one was created. */
+ MemoryContextDelete(per_parsetree_context);
+ }
+
+ debug_query_string = save_debug_query_string;
+ end_replication_step();
+}
+
/*
* Logical replication protocol message dispatcher.
@@ -2553,6 +2783,10 @@ apply_dispatch(StringInfo s)
*/
break;
+ case LOGICAL_REP_MSG_DDLMESSAGE:
+ apply_handle_ddl(s);
+ break;
+
case LOGICAL_REP_MSG_STREAM_START:
apply_handle_stream_start(s);
break;
diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c
index 2ecaa5b907..b1cda6f630 100644
--- a/src/backend/replication/pgoutput/pgoutput.c
+++ b/src/backend/replication/pgoutput/pgoutput.c
@@ -53,6 +53,11 @@ static void pgoutput_message(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr message_lsn,
bool transactional, const char *prefix,
Size sz, const char *message);
+static void pgoutput_ddlmessage(LogicalDecodingContext *ctx,
+ ReorderBufferTXN *txn, XLogRecPtr message_lsn,
+ const char *prefix, Oid relid,
+ DeparsedCommandType cmdtype,
+ Size sz, const char *message);
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx,
RepOriginId origin_id);
static void pgoutput_begin_prepare_txn(LogicalDecodingContext *ctx,
@@ -256,6 +261,7 @@ _PG_output_plugin_init(OutputPluginCallbacks *cb)
cb->change_cb = pgoutput_change;
cb->truncate_cb = pgoutput_truncate;
cb->message_cb = pgoutput_message;
+ cb->ddlmessage_cb = pgoutput_ddlmessage;
cb->commit_cb = pgoutput_commit_txn;
cb->begin_prepare_cb = pgoutput_begin_prepare_txn;
@@ -272,6 +278,7 @@ _PG_output_plugin_init(OutputPluginCallbacks *cb)
cb->stream_commit_cb = pgoutput_stream_commit;
cb->stream_change_cb = pgoutput_change;
cb->stream_message_cb = pgoutput_message;
+ cb->stream_ddlmessage_cb = pgoutput_ddlmessage;
cb->stream_truncate_cb = pgoutput_truncate;
/* transaction streaming - two-phase commit */
cb->stream_prepare_cb = pgoutput_stream_prepare_txn;
@@ -426,6 +433,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
/* This plugin uses binary protocol. */
opt->output_type = OUTPUT_PLUGIN_BINARY_OUTPUT;
+ opt->receive_rewrites = true;
/*
* This is replication start and not slot initialization.
@@ -499,6 +507,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
/* Init publication state. */
data->publications = NIL;
+ data->deleted_relids = NIL;
publications_valid = false;
CacheRegisterSyscacheCallback(PUBLICATIONOID,
publication_invalidation_cb,
@@ -1377,9 +1386,22 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
ReorderBufferChangeType action = change->action;
TupleTableSlot *old_slot = NULL;
TupleTableSlot *new_slot = NULL;
+ bool table_rewrite = false;
update_replication_progress(ctx, false);
+ /*
+ * For heap rewrites, we might need to replicate them if the rewritten
+ * table publishes rewrite ddl message. So get the actual relation here
+ * and check the pubaction later.
+ */
+ if (relation->rd_rel->relrewrite)
+ {
+ table_rewrite = true;
+ relation = RelationIdGetRelation(relation->rd_rel->relrewrite);
+ targetrel = relation;
+ }
+
if (!is_publishable_relation(relation))
return;
@@ -1413,6 +1435,13 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
Assert(false);
}
+ /*
+ * We don't publish table rewrite change unless we publish the rewrite ddl
+ * message.
+ */
+ if (table_rewrite && !relentry->pubactions.pubddl)
+ return;
+
/* Avoid leaking memory by using and resetting our own context */
old = MemoryContextSwitchTo(data->context);
@@ -1442,8 +1471,8 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
}
/* Check row filter */
- if (!pgoutput_row_filter(targetrel, NULL, &new_slot, relentry,
- &action))
+ if (!table_rewrite &&
+ !pgoutput_row_filter(targetrel, NULL, &new_slot, relentry, &action))
break;
/*
@@ -1463,8 +1492,19 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
maybe_send_schema(ctx, change, relation, relentry);
OutputPluginPrepareWrite(ctx, true);
- logicalrep_write_insert(ctx->out, xid, targetrel, new_slot,
- data->binary, relentry->columns);
+
+ /*
+ * Convert the rewrite inserts to updates so that the subscriber
+ * can replay it. This is needed to make sure the data between
+ * publisher and subscriber is consistent.
+ */
+ if (table_rewrite)
+ logicalrep_write_update(ctx->out, xid, targetrel,
+ NULL, new_slot, data->binary,
+ relentry->columns);
+ else
+ logicalrep_write_insert(ctx->out, xid, targetrel, new_slot,
+ data->binary, relentry->columns);
OutputPluginWrite(ctx, true);
break;
case REORDER_BUFFER_CHANGE_UPDATE:
@@ -1594,6 +1634,9 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
ancestor = NULL;
}
+ if (table_rewrite)
+ RelationClose(relation);
+
/* Cleanup */
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
@@ -1671,8 +1714,8 @@ pgoutput_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
static void
pgoutput_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
- XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz,
- const char *message)
+ XLogRecPtr message_lsn, bool transactional,
+ const char *prefix, Size sz, const char *message)
{
PGOutputData *data = (PGOutputData *) ctx->output_plugin_private;
TransactionId xid = InvalidTransactionId;
@@ -1712,6 +1755,139 @@ pgoutput_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
OutputPluginWrite(ctx, true);
}
+static void
+pgoutput_ddlmessage(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix, Oid relid, DeparsedCommandType cmdtype,
+ Size sz, const char *message)
+{
+ PGOutputData *data = (PGOutputData *) ctx->output_plugin_private;
+ PGOutputTxnData *txndata = (PGOutputTxnData *) txn->output_plugin_private;
+ Relation relation = NULL;
+ TransactionId xid = InvalidTransactionId;
+ RelationSyncEntry *relentry;
+
+ /*
+ * Remember the xid for the message in streaming mode. See
+ * pgoutput_change.
+ */
+ if (in_streaming)
+ xid = txn->xid;
+
+ switch (cmdtype)
+ {
+ case DCT_TableDropStart:
+
+ /*
+ * On DROP start, add the relid to a deleted_relid list if the
+ * relid is part of a publication that supports ddl publication.
+ * We need this because on DROP end, the relid will no longer be
+ * valid. Later on Drop end, verify that the drop is for a relid
+ * that is on the deleted_rid list, and only then send the ddl
+ * message.
+ */
+ relation = RelationIdGetRelation(relid);
+
+ Assert(relation);
+ relentry = get_rel_sync_entry(data, relation);
+
+ if (relentry->pubactions.pubddl)
+ data->deleted_relids = lappend_oid(data->deleted_relids, relid);
+
+ RelationClose(relation);
+ return;
+ case DCT_TableDropEnd:
+ if (!list_member_oid(data->deleted_relids, relid))
+ return;
+ else
+ data->deleted_relids = list_delete_oid(data->deleted_relids, relid);
+ break;
+ case DCT_TableAlter:
+
+ /*
+ * For table rewrite ddl, we first send the original ddl message
+ * to subscriber, then convert the upcoming rewrite INSERT to
+ * UPDATE and send them to subscriber so that the data between
+ * publisher and subscriber can always be consistent.
+ *
+ * We do this way because of two reason:
+ *
+ * (1) The data before the rewrite ddl could already be different
+ * among publisher and subscriber. To make sure the extra data in
+ * subscriber which doesn't exist in publisher also get rewritten,
+ * we need to let the subscriber execute the original rewrite ddl
+ * to rewrite all the data at first.
+ *
+ * (2) the data after executing rewrite ddl could be different
+ * among publisher and subscriber(due to different
+ * functions/operators used during rewrite), so we need to
+ * replicate the rewrite UPDATEs to keep the data consistent.
+ *
+ * TO IMPROVE: We could improve this by letting the subscriber
+ * only rewrite the extra data instead of doing fully rewrite and
+ * use the upcoming rewrite UPDATEs to rewrite the rest data.
+ * Besides, we may not need to send rewrite changes for all type
+ * of rewrite ddl, for example, it seems fine to skip sending
+ * rewrite changes for ALTER TABLE SET LOGGED as the data in the
+ * table doesn't actually be changed.
+ */
+ relation = RelationIdGetRelation(relid);
+ Assert(relation);
+
+ relentry = get_rel_sync_entry(data, relation);
+
+ /*
+ * Skip sending this ddl if we don't publish ddl message or the
+ * ddl need to be published via its root relation.
+ */
+ if (!relentry->pubactions.pubddl ||
+ relentry->publish_as_relid != relid)
+ {
+ RelationClose(relation);
+ return;
+ }
+
+ break;
+ case DCT_SimpleCmd:
+ relation = RelationIdGetRelation(relid);
+
+ if (relation == NULL)
+ break;
+
+ relentry = get_rel_sync_entry(data, relation);
+
+ if (!relentry->pubactions.pubddl)
+ {
+ RelationClose(relation);
+ return;
+ }
+
+ break;
+ case DCT_ObjectDrop:
+ /* do nothing */
+ break;
+ default:
+ elog(ERROR, "unsupported type %d", cmdtype);
+ break;
+ }
+
+ /* Send BEGIN if we haven't yet */
+ if (txndata && !txndata->sent_begin_txn)
+ pgoutput_send_begin(ctx, txn);
+
+ OutputPluginPrepareWrite(ctx, true);
+ logicalrep_write_ddlmessage(ctx->out,
+ xid,
+ message_lsn,
+ prefix,
+ sz,
+ message);
+ OutputPluginWrite(ctx, true);
+
+ if (relation)
+ RelationClose(relation);
+}
+
/*
* Return true if the data is associated with an origin and the user has
* requested the changes that don't have an origin, false otherwise.
@@ -1993,7 +2169,8 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
entry->schema_sent = false;
entry->streamed_txns = NIL;
entry->pubactions.pubinsert = entry->pubactions.pubupdate =
- entry->pubactions.pubdelete = entry->pubactions.pubtruncate = false;
+ entry->pubactions.pubdelete = entry->pubactions.pubtruncate =
+ entry->pubactions.pubddl = false;
entry->new_slot = NULL;
entry->old_slot = NULL;
memset(entry->exprstate, 0, sizeof(entry->exprstate));
@@ -2051,6 +2228,7 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
entry->pubactions.pubupdate = false;
entry->pubactions.pubdelete = false;
entry->pubactions.pubtruncate = false;
+ entry->pubactions.pubddl = false;
/*
* Tuple slots cleanups. (Will be rebuilt later if needed).
@@ -2164,6 +2342,7 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
entry->pubactions.pubupdate |= pub->pubactions.pubupdate;
entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
entry->pubactions.pubtruncate |= pub->pubactions.pubtruncate;
+ entry->pubactions.pubddl |= pub->pubactions.pubddl;
/*
* We want to publish the changes as the top-most ancestor
@@ -2349,6 +2528,7 @@ rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
{
entry->replicate_valid = false;
}
+
}
/* Send Replication origin */
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 00dc0f2403..f1b4d093ce 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -5683,6 +5683,7 @@ RelationBuildPublicationDesc(Relation relation, PublicationDesc *pubdesc)
pubdesc->pubactions.pubupdate |= pubform->pubupdate;
pubdesc->pubactions.pubdelete |= pubform->pubdelete;
pubdesc->pubactions.pubtruncate |= pubform->pubtruncate;
+ pubdesc->pubactions.pubddl |= pubform->pubddl;
/*
* Check if all columns referenced in the filter expression are part
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index bd9b066e4e..97f434a7e8 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -3897,6 +3897,7 @@ getPublications(Archive *fout, int *numPublications)
int i_pubupdate;
int i_pubdelete;
int i_pubtruncate;
+ int i_pubddl;
int i_pubviaroot;
int i,
ntups;
@@ -3912,23 +3913,23 @@ getPublications(Archive *fout, int *numPublications)
resetPQExpBuffer(query);
/* Get the publications. */
- if (fout->remoteVersion >= 130000)
+ if (fout->remoteVersion >= 150000)
appendPQExpBufferStr(query,
"SELECT p.tableoid, p.oid, p.pubname, "
"p.pubowner, "
- "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, p.pubviaroot "
+ "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, p.pubddl, p.pubviaroot "
"FROM pg_publication p");
else if (fout->remoteVersion >= 110000)
appendPQExpBufferStr(query,
"SELECT p.tableoid, p.oid, p.pubname, "
"p.pubowner, "
- "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, false AS pubviaroot "
+ "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, false as p.pubddl, false AS pubviaroot "
"FROM pg_publication p");
else
appendPQExpBufferStr(query,
"SELECT p.tableoid, p.oid, p.pubname, "
"p.pubowner, "
- "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, false AS pubtruncate, false AS pubviaroot "
+ "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, false AS pubtruncate, false as p.pubddl, false AS pubviaroot "
"FROM pg_publication p");
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
@@ -3944,6 +3945,7 @@ getPublications(Archive *fout, int *numPublications)
i_pubupdate = PQfnumber(res, "pubupdate");
i_pubdelete = PQfnumber(res, "pubdelete");
i_pubtruncate = PQfnumber(res, "pubtruncate");
+ i_pubddl = PQfnumber(res, "pubddl");
i_pubviaroot = PQfnumber(res, "pubviaroot");
pubinfo = pg_malloc(ntups * sizeof(PublicationInfo));
@@ -3967,6 +3969,8 @@ getPublications(Archive *fout, int *numPublications)
(strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
pubinfo[i].pubtruncate =
(strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
+ pubinfo[i].pubddl =
+ (strcmp(PQgetvalue(res, i, i_pubddl), "t") == 0);
pubinfo[i].pubviaroot =
(strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0);
@@ -4046,6 +4050,15 @@ dumpPublication(Archive *fout, const PublicationInfo *pubinfo)
first = false;
}
+ if (pubinfo->pubddl)
+ {
+ if (!first)
+ appendPQExpBufferStr(query, ", ");
+
+ appendPQExpBufferStr(query, "ddl");
+ first = false;
+ }
+
appendPQExpBufferChar(query, '\'');
if (pubinfo->pubviaroot)
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 427f5d45f6..685683eeb0 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -620,6 +620,7 @@ typedef struct _PublicationInfo
bool pubdelete;
bool pubtruncate;
bool pubviaroot;
+ bool pubddl;
} PublicationInfo;
/*
diff --git a/src/bin/pg_waldump/rmgrdesc.c b/src/bin/pg_waldump/rmgrdesc.c
index 6b8c17bb4c..792f438959 100644
--- a/src/bin/pg_waldump/rmgrdesc.c
+++ b/src/bin/pg_waldump/rmgrdesc.c
@@ -27,6 +27,7 @@
#include "commands/sequence.h"
#include "commands/tablespace.h"
#include "replication/message.h"
+#include "replication/ddlmessage.h"
#include "replication/origin.h"
#include "rmgrdesc.h"
#include "storage/standbydefs.h"
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index c645d66418..2e94fca744 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -6159,7 +6159,7 @@ listPublications(const char *pattern)
PQExpBufferData buf;
PGresult *res;
printQueryOpt myopt = pset.popt;
- static const bool translate_columns[] = {false, false, false, false, false, false, false, false};
+ static const bool translate_columns[] = {false, false, false, false, false, false, false, false, false};
if (pset.sversion < 100000)
{
@@ -6194,6 +6194,10 @@ listPublications(const char *pattern)
appendPQExpBuffer(&buf,
",\n pubviaroot AS \"%s\"",
gettext_noop("Via root"));
+ if (pset.sversion >= 140000)
+ appendPQExpBuffer(&buf,
+ ",\n pubddl AS \"%s\"",
+ gettext_noop("DDLs"));
appendPQExpBufferStr(&buf,
"\nFROM pg_catalog.pg_publication\n");
@@ -6284,6 +6288,7 @@ describePublications(const char *pattern)
PGresult *res;
bool has_pubtruncate;
bool has_pubviaroot;
+ bool has_pubddl;
PQExpBufferData title;
printTableContent cont;
@@ -6300,6 +6305,7 @@ describePublications(const char *pattern)
has_pubtruncate = (pset.sversion >= 110000);
has_pubviaroot = (pset.sversion >= 130000);
+ has_pubddl = (pset.sversion >= 150000);
initPQExpBuffer(&buf);
@@ -6313,6 +6319,9 @@ describePublications(const char *pattern)
if (has_pubviaroot)
appendPQExpBufferStr(&buf,
", pubviaroot");
+ if (has_pubddl)
+ appendPQExpBufferStr(&buf,
+ ", pubddl");
appendPQExpBufferStr(&buf,
"\nFROM pg_catalog.pg_publication\n");
@@ -6364,6 +6373,8 @@ describePublications(const char *pattern)
ncols++;
if (has_pubviaroot)
ncols++;
+ if (has_pubddl)
+ ncols++;
initPQExpBuffer(&title);
printfPQExpBuffer(&title, _("Publication %s"), pubname);
@@ -6378,6 +6389,8 @@ describePublications(const char *pattern)
printTableAddHeader(&cont, gettext_noop("Truncates"), true, align);
if (has_pubviaroot)
printTableAddHeader(&cont, gettext_noop("Via root"), true, align);
+ if (has_pubddl)
+ printTableAddHeader(&cont, gettext_noop("DDLs"), true, align);
printTableAddCell(&cont, PQgetvalue(res, i, 2), false, false);
printTableAddCell(&cont, PQgetvalue(res, i, 3), false, false);
@@ -6388,6 +6401,8 @@ describePublications(const char *pattern)
printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false);
if (has_pubviaroot)
printTableAddCell(&cont, PQgetvalue(res, i, 8), false, false);
+ if (has_pubddl)
+ printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false);
if (!puballtables)
{
diff --git a/src/include/access/rmgrlist.h b/src/include/access/rmgrlist.h
index 000bcbfdaf..37dfd451f6 100644
--- a/src/include/access/rmgrlist.h
+++ b/src/include/access/rmgrlist.h
@@ -47,3 +47,4 @@ PG_RMGR(RM_COMMIT_TS_ID, "CommitTs", commit_ts_redo, commit_ts_desc, commit_ts_i
PG_RMGR(RM_REPLORIGIN_ID, "ReplicationOrigin", replorigin_redo, replorigin_desc, replorigin_identify, NULL, NULL, NULL, NULL)
PG_RMGR(RM_GENERIC_ID, "Generic", generic_redo, generic_desc, generic_identify, NULL, NULL, generic_mask, NULL)
PG_RMGR(RM_LOGICALMSG_ID, "LogicalMessage", logicalmsg_redo, logicalmsg_desc, logicalmsg_identify, NULL, NULL, NULL, logicalmsg_decode)
+PG_RMGR(RM_LOGICALDDLMSG_ID, "LogicalDDLMessage", logicalddlmsg_redo, logicalddlmsg_desc, logicalddlmsg_identify, NULL, NULL, NULL, logicalddlmsg_decode)
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index c2f1de2801..84f87e1c02 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11833,4 +11833,13 @@
{ oid => '4643', descr => 'expand JSON format DDL to a plain DDL command',
proname => 'ddl_deparse_expand_command', prorettype => 'text',
proargtypes => 'text', prosrc => 'ddl_deparse_expand_command' },
+{ oid => '4644', descr => 'trigger for ddl command deparse',
+ proname => 'publication_deparse_ddl_command_end', prorettype => 'event_trigger',
+ proargtypes => '', prosrc => 'publication_deparse_ddl_command_end' },
+{ oid => '4645', descr => 'trigger for ddl command deparse start',
+ proname => 'publication_deparse_ddl_command_start', prorettype => 'event_trigger',
+ proargtypes => '', prosrc => 'publication_deparse_ddl_command_start' },
+{ oid => '4646', descr => 'trigger for ddl command deparse table rewrite',
+ proname => 'publication_deparse_table_rewrite', prorettype => 'event_trigger',
+ proargtypes => '', prosrc => 'publication_deparse_table_rewrite' },
]
diff --git a/src/include/catalog/pg_publication.h b/src/include/catalog/pg_publication.h
index ecf5a28e00..dafd48376a 100644
--- a/src/include/catalog/pg_publication.h
+++ b/src/include/catalog/pg_publication.h
@@ -54,6 +54,9 @@ CATALOG(pg_publication,6104,PublicationRelationId)
/* true if partition changes are published using root schema */
bool pubviaroot;
+
+ /* true if table creations are published */
+ bool pubddl;
} FormData_pg_publication;
/* ----------------
@@ -72,6 +75,7 @@ typedef struct PublicationActions
bool pubupdate;
bool pubdelete;
bool pubtruncate;
+ bool pubddl;
} PublicationActions;
typedef struct PublicationDesc
diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h
index 10091c3aaf..fd2ee7ffe4 100644
--- a/src/include/commands/event_trigger.h
+++ b/src/include/commands/event_trigger.h
@@ -71,7 +71,8 @@ extern void EventTriggerCollectSimpleCommand(ObjectAddress address,
extern void EventTriggerAlterTableStart(Node *parsetree);
extern void EventTriggerAlterTableRelid(Oid objectId);
extern void EventTriggerCollectAlterTableSubcmd(Node *subcmd,
- ObjectAddress address);
+ ObjectAddress address,
+ bool rewrite);
extern void EventTriggerAlterTableEnd(void);
extern void EventTriggerCollectGrant(InternalGrant *istmt);
diff --git a/src/include/replication/ddlmessage.h b/src/include/replication/ddlmessage.h
new file mode 100644
index 0000000000..31a766feea
--- /dev/null
+++ b/src/include/replication/ddlmessage.h
@@ -0,0 +1,60 @@
+/*-------------------------------------------------------------------------
+ * ddlmessage.h
+ * Exports from replication/logical/ddlmessage.c
+ *
+ * Copyright (c) 2022, PostgreSQL Global Development Group
+ *
+ * src/include/replication/ddlmessage.h
+ *-------------------------------------------------------------------------
+ */
+#ifndef PG_LOGICAL_DDL_MESSAGE_H
+#define PG_LOGICAL_DDL_MESSAGE_H
+
+#include "access/xlog.h"
+#include "access/xlogdefs.h"
+#include "access/xlogreader.h"
+#include "nodes/nodes.h"
+
+
+/*
+ * Support for keeping track of deparsed commands.
+ */
+typedef enum DeparsedCommandType
+{
+ DCT_SimpleCmd,
+ DCT_TableDropStart,
+ DCT_TableDropEnd,
+ DCT_TableAlter,
+ DCT_ObjectCreate,
+ DCT_ObjectDrop
+} DeparsedCommandType;
+
+/*
+ * Generic logical decoding DDL message wal record.
+ */
+typedef struct xl_logical_ddl_message
+{
+ Oid dbId; /* database Oid emitted from */
+ Size prefix_size; /* length of prefix */
+ Oid relid; /* id of the table */
+ DeparsedCommandType cmdtype; /* type of sql command */
+ Size message_size; /* size of the message */
+
+ /*
+ * payload, including null-terminated prefix of length prefix_size
+ */
+ char message[FLEXIBLE_ARRAY_MEMBER];
+} xl_logical_ddl_message;
+
+#define SizeOfLogicalDDLMessage (offsetof(xl_logical_ddl_message, message))
+
+extern XLogRecPtr LogLogicalDDLMessage(const char *prefix, Oid relid, DeparsedCommandType cmdtype,
+ const char *ddl_message, size_t size);
+
+/* RMGR API*/
+#define XLOG_LOGICAL_DDL_MESSAGE 0x00
+void logicalddlmsg_redo(XLogReaderState *record);
+void logicalddlmsg_desc(StringInfo buf, XLogReaderState *record);
+const char *logicalddlmsg_identify(uint8 info);
+
+#endif
diff --git a/src/include/replication/decode.h b/src/include/replication/decode.h
index 741bf65cf7..427a7b997d 100644
--- a/src/include/replication/decode.h
+++ b/src/include/replication/decode.h
@@ -27,6 +27,7 @@ extern void heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
extern void xact_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
extern void standby_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
extern void logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
+extern void logicalddlmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
extern void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx,
XLogReaderState *record);
diff --git a/src/include/replication/logicalproto.h b/src/include/replication/logicalproto.h
index 7eaa4c97ed..5d617484fb 100644
--- a/src/include/replication/logicalproto.h
+++ b/src/include/replication/logicalproto.h
@@ -61,6 +61,7 @@ typedef enum LogicalRepMsgType
LOGICAL_REP_MSG_RELATION = 'R',
LOGICAL_REP_MSG_TYPE = 'Y',
LOGICAL_REP_MSG_MESSAGE = 'M',
+ LOGICAL_REP_MSG_DDLMESSAGE = 'L',
LOGICAL_REP_MSG_BEGIN_PREPARE = 'b',
LOGICAL_REP_MSG_PREPARE = 'P',
LOGICAL_REP_MSG_COMMIT_PREPARED = 'K',
@@ -229,7 +230,11 @@ extern void logicalrep_write_truncate(StringInfo out, TransactionId xid,
extern List *logicalrep_read_truncate(StringInfo in,
bool *cascade, bool *restart_seqs);
extern void logicalrep_write_message(StringInfo out, TransactionId xid, XLogRecPtr lsn,
- bool transactional, const char *prefix, Size sz, const char *message);
+ bool transactional, const char *prefix,
+ Size sz, const char *message);
+extern void logicalrep_write_ddlmessage(StringInfo out, TransactionId xid, XLogRecPtr lsn,
+ const char *prefix, Size sz, const char *message);
+extern char *logicalrep_read_ddlmessage(StringInfo in, XLogRecPtr *lsn, const char **prefix, Size *sz);
extern void logicalrep_write_rel(StringInfo out, TransactionId xid,
Relation rel, Bitmapset *columns);
extern LogicalRepRelation *logicalrep_read_rel(StringInfo in);
diff --git a/src/include/replication/output_plugin.h b/src/include/replication/output_plugin.h
index b7d28d7045..763e43f6be 100644
--- a/src/include/replication/output_plugin.h
+++ b/src/include/replication/output_plugin.h
@@ -90,6 +90,18 @@ typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
Size message_size,
const char *message);
+/*
+ * Called for the logical decoding DDL messages.
+ */
+typedef void (*LogicalDecodeDDLMessageCB) (struct LogicalDecodingContext *ctx,
+ ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix,
+ Oid relid,
+ DeparsedCommandType cmdtype,
+ Size message_size,
+ const char *message);
+
/*
* Filter changes by origin.
*/
@@ -201,6 +213,19 @@ typedef void (*LogicalDecodeStreamMessageCB) (struct LogicalDecodingContext *ctx
Size message_size,
const char *message);
+/*
+ * Callback for streaming logical decoding DDL messages from in-progress
+ * transactions.
+ */
+typedef void (*LogicalDecodeStreamDDLMessageCB) (struct LogicalDecodingContext *ctx,
+ ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix,
+ Oid relid,
+ DeparsedCommandType cmdtype,
+ Size message_size,
+ const char *message);
+
/*
* Callback for streaming truncates from in-progress transactions.
*/
@@ -221,6 +246,7 @@ typedef struct OutputPluginCallbacks
LogicalDecodeTruncateCB truncate_cb;
LogicalDecodeCommitCB commit_cb;
LogicalDecodeMessageCB message_cb;
+ LogicalDecodeDDLMessageCB ddlmessage_cb;
LogicalDecodeFilterByOriginCB filter_by_origin_cb;
LogicalDecodeShutdownCB shutdown_cb;
@@ -239,6 +265,7 @@ typedef struct OutputPluginCallbacks
LogicalDecodeStreamCommitCB stream_commit_cb;
LogicalDecodeStreamChangeCB stream_change_cb;
LogicalDecodeStreamMessageCB stream_message_cb;
+ LogicalDecodeStreamDDLMessageCB stream_ddlmessage_cb;
LogicalDecodeStreamTruncateCB stream_truncate_cb;
} OutputPluginCallbacks;
diff --git a/src/include/replication/pgoutput.h b/src/include/replication/pgoutput.h
index 02027550e2..83e0b1e2e5 100644
--- a/src/include/replication/pgoutput.h
+++ b/src/include/replication/pgoutput.h
@@ -25,6 +25,7 @@ typedef struct PGOutputData
uint32 protocol_version;
List *publication_names;
List *publications;
+ List *deleted_relids;
bool binary;
bool streaming;
bool messages;
diff --git a/src/include/replication/reorderbuffer.h b/src/include/replication/reorderbuffer.h
index 02b59a1931..a9a200f802 100644
--- a/src/include/replication/reorderbuffer.h
+++ b/src/include/replication/reorderbuffer.h
@@ -11,6 +11,8 @@
#include "access/htup_details.h"
#include "lib/ilist.h"
+#include "nodes/nodes.h"
+#include "replication/ddlmessage.h"
#include "storage/sinval.h"
#include "utils/hsearch.h"
#include "utils/relcache.h"
@@ -56,6 +58,7 @@ typedef enum ReorderBufferChangeType
REORDER_BUFFER_CHANGE_INSERT,
REORDER_BUFFER_CHANGE_UPDATE,
REORDER_BUFFER_CHANGE_DELETE,
+ REORDER_BUFFER_CHANGE_DDLMESSAGE,
REORDER_BUFFER_CHANGE_MESSAGE,
REORDER_BUFFER_CHANGE_INVALIDATION,
REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT,
@@ -130,6 +133,16 @@ typedef struct ReorderBufferChange
char *message;
} msg;
+ /* DDL Message. */
+ struct
+ {
+ char *prefix;
+ Size message_size;
+ char *message;
+ Oid relid;
+ DeparsedCommandType cmdtype;
+ } ddlmsg;
+
/* New snapshot, set when action == *_INTERNAL_SNAPSHOT */
Snapshot snapshot;
@@ -435,6 +448,16 @@ typedef void (*ReorderBufferMessageCB) (ReorderBuffer *rb,
const char *prefix, Size sz,
const char *message);
+/* DDL message callback signature */
+typedef void (*ReorderBufferDDLMessageCB) (ReorderBuffer *rb,
+ ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix,
+ Oid relid,
+ DeparsedCommandType cmdtype,
+ Size sz,
+ const char *message);
+
/* begin prepare callback signature */
typedef void (*ReorderBufferBeginPrepareCB) (ReorderBuffer *rb,
ReorderBufferTXN *txn);
@@ -501,6 +524,17 @@ typedef void (*ReorderBufferStreamMessageCB) (
const char *prefix, Size sz,
const char *message);
+/* stream DDL message callback signature */
+typedef void (*ReorderBufferStreamDDLMessageCB) (
+ ReorderBuffer *rb,
+ ReorderBufferTXN *txn,
+ XLogRecPtr message_lsn,
+ const char *prefix,
+ Oid relid,
+ DeparsedCommandType cmdtype,
+ Size sz,
+ const char *message);
+
/* stream truncate callback signature */
typedef void (*ReorderBufferStreamTruncateCB) (
ReorderBuffer *rb,
@@ -552,6 +586,7 @@ struct ReorderBuffer
ReorderBufferApplyTruncateCB apply_truncate;
ReorderBufferCommitCB commit;
ReorderBufferMessageCB message;
+ ReorderBufferDDLMessageCB ddlmessage;
/*
* Callbacks to be called when streaming a transaction at prepare time.
@@ -571,6 +606,7 @@ struct ReorderBuffer
ReorderBufferStreamCommitCB stream_commit;
ReorderBufferStreamChangeCB stream_change;
ReorderBufferStreamMessageCB stream_message;
+ ReorderBufferStreamDDLMessageCB stream_ddlmessage;
ReorderBufferStreamTruncateCB stream_truncate;
/*
@@ -650,6 +686,9 @@ extern void ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
Snapshot snap, XLogRecPtr lsn,
bool transactional, const char *prefix,
Size message_size, const char *message);
+extern void ReorderBufferQueueDDLMessage(ReorderBuffer *, TransactionId, XLogRecPtr lsn,
+ const char *prefix, Size message_size,
+ const char *message, Oid relid, DeparsedCommandType cmdtype);
extern void ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn);
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index a7f5700edc..bfc73c2328 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -5970,9 +5970,9 @@ List of schemas
(0 rows)
\dRp "no.such.publication"
- List of publications
- Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
-------+-------+------------+---------+---------+---------+-----------+----------
+ List of publications
+ Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+------+-------+------------+---------+---------+---------+-----------+----------+------
(0 rows)
\dRs "no.such.subscription"
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index 427f87ea07..2b7cd0d596 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -30,20 +30,20 @@ ERROR: conflicting or redundant options
LINE 1: ...ub_xxx WITH (publish_via_partition_root = 'true', publish_vi...
^
\dRp
- List of publications
- Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------+--------------------------+------------+---------+---------+---------+-----------+----------
- testpib_ins_trunct | regress_publication_user | f | t | f | f | f | f
- testpub_default | regress_publication_user | f | f | t | f | f | f
+ List of publications
+ Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ testpib_ins_trunct | regress_publication_user | f | t | f | f | f | f | f
+ testpub_default | regress_publication_user | f | f | t | f | f | f | f
(2 rows)
ALTER PUBLICATION testpub_default SET (publish = 'insert, update, delete');
\dRp
- List of publications
- Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------+--------------------------+------------+---------+---------+---------+-----------+----------
- testpib_ins_trunct | regress_publication_user | f | t | f | f | f | f
- testpub_default | regress_publication_user | f | t | t | t | f | f
+ List of publications
+ Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ testpib_ins_trunct | regress_publication_user | f | t | f | f | f | f | f
+ testpub_default | regress_publication_user | f | t | t | t | f | f | f
(2 rows)
--- adding tables
@@ -87,10 +87,10 @@ RESET client_min_messages;
-- should be able to add schema to 'FOR TABLE' publication
ALTER PUBLICATION testpub_fortable ADD TABLES IN SCHEMA pub_test;
\dRp+ testpub_fortable
- Publication testpub_fortable
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_fortable
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"public.testpub_tbl1"
Tables from schemas:
@@ -99,20 +99,20 @@ Tables from schemas:
-- should be able to drop schema from 'FOR TABLE' publication
ALTER PUBLICATION testpub_fortable DROP TABLES IN SCHEMA pub_test;
\dRp+ testpub_fortable
- Publication testpub_fortable
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_fortable
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"public.testpub_tbl1"
-- should be able to set schema to 'FOR TABLE' publication
ALTER PUBLICATION testpub_fortable SET TABLES IN SCHEMA pub_test;
\dRp+ testpub_fortable
- Publication testpub_fortable
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_fortable
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test"
@@ -123,10 +123,10 @@ CREATE PUBLICATION testpub_forschema FOR TABLES IN SCHEMA pub_test;
CREATE PUBLICATION testpub_for_tbl_schema FOR TABLES IN SCHEMA pub_test, TABLE pub_test.testpub_nopk;
RESET client_min_messages;
\dRp+ testpub_for_tbl_schema
- Publication testpub_for_tbl_schema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_for_tbl_schema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"pub_test.testpub_nopk"
Tables from schemas:
@@ -135,10 +135,10 @@ Tables from schemas:
-- should be able to add a table of the same schema to the schema publication
ALTER PUBLICATION testpub_forschema ADD TABLE pub_test.testpub_nopk;
\dRp+ testpub_forschema
- Publication testpub_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"pub_test.testpub_nopk"
Tables from schemas:
@@ -147,10 +147,10 @@ Tables from schemas:
-- should be able to drop the table
ALTER PUBLICATION testpub_forschema DROP TABLE pub_test.testpub_nopk;
\dRp+ testpub_forschema
- Publication testpub_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test"
@@ -161,10 +161,10 @@ ERROR: relation "testpub_nopk" is not part of the publication
-- should be able to set table to schema publication
ALTER PUBLICATION testpub_forschema SET TABLE pub_test.testpub_nopk;
\dRp+ testpub_forschema
- Publication testpub_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"pub_test.testpub_nopk"
@@ -186,10 +186,10 @@ Publications:
"testpub_foralltables"
\dRp+ testpub_foralltables
- Publication testpub_foralltables
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | t | t | t | f | f | f
+ Publication testpub_foralltables
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | t | t | t | f | f | f | f
(1 row)
DROP TABLE testpub_tbl2;
@@ -201,19 +201,19 @@ CREATE PUBLICATION testpub3 FOR TABLE testpub_tbl3;
CREATE PUBLICATION testpub4 FOR TABLE ONLY testpub_tbl3;
RESET client_min_messages;
\dRp+ testpub3
- Publication testpub3
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub3
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"public.testpub_tbl3"
"public.testpub_tbl3a"
\dRp+ testpub4
- Publication testpub4
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub4
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"public.testpub_tbl3"
@@ -234,10 +234,10 @@ UPDATE testpub_parted1 SET a = 1;
-- only parent is listed as being in publication, not the partition
ALTER PUBLICATION testpub_forparted ADD TABLE testpub_parted;
\dRp+ testpub_forparted
- Publication testpub_forparted
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_forparted
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"public.testpub_parted"
@@ -252,10 +252,10 @@ ALTER TABLE testpub_parted DETACH PARTITION testpub_parted1;
UPDATE testpub_parted1 SET a = 1;
ALTER PUBLICATION testpub_forparted SET (publish_via_partition_root = true);
\dRp+ testpub_forparted
- Publication testpub_forparted
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | t
+ Publication testpub_forparted
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | t | f
Tables:
"public.testpub_parted"
@@ -284,10 +284,10 @@ SET client_min_messages = 'ERROR';
CREATE PUBLICATION testpub5 FOR TABLE testpub_rf_tbl1, testpub_rf_tbl2 WHERE (c <> 'test' AND d < 5) WITH (publish = 'insert');
RESET client_min_messages;
\dRp+ testpub5
- Publication testpub5
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | f | f
+ Publication testpub5
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | f | f | f
Tables:
"public.testpub_rf_tbl1"
"public.testpub_rf_tbl2" WHERE ((c <> 'test'::text) AND (d < 5))
@@ -300,10 +300,10 @@ Tables:
ALTER PUBLICATION testpub5 ADD TABLE testpub_rf_tbl3 WHERE (e > 1000 AND e < 2000);
\dRp+ testpub5
- Publication testpub5
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | f | f
+ Publication testpub5
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | f | f | f
Tables:
"public.testpub_rf_tbl1"
"public.testpub_rf_tbl2" WHERE ((c <> 'test'::text) AND (d < 5))
@@ -319,10 +319,10 @@ Publications:
ALTER PUBLICATION testpub5 DROP TABLE testpub_rf_tbl2;
\dRp+ testpub5
- Publication testpub5
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | f | f
+ Publication testpub5
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | f | f | f
Tables:
"public.testpub_rf_tbl1"
"public.testpub_rf_tbl3" WHERE ((e > 1000) AND (e < 2000))
@@ -330,10 +330,10 @@ Tables:
-- remove testpub_rf_tbl1 and add testpub_rf_tbl3 again (another WHERE expression)
ALTER PUBLICATION testpub5 SET TABLE testpub_rf_tbl3 WHERE (e > 300 AND e < 500);
\dRp+ testpub5
- Publication testpub5
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | f | f
+ Publication testpub5
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | f | f | f
Tables:
"public.testpub_rf_tbl3" WHERE ((e > 300) AND (e < 500))
@@ -366,10 +366,10 @@ SET client_min_messages = 'ERROR';
CREATE PUBLICATION testpub_syntax1 FOR TABLE testpub_rf_tbl1, ONLY testpub_rf_tbl3 WHERE (e < 999) WITH (publish = 'insert');
RESET client_min_messages;
\dRp+ testpub_syntax1
- Publication testpub_syntax1
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | f | f
+ Publication testpub_syntax1
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | f | f | f
Tables:
"public.testpub_rf_tbl1"
"public.testpub_rf_tbl3" WHERE (e < 999)
@@ -379,10 +379,10 @@ SET client_min_messages = 'ERROR';
CREATE PUBLICATION testpub_syntax2 FOR TABLE testpub_rf_tbl1, testpub_rf_schema1.testpub_rf_tbl5 WHERE (h < 999) WITH (publish = 'insert');
RESET client_min_messages;
\dRp+ testpub_syntax2
- Publication testpub_syntax2
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | f | f
+ Publication testpub_syntax2
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | f | f | f
Tables:
"public.testpub_rf_tbl1"
"testpub_rf_schema1.testpub_rf_tbl5" WHERE (h < 999)
@@ -497,10 +497,10 @@ CREATE PUBLICATION testpub6 FOR TABLES IN SCHEMA testpub_rf_schema2;
ALTER PUBLICATION testpub6 SET TABLES IN SCHEMA testpub_rf_schema2, TABLE testpub_rf_schema2.testpub_rf_tbl6 WHERE (i < 99);
RESET client_min_messages;
\dRp+ testpub6
- Publication testpub6
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub6
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"testpub_rf_schema2.testpub_rf_tbl6" WHERE (i < 99)
Tables from schemas:
@@ -714,10 +714,10 @@ CREATE PUBLICATION testpub_table_ins WITH (publish = 'insert, truncate');
RESET client_min_messages;
ALTER PUBLICATION testpub_table_ins ADD TABLE testpub_tbl5 (a); -- ok
\dRp+ testpub_table_ins
- Publication testpub_table_ins
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | f | f | t | f
+ Publication testpub_table_ins
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | f | f | t | f | f
Tables:
"public.testpub_tbl5" (a)
@@ -891,10 +891,10 @@ CREATE TABLE testpub_tbl_both_filters (a int, b int, c int, PRIMARY KEY (a,c));
ALTER TABLE testpub_tbl_both_filters REPLICA IDENTITY USING INDEX testpub_tbl_both_filters_pkey;
ALTER PUBLICATION testpub_both_filters ADD TABLE testpub_tbl_both_filters (a,c) WHERE (c != 1);
\dRp+ testpub_both_filters
- Publication testpub_both_filters
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_both_filters
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"public.testpub_tbl_both_filters" (a, c) WHERE (c <> 1)
@@ -1099,10 +1099,10 @@ ERROR: relation "testpub_tbl1" is already member of publication "testpub_fortbl
CREATE PUBLICATION testpub_fortbl FOR TABLE testpub_tbl1;
ERROR: publication "testpub_fortbl" already exists
\dRp+ testpub_fortbl
- Publication testpub_fortbl
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_fortbl
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"pub_test.testpub_nopk"
"public.testpub_tbl1"
@@ -1140,10 +1140,10 @@ Publications:
"testpub_fortbl"
\dRp+ testpub_default
- Publication testpub_default
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | f | f
+ Publication testpub_default
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | f | f | f
Tables:
"pub_test.testpub_nopk"
"public.testpub_tbl1"
@@ -1221,10 +1221,10 @@ REVOKE CREATE ON DATABASE regression FROM regress_publication_user2;
DROP TABLE testpub_parted;
DROP TABLE testpub_tbl1;
\dRp+ testpub_default
- Publication testpub_default
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | f | f
+ Publication testpub_default
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | f | f | f
(1 row)
-- fail - must be owner of publication
@@ -1234,20 +1234,20 @@ ERROR: must be owner of publication testpub_default
RESET ROLE;
ALTER PUBLICATION testpub_default RENAME TO testpub_foo;
\dRp testpub_foo
- List of publications
- Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
--------------+--------------------------+------------+---------+---------+---------+-----------+----------
- testpub_foo | regress_publication_user | f | t | t | t | f | f
+ List of publications
+ Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+-------------+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ testpub_foo | regress_publication_user | f | t | t | t | f | f | f
(1 row)
-- rename back to keep the rest simple
ALTER PUBLICATION testpub_foo RENAME TO testpub_default;
ALTER PUBLICATION testpub_default OWNER TO regress_publication_user2;
\dRp testpub_default
- List of publications
- Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
------------------+---------------------------+------------+---------+---------+---------+-----------+----------
- testpub_default | regress_publication_user2 | f | t | t | t | f | f
+ List of publications
+ Name | Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+-----------------+---------------------------+------------+---------+---------+---------+-----------+----------+------
+ testpub_default | regress_publication_user2 | f | t | t | t | f | f | f
(1 row)
-- adding schemas and tables
@@ -1263,19 +1263,19 @@ CREATE TABLE "CURRENT_SCHEMA"."CURRENT_SCHEMA"(id int);
SET client_min_messages = 'ERROR';
CREATE PUBLICATION testpub1_forschema FOR TABLES IN SCHEMA pub_test1;
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
CREATE PUBLICATION testpub2_forschema FOR TABLES IN SCHEMA pub_test1, pub_test2, pub_test3;
\dRp+ testpub2_forschema
- Publication testpub2_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub2_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1289,44 +1289,44 @@ CREATE PUBLICATION testpub6_forschema FOR TABLES IN SCHEMA "CURRENT_SCHEMA", CUR
CREATE PUBLICATION testpub_fortable FOR TABLE "CURRENT_SCHEMA"."CURRENT_SCHEMA";
RESET client_min_messages;
\dRp+ testpub3_forschema
- Publication testpub3_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub3_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"public"
\dRp+ testpub4_forschema
- Publication testpub4_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub4_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"CURRENT_SCHEMA"
\dRp+ testpub5_forschema
- Publication testpub5_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub5_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"CURRENT_SCHEMA"
"public"
\dRp+ testpub6_forschema
- Publication testpub6_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub6_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"CURRENT_SCHEMA"
"public"
\dRp+ testpub_fortable
- Publication testpub_fortable
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_fortable
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"CURRENT_SCHEMA.CURRENT_SCHEMA"
@@ -1360,10 +1360,10 @@ ERROR: schema "testpub_view" does not exist
-- dropping the schema should reflect the change in publication
DROP SCHEMA pub_test3;
\dRp+ testpub2_forschema
- Publication testpub2_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub2_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1371,20 +1371,20 @@ Tables from schemas:
-- renaming the schema should reflect the change in publication
ALTER SCHEMA pub_test1 RENAME to pub_test1_renamed;
\dRp+ testpub2_forschema
- Publication testpub2_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub2_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1_renamed"
"pub_test2"
ALTER SCHEMA pub_test1_renamed RENAME to pub_test1;
\dRp+ testpub2_forschema
- Publication testpub2_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub2_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1392,10 +1392,10 @@ Tables from schemas:
-- alter publication add schema
ALTER PUBLICATION testpub1_forschema ADD TABLES IN SCHEMA pub_test2;
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1404,10 +1404,10 @@ Tables from schemas:
ALTER PUBLICATION testpub1_forschema ADD TABLES IN SCHEMA non_existent_schema;
ERROR: schema "non_existent_schema" does not exist
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1416,10 +1416,10 @@ Tables from schemas:
ALTER PUBLICATION testpub1_forschema ADD TABLES IN SCHEMA pub_test1;
ERROR: schema "pub_test1" is already member of publication "testpub1_forschema"
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1427,10 +1427,10 @@ Tables from schemas:
-- alter publication drop schema
ALTER PUBLICATION testpub1_forschema DROP TABLES IN SCHEMA pub_test2;
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
@@ -1438,10 +1438,10 @@ Tables from schemas:
ALTER PUBLICATION testpub1_forschema DROP TABLES IN SCHEMA pub_test2;
ERROR: tables from schema "pub_test2" are not part of the publication
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
@@ -1449,29 +1449,29 @@ Tables from schemas:
ALTER PUBLICATION testpub1_forschema DROP TABLES IN SCHEMA non_existent_schema;
ERROR: schema "non_existent_schema" does not exist
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
-- drop all schemas
ALTER PUBLICATION testpub1_forschema DROP TABLES IN SCHEMA pub_test1;
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
(1 row)
-- alter publication set multiple schema
ALTER PUBLICATION testpub1_forschema SET TABLES IN SCHEMA pub_test1, pub_test2;
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1480,10 +1480,10 @@ Tables from schemas:
ALTER PUBLICATION testpub1_forschema SET TABLES IN SCHEMA non_existent_schema;
ERROR: schema "non_existent_schema" does not exist
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
"pub_test2"
@@ -1492,10 +1492,10 @@ Tables from schemas:
-- removing the duplicate schemas
ALTER PUBLICATION testpub1_forschema SET TABLES IN SCHEMA pub_test1, pub_test1;
\dRp+ testpub1_forschema
- Publication testpub1_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub1_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
@@ -1574,18 +1574,18 @@ SET client_min_messages = 'ERROR';
CREATE PUBLICATION testpub3_forschema;
RESET client_min_messages;
\dRp+ testpub3_forschema
- Publication testpub3_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub3_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
(1 row)
ALTER PUBLICATION testpub3_forschema SET TABLES IN SCHEMA pub_test1;
\dRp+ testpub3_forschema
- Publication testpub3_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub3_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables from schemas:
"pub_test1"
@@ -1595,20 +1595,20 @@ CREATE PUBLICATION testpub_forschema_fortable FOR TABLES IN SCHEMA pub_test1, TA
CREATE PUBLICATION testpub_fortable_forschema FOR TABLE pub_test2.tbl1, TABLES IN SCHEMA pub_test1;
RESET client_min_messages;
\dRp+ testpub_forschema_fortable
- Publication testpub_forschema_fortable
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_forschema_fortable
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"pub_test2.tbl1"
Tables from schemas:
"pub_test1"
\dRp+ testpub_fortable_forschema
- Publication testpub_fortable_forschema
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
---------------------------+------------+---------+---------+---------+-----------+----------
- regress_publication_user | f | t | t | t | t | f
+ Publication testpub_fortable_forschema
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root | DDLs
+--------------------------+------------+---------+---------+---------+-----------+----------+------
+ regress_publication_user | f | t | t | t | t | f | f
Tables:
"pub_test2.tbl1"
Tables from schemas:
--
2.32.0
[text/x-patch] v30-0003-Support-CREATE-TABLE-AS-SELECT-INTO.patch (15.0K, 4-v30-0003-Support-CREATE-TABLE-AS-SELECT-INTO.patch)
download | inline diff:
From 17025df6e2280e4c573c3cc9939cb7e5d7ccaba6 Mon Sep 17 00:00:00 2001
From: "vignesh.c" <vignesh21@gmail.com>
Date: Wed, 19 Oct 2022 10:49:56 +0530
Subject: [PATCH v30 3/4] Support CREATE TABLE AS SELECT INTO
The main idea of replicating the CREATE TABLE AS is that we deprase the CREATE
TABLE AS into a simple CREATE TABLE(without subquery) command and WAL log it
after creating the table and before writing data into the table and replicate
the incoming writes later as normal INSERTs. In this apporach, we don't execute
the subquery on subscriber so that don't need to make sure all the objects
referenced in the subquery also exists in subscriber. And This approach works
for all kind of commands(e.g. CRAETE TABLE AS [SELECT][EXECUTE][VALUES])
Introducing a new type of event trigger "table_init_write". which would be fired
for CREATE TABLE AS/SELECT INTO after creating the table and
before any other modification. we deparse the command in the table_init_write event
trigger and WAL log the deparsed json string. The walsender will send the
string to subscriber. And incoming INSERTs will also be replicated.
---
src/backend/commands/createas.c | 10 ++
src/backend/commands/ddl_deparse.c | 23 ++++
src/backend/commands/event_trigger.c | 172 ++++++++++++++++++++++++-
src/backend/commands/publicationcmds.c | 9 ++
src/backend/tcop/utility.c | 2 +
src/backend/utils/cache/evtcache.c | 2 +
src/include/catalog/pg_proc.dat | 3 +
src/include/commands/event_trigger.h | 4 +
src/include/tcop/deparse_utility.h | 9 +-
src/include/utils/evtcache.h | 3 +-
10 files changed, 231 insertions(+), 6 deletions(-)
diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 152c29b551..b7795da87d 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -34,6 +34,7 @@
#include "catalog/namespace.h"
#include "catalog/toasting.h"
#include "commands/createas.h"
+#include "commands/event_trigger.h"
#include "commands/matview.h"
#include "commands/prepare.h"
#include "commands/tablecmds.h"
@@ -143,6 +144,15 @@ create_ctas_internal(List *attrList, IntoClause *into)
StoreViewQuery(intoRelationAddr.objectId, query, false);
CommandCounterIncrement();
}
+ else
+ {
+ /*
+ * Fire the trigger for table_init_write after creating the table so
+ * that we can access the catalog info about the newly created table
+ * in the trigger function.
+ */
+ EventTriggerTableInitWrite((Node *) create, intoRelationAddr);
+ }
return intoRelationAddr;
}
diff --git a/src/backend/commands/ddl_deparse.c b/src/backend/commands/ddl_deparse.c
index a4ab3afca3..7f1373921d 100755
--- a/src/backend/commands/ddl_deparse.c
+++ b/src/backend/commands/ddl_deparse.c
@@ -8211,6 +8211,26 @@ deparse_Type_Typmod_Out(ObjTree *parent, Form_pg_type typForm)
return new_object_object(typmodout);
}
+/*
+ * Deparse CREATE TABLE AS command.
+ *
+ * deparse_CreateStmt do the actual work as we deparse the final CreateStmt for
+ * CREATE TABLE AS command.
+ */
+static ObjTree *
+deparse_CreateTableAsStmt(CollectedCommand *cmd)
+{
+ Oid objectId;
+ Node *parsetree;
+
+ Assert(cmd->type == SCT_CreateTableAs);
+
+ parsetree = cmd->d.ctas.real_create;
+ objectId = cmd->d.ctas.address.objectId;
+
+ return deparse_CreateStmt(objectId, parsetree);
+}
+
/*
* Deparse the type analyze option.
@@ -8814,6 +8834,9 @@ deparse_utility_command(CollectedCommand *cmd, bool verbose_mode)
case SCT_Grant:
tree = deparse_GrantStmt(cmd);
break;
+ case SCT_CreateTableAs:
+ tree = deparse_CreateTableAsStmt(cmd);
+ break;
case SCT_AlterOpFamily:
tree = deparse_AlterOpFamily(cmd);
break;
diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c
index 0a54f30126..8dc9a0933f 100644
--- a/src/backend/commands/event_trigger.c
+++ b/src/backend/commands/event_trigger.c
@@ -133,7 +133,8 @@ CreateEventTrigger(CreateEventTrigStmt *stmt)
if (strcmp(stmt->eventname, "ddl_command_start") != 0 &&
strcmp(stmt->eventname, "ddl_command_end") != 0 &&
strcmp(stmt->eventname, "sql_drop") != 0 &&
- strcmp(stmt->eventname, "table_rewrite") != 0)
+ strcmp(stmt->eventname, "table_rewrite") != 0 &&
+ strcmp(stmt->eventname, "table_init_write") != 0)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unrecognized event name \"%s\"",
@@ -159,7 +160,8 @@ CreateEventTrigger(CreateEventTrigStmt *stmt)
/* Validate tag list, if any. */
if ((strcmp(stmt->eventname, "ddl_command_start") == 0 ||
strcmp(stmt->eventname, "ddl_command_end") == 0 ||
- strcmp(stmt->eventname, "sql_drop") == 0)
+ strcmp(stmt->eventname, "sql_drop") == 0 ||
+ strcmp(stmt->eventname, "table_init_write") == 0)
&& tags != NULL)
validate_ddl_tags("tag", tags);
else if (strcmp(stmt->eventname, "table_rewrite") == 0
@@ -585,7 +587,8 @@ EventTriggerCommonSetup(Node *parsetree,
dbgtag = CreateCommandTag(parsetree);
if (event == EVT_DDLCommandStart ||
event == EVT_DDLCommandEnd ||
- event == EVT_SQLDrop)
+ event == EVT_SQLDrop ||
+ event == EVT_TableInitWrite)
{
if (!command_tag_event_trigger_ok(dbgtag))
elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag));
@@ -868,6 +871,163 @@ EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason)
CommandCounterIncrement();
}
+
+/*
+ * EventTriggerTableInitWriteStart
+ * Prepare to receive data on an CREATE TABLE AS/SELET INTO command about
+ * to be executed.
+ */
+void
+EventTriggerTableInitWriteStart(Node *parsetree)
+{
+ MemoryContext oldcxt;
+ CollectedCommand *command;
+
+ /* ignore if event trigger context not set, or collection disabled */
+ if (!currentEventTriggerState ||
+ currentEventTriggerState->commandCollectionInhibited)
+ return;
+
+ oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
+
+ command = palloc(sizeof(CollectedCommand));
+
+ command->type = SCT_CreateTableAs;
+ command->in_extension = creating_extension;
+ command->d.ctas.address = InvalidObjectAddress;
+ command->d.ctas.real_create = NULL;
+ command->parsetree = copyObject(parsetree);
+
+ command->parent = currentEventTriggerState->currentCommand;
+ currentEventTriggerState->currentCommand = command;
+
+ MemoryContextSwitchTo(oldcxt);
+}
+
+/*
+ * EventTriggerTableInitWriteEnd
+ * Finish up saving an CREATE TABLE AS/SELECT INTO command.
+ *
+ * FIXME this API isn't considering the possibility that an xact/subxact is
+ * aborted partway through. Probably it's best to add an
+ * AtEOSubXact_EventTriggers() to fix this.
+ */
+void
+EventTriggerTableInitWriteEnd(void)
+{
+ CollectedCommand *parent;
+
+ /* ignore if event trigger context not set, or collection disabled */
+ if (!currentEventTriggerState ||
+ currentEventTriggerState->commandCollectionInhibited)
+ return;
+
+ parent = currentEventTriggerState->currentCommand->parent;
+
+ pfree(currentEventTriggerState->currentCommand);
+
+ currentEventTriggerState->currentCommand = parent;
+}
+
+/*
+ * publication_deparse_table_init_write
+ *
+ * Deparse the ddl table create command and log it.
+ */
+Datum
+publication_deparse_table_init_write(PG_FUNCTION_ARGS)
+{
+ char relpersist;
+ CollectedCommand *cmd;
+ char *json_string;
+
+ if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+ elog(ERROR, "not fired by event trigger manager");
+
+ cmd = currentEventTriggerState->currentCommand;
+ Assert(cmd);
+
+ relpersist = get_rel_persistence(cmd->d.simple.address.objectId);
+
+ /*
+ * Do not generate wal log for commands whose target table is a temporary
+ * table.
+ *
+ * We will generate wal logs for unlogged tables so that unlogged tables
+ * can also be created and altered on the subscriber side. This makes it
+ * possible to directly replay the SET LOGGED command and the incoming
+ * rewrite message without creating a new table.
+ */
+ if (relpersist == RELPERSISTENCE_TEMP)
+ return PointerGetDatum(NULL);
+
+ /* Deparse the DDL command and WAL log it to allow decoding of the same. */
+ json_string = deparse_utility_command(cmd, false);
+
+ if (json_string != NULL)
+ LogLogicalDDLMessage("deparse", cmd->d.simple.address.objectId, DCT_SimpleCmd,
+ json_string, strlen(json_string) + 1);
+
+ return PointerGetDatum(NULL);
+}
+
+/*
+ * Fire table_init_rewrite triggers.
+ */
+void
+EventTriggerTableInitWrite(Node *real_create, ObjectAddress address)
+{
+ List *runlist;
+ EventTriggerData trigdata;
+ CollectedCommand *command;
+
+ /*
+ * See EventTriggerDDLCommandStart for a discussion about why event
+ * triggers are disabled in single user mode.
+ */
+ if (!IsUnderPostmaster)
+ return;
+
+ /*
+ * Also do nothing if our state isn't set up, which it won't be if there
+ * weren't any relevant event triggers at the start of the current DDL
+ * command. This test might therefore seem optional, but it's
+ * *necessary*, because EventTriggerCommonSetup might find triggers that
+ * didn't exist at the time the command started.
+ */
+ if (!currentEventTriggerState)
+ return;
+
+ /* Do nothing if no command was collected. */
+ if (!currentEventTriggerState->currentCommand)
+ return;
+
+ command = currentEventTriggerState->currentCommand;
+
+ runlist = EventTriggerCommonSetup(command->parsetree,
+ EVT_TableInitWrite,
+ "table_init_write",
+ &trigdata);
+ if (runlist == NIL)
+ return;
+
+ /* Set the real CreateTable statment and object address */
+ command->d.ctas.real_create = real_create;
+ command->d.ctas.address = address;
+
+ /* Run the triggers. */
+ EventTriggerInvoke(runlist, &trigdata);
+
+ /* Cleanup. */
+ list_free(runlist);
+
+ /*
+ * Make sure anything the event triggers did will be visible to the main
+ * command.
+ */
+ CommandCounterIncrement();
+}
+
/*
* Invoke each event trigger in a list of event triggers.
*/
@@ -1149,7 +1309,8 @@ trackDroppedObjectsNeeded(void)
*/
return (EventCacheLookup(EVT_SQLDrop) != NIL) ||
(EventCacheLookup(EVT_TableRewrite) != NIL) ||
- (EventCacheLookup(EVT_DDLCommandEnd) != NIL);
+ (EventCacheLookup(EVT_DDLCommandEnd) != NIL) ||
+ (EventCacheLookup(EVT_TableInitWrite) != NIL);
}
/*
@@ -1868,6 +2029,7 @@ pg_event_trigger_ddl_commands(PG_FUNCTION_ARGS)
case SCT_AlterOpFamily:
case SCT_CreateOpClass:
case SCT_AlterTSConfig:
+ case SCT_CreateTableAs:
{
char *identity;
char *type;
@@ -1885,6 +2047,8 @@ pg_event_trigger_ddl_commands(PG_FUNCTION_ARGS)
addr = cmd->d.createopc.address;
else if (cmd->type == SCT_AlterTSConfig)
addr = cmd->d.atscfg.address;
+ else if (cmd->type == SCT_AlterTSConfig)
+ addr = cmd->d.ctas.address;
/*
* If an object was dropped in the same command we may end
diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c
index 13992551f2..e46270d575 100644
--- a/src/backend/commands/publicationcmds.c
+++ b/src/backend/commands/publicationcmds.c
@@ -1015,6 +1015,11 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
CMDTAG_DROP_STATISTICS
};
+ CommandTag init_commands[] = {
+ CMDTAG_CREATE_TABLE_AS,
+ CMDTAG_SELECT_INTO
+ };
+
/* Create the ddl_command_end event trigger */
CreateDDLReplicaEventTrigger("ddl_command_end", end_commands,
lengthof(end_commands), myself, puboid);
@@ -1026,6 +1031,10 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
/* Create the table_rewrite event trigger */
CreateDDLReplicaEventTrigger("table_rewrite", rewrite_commands,
lengthof(rewrite_commands), myself, puboid);
+
+ /* Create the table_init_write event trigger */
+ CreateDDLReplicaEventTrigger("table_init_write", init_commands,
+ lengthof(init_commands), myself, puboid);
}
table_close(rel, RowExclusiveLock);
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index bea35a6281..50839c0d96 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1666,8 +1666,10 @@ ProcessUtilitySlow(ParseState *pstate,
break;
case T_CreateTableAsStmt:
+ EventTriggerTableInitWriteStart(parsetree);
address = ExecCreateTableAs(pstate, (CreateTableAsStmt *) parsetree,
params, queryEnv, qc);
+ EventTriggerTableInitWriteEnd();
break;
case T_RefreshMatViewStmt:
diff --git a/src/backend/utils/cache/evtcache.c b/src/backend/utils/cache/evtcache.c
index f7f7165f7f..7fb8cb291d 100644
--- a/src/backend/utils/cache/evtcache.c
+++ b/src/backend/utils/cache/evtcache.c
@@ -167,6 +167,8 @@ BuildEventTriggerCache(void)
event = EVT_SQLDrop;
else if (strcmp(evtevent, "table_rewrite") == 0)
event = EVT_TableRewrite;
+ else if (strcmp(evtevent, "table_init_write") == 0)
+ event = EVT_TableInitWrite;
else
continue;
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 84f87e1c02..8ddfd0ef45 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11842,4 +11842,7 @@
{ oid => '4646', descr => 'trigger for ddl command deparse table rewrite',
proname => 'publication_deparse_table_rewrite', prorettype => 'event_trigger',
proargtypes => '', prosrc => 'publication_deparse_table_rewrite' },
+{ oid => '4647', descr => 'trigger for ddl command deparse table init',
+ proname => 'publication_deparse_table_init_write', prorettype => 'event_trigger',
+ proargtypes => '', prosrc => 'publication_deparse_table_init_write' },
]
diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h
index fd2ee7ffe4..a9e0f71f6a 100644
--- a/src/include/commands/event_trigger.h
+++ b/src/include/commands/event_trigger.h
@@ -55,6 +55,10 @@ extern void EventTriggerDDLCommandEnd(Node *parsetree);
extern void EventTriggerSQLDrop(Node *parsetree);
extern void EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason);
+extern void EventTriggerTableInitWriteStart(Node *parsetree);
+extern void EventTriggerTableInitWrite(Node *parsetree, ObjectAddress address);
+extern void EventTriggerTableInitWriteEnd(void);
+
extern bool EventTriggerBeginCompleteQuery(void);
extern void EventTriggerEndCompleteQuery(void);
extern bool trackDroppedObjectsNeeded(void);
diff --git a/src/include/tcop/deparse_utility.h b/src/include/tcop/deparse_utility.h
index b53294bf57..3d294a0371 100644
--- a/src/include/tcop/deparse_utility.h
+++ b/src/include/tcop/deparse_utility.h
@@ -29,7 +29,8 @@ typedef enum CollectedCommandType
SCT_AlterOpFamily,
SCT_AlterDefaultPrivileges,
SCT_CreateOpClass,
- SCT_AlterTSConfig
+ SCT_AlterTSConfig,
+ SCT_CreateTableAs
} CollectedCommandType;
/*
@@ -101,6 +102,12 @@ typedef struct CollectedCommand
{
ObjectType objtype;
} defprivs;
+
+ struct
+ {
+ ObjectAddress address;
+ Node *real_create;
+ } ctas;
} d;
struct CollectedCommand *parent; /* when nested */
diff --git a/src/include/utils/evtcache.h b/src/include/utils/evtcache.h
index ddb67a68fa..1e648317ae 100644
--- a/src/include/utils/evtcache.h
+++ b/src/include/utils/evtcache.h
@@ -22,7 +22,8 @@ typedef enum
EVT_DDLCommandStart,
EVT_DDLCommandEnd,
EVT_SQLDrop,
- EVT_TableRewrite
+ EVT_TableRewrite,
+ EVT_TableInitWrite
} EventTriggerEvent;
typedef struct
--
2.32.0
[text/x-patch] v30-0004-Test-cases-for-DDL-replication.patch (24.6K, 5-v30-0004-Test-cases-for-DDL-replication.patch)
download | inline diff:
From a601f3d7edf81b7bb5fa4903c5a87e19ea797ce6 Mon Sep 17 00:00:00 2001
From: Ajin Cherian <ajinc@fast.au.fujitsu.com>
Date: Thu, 13 Oct 2022 08:28:57 -0400
Subject: [PATCH v30 4/4] Test cases for DDL replication.
Author: Takamichi Osumi
---
.../subscription/t/032_ddl_replication.pl | 465 ++++++++++++++++++
1 file changed, 465 insertions(+)
create mode 100644 src/test/subscription/t/032_ddl_replication.pl
diff --git a/src/test/subscription/t/032_ddl_replication.pl b/src/test/subscription/t/032_ddl_replication.pl
new file mode 100644
index 0000000000..66dcb012a6
--- /dev/null
+++ b/src/test/subscription/t/032_ddl_replication.pl
@@ -0,0 +1,465 @@
+# Copyright (c) 2022, PostgreSQL Global Development Group
+# Regression tests for logical replication of DDLs
+#
+use strict;
+use warnings;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
+$node_publisher->init(allows_streaming => 'logical');
+$node_publisher->start;
+
+my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
+$node_subscriber->init(allows_streaming => 'logical');
+$node_subscriber->start;
+
+my $ddl = "CREATE TABLE test_rep(id int primary key, name varchar, value integer);";
+$node_publisher->safe_psql('postgres', $ddl);
+$node_publisher->safe_psql('postgres', "INSERT INTO test_rep VALUES (1, 'data1', 1);");
+$node_subscriber->safe_psql('postgres', $ddl);
+
+my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
+$node_publisher->safe_psql('postgres',
+ "CREATE PUBLICATION mypub FOR ALL TABLES with (publish = 'insert, update, delete, ddl');");
+$node_subscriber->safe_psql('postgres',
+ "CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;");
+$node_publisher->wait_for_catchup('mysub');
+
+# Make sure we have fully synchronized the table.
+# This prevents ALTER TABLE command below from being executed during table synchronization.
+$node_subscriber->poll_query_until('postgres',
+ "SELECT COUNT(1) = 0 FROM pg_subscription_rel sr WHERE sr.srsubstate NOT IN ('s', 'r') AND sr.srrelid = 'test_rep'::regclass"
+);
+
+# Test ALTER TABLE ADD
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep ADD c4 int;");
+$node_publisher->safe_psql('postgres', "INSERT INTO test_rep VALUES (2, 'data2', 2, 2);");
+$node_publisher->wait_for_catchup('mysub');
+my $result = $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_rep WHERE c4 = 2;");
+is($result, qq(1), 'ALTER test_rep ADD replicated');
+
+# Test ALTER TABLE DROP
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep DROP c4;");
+$node_publisher->safe_psql('postgres', "DELETE FROM test_rep where id = 2;");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from test_rep;");
+is($result, qq(1), 'ALTER test_rep DROP replicated');
+
+# Test ALTER TABLE ALTER TYPE
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep ALTER value TYPE varchar");
+$node_publisher->safe_psql('postgres', "INSERT INTO test_rep VALUES (3, 'data3', '3');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_rep WHERE value = '3';");
+is($result, qq(1), 'ALTER test_rep ALTER COLUMN TYPE replicated');
+
+# Test ALTER TABLE ALTER SET DEFAULT
+# Check if we have the default value after the direct insert to subscriber node.
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep ALTER COLUMN value SET DEFAULT 'foo'");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->safe_psql('postgres', "INSERT INTO test_rep VALUES (4, 'data4');");
+$result = $node_subscriber->safe_psql('postgres', "SELECT value FROM test_rep WHERE id = 4;");
+is($result, 'foo', 'ALTER test_rep ALTER SET DEFAULT replicated');
+
+# Test ALTER TABLE ALTER DROP DEFAULT
+# Check if we don't have the default value previously defined.
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep ALTER COLUMN value DROP DEFAULT;");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->safe_psql('postgres', "INSERT INTO test_rep VALUES (5, 'data5');");
+$result = $node_subscriber->safe_psql('postgres', "SELECT value IS NULL FROM test_rep WHERE id = 5;");
+is($result, q(t), 'ALTER test_rep ALTER DROP DEFAULT replicated');
+
+# Test ALTER TABLE ALTER SET NOT NULL
+# Remove the existing record that contains null value first.
+my ($stdout, $stderr);
+$node_subscriber->safe_psql('postgres', "DELETE FROM test_rep WHERE id = 5;");
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep ALTER value SET NOT NULL;");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO test_rep VALUES (6, 'data6');",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: null value in column \"value\" of relation \"test_rep\" violates not-null constraint/
+ or die "failed to replicate ALTER TABLE ALTER SET NOT NULL";
+
+# Test ALTER TABLE ALTER DROP NOT NULL
+$node_publisher->safe_psql('postgres', "ALTER TABLE test_rep ALTER value DROP NOT NULL;");
+$node_publisher->wait_for_catchup('mysub');
+# Insert same data that has NULL value. This failed before but now should succeed.
+$node_subscriber->safe_psql('postgres', "INSERT INTO test_rep VALUES (6, 'data6');");
+$result = $node_subscriber->safe_psql('postgres', "SELECT value IS NULL FROM test_rep WHERE id = 6;");
+is($result, q(t), "ALTER test_rep ALTER DROP NOT NULL replicated");
+
+# Test ALTER TABLE SET UNLOGGED
+$node_publisher->safe_psql('postgres', 'ALTER TABLE test_rep SET UNLOGGED;');
+$node_publisher->wait_for_catchup('mysub');
+$node_publisher->safe_psql('postgres', "INSERT INTO test_rep VALUES (7, 'data7', '7');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_rep WHERE id = 7;");
+is($result, qq(0), 'ALTER test_rep SET UNLOGGED replicated');
+
+# Test ALTER TABLE SET LOGGED
+$node_publisher->safe_psql('postgres', 'ALTER TABLE test_rep SET LOGGED;');
+$node_publisher->wait_for_catchup('mysub');
+$node_publisher->safe_psql('postgres', "INSERT INTO test_rep VALUES (8, 'data8', '8');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_rep WHERE id = 8;");
+is($result, qq(1), 'ALTER test_rep SET LOGGED replicated');
+
+# Test CREATE TABLE and DML changes
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (a int, b varchar);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from pg_tables where tablename = 'tmp';");
+is($result, qq(1), 'CREATE tmp is replicated');
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp values (1, 'a')");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp values (2, 'b')");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(2), 'DML Changes to tmp are replicated');
+
+# Test CREATE TABLE INHERITS
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp2 (c3 int) INHERITS (tmp);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp2 VALUES (1, 'a', 1);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from pg_tables where tablename = 'tmp2';");
+is($result, qq(1), 'CREATE TABLE INHERITS is replicated');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp2;");
+is($result, qq(1), 'inserting some data to inherited table replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp2");
+
+# Test CREATE TABLE LIKE
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp3 (c3 int, LIKE tmp);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp3 VALUES (1, 1, 'a');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from pg_tables where tablename = 'tmp3';");
+is($result, qq(1), 'CREATE TABLE LIKE replicated');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp3;");
+is($result, qq(1), 'insert some data to a table defined with LIKE replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp3");
+
+# Test DROP TABLE
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from pg_tables where tablename = 'tmp';");
+is($result, qq(0), 'TABLE tmp is dropped');
+
+# Test CREATE UNLOGGED TABLE
+$node_publisher->safe_psql('postgres', "CREATE UNLOGGED TABLE tmp (id int);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from pg_tables where tablename = 'tmp';");
+is($result, qq(1), 'CREATE UNLOGGED TABLE is replicated correctly');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(0), 'inserting data to unlogged table is not replicated correctly');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# Test CREATE TABLE IF NOT EXISTS
+$node_publisher->safe_psql('postgres', "CREATE TABLE IF NOT EXISTS tmp (id int);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from pg_tables where tablename = 'tmp';");
+is($result, qq(1), 'CREATE TABLE IF NOT EXISTS replicated');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(1), 'inserting data to a table replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# Test CREATE TABLE IF NOT EXISTS (check if we skip to create a table
+# when we have the table on the subscriber in advance, and if we succeed
+# in replicating changes.)
+$node_subscriber->safe_psql('postgres', "CREATE TABLE tmp (id int);");
+$node_publisher->safe_psql('postgres', "CREATE TABLE IF NOT EXISTS tmp (id int);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(1), 'CREATE TABLE IF NOT EXISTS replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# Test CREATE TABLE IF NOT EXISTS (check if we skip to create a table
+# when we have the table on the publisher, but not on the subscriber.)
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int);");
+$node_publisher->safe_psql('postgres', "CREATE TABLE IF NOT EXISTS tmp (id int);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(1), 'CREATE TABLE IF NOT EXISTS replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# Test CREATE TABLE with collate
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (name text COLLATE \"C\");");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES ('foo');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT collation_name FROM information_schema.columns WHERE table_name = 'tmp';");
+is($result, qq(C), 'CREATE TABLE with collate replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# Test CREATE TABLE with named constraint
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int CONSTRAINT \"must be bigger than 10\" CHECK (id > 10));");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (1);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: new row for relation "tmp" violates check constraint "must be bigger than 10"/
+ or die "failed to replicate named constraint at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# Test CREATE TABLE with various types of constraints.
+# NOT NULL constraint
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int, name text NOT NULL);");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (1);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: null value in column "name" of relation "tmp" violates not-null constraint/
+ or die "failed to replicate non null constraint at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# NULL constraint
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int, name text NULL);");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$result = $node_subscriber->safe_psql('postgres', "SELECT name IS NULL FROM tmp;");
+is($result, qq(t), "CREATE TABLE with NULL constraint replicated");
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# CHECK constraint
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int, product_ame text, price int CHECK (price > 0));");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (1, 'foo', -100);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: new row for relation "tmp" violates check constraint "tmp_price_check"/
+ or die "failed to replicate CHECK constraint";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# DEFAULT
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int, name text DEFAULT 'foo');");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$result = $node_subscriber->safe_psql('postgres', "SELECT name from tmp;");
+is($result, qq(foo), "CREATE TABLE with default value replicated");
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+
+# UNIQUE constraint
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int UNIQUE);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (1);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: duplicate key value violates unique constraint "tmp_id_key"/
+ or die "failed to replicate constraint at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# PRIMARY KEY
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int PRIMARY KEY, name text);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1, 'foo');");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (1, 'bar');",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: duplicate key value violates unique constraint "tmp_pkey"/
+ or die "failed to replicate primary key at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# EXCLUDE
+$node_publisher->safe_psql('postgres', "CREATE TABLE circles (c circle, EXCLUDE USING gist (c WITH &&));");
+$node_publisher->safe_psql('postgres', "INSERT INTO circles VALUES ('<(1, 1), 1>'::circle);");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO circles VALUES ('<(1, 1), 1>'::circle);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: conflicting key value violates exclusion constraint "circles_c_excl"/
+ or die "failed to replicate EXCLUDE at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE circles");
+
+# REFERENCES
+$node_publisher->safe_psql('postgres', "CREATE TABLE product (id int PRIMARY KEY, name text);");
+$node_publisher->safe_psql('postgres', "INSERT INTO product VALUES (1, 'foo');");
+$node_publisher->safe_psql('postgres', "INSERT INTO product VALUES (2, 'bar');");
+$node_publisher->safe_psql('postgres', "CREATE TABLE orders (order_id int PRIMARY KEY, product_id int REFERENCES product (id))");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO orders VALUES (1, 10)",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: insert or update on table "orders" violates foreign key constraint "orders_product_id_fkey"/
+ or die "failed to replicate REFERENCES at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE orders");
+$node_publisher->safe_psql('postgres', "DROP TABLE product");
+
+# DEFERRABLE
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int PRIMARY KEY DEFERRABLE, name text);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1, 'foo');");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (2, 'bar');");
+$node_publisher->wait_for_catchup('mysub');
+# Quick check of deferrable clause
+$node_subscriber->safe_psql('postgres', "UPDATE tmp SET id = id + 1;");
+# Also, execute a test that should fail for INITIALLY IMMEDIATE(the default)
+$node_subscriber->psql('postgres', qq(
+BEGIN;
+UPDATE tmp SET id = id + 1;
+INSERT INTO tmp VALUES (3, 'foobar');
+DELETE FROM tmp WHERE id = 3;
+COMMIT;
+), on_error_stop => 0, stderr => \$stderr, stdout => \$stdout);
+$stderr =~ /ERROR: duplicate key value violates unique constraint "tmp_pkey"/
+ or die "failed to replicate DEFERRABLE at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# NOT DEFERRABLE
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int PRIMARY KEY NOT DEFERRABLE, name text);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1, 'foo');");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (2, 'bar');");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "UPDATE tmp SET id = id + 1;",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: duplicate key value violates unique constraint "tmp_pkey"/
+ or die "failed to replicate NOT DEFERRABLE at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# DEFERRABLE and INITIALLY DEFERRED
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int PRIMARY KEY DEFERRABLE INITIALLY DEFERRED, name text);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1, 'foo');");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (2, 'bar');");
+$node_publisher->wait_for_catchup('mysub');
+# Quick check of deferrable clause
+$node_subscriber->safe_psql('postgres', "UPDATE tmp SET id = id + 1;");
+# Also, execute a test that should succeed for INITIALLY DEFERRED
+$node_subscriber->safe_psql('postgres', qq(
+BEGIN;
+UPDATE tmp SET id = id + 1;
+INSERT INTO tmp VALUES (3, 'foobar');
+DELETE FROM tmp WHERE id = 3;
+COMMIT;
+));
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# Test CREATE TABLE with table constraint
+# We will set two checks and conduct two inserts that should fail respectively.
+$node_publisher->safe_psql('postgres',
+ "CREATE TABLE tmp (price int, discounted_price int, CHECK (discounted_price > 0 AND price > discounted_price));");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (100, 0);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: new row for relation "tmp" violates check constraint "tmp_check"/
+ or die "failed to replicate table constraint (first condition) at creating table";
+$node_subscriber->psql('postgres', "INSERT INTO tmp VALUES (50, 100);",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stderr =~ /ERROR: new row for relation "tmp" violates check constraint "tmp_check"/
+ or die "failed to replicate table constraint (second condition) at creating table";
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# Test CREATE TABLE WITH strorage_parameter
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int) WITH (fillfactor = 80);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$node_subscriber->psql('postgres', "SELECT reloptions FROM pg_class WHERE relname = 'tmp';",
+ on_error_stop => 0,
+ stderr => \$stderr,
+ stdout => \$stdout);
+$stdout =~ /{fillfactor=80}/
+ or die "failed to replicate storage option";
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(1), 'CREATE TABLE with storage_parameter replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# Test CREATE TABLE TABLESPACE (creating a tablespace is not replicated)
+# Prepare the directories for the publisher and subscriber first.
+my ($basedir, $tablespace_dir);
+
+$basedir = $node_publisher->basedir;
+$tablespace_dir = "$basedir/tblspc_pub";
+mkdir($tablespace_dir);
+$node_publisher->safe_psql('postgres', "CREATE TABLESPACE mytblspc LOCATION '$tablespace_dir';");
+$basedir = $node_subscriber->basedir;
+$tablespace_dir = "$basedir/tblspc_sub";
+mkdir ($tablespace_dir);
+$node_subscriber->safe_psql('postgres', "CREATE TABLESPACE mytblspc LOCATION '$tablespace_dir';");
+
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (id int) TABLESPACE mytblspc;");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1);");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(1), 'CREATE TABLE TABLESPACE replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp;");
+
+# Test CREATE TYPE
+$node_publisher->safe_psql('postgres', "CREATE TYPE mytype AS (id int, name text, age int);");
+$node_publisher->wait_for_catchup('mysub');
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp OF mytype;");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES (1, 'bar');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT count(*) from tmp;");
+is($result, qq(1), 'CREATE TABLE OF replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+$node_publisher->safe_psql('postgres', "DROP TYPE mytype");
+
+# Test CREATE ENUM TYPE
+$node_publisher->safe_psql('postgres', "CREATE TYPE myenumtype AS ENUM ('new', 'open', 'closed');");
+$node_publisher->wait_for_catchup('mysub');
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (status myenumtype);");
+$node_publisher->safe_psql('postgres', "INSERT INTO tmp VALUES ('new');");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT * from tmp;");
+is($result, qq(new), 'CREATE TABLE OF replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+$node_publisher->safe_psql('postgres', "DROP TYPE myenumtype");
+
+# Test CREATE RANGE TYPE
+$node_publisher->safe_psql('postgres', "CREATE TYPE float8_range AS RANGE (subtype = float8, subtype_diff = float8mi);");
+$node_publisher->wait_for_catchup('mysub');
+$node_publisher->safe_psql('postgres', "CREATE TABLE tmp (val float8_range);");
+$node_publisher->safe_psql('postgres', "insert into tmp values(float8_range(-12.34, '1.111113e3'));");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT * from tmp;");
+is($result, qq([-12.34,1111.113\)), 'CREATE TABLE OF replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+$node_publisher->safe_psql('postgres', "DROP TYPE float8_range");
+
+# Test CREATE BASE DATA TYPE and use it in table definition
+$node_publisher->safe_psql('postgres', "
+CREATE TYPE int42;
+CREATE FUNCTION int42_in(cstring)
+ RETURNS int42
+ AS 'int4in'
+ LANGUAGE internal STRICT IMMUTABLE;
+CREATE FUNCTION int42_out(int42)
+ RETURNS cstring
+ AS 'int4out'
+ LANGUAGE internal STRICT IMMUTABLE;
+CREATE TYPE int42 (
+ internallength = 4,
+ input = int42_in,
+ output = int42_out,
+ alignment = int4,
+ default = 42,
+ passedbyvalue
+);
+CREATE TABLE tmp (f1 int42);
+INSERT INTO tmp DEFAULT VALUES;
+");
+$node_publisher->wait_for_catchup('mysub');
+$result = $node_subscriber->safe_psql('postgres', "SELECT * from tmp;");
+is($result, qq(42), 'CREATE TABLE OF replicated');
+$node_publisher->safe_psql('postgres', "DROP TABLE tmp");
+$node_publisher->safe_psql('postgres', "DROP TYPE int42 cascade");
+
+pass "DDL replication tests passed:";
+
+$node_subscriber->stop;
+$node_publisher->stop;
+
+done_testing();
--
2.32.0
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-general@postgresql.org
Cc: vignesh21@gmail.com, dilipbalaut@gmail.com, itsajin@gmail.com, zhengli10@gmail.com, alvherre@alvh.no-ip.org, houzj.fnst@fujitsu.com, smithpb2250@gmail.com, amit.kapila16@gmail.com, sawada.mshk@gmail.com, japinli@hotmail.com, rajesh.rs0541@gmail.com, pgsql-hackers@lists.postgresql.org
Subject: Re: Support logical replication of DDLs
In-Reply-To: <CALDaNm08gZq9a7xnsbaJMmHmi29_kbEuyShHHfxAKLXPh6btWQ@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox