summaryrefslogtreecommitdiffstats
path: root/src/backend/tcop
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/tcop')
-rw-r--r--src/backend/tcop/Makefile23
-rw-r--r--src/backend/tcop/cmdtag.c98
-rw-r--r--src/backend/tcop/dest.c300
-rw-r--r--src/backend/tcop/fastpath.c458
-rw-r--r--src/backend/tcop/postgres.c5070
-rw-r--r--src/backend/tcop/pquery.c1807
-rw-r--r--src/backend/tcop/utility.c3761
7 files changed, 11517 insertions, 0 deletions
diff --git a/src/backend/tcop/Makefile b/src/backend/tcop/Makefile
new file mode 100644
index 0000000..f662a7d
--- /dev/null
+++ b/src/backend/tcop/Makefile
@@ -0,0 +1,23 @@
+#-------------------------------------------------------------------------
+#
+# Makefile--
+# Makefile for tcop
+#
+# IDENTIFICATION
+# src/backend/tcop/Makefile
+#
+#-------------------------------------------------------------------------
+
+subdir = src/backend/tcop
+top_builddir = ../../..
+include $(top_builddir)/src/Makefile.global
+
+OBJS = \
+ cmdtag.o \
+ dest.o \
+ fastpath.o \
+ postgres.o \
+ pquery.o \
+ utility.o
+
+include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/tcop/cmdtag.c b/src/backend/tcop/cmdtag.c
new file mode 100644
index 0000000..262484f
--- /dev/null
+++ b/src/backend/tcop/cmdtag.c
@@ -0,0 +1,98 @@
+/*-------------------------------------------------------------------------
+ *
+ * cmdtag.c
+ * Data and routines for commandtag names and enumeration.
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/backend/tcop/cmdtag.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "miscadmin.h"
+#include "tcop/cmdtag.h"
+
+
+typedef struct CommandTagBehavior
+{
+ const char *name;
+ const bool event_trigger_ok;
+ const bool table_rewrite_ok;
+ const bool display_rowcount;
+} CommandTagBehavior;
+
+#define PG_CMDTAG(tag, name, evtrgok, rwrok, rowcnt) \
+ { name, evtrgok, rwrok, rowcnt },
+
+static const CommandTagBehavior tag_behavior[COMMAND_TAG_NEXTTAG] = {
+#include "tcop/cmdtaglist.h"
+};
+
+#undef PG_CMDTAG
+
+void
+InitializeQueryCompletion(QueryCompletion *qc)
+{
+ qc->commandTag = CMDTAG_UNKNOWN;
+ qc->nprocessed = 0;
+}
+
+const char *
+GetCommandTagName(CommandTag commandTag)
+{
+ return tag_behavior[commandTag].name;
+}
+
+bool
+command_tag_display_rowcount(CommandTag commandTag)
+{
+ return tag_behavior[commandTag].display_rowcount;
+}
+
+bool
+command_tag_event_trigger_ok(CommandTag commandTag)
+{
+ return tag_behavior[commandTag].event_trigger_ok;
+}
+
+bool
+command_tag_table_rewrite_ok(CommandTag commandTag)
+{
+ return tag_behavior[commandTag].table_rewrite_ok;
+}
+
+/*
+ * Search CommandTag by name
+ *
+ * Returns CommandTag, or CMDTAG_UNKNOWN if not recognized
+ */
+CommandTag
+GetCommandTagEnum(const char *commandname)
+{
+ const CommandTagBehavior *base,
+ *last,
+ *position;
+ int result;
+
+ if (commandname == NULL || *commandname == '\0')
+ return CMDTAG_UNKNOWN;
+
+ base = tag_behavior;
+ last = tag_behavior + lengthof(tag_behavior) - 1;
+ while (last >= base)
+ {
+ position = base + ((last - base) >> 1);
+ result = pg_strcasecmp(commandname, position->name);
+ if (result == 0)
+ return (CommandTag) (position - tag_behavior);
+ else if (result < 0)
+ last = position - 1;
+ else
+ base = position + 1;
+ }
+ return CMDTAG_UNKNOWN;
+}
diff --git a/src/backend/tcop/dest.c b/src/backend/tcop/dest.c
new file mode 100644
index 0000000..c952cbe
--- /dev/null
+++ b/src/backend/tcop/dest.c
@@ -0,0 +1,300 @@
+/*-------------------------------------------------------------------------
+ *
+ * dest.c
+ * support for communication destinations
+ *
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/backend/tcop/dest.c
+ *
+ *-------------------------------------------------------------------------
+ */
+/*
+ * INTERFACE ROUTINES
+ * BeginCommand - initialize the destination at start of command
+ * CreateDestReceiver - create tuple receiver object for destination
+ * EndCommand - clean up the destination at end of command
+ * NullCommand - tell dest that an empty query string was recognized
+ * ReadyForQuery - tell dest that we are ready for a new query
+ *
+ * NOTES
+ * These routines do the appropriate work before and after
+ * tuples are returned by a query to keep the backend and the
+ * "destination" portals synchronized.
+ */
+
+#include "postgres.h"
+
+#include "access/printsimple.h"
+#include "access/printtup.h"
+#include "access/xact.h"
+#include "commands/copy.h"
+#include "commands/createas.h"
+#include "commands/matview.h"
+#include "executor/functions.h"
+#include "executor/tqueue.h"
+#include "executor/tstoreReceiver.h"
+#include "libpq/libpq.h"
+#include "libpq/pqformat.h"
+#include "utils/portal.h"
+
+
+/* ----------------
+ * dummy DestReceiver functions
+ * ----------------
+ */
+static bool
+donothingReceive(TupleTableSlot *slot, DestReceiver *self)
+{
+ return true;
+}
+
+static void
+donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
+{
+}
+
+static void
+donothingCleanup(DestReceiver *self)
+{
+ /* this is used for both shutdown and destroy methods */
+}
+
+/* ----------------
+ * static DestReceiver structs for dest types needing no local state
+ * ----------------
+ */
+static const DestReceiver donothingDR = {
+ donothingReceive, donothingStartup, donothingCleanup, donothingCleanup,
+ DestNone
+};
+
+static const DestReceiver debugtupDR = {
+ debugtup, debugStartup, donothingCleanup, donothingCleanup,
+ DestDebug
+};
+
+static const DestReceiver printsimpleDR = {
+ printsimple, printsimple_startup, donothingCleanup, donothingCleanup,
+ DestRemoteSimple
+};
+
+static const DestReceiver spi_printtupDR = {
+ spi_printtup, spi_dest_startup, donothingCleanup, donothingCleanup,
+ DestSPI
+};
+
+/*
+ * Globally available receiver for DestNone.
+ *
+ * It's ok to cast the constness away as any modification of the none receiver
+ * would be a bug (which gets easier to catch this way).
+ */
+DestReceiver *None_Receiver = (DestReceiver *) &donothingDR;
+
+/* ----------------
+ * BeginCommand - initialize the destination at start of command
+ * ----------------
+ */
+void
+BeginCommand(CommandTag commandTag, CommandDest dest)
+{
+ /* Nothing to do at present */
+}
+
+/* ----------------
+ * CreateDestReceiver - return appropriate receiver function set for dest
+ * ----------------
+ */
+DestReceiver *
+CreateDestReceiver(CommandDest dest)
+{
+ /*
+ * It's ok to cast the constness away as any modification of the none
+ * receiver would be a bug (which gets easier to catch this way).
+ */
+
+ switch (dest)
+ {
+ case DestRemote:
+ case DestRemoteExecute:
+ return printtup_create_DR(dest);
+
+ case DestRemoteSimple:
+ return unconstify(DestReceiver *, &printsimpleDR);
+
+ case DestNone:
+ return unconstify(DestReceiver *, &donothingDR);
+
+ case DestDebug:
+ return unconstify(DestReceiver *, &debugtupDR);
+
+ case DestSPI:
+ return unconstify(DestReceiver *, &spi_printtupDR);
+
+ case DestTuplestore:
+ return CreateTuplestoreDestReceiver();
+
+ case DestIntoRel:
+ return CreateIntoRelDestReceiver(NULL);
+
+ case DestCopyOut:
+ return CreateCopyDestReceiver();
+
+ case DestSQLFunction:
+ return CreateSQLFunctionDestReceiver();
+
+ case DestTransientRel:
+ return CreateTransientRelDestReceiver(InvalidOid);
+
+ case DestTupleQueue:
+ return CreateTupleQueueDestReceiver(NULL);
+ }
+
+ /* should never get here */
+ pg_unreachable();
+}
+
+/* ----------------
+ * EndCommand - clean up the destination at end of command
+ * ----------------
+ */
+void
+EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
+{
+ char completionTag[COMPLETION_TAG_BUFSIZE];
+ CommandTag tag;
+ const char *tagname;
+
+ switch (dest)
+ {
+ case DestRemote:
+ case DestRemoteExecute:
+ case DestRemoteSimple:
+
+ /*
+ * We assume the tagname is plain ASCII and therefore requires no
+ * encoding conversion.
+ *
+ * We no longer display LastOid, but to preserve the wire
+ * protocol, we write InvalidOid where the LastOid used to be
+ * written.
+ *
+ * All cases where LastOid was written also write nprocessed
+ * count, so just Assert that rather than having an extra test.
+ */
+ tag = qc->commandTag;
+ tagname = GetCommandTagName(tag);
+
+ if (command_tag_display_rowcount(tag) && !force_undecorated_output)
+ snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
+ tag == CMDTAG_INSERT ?
+ "%s 0 " UINT64_FORMAT : "%s " UINT64_FORMAT,
+ tagname, qc->nprocessed);
+ else
+ snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s", tagname);
+ pq_putmessage('C', completionTag, strlen(completionTag) + 1);
+
+ case DestNone:
+ case DestDebug:
+ case DestSPI:
+ case DestTuplestore:
+ case DestIntoRel:
+ case DestCopyOut:
+ case DestSQLFunction:
+ case DestTransientRel:
+ case DestTupleQueue:
+ break;
+ }
+}
+
+/* ----------------
+ * EndReplicationCommand - stripped down version of EndCommand
+ *
+ * For use by replication commands.
+ * ----------------
+ */
+void
+EndReplicationCommand(const char *commandTag)
+{
+ pq_putmessage('C', commandTag, strlen(commandTag) + 1);
+}
+
+/* ----------------
+ * NullCommand - tell dest that an empty query string was recognized
+ *
+ * This ensures that there will be a recognizable end to the response
+ * to an Execute message in the extended query protocol.
+ * ----------------
+ */
+void
+NullCommand(CommandDest dest)
+{
+ switch (dest)
+ {
+ case DestRemote:
+ case DestRemoteExecute:
+ case DestRemoteSimple:
+
+ /* Tell the FE that we saw an empty query string */
+ pq_putemptymessage('I');
+ break;
+
+ case DestNone:
+ case DestDebug:
+ case DestSPI:
+ case DestTuplestore:
+ case DestIntoRel:
+ case DestCopyOut:
+ case DestSQLFunction:
+ case DestTransientRel:
+ case DestTupleQueue:
+ break;
+ }
+}
+
+/* ----------------
+ * ReadyForQuery - tell dest that we are ready for a new query
+ *
+ * The ReadyForQuery message is sent so that the FE can tell when
+ * we are done processing a query string.
+ * In versions 3.0 and up, it also carries a transaction state indicator.
+ *
+ * Note that by flushing the stdio buffer here, we can avoid doing it
+ * most other places and thus reduce the number of separate packets sent.
+ * ----------------
+ */
+void
+ReadyForQuery(CommandDest dest)
+{
+ switch (dest)
+ {
+ case DestRemote:
+ case DestRemoteExecute:
+ case DestRemoteSimple:
+ {
+ StringInfoData buf;
+
+ pq_beginmessage(&buf, 'Z');
+ pq_sendbyte(&buf, TransactionBlockStatusCode());
+ pq_endmessage(&buf);
+ }
+ /* Flush output at end of cycle in any case. */
+ pq_flush();
+ break;
+
+ case DestNone:
+ case DestDebug:
+ case DestSPI:
+ case DestTuplestore:
+ case DestIntoRel:
+ case DestCopyOut:
+ case DestSQLFunction:
+ case DestTransientRel:
+ case DestTupleQueue:
+ break;
+ }
+}
diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c
new file mode 100644
index 0000000..d429aa4
--- /dev/null
+++ b/src/backend/tcop/fastpath.c
@@ -0,0 +1,458 @@
+/*-------------------------------------------------------------------------
+ *
+ * fastpath.c
+ * routines to handle function requests from the frontend
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/tcop/fastpath.c
+ *
+ * NOTES
+ * This cruft is the server side of PQfn.
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "access/xact.h"
+#include "catalog/objectaccess.h"
+#include "catalog/pg_proc.h"
+#include "libpq/libpq.h"
+#include "libpq/pqformat.h"
+#include "mb/pg_wchar.h"
+#include "miscadmin.h"
+#include "port/pg_bswap.h"
+#include "tcop/fastpath.h"
+#include "tcop/tcopprot.h"
+#include "utils/acl.h"
+#include "utils/lsyscache.h"
+#include "utils/snapmgr.h"
+#include "utils/syscache.h"
+
+
+/*
+ * Formerly, this code attempted to cache the function and type info
+ * looked up by fetch_fp_info, but only for the duration of a single
+ * transaction command (since in theory the info could change between
+ * commands). This was utterly useless, because postgres.c executes
+ * each fastpath call as a separate transaction command, and so the
+ * cached data could never actually have been reused. If it had worked
+ * as intended, it would have had problems anyway with dangling references
+ * in the FmgrInfo struct. So, forget about caching and just repeat the
+ * syscache fetches on each usage. They're not *that* expensive.
+ */
+struct fp_info
+{
+ Oid funcid;
+ FmgrInfo flinfo; /* function lookup info for funcid */
+ Oid namespace; /* other stuff from pg_proc */
+ Oid rettype;
+ Oid argtypes[FUNC_MAX_ARGS];
+ char fname[NAMEDATALEN]; /* function name for logging */
+};
+
+
+static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
+ FunctionCallInfo fcinfo);
+
+/* ----------------
+ * SendFunctionResult
+ * ----------------
+ */
+static void
+SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
+{
+ StringInfoData buf;
+
+ pq_beginmessage(&buf, 'V');
+
+ if (isnull)
+ {
+ pq_sendint32(&buf, -1);
+ }
+ else
+ {
+ if (format == 0)
+ {
+ Oid typoutput;
+ bool typisvarlena;
+ char *outputstr;
+
+ getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
+ outputstr = OidOutputFunctionCall(typoutput, retval);
+ pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
+ pfree(outputstr);
+ }
+ else if (format == 1)
+ {
+ Oid typsend;
+ bool typisvarlena;
+ bytea *outputbytes;
+
+ getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
+ outputbytes = OidSendFunctionCall(typsend, retval);
+ pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
+ pq_sendbytes(&buf, VARDATA(outputbytes),
+ VARSIZE(outputbytes) - VARHDRSZ);
+ pfree(outputbytes);
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("unsupported format code: %d", format)));
+ }
+
+ pq_endmessage(&buf);
+}
+
+/*
+ * fetch_fp_info
+ *
+ * Performs catalog lookups to load a struct fp_info 'fip' for the
+ * function 'func_id'.
+ */
+static void
+fetch_fp_info(Oid func_id, struct fp_info *fip)
+{
+ HeapTuple func_htp;
+ Form_pg_proc pp;
+
+ Assert(fip != NULL);
+
+ /*
+ * Since the validity of this structure is determined by whether the
+ * funcid is OK, we clear the funcid here. It must not be set to the
+ * correct value until we are about to return with a good struct fp_info,
+ * since we can be interrupted (i.e., with an ereport(ERROR, ...)) at any
+ * time. [No longer really an issue since we don't save the struct
+ * fp_info across transactions anymore, but keep it anyway.]
+ */
+ MemSet(fip, 0, sizeof(struct fp_info));
+ fip->funcid = InvalidOid;
+
+ func_htp = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_id));
+ if (!HeapTupleIsValid(func_htp))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_FUNCTION),
+ errmsg("function with OID %u does not exist", func_id)));
+ pp = (Form_pg_proc) GETSTRUCT(func_htp);
+
+ /* reject pg_proc entries that are unsafe to call via fastpath */
+ if (pp->prokind != PROKIND_FUNCTION || pp->proretset)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot call function \"%s\" via fastpath interface",
+ NameStr(pp->proname))));
+
+ /* watch out for catalog entries with more than FUNC_MAX_ARGS args */
+ if (pp->pronargs > FUNC_MAX_ARGS)
+ elog(ERROR, "function %s has more than %d arguments",
+ NameStr(pp->proname), FUNC_MAX_ARGS);
+
+ fip->namespace = pp->pronamespace;
+ fip->rettype = pp->prorettype;
+ memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
+ strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
+
+ ReleaseSysCache(func_htp);
+
+ fmgr_info(func_id, &fip->flinfo);
+
+ /*
+ * This must be last!
+ */
+ fip->funcid = func_id;
+}
+
+
+/*
+ * HandleFunctionRequest
+ *
+ * Server side of PQfn (fastpath function calls from the frontend).
+ * This corresponds to the libpq protocol symbol "F".
+ *
+ * INPUT:
+ * postgres.c has already read the message body and will pass it in
+ * msgBuf.
+ *
+ * Note: palloc()s done here and in the called function do not need to be
+ * cleaned up explicitly. We are called from PostgresMain() in the
+ * MessageContext memory context, which will be automatically reset when
+ * control returns to PostgresMain.
+ */
+void
+HandleFunctionRequest(StringInfo msgBuf)
+{
+ LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
+ Oid fid;
+ AclResult aclresult;
+ int16 rformat;
+ Datum retval;
+ struct fp_info my_fp;
+ struct fp_info *fip;
+ bool callit;
+ bool was_logged = false;
+ char msec_str[32];
+
+ /*
+ * We only accept COMMIT/ABORT if we are in an aborted transaction, and
+ * COMMIT/ABORT cannot be executed through the fastpath interface.
+ */
+ if (IsAbortedTransactionBlockState())
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block")));
+
+ /*
+ * Now that we know we are in a valid transaction, set snapshot in case
+ * needed by function itself or one of the datatype I/O routines.
+ */
+ PushActiveSnapshot(GetTransactionSnapshot());
+
+ /*
+ * Begin parsing the buffer contents.
+ */
+ fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
+
+ /*
+ * There used to be a lame attempt at caching lookup info here. Now we
+ * just do the lookups on every call.
+ */
+ fip = &my_fp;
+ fetch_fp_info(fid, fip);
+
+ /* Log as soon as we have the function OID and name */
+ if (log_statement == LOGSTMT_ALL)
+ {
+ ereport(LOG,
+ (errmsg("fastpath function call: \"%s\" (OID %u)",
+ fip->fname, fid)));
+ was_logged = true;
+ }
+
+ /*
+ * Check permission to access and call function. Since we didn't go
+ * through a normal name lookup, we need to check schema usage too.
+ */
+ aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
+ if (aclresult != ACLCHECK_OK)
+ aclcheck_error(aclresult, OBJECT_SCHEMA,
+ get_namespace_name(fip->namespace));
+ InvokeNamespaceSearchHook(fip->namespace, true);
+
+ aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
+ if (aclresult != ACLCHECK_OK)
+ aclcheck_error(aclresult, OBJECT_FUNCTION,
+ get_func_name(fid));
+ InvokeFunctionExecuteHook(fid);
+
+ /*
+ * Prepare function call info block and insert arguments.
+ *
+ * Note: for now we pass collation = InvalidOid, so collation-sensitive
+ * functions can't be called this way. Perhaps we should pass
+ * DEFAULT_COLLATION_OID, instead?
+ */
+ InitFunctionCallInfoData(*fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
+
+ rformat = parse_fcall_arguments(msgBuf, fip, fcinfo);
+
+ /* Verify we reached the end of the message where expected. */
+ pq_getmsgend(msgBuf);
+
+ /*
+ * If func is strict, must not call it for null args.
+ */
+ callit = true;
+ if (fip->flinfo.fn_strict)
+ {
+ int i;
+
+ for (i = 0; i < fcinfo->nargs; i++)
+ {
+ if (fcinfo->args[i].isnull)
+ {
+ callit = false;
+ break;
+ }
+ }
+ }
+
+ if (callit)
+ {
+ /* Okay, do it ... */
+ retval = FunctionCallInvoke(fcinfo);
+ }
+ else
+ {
+ fcinfo->isnull = true;
+ retval = (Datum) 0;
+ }
+
+ /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
+ CHECK_FOR_INTERRUPTS();
+
+ SendFunctionResult(retval, fcinfo->isnull, fip->rettype, rformat);
+
+ /* We no longer need the snapshot */
+ PopActiveSnapshot();
+
+ /*
+ * Emit duration logging if appropriate.
+ */
+ switch (check_log_duration(msec_str, was_logged))
+ {
+ case 1:
+ ereport(LOG,
+ (errmsg("duration: %s ms", msec_str)));
+ break;
+ case 2:
+ ereport(LOG,
+ (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
+ msec_str, fip->fname, fid)));
+ break;
+ }
+}
+
+/*
+ * Parse function arguments in a 3.0 protocol message
+ *
+ * Argument values are loaded into *fcinfo, and the desired result format
+ * is returned.
+ */
+static int16
+parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
+ FunctionCallInfo fcinfo)
+{
+ int nargs;
+ int i;
+ int numAFormats;
+ int16 *aformats = NULL;
+ StringInfoData abuf;
+
+ /* Get the argument format codes */
+ numAFormats = pq_getmsgint(msgBuf, 2);
+ if (numAFormats > 0)
+ {
+ aformats = (int16 *) palloc(numAFormats * sizeof(int16));
+ for (i = 0; i < numAFormats; i++)
+ aformats[i] = pq_getmsgint(msgBuf, 2);
+ }
+
+ nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
+
+ if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("function call message contains %d arguments but function requires %d",
+ nargs, fip->flinfo.fn_nargs)));
+
+ fcinfo->nargs = nargs;
+
+ if (numAFormats > 1 && numAFormats != nargs)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("function call message contains %d argument formats but %d arguments",
+ numAFormats, nargs)));
+
+ initStringInfo(&abuf);
+
+ /*
+ * Copy supplied arguments into arg vector.
+ */
+ for (i = 0; i < nargs; ++i)
+ {
+ int argsize;
+ int16 aformat;
+
+ argsize = pq_getmsgint(msgBuf, 4);
+ if (argsize == -1)
+ {
+ fcinfo->args[i].isnull = true;
+ }
+ else
+ {
+ fcinfo->args[i].isnull = false;
+ if (argsize < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("invalid argument size %d in function call message",
+ argsize)));
+
+ /* Reset abuf to empty, and insert raw data into it */
+ resetStringInfo(&abuf);
+ appendBinaryStringInfo(&abuf,
+ pq_getmsgbytes(msgBuf, argsize),
+ argsize);
+ }
+
+ if (numAFormats > 1)
+ aformat = aformats[i];
+ else if (numAFormats > 0)
+ aformat = aformats[0];
+ else
+ aformat = 0; /* default = text */
+
+ if (aformat == 0)
+ {
+ Oid typinput;
+ Oid typioparam;
+ char *pstring;
+
+ getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
+
+ /*
+ * Since stringinfo.c keeps a trailing null in place even for
+ * binary data, the contents of abuf are a valid C string. We
+ * have to do encoding conversion before calling the typinput
+ * routine, though.
+ */
+ if (argsize == -1)
+ pstring = NULL;
+ else
+ pstring = pg_client_to_server(abuf.data, argsize);
+
+ fcinfo->args[i].value = OidInputFunctionCall(typinput, pstring,
+ typioparam, -1);
+ /* Free result of encoding conversion, if any */
+ if (pstring && pstring != abuf.data)
+ pfree(pstring);
+ }
+ else if (aformat == 1)
+ {
+ Oid typreceive;
+ Oid typioparam;
+ StringInfo bufptr;
+
+ /* Call the argument type's binary input converter */
+ getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
+
+ if (argsize == -1)
+ bufptr = NULL;
+ else
+ bufptr = &abuf;
+
+ fcinfo->args[i].value = OidReceiveFunctionCall(typreceive, bufptr,
+ typioparam, -1);
+
+ /* Trouble if it didn't eat the whole buffer */
+ if (argsize != -1 && abuf.cursor != abuf.len)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
+ errmsg("incorrect binary data format in function argument %d",
+ i + 1)));
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("unsupported format code: %d", aformat)));
+ }
+
+ /* Return result format code */
+ return (int16) pq_getmsgint(msgBuf, 2);
+}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
new file mode 100644
index 0000000..66294ab
--- /dev/null
+++ b/src/backend/tcop/postgres.c
@@ -0,0 +1,5070 @@
+/*-------------------------------------------------------------------------
+ *
+ * postgres.c
+ * POSTGRES C Backend Interface
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/tcop/postgres.c
+ *
+ * NOTES
+ * this is the "main" module of the postgres backend and
+ * hence the main module of the "traffic cop".
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include <fcntl.h>
+#include <limits.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+
+#ifndef HAVE_GETRUSAGE
+#include "rusagestub.h"
+#endif
+
+#include "access/parallel.h"
+#include "access/printtup.h"
+#include "access/xact.h"
+#include "catalog/pg_type.h"
+#include "commands/async.h"
+#include "commands/prepare.h"
+#include "common/pg_prng.h"
+#include "jit/jit.h"
+#include "libpq/libpq.h"
+#include "libpq/pqformat.h"
+#include "libpq/pqsignal.h"
+#include "mb/pg_wchar.h"
+#include "mb/stringinfo_mb.h"
+#include "miscadmin.h"
+#include "nodes/print.h"
+#include "optimizer/optimizer.h"
+#include "parser/analyze.h"
+#include "parser/parser.h"
+#include "pg_getopt.h"
+#include "pg_trace.h"
+#include "pgstat.h"
+#include "postmaster/autovacuum.h"
+#include "postmaster/interrupt.h"
+#include "postmaster/postmaster.h"
+#include "replication/logicallauncher.h"
+#include "replication/logicalworker.h"
+#include "replication/slot.h"
+#include "replication/walsender.h"
+#include "rewrite/rewriteHandler.h"
+#include "storage/bufmgr.h"
+#include "storage/ipc.h"
+#include "storage/pmsignal.h"
+#include "storage/proc.h"
+#include "storage/procsignal.h"
+#include "storage/sinval.h"
+#include "tcop/fastpath.h"
+#include "tcop/pquery.h"
+#include "tcop/tcopprot.h"
+#include "tcop/utility.h"
+#include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/ps_status.h"
+#include "utils/snapmgr.h"
+#include "utils/timeout.h"
+#include "utils/timestamp.h"
+
+/* ----------------
+ * global variables
+ * ----------------
+ */
+const char *debug_query_string; /* client-supplied query string */
+
+/* Note: whereToSendOutput is initialized for the bootstrap/standalone case */
+CommandDest whereToSendOutput = DestDebug;
+
+/* flag for logging end of session */
+bool Log_disconnections = false;
+
+int log_statement = LOGSTMT_NONE;
+
+/* GUC variable for maximum stack depth (measured in kilobytes) */
+int max_stack_depth = 100;
+
+/* wait N seconds to allow attach from a debugger */
+int PostAuthDelay = 0;
+
+/* Time between checks that the client is still connected. */
+int client_connection_check_interval = 0;
+
+/* ----------------
+ * private typedefs etc
+ * ----------------
+ */
+
+/* type of argument for bind_param_error_callback */
+typedef struct BindParamCbData
+{
+ const char *portalName;
+ int paramno; /* zero-based param number, or -1 initially */
+ const char *paramval; /* textual input string, if available */
+} BindParamCbData;
+
+/* ----------------
+ * private variables
+ * ----------------
+ */
+
+/* max_stack_depth converted to bytes for speed of checking */
+static long max_stack_depth_bytes = 100 * 1024L;
+
+/*
+ * Stack base pointer -- initialized by PostmasterMain and inherited by
+ * subprocesses (but see also InitPostmasterChild).
+ */
+static char *stack_base_ptr = NULL;
+
+/*
+ * On IA64 we also have to remember the register stack base.
+ */
+#if defined(__ia64__) || defined(__ia64)
+static char *register_stack_base_ptr = NULL;
+#endif
+
+/*
+ * Flag to keep track of whether we have started a transaction.
+ * For extended query protocol this has to be remembered across messages.
+ */
+static bool xact_started = false;
+
+/*
+ * Flag to indicate that we are doing the outer loop's read-from-client,
+ * as opposed to any random read from client that might happen within
+ * commands like COPY FROM STDIN.
+ */
+static bool DoingCommandRead = false;
+
+/*
+ * Flags to implement skip-till-Sync-after-error behavior for messages of
+ * the extended query protocol.
+ */
+static bool doing_extended_query_message = false;
+static bool ignore_till_sync = false;
+
+/*
+ * If an unnamed prepared statement exists, it's stored here.
+ * We keep it separate from the hashtable kept by commands/prepare.c
+ * in order to reduce overhead for short-lived queries.
+ */
+static CachedPlanSource *unnamed_stmt_psrc = NULL;
+
+/* assorted command-line switches */
+static const char *userDoption = NULL; /* -D switch */
+static bool EchoQuery = false; /* -E switch */
+static bool UseSemiNewlineNewline = false; /* -j switch */
+
+/* whether or not, and why, we were canceled by conflict with recovery */
+static bool RecoveryConflictPending = false;
+static bool RecoveryConflictRetryable = true;
+static ProcSignalReason RecoveryConflictReason;
+
+/* reused buffer to pass to SendRowDescriptionMessage() */
+static MemoryContext row_description_context = NULL;
+static StringInfoData row_description_buf;
+
+/* ----------------------------------------------------------------
+ * decls for routines only used in this file
+ * ----------------------------------------------------------------
+ */
+static int InteractiveBackend(StringInfo inBuf);
+static int interactive_getc(void);
+static int SocketBackend(StringInfo inBuf);
+static int ReadCommand(StringInfo inBuf);
+static void forbidden_in_wal_sender(char firstchar);
+static bool check_log_statement(List *stmt_list);
+static int errdetail_execute(List *raw_parsetree_list);
+static int errdetail_params(ParamListInfo params);
+static int errdetail_abort(void);
+static int errdetail_recovery_conflict(void);
+static void bind_param_error_callback(void *arg);
+static void start_xact_command(void);
+static void finish_xact_command(void);
+static bool IsTransactionExitStmt(Node *parsetree);
+static bool IsTransactionExitStmtList(List *pstmts);
+static bool IsTransactionStmtList(List *pstmts);
+static void drop_unnamed_stmt(void);
+static void log_disconnections(int code, Datum arg);
+static void enable_statement_timeout(void);
+static void disable_statement_timeout(void);
+
+
+/* ----------------------------------------------------------------
+ * routines to obtain user input
+ * ----------------------------------------------------------------
+ */
+
+/* ----------------
+ * InteractiveBackend() is called for user interactive connections
+ *
+ * the string entered by the user is placed in its parameter inBuf,
+ * and we act like a Q message was received.
+ *
+ * EOF is returned if end-of-file input is seen; time to shut down.
+ * ----------------
+ */
+
+static int
+InteractiveBackend(StringInfo inBuf)
+{
+ int c; /* character read from getc() */
+
+ /*
+ * display a prompt and obtain input from the user
+ */
+ printf("backend> ");
+ fflush(stdout);
+
+ resetStringInfo(inBuf);
+
+ /*
+ * Read characters until EOF or the appropriate delimiter is seen.
+ */
+ while ((c = interactive_getc()) != EOF)
+ {
+ if (c == '\n')
+ {
+ if (UseSemiNewlineNewline)
+ {
+ /*
+ * In -j mode, semicolon followed by two newlines ends the
+ * command; otherwise treat newline as regular character.
+ */
+ if (inBuf->len > 1 &&
+ inBuf->data[inBuf->len - 1] == '\n' &&
+ inBuf->data[inBuf->len - 2] == ';')
+ {
+ /* might as well drop the second newline */
+ break;
+ }
+ }
+ else
+ {
+ /*
+ * In plain mode, newline ends the command unless preceded by
+ * backslash.
+ */
+ if (inBuf->len > 0 &&
+ inBuf->data[inBuf->len - 1] == '\\')
+ {
+ /* discard backslash from inBuf */
+ inBuf->data[--inBuf->len] = '\0';
+ /* discard newline too */
+ continue;
+ }
+ else
+ {
+ /* keep the newline character, but end the command */
+ appendStringInfoChar(inBuf, '\n');
+ break;
+ }
+ }
+ }
+
+ /* Not newline, or newline treated as regular character */
+ appendStringInfoChar(inBuf, (char) c);
+ }
+
+ /* No input before EOF signal means time to quit. */
+ if (c == EOF && inBuf->len == 0)
+ return EOF;
+
+ /*
+ * otherwise we have a user query so process it.
+ */
+
+ /* Add '\0' to make it look the same as message case. */
+ appendStringInfoChar(inBuf, (char) '\0');
+
+ /*
+ * if the query echo flag was given, print the query..
+ */
+ if (EchoQuery)
+ printf("statement: %s\n", inBuf->data);
+ fflush(stdout);
+
+ return 'Q';
+}
+
+/*
+ * interactive_getc -- collect one character from stdin
+ *
+ * Even though we are not reading from a "client" process, we still want to
+ * respond to signals, particularly SIGTERM/SIGQUIT.
+ */
+static int
+interactive_getc(void)
+{
+ int c;
+
+ /*
+ * This will not process catchup interrupts or notifications while
+ * reading. But those can't really be relevant for a standalone backend
+ * anyway. To properly handle SIGTERM there's a hack in die() that
+ * directly processes interrupts at this stage...
+ */
+ CHECK_FOR_INTERRUPTS();
+
+ c = getc(stdin);
+
+ ProcessClientReadInterrupt(false);
+
+ return c;
+}
+
+/* ----------------
+ * SocketBackend() Is called for frontend-backend connections
+ *
+ * Returns the message type code, and loads message body data into inBuf.
+ *
+ * EOF is returned if the connection is lost.
+ * ----------------
+ */
+static int
+SocketBackend(StringInfo inBuf)
+{
+ int qtype;
+ int maxmsglen;
+
+ /*
+ * Get message type code from the frontend.
+ */
+ HOLD_CANCEL_INTERRUPTS();
+ pq_startmsgread();
+ qtype = pq_getbyte();
+
+ if (qtype == EOF) /* frontend disconnected */
+ {
+ if (IsTransactionState())
+ ereport(COMMERROR,
+ (errcode(ERRCODE_CONNECTION_FAILURE),
+ errmsg("unexpected EOF on client connection with an open transaction")));
+ else
+ {
+ /*
+ * Can't send DEBUG log messages to client at this point. Since
+ * we're disconnecting right away, we don't need to restore
+ * whereToSendOutput.
+ */
+ whereToSendOutput = DestNone;
+ ereport(DEBUG1,
+ (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
+ errmsg_internal("unexpected EOF on client connection")));
+ }
+ return qtype;
+ }
+
+ /*
+ * Validate message type code before trying to read body; if we have lost
+ * sync, better to say "command unknown" than to run out of memory because
+ * we used garbage as a length word. We can also select a type-dependent
+ * limit on what a sane length word could be. (The limit could be chosen
+ * more granularly, but it's not clear it's worth fussing over.)
+ *
+ * This also gives us a place to set the doing_extended_query_message flag
+ * as soon as possible.
+ */
+ switch (qtype)
+ {
+ case 'Q': /* simple query */
+ maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
+ doing_extended_query_message = false;
+ break;
+
+ case 'F': /* fastpath function call */
+ maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
+ doing_extended_query_message = false;
+ break;
+
+ case 'X': /* terminate */
+ maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
+ doing_extended_query_message = false;
+ ignore_till_sync = false;
+ break;
+
+ case 'B': /* bind */
+ case 'P': /* parse */
+ maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
+ doing_extended_query_message = true;
+ break;
+
+ case 'C': /* close */
+ case 'D': /* describe */
+ case 'E': /* execute */
+ case 'H': /* flush */
+ maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
+ doing_extended_query_message = true;
+ break;
+
+ case 'S': /* sync */
+ maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
+ /* stop any active skip-till-Sync */
+ ignore_till_sync = false;
+ /* mark not-extended, so that a new error doesn't begin skip */
+ doing_extended_query_message = false;
+ break;
+
+ case 'd': /* copy data */
+ maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
+ doing_extended_query_message = false;
+ break;
+
+ case 'c': /* copy done */
+ case 'f': /* copy fail */
+ maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
+ doing_extended_query_message = false;
+ break;
+
+ default:
+
+ /*
+ * Otherwise we got garbage from the frontend. We treat this as
+ * fatal because we have probably lost message boundary sync, and
+ * there's no good way to recover.
+ */
+ ereport(FATAL,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("invalid frontend message type %d", qtype)));
+ maxmsglen = 0; /* keep compiler quiet */
+ break;
+ }
+
+ /*
+ * In protocol version 3, all frontend messages have a length word next
+ * after the type code; we can read the message contents independently of
+ * the type.
+ */
+ if (pq_getmessage(inBuf, maxmsglen))
+ return EOF; /* suitable message already logged */
+ RESUME_CANCEL_INTERRUPTS();
+
+ return qtype;
+}
+
+/* ----------------
+ * ReadCommand reads a command from either the frontend or
+ * standard input, places it in inBuf, and returns the
+ * message type code (first byte of the message).
+ * EOF is returned if end of file.
+ * ----------------
+ */
+static int
+ReadCommand(StringInfo inBuf)
+{
+ int result;
+
+ if (whereToSendOutput == DestRemote)
+ result = SocketBackend(inBuf);
+ else
+ result = InteractiveBackend(inBuf);
+ return result;
+}
+
+/*
+ * ProcessClientReadInterrupt() - Process interrupts specific to client reads
+ *
+ * This is called just before and after low-level reads.
+ * 'blocked' is true if no data was available to read and we plan to retry,
+ * false if about to read or done reading.
+ *
+ * Must preserve errno!
+ */
+void
+ProcessClientReadInterrupt(bool blocked)
+{
+ int save_errno = errno;
+
+ if (DoingCommandRead)
+ {
+ /* Check for general interrupts that arrived before/while reading */
+ CHECK_FOR_INTERRUPTS();
+
+ /* Process sinval catchup interrupts, if any */
+ if (catchupInterruptPending)
+ ProcessCatchupInterrupt();
+
+ /* Process notify interrupts, if any */
+ if (notifyInterruptPending)
+ ProcessNotifyInterrupt(true);
+ }
+ else if (ProcDiePending)
+ {
+ /*
+ * We're dying. If there is no data available to read, then it's safe
+ * (and sane) to handle that now. If we haven't tried to read yet,
+ * make sure the process latch is set, so that if there is no data
+ * then we'll come back here and die. If we're done reading, also
+ * make sure the process latch is set, as we might've undesirably
+ * cleared it while reading.
+ */
+ if (blocked)
+ CHECK_FOR_INTERRUPTS();
+ else
+ SetLatch(MyLatch);
+ }
+
+ errno = save_errno;
+}
+
+/*
+ * ProcessClientWriteInterrupt() - Process interrupts specific to client writes
+ *
+ * This is called just before and after low-level writes.
+ * 'blocked' is true if no data could be written and we plan to retry,
+ * false if about to write or done writing.
+ *
+ * Must preserve errno!
+ */
+void
+ProcessClientWriteInterrupt(bool blocked)
+{
+ int save_errno = errno;
+
+ if (ProcDiePending)
+ {
+ /*
+ * We're dying. If it's not possible to write, then we should handle
+ * that immediately, else a stuck client could indefinitely delay our
+ * response to the signal. If we haven't tried to write yet, make
+ * sure the process latch is set, so that if the write would block
+ * then we'll come back here and die. If we're done writing, also
+ * make sure the process latch is set, as we might've undesirably
+ * cleared it while writing.
+ */
+ if (blocked)
+ {
+ /*
+ * Don't mess with whereToSendOutput if ProcessInterrupts wouldn't
+ * service ProcDiePending.
+ */
+ if (InterruptHoldoffCount == 0 && CritSectionCount == 0)
+ {
+ /*
+ * We don't want to send the client the error message, as a)
+ * that would possibly block again, and b) it would likely
+ * lead to loss of protocol sync because we may have already
+ * sent a partial protocol message.
+ */
+ if (whereToSendOutput == DestRemote)
+ whereToSendOutput = DestNone;
+
+ CHECK_FOR_INTERRUPTS();
+ }
+ }
+ else
+ SetLatch(MyLatch);
+ }
+
+ errno = save_errno;
+}
+
+/*
+ * Do raw parsing (only).
+ *
+ * A list of parsetrees (RawStmt nodes) is returned, since there might be
+ * multiple commands in the given string.
+ *
+ * NOTE: for interactive queries, it is important to keep this routine
+ * separate from the analysis & rewrite stages. Analysis and rewriting
+ * cannot be done in an aborted transaction, since they require access to
+ * database tables. So, we rely on the raw parser to determine whether
+ * we've seen a COMMIT or ABORT command; when we are in abort state, other
+ * commands are not processed any further than the raw parse stage.
+ */
+List *
+pg_parse_query(const char *query_string)
+{
+ List *raw_parsetree_list;
+
+ TRACE_POSTGRESQL_QUERY_PARSE_START(query_string);
+
+ if (log_parser_stats)
+ ResetUsage();
+
+ raw_parsetree_list = raw_parser(query_string, RAW_PARSE_DEFAULT);
+
+ if (log_parser_stats)
+ ShowUsage("PARSER STATISTICS");
+
+#ifdef COPY_PARSE_PLAN_TREES
+ /* Optional debugging check: pass raw parsetrees through copyObject() */
+ {
+ List *new_list = copyObject(raw_parsetree_list);
+
+ /* This checks both copyObject() and the equal() routines... */
+ if (!equal(new_list, raw_parsetree_list))
+ elog(WARNING, "copyObject() failed to produce an equal raw parse tree");
+ else
+ raw_parsetree_list = new_list;
+ }
+#endif
+
+ /*
+ * Currently, outfuncs/readfuncs support is missing for many raw parse
+ * tree nodes, so we don't try to implement WRITE_READ_PARSE_PLAN_TREES
+ * here.
+ */
+
+ TRACE_POSTGRESQL_QUERY_PARSE_DONE(query_string);
+
+ return raw_parsetree_list;
+}
+
+/*
+ * Given a raw parsetree (gram.y output), and optionally information about
+ * types of parameter symbols ($n), perform parse analysis and rule rewriting.
+ *
+ * A list of Query nodes is returned, since either the analyzer or the
+ * rewriter might expand one query to several.
+ *
+ * NOTE: for reasons mentioned above, this must be separate from raw parsing.
+ */
+List *
+pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree,
+ const char *query_string,
+ const Oid *paramTypes,
+ int numParams,
+ QueryEnvironment *queryEnv)
+{
+ Query *query;
+ List *querytree_list;
+
+ TRACE_POSTGRESQL_QUERY_REWRITE_START(query_string);
+
+ /*
+ * (1) Perform parse analysis.
+ */
+ if (log_parser_stats)
+ ResetUsage();
+
+ query = parse_analyze_fixedparams(parsetree, query_string, paramTypes, numParams,
+ queryEnv);
+
+ if (log_parser_stats)
+ ShowUsage("PARSE ANALYSIS STATISTICS");
+
+ /*
+ * (2) Rewrite the queries, as necessary
+ */
+ querytree_list = pg_rewrite_query(query);
+
+ TRACE_POSTGRESQL_QUERY_REWRITE_DONE(query_string);
+
+ return querytree_list;
+}
+
+/*
+ * Do parse analysis and rewriting. This is the same as
+ * pg_analyze_and_rewrite_fixedparams except that it's okay to deduce
+ * information about $n symbol datatypes from context.
+ */
+List *
+pg_analyze_and_rewrite_varparams(RawStmt *parsetree,
+ const char *query_string,
+ Oid **paramTypes,
+ int *numParams,
+ QueryEnvironment *queryEnv)
+{
+ Query *query;
+ List *querytree_list;
+
+ TRACE_POSTGRESQL_QUERY_REWRITE_START(query_string);
+
+ /*
+ * (1) Perform parse analysis.
+ */
+ if (log_parser_stats)
+ ResetUsage();
+
+ query = parse_analyze_varparams(parsetree, query_string, paramTypes, numParams,
+ queryEnv);
+
+ /*
+ * Check all parameter types got determined.
+ */
+ for (int i = 0; i < *numParams; i++)
+ {
+ Oid ptype = (*paramTypes)[i];
+
+ if (ptype == InvalidOid || ptype == UNKNOWNOID)
+ ereport(ERROR,
+ (errcode(ERRCODE_INDETERMINATE_DATATYPE),
+ errmsg("could not determine data type of parameter $%d",
+ i + 1)));
+ }
+
+ if (log_parser_stats)
+ ShowUsage("PARSE ANALYSIS STATISTICS");
+
+ /*
+ * (2) Rewrite the queries, as necessary
+ */
+ querytree_list = pg_rewrite_query(query);
+
+ TRACE_POSTGRESQL_QUERY_REWRITE_DONE(query_string);
+
+ return querytree_list;
+}
+
+/*
+ * Do parse analysis and rewriting. This is the same as
+ * pg_analyze_and_rewrite_fixedparams except that, instead of a fixed list of
+ * parameter datatypes, a parser callback is supplied that can do
+ * external-parameter resolution and possibly other things.
+ */
+List *
+pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
+ const char *query_string,
+ ParserSetupHook parserSetup,
+ void *parserSetupArg,
+ QueryEnvironment *queryEnv)
+{
+ Query *query;
+ List *querytree_list;
+
+ TRACE_POSTGRESQL_QUERY_REWRITE_START(query_string);
+
+ /*
+ * (1) Perform parse analysis.
+ */
+ if (log_parser_stats)
+ ResetUsage();
+
+ query = parse_analyze_withcb(parsetree, query_string, parserSetup, parserSetupArg,
+ queryEnv);
+
+ if (log_parser_stats)
+ ShowUsage("PARSE ANALYSIS STATISTICS");
+
+ /*
+ * (2) Rewrite the queries, as necessary
+ */
+ querytree_list = pg_rewrite_query(query);
+
+ TRACE_POSTGRESQL_QUERY_REWRITE_DONE(query_string);
+
+ return querytree_list;
+}
+
+/*
+ * Perform rewriting of a query produced by parse analysis.
+ *
+ * Note: query must just have come from the parser, because we do not do
+ * AcquireRewriteLocks() on it.
+ */
+List *
+pg_rewrite_query(Query *query)
+{
+ List *querytree_list;
+
+ if (Debug_print_parse)
+ elog_node_display(LOG, "parse tree", query,
+ Debug_pretty_print);
+
+ if (log_parser_stats)
+ ResetUsage();
+
+ if (query->commandType == CMD_UTILITY)
+ {
+ /* don't rewrite utilities, just dump 'em into result list */
+ querytree_list = list_make1(query);
+ }
+ else
+ {
+ /* rewrite regular queries */
+ querytree_list = QueryRewrite(query);
+ }
+
+ if (log_parser_stats)
+ ShowUsage("REWRITER STATISTICS");
+
+#ifdef COPY_PARSE_PLAN_TREES
+ /* Optional debugging check: pass querytree through copyObject() */
+ {
+ List *new_list;
+
+ new_list = copyObject(querytree_list);
+ /* This checks both copyObject() and the equal() routines... */
+ if (!equal(new_list, querytree_list))
+ elog(WARNING, "copyObject() failed to produce equal parse tree");
+ else
+ querytree_list = new_list;
+ }
+#endif
+
+#ifdef WRITE_READ_PARSE_PLAN_TREES
+ /* Optional debugging check: pass querytree through outfuncs/readfuncs */
+ {
+ List *new_list = NIL;
+ ListCell *lc;
+
+ /*
+ * We currently lack outfuncs/readfuncs support for most utility
+ * statement types, so only attempt to write/read non-utility queries.
+ */
+ foreach(lc, querytree_list)
+ {
+ Query *query = lfirst_node(Query, lc);
+
+ if (query->commandType != CMD_UTILITY)
+ {
+ char *str = nodeToString(query);
+ Query *new_query = stringToNodeWithLocations(str);
+
+ /*
+ * queryId is not saved in stored rules, but we must preserve
+ * it here to avoid breaking pg_stat_statements.
+ */
+ new_query->queryId = query->queryId;
+
+ new_list = lappend(new_list, new_query);
+ pfree(str);
+ }
+ else
+ new_list = lappend(new_list, query);
+ }
+
+ /* This checks both outfuncs/readfuncs and the equal() routines... */
+ if (!equal(new_list, querytree_list))
+ elog(WARNING, "outfuncs/readfuncs failed to produce equal parse tree");
+ else
+ querytree_list = new_list;
+ }
+#endif
+
+ if (Debug_print_rewritten)
+ elog_node_display(LOG, "rewritten parse tree", querytree_list,
+ Debug_pretty_print);
+
+ return querytree_list;
+}
+
+
+/*
+ * Generate a plan for a single already-rewritten query.
+ * This is a thin wrapper around planner() and takes the same parameters.
+ */
+PlannedStmt *
+pg_plan_query(Query *querytree, const char *query_string, int cursorOptions,
+ ParamListInfo boundParams)
+{
+ PlannedStmt *plan;
+
+ /* Utility commands have no plans. */
+ if (querytree->commandType == CMD_UTILITY)
+ return NULL;
+
+ /* Planner must have a snapshot in case it calls user-defined functions. */
+ Assert(ActiveSnapshotSet());
+
+ TRACE_POSTGRESQL_QUERY_PLAN_START();
+
+ if (log_planner_stats)
+ ResetUsage();
+
+ /* call the optimizer */
+ plan = planner(querytree, query_string, cursorOptions, boundParams);
+
+ if (log_planner_stats)
+ ShowUsage("PLANNER STATISTICS");
+
+#ifdef COPY_PARSE_PLAN_TREES
+ /* Optional debugging check: pass plan tree through copyObject() */
+ {
+ PlannedStmt *new_plan = copyObject(plan);
+
+ /*
+ * equal() currently does not have routines to compare Plan nodes, so
+ * don't try to test equality here. Perhaps fix someday?
+ */
+#ifdef NOT_USED
+ /* This checks both copyObject() and the equal() routines... */
+ if (!equal(new_plan, plan))
+ elog(WARNING, "copyObject() failed to produce an equal plan tree");
+ else
+#endif
+ plan = new_plan;
+ }
+#endif
+
+#ifdef WRITE_READ_PARSE_PLAN_TREES
+ /* Optional debugging check: pass plan tree through outfuncs/readfuncs */
+ {
+ char *str;
+ PlannedStmt *new_plan;
+
+ str = nodeToString(plan);
+ new_plan = stringToNodeWithLocations(str);
+ pfree(str);
+
+ /*
+ * equal() currently does not have routines to compare Plan nodes, so
+ * don't try to test equality here. Perhaps fix someday?
+ */
+#ifdef NOT_USED
+ /* This checks both outfuncs/readfuncs and the equal() routines... */
+ if (!equal(new_plan, plan))
+ elog(WARNING, "outfuncs/readfuncs failed to produce an equal plan tree");
+ else
+#endif
+ plan = new_plan;
+ }
+#endif
+
+ /*
+ * Print plan if debugging.
+ */
+ if (Debug_print_plan)
+ elog_node_display(LOG, "plan", plan, Debug_pretty_print);
+
+ TRACE_POSTGRESQL_QUERY_PLAN_DONE();
+
+ return plan;
+}
+
+/*
+ * Generate plans for a list of already-rewritten queries.
+ *
+ * For normal optimizable statements, invoke the planner. For utility
+ * statements, just make a wrapper PlannedStmt node.
+ *
+ * The result is a list of PlannedStmt nodes.
+ */
+List *
+pg_plan_queries(List *querytrees, const char *query_string, int cursorOptions,
+ ParamListInfo boundParams)
+{
+ List *stmt_list = NIL;
+ ListCell *query_list;
+
+ foreach(query_list, querytrees)
+ {
+ Query *query = lfirst_node(Query, query_list);
+ PlannedStmt *stmt;
+
+ if (query->commandType == CMD_UTILITY)
+ {
+ /* Utility commands require no planning. */
+ stmt = makeNode(PlannedStmt);
+ stmt->commandType = CMD_UTILITY;
+ stmt->canSetTag = query->canSetTag;
+ stmt->utilityStmt = query->utilityStmt;
+ stmt->stmt_location = query->stmt_location;
+ stmt->stmt_len = query->stmt_len;
+ stmt->queryId = query->queryId;
+ }
+ else
+ {
+ stmt = pg_plan_query(query, query_string, cursorOptions,
+ boundParams);
+ }
+
+ stmt_list = lappend(stmt_list, stmt);
+ }
+
+ return stmt_list;
+}
+
+
+/*
+ * exec_simple_query
+ *
+ * Execute a "simple Query" protocol message.
+ */
+static void
+exec_simple_query(const char *query_string)
+{
+ CommandDest dest = whereToSendOutput;
+ MemoryContext oldcontext;
+ List *parsetree_list;
+ ListCell *parsetree_item;
+ bool save_log_statement_stats = log_statement_stats;
+ bool was_logged = false;
+ bool use_implicit_block;
+ char msec_str[32];
+
+ /*
+ * Report query to various monitoring facilities.
+ */
+ debug_query_string = query_string;
+
+ pgstat_report_activity(STATE_RUNNING, query_string);
+
+ TRACE_POSTGRESQL_QUERY_START(query_string);
+
+ /*
+ * We use save_log_statement_stats so ShowUsage doesn't report incorrect
+ * results because ResetUsage wasn't called.
+ */
+ if (save_log_statement_stats)
+ ResetUsage();
+
+ /*
+ * Start up a transaction command. All queries generated by the
+ * query_string will be in this same command block, *unless* we find a
+ * BEGIN/COMMIT/ABORT statement; we have to force a new xact command after
+ * one of those, else bad things will happen in xact.c. (Note that this
+ * will normally change current memory context.)
+ */
+ start_xact_command();
+
+ /*
+ * Zap any pre-existing unnamed statement. (While not strictly necessary,
+ * it seems best to define simple-Query mode as if it used the unnamed
+ * statement and portal; this ensures we recover any storage used by prior
+ * unnamed operations.)
+ */
+ drop_unnamed_stmt();
+
+ /*
+ * Switch to appropriate context for constructing parsetrees.
+ */
+ oldcontext = MemoryContextSwitchTo(MessageContext);
+
+ /*
+ * Do basic parsing of the query or queries (this should be safe even if
+ * we are in aborted transaction state!)
+ */
+ parsetree_list = pg_parse_query(query_string);
+
+ /* Log immediately if dictated by log_statement */
+ if (check_log_statement(parsetree_list))
+ {
+ ereport(LOG,
+ (errmsg("statement: %s", query_string),
+ errhidestmt(true),
+ errdetail_execute(parsetree_list)));
+ was_logged = true;
+ }
+
+ /*
+ * Switch back to transaction context to enter the loop.
+ */
+ MemoryContextSwitchTo(oldcontext);
+
+ /*
+ * For historical reasons, if multiple SQL statements are given in a
+ * single "simple Query" message, we execute them as a single transaction,
+ * unless explicit transaction control commands are included to make
+ * portions of the list be separate transactions. To represent this
+ * behavior properly in the transaction machinery, we use an "implicit"
+ * transaction block.
+ */
+ use_implicit_block = (list_length(parsetree_list) > 1);
+
+ /*
+ * Run through the raw parsetree(s) and process each one.
+ */
+ foreach(parsetree_item, parsetree_list)
+ {
+ RawStmt *parsetree = lfirst_node(RawStmt, parsetree_item);
+ bool snapshot_set = false;
+ CommandTag commandTag;
+ QueryCompletion qc;
+ MemoryContext per_parsetree_context = NULL;
+ List *querytree_list,
+ *plantree_list;
+ Portal portal;
+ DestReceiver *receiver;
+ int16 format;
+
+ pgstat_report_query_id(0, true);
+
+ /*
+ * Get the command name for use in status display (it also becomes the
+ * default completion tag, down inside PortalRun). Set ps_status and
+ * do any special start-of-SQL-command processing needed by the
+ * destination.
+ */
+ commandTag = CreateCommandTag(parsetree->stmt);
+
+ set_ps_display(GetCommandTagName(commandTag));
+
+ BeginCommand(commandTag, dest);
+
+ /*
+ * If we are in an aborted transaction, reject all commands except
+ * COMMIT/ABORT. It is important that this test occur before we try
+ * to do parse analysis, rewrite, or planning, since all those phases
+ * try to do database accesses, which may fail in abort state. (It
+ * might be safe to allow some additional utility commands in this
+ * state, but not many...)
+ */
+ if (IsAbortedTransactionBlockState() &&
+ !IsTransactionExitStmt(parsetree->stmt))
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block"),
+ errdetail_abort()));
+
+ /* Make sure we are in a transaction command */
+ start_xact_command();
+
+ /*
+ * If using an implicit transaction block, and we're not already in a
+ * transaction block, start an implicit block to force this statement
+ * to be grouped together with any following ones. (We must do this
+ * each time through the loop; otherwise, a COMMIT/ROLLBACK in the
+ * list would cause later statements to not be grouped.)
+ */
+ if (use_implicit_block)
+ BeginImplicitTransactionBlock();
+
+ /* If we got a cancel signal in parsing or prior command, quit */
+ CHECK_FOR_INTERRUPTS();
+
+ /*
+ * Set up a snapshot if parse analysis/planning will need one.
+ */
+ if (analyze_requires_snapshot(parsetree))
+ {
+ PushActiveSnapshot(GetTransactionSnapshot());
+ snapshot_set = true;
+ }
+
+ /*
+ * OK to analyze, rewrite, and plan this query.
+ *
+ * Switch to appropriate context for constructing query and plan trees
+ * (these can't be in the transaction context, as that will get reset
+ * when the command is COMMIT/ROLLBACK). If we have multiple
+ * parsetrees, we use a separate context for each one, so that we can
+ * free that memory before moving on to the next one. But for the
+ * last (or only) parsetree, just use MessageContext, which will be
+ * reset shortly after completion anyway. In event of an error, the
+ * per_parsetree_context will be deleted when MessageContext is reset.
+ */
+ if (lnext(parsetree_list, parsetree_item) != NULL)
+ {
+ per_parsetree_context =
+ AllocSetContextCreate(MessageContext,
+ "per-parsetree message context",
+ ALLOCSET_DEFAULT_SIZES);
+ oldcontext = MemoryContextSwitchTo(per_parsetree_context);
+ }
+ else
+ oldcontext = MemoryContextSwitchTo(MessageContext);
+
+ querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree, query_string,
+ NULL, 0, NULL);
+
+ plantree_list = pg_plan_queries(querytree_list, query_string,
+ CURSOR_OPT_PARALLEL_OK, NULL);
+
+ /*
+ * Done with the snapshot used for parsing/planning.
+ *
+ * While it looks promising to reuse the same snapshot for query
+ * execution (at least for simple protocol), unfortunately it causes
+ * execution to use a snapshot that has been acquired before locking
+ * any of the tables mentioned in the query. This creates user-
+ * visible anomalies, so refrain. Refer to
+ * https://postgr.es/m/flat/5075D8DF.6050500@fuzzy.cz for details.
+ */
+ if (snapshot_set)
+ PopActiveSnapshot();
+
+ /* If we got a cancel signal in analysis or planning, quit */
+ CHECK_FOR_INTERRUPTS();
+
+ /*
+ * Create unnamed portal to run the query or queries in. If there
+ * already is one, silently drop it.
+ */
+ portal = CreatePortal("", true, true);
+ /* Don't display the portal in pg_cursors */
+ portal->visible = false;
+
+ /*
+ * We don't have to copy anything into the portal, because everything
+ * we are passing here is in MessageContext or the
+ * per_parsetree_context, and so will outlive the portal anyway.
+ */
+ PortalDefineQuery(portal,
+ NULL,
+ query_string,
+ commandTag,
+ plantree_list,
+ NULL);
+
+ /*
+ * Start the portal. No parameters here.
+ */
+ PortalStart(portal, NULL, 0, InvalidSnapshot);
+
+ /*
+ * Select the appropriate output format: text unless we are doing a
+ * FETCH from a binary cursor. (Pretty grotty to have to do this here
+ * --- but it avoids grottiness in other places. Ah, the joys of
+ * backward compatibility...)
+ */
+ format = 0; /* TEXT is default */
+ if (IsA(parsetree->stmt, FetchStmt))
+ {
+ FetchStmt *stmt = (FetchStmt *) parsetree->stmt;
+
+ if (!stmt->ismove)
+ {
+ Portal fportal = GetPortalByName(stmt->portalname);
+
+ if (PortalIsValid(fportal) &&
+ (fportal->cursorOptions & CURSOR_OPT_BINARY))
+ format = 1; /* BINARY */
+ }
+ }
+ PortalSetResultFormat(portal, 1, &format);
+
+ /*
+ * Now we can create the destination receiver object.
+ */
+ receiver = CreateDestReceiver(dest);
+ if (dest == DestRemote)
+ SetRemoteDestReceiverParams(receiver, portal);
+
+ /*
+ * Switch back to transaction context for execution.
+ */
+ MemoryContextSwitchTo(oldcontext);
+
+ /*
+ * Run the portal to completion, and then drop it (and the receiver).
+ */
+ (void) PortalRun(portal,
+ FETCH_ALL,
+ true, /* always top level */
+ true,
+ receiver,
+ receiver,
+ &qc);
+
+ receiver->rDestroy(receiver);
+
+ PortalDrop(portal, false);
+
+ if (lnext(parsetree_list, parsetree_item) == NULL)
+ {
+ /*
+ * If this is the last parsetree of the query string, close down
+ * transaction statement before reporting command-complete. This
+ * is so that any end-of-transaction errors are reported before
+ * the command-complete message is issued, to avoid confusing
+ * clients who will expect either a command-complete message or an
+ * error, not one and then the other. Also, if we're using an
+ * implicit transaction block, we must close that out first.
+ */
+ if (use_implicit_block)
+ EndImplicitTransactionBlock();
+ finish_xact_command();
+ }
+ else if (IsA(parsetree->stmt, TransactionStmt))
+ {
+ /*
+ * If this was a transaction control statement, commit it. We will
+ * start a new xact command for the next command.
+ */
+ finish_xact_command();
+ }
+ else
+ {
+ /*
+ * We had better not see XACT_FLAGS_NEEDIMMEDIATECOMMIT set if
+ * we're not calling finish_xact_command(). (The implicit
+ * transaction block should have prevented it from getting set.)
+ */
+ Assert(!(MyXactFlags & XACT_FLAGS_NEEDIMMEDIATECOMMIT));
+
+ /*
+ * We need a CommandCounterIncrement after every query, except
+ * those that start or end a transaction block.
+ */
+ CommandCounterIncrement();
+
+ /*
+ * Disable statement timeout between queries of a multi-query
+ * string, so that the timeout applies separately to each query.
+ * (Our next loop iteration will start a fresh timeout.)
+ */
+ disable_statement_timeout();
+ }
+
+ /*
+ * Tell client that we're done with this query. Note we emit exactly
+ * one EndCommand report for each raw parsetree, thus one for each SQL
+ * command the client sent, regardless of rewriting. (But a command
+ * aborted by error will not send an EndCommand report at all.)
+ */
+ EndCommand(&qc, dest, false);
+
+ /* Now we may drop the per-parsetree context, if one was created. */
+ if (per_parsetree_context)
+ MemoryContextDelete(per_parsetree_context);
+ } /* end loop over parsetrees */
+
+ /*
+ * Close down transaction statement, if one is open. (This will only do
+ * something if the parsetree list was empty; otherwise the last loop
+ * iteration already did it.)
+ */
+ finish_xact_command();
+
+ /*
+ * If there were no parsetrees, return EmptyQueryResponse message.
+ */
+ if (!parsetree_list)
+ NullCommand(dest);
+
+ /*
+ * Emit duration logging if appropriate.
+ */
+ switch (check_log_duration(msec_str, was_logged))
+ {
+ case 1:
+ ereport(LOG,
+ (errmsg("duration: %s ms", msec_str),
+ errhidestmt(true)));
+ break;
+ case 2:
+ ereport(LOG,
+ (errmsg("duration: %s ms statement: %s",
+ msec_str, query_string),
+ errhidestmt(true),
+ errdetail_execute(parsetree_list)));
+ break;
+ }
+
+ if (save_log_statement_stats)
+ ShowUsage("QUERY STATISTICS");
+
+ TRACE_POSTGRESQL_QUERY_DONE(query_string);
+
+ debug_query_string = NULL;
+}
+
+/*
+ * exec_parse_message
+ *
+ * Execute a "Parse" protocol message.
+ */
+static void
+exec_parse_message(const char *query_string, /* string to execute */
+ const char *stmt_name, /* name for prepared stmt */
+ Oid *paramTypes, /* parameter types */
+ int numParams) /* number of parameters */
+{
+ MemoryContext unnamed_stmt_context = NULL;
+ MemoryContext oldcontext;
+ List *parsetree_list;
+ RawStmt *raw_parse_tree;
+ List *querytree_list;
+ CachedPlanSource *psrc;
+ bool is_named;
+ bool save_log_statement_stats = log_statement_stats;
+ char msec_str[32];
+
+ /*
+ * Report query to various monitoring facilities.
+ */
+ debug_query_string = query_string;
+
+ pgstat_report_activity(STATE_RUNNING, query_string);
+
+ set_ps_display("PARSE");
+
+ if (save_log_statement_stats)
+ ResetUsage();
+
+ ereport(DEBUG2,
+ (errmsg_internal("parse %s: %s",
+ *stmt_name ? stmt_name : "<unnamed>",
+ query_string)));
+
+ /*
+ * Start up a transaction command so we can run parse analysis etc. (Note
+ * that this will normally change current memory context.) Nothing happens
+ * if we are already in one. This also arms the statement timeout if
+ * necessary.
+ */
+ start_xact_command();
+
+ /*
+ * Switch to appropriate context for constructing parsetrees.
+ *
+ * We have two strategies depending on whether the prepared statement is
+ * named or not. For a named prepared statement, we do parsing in
+ * MessageContext and copy the finished trees into the prepared
+ * statement's plancache entry; then the reset of MessageContext releases
+ * temporary space used by parsing and rewriting. For an unnamed prepared
+ * statement, we assume the statement isn't going to hang around long, so
+ * getting rid of temp space quickly is probably not worth the costs of
+ * copying parse trees. So in this case, we create the plancache entry's
+ * query_context here, and do all the parsing work therein.
+ */
+ is_named = (stmt_name[0] != '\0');
+ if (is_named)
+ {
+ /* Named prepared statement --- parse in MessageContext */
+ oldcontext = MemoryContextSwitchTo(MessageContext);
+ }
+ else
+ {
+ /* Unnamed prepared statement --- release any prior unnamed stmt */
+ drop_unnamed_stmt();
+ /* Create context for parsing */
+ unnamed_stmt_context =
+ AllocSetContextCreate(MessageContext,
+ "unnamed prepared statement",
+ ALLOCSET_DEFAULT_SIZES);
+ oldcontext = MemoryContextSwitchTo(unnamed_stmt_context);
+ }
+
+ /*
+ * Do basic parsing of the query or queries (this should be safe even if
+ * we are in aborted transaction state!)
+ */
+ parsetree_list = pg_parse_query(query_string);
+
+ /*
+ * We only allow a single user statement in a prepared statement. This is
+ * mainly to keep the protocol simple --- otherwise we'd need to worry
+ * about multiple result tupdescs and things like that.
+ */
+ if (list_length(parsetree_list) > 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("cannot insert multiple commands into a prepared statement")));
+
+ if (parsetree_list != NIL)
+ {
+ bool snapshot_set = false;
+
+ raw_parse_tree = linitial_node(RawStmt, parsetree_list);
+
+ /*
+ * If we are in an aborted transaction, reject all commands except
+ * COMMIT/ROLLBACK. It is important that this test occur before we
+ * try to do parse analysis, rewrite, or planning, since all those
+ * phases try to do database accesses, which may fail in abort state.
+ * (It might be safe to allow some additional utility commands in this
+ * state, but not many...)
+ */
+ if (IsAbortedTransactionBlockState() &&
+ !IsTransactionExitStmt(raw_parse_tree->stmt))
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block"),
+ errdetail_abort()));
+
+ /*
+ * Create the CachedPlanSource before we do parse analysis, since it
+ * needs to see the unmodified raw parse tree.
+ */
+ psrc = CreateCachedPlan(raw_parse_tree, query_string,
+ CreateCommandTag(raw_parse_tree->stmt));
+
+ /*
+ * Set up a snapshot if parse analysis will need one.
+ */
+ if (analyze_requires_snapshot(raw_parse_tree))
+ {
+ PushActiveSnapshot(GetTransactionSnapshot());
+ snapshot_set = true;
+ }
+
+ /*
+ * Analyze and rewrite the query. Note that the originally specified
+ * parameter set is not required to be complete, so we have to use
+ * pg_analyze_and_rewrite_varparams().
+ */
+ querytree_list = pg_analyze_and_rewrite_varparams(raw_parse_tree,
+ query_string,
+ &paramTypes,
+ &numParams,
+ NULL);
+
+ /* Done with the snapshot used for parsing */
+ if (snapshot_set)
+ PopActiveSnapshot();
+ }
+ else
+ {
+ /* Empty input string. This is legal. */
+ raw_parse_tree = NULL;
+ psrc = CreateCachedPlan(raw_parse_tree, query_string,
+ CMDTAG_UNKNOWN);
+ querytree_list = NIL;
+ }
+
+ /*
+ * CachedPlanSource must be a direct child of MessageContext before we
+ * reparent unnamed_stmt_context under it, else we have a disconnected
+ * circular subgraph. Klugy, but less so than flipping contexts even more
+ * above.
+ */
+ if (unnamed_stmt_context)
+ MemoryContextSetParent(psrc->context, MessageContext);
+
+ /* Finish filling in the CachedPlanSource */
+ CompleteCachedPlan(psrc,
+ querytree_list,
+ unnamed_stmt_context,
+ paramTypes,
+ numParams,
+ NULL,
+ NULL,
+ CURSOR_OPT_PARALLEL_OK, /* allow parallel mode */
+ true); /* fixed result */
+
+ /* If we got a cancel signal during analysis, quit */
+ CHECK_FOR_INTERRUPTS();
+
+ if (is_named)
+ {
+ /*
+ * Store the query as a prepared statement.
+ */
+ StorePreparedStatement(stmt_name, psrc, false);
+ }
+ else
+ {
+ /*
+ * We just save the CachedPlanSource into unnamed_stmt_psrc.
+ */
+ SaveCachedPlan(psrc);
+ unnamed_stmt_psrc = psrc;
+ }
+
+ MemoryContextSwitchTo(oldcontext);
+
+ /*
+ * We do NOT close the open transaction command here; that only happens
+ * when the client sends Sync. Instead, do CommandCounterIncrement just
+ * in case something happened during parse/plan.
+ */
+ CommandCounterIncrement();
+
+ /*
+ * Send ParseComplete.
+ */
+ if (whereToSendOutput == DestRemote)
+ pq_putemptymessage('1');
+
+ /*
+ * Emit duration logging if appropriate.
+ */
+ switch (check_log_duration(msec_str, false))
+ {
+ case 1:
+ ereport(LOG,
+ (errmsg("duration: %s ms", msec_str),
+ errhidestmt(true)));
+ break;
+ case 2:
+ ereport(LOG,
+ (errmsg("duration: %s ms parse %s: %s",
+ msec_str,
+ *stmt_name ? stmt_name : "<unnamed>",
+ query_string),
+ errhidestmt(true)));
+ break;
+ }
+
+ if (save_log_statement_stats)
+ ShowUsage("PARSE MESSAGE STATISTICS");
+
+ debug_query_string = NULL;
+}
+
+/*
+ * exec_bind_message
+ *
+ * Process a "Bind" message to create a portal from a prepared statement
+ */
+static void
+exec_bind_message(StringInfo input_message)
+{
+ const char *portal_name;
+ const char *stmt_name;
+ int numPFormats;
+ int16 *pformats = NULL;
+ int numParams;
+ int numRFormats;
+ int16 *rformats = NULL;
+ CachedPlanSource *psrc;
+ CachedPlan *cplan;
+ Portal portal;
+ char *query_string;
+ char *saved_stmt_name;
+ ParamListInfo params;
+ MemoryContext oldContext;
+ bool save_log_statement_stats = log_statement_stats;
+ bool snapshot_set = false;
+ char msec_str[32];
+ ParamsErrorCbData params_data;
+ ErrorContextCallback params_errcxt;
+
+ /* Get the fixed part of the message */
+ portal_name = pq_getmsgstring(input_message);
+ stmt_name = pq_getmsgstring(input_message);
+
+ ereport(DEBUG2,
+ (errmsg_internal("bind %s to %s",
+ *portal_name ? portal_name : "<unnamed>",
+ *stmt_name ? stmt_name : "<unnamed>")));
+
+ /* Find prepared statement */
+ if (stmt_name[0] != '\0')
+ {
+ PreparedStatement *pstmt;
+
+ pstmt = FetchPreparedStatement(stmt_name, true);
+ psrc = pstmt->plansource;
+ }
+ else
+ {
+ /* special-case the unnamed statement */
+ psrc = unnamed_stmt_psrc;
+ if (!psrc)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
+ errmsg("unnamed prepared statement does not exist")));
+ }
+
+ /*
+ * Report query to various monitoring facilities.
+ */
+ debug_query_string = psrc->query_string;
+
+ pgstat_report_activity(STATE_RUNNING, psrc->query_string);
+
+ set_ps_display("BIND");
+
+ if (save_log_statement_stats)
+ ResetUsage();
+
+ /*
+ * Start up a transaction command so we can call functions etc. (Note that
+ * this will normally change current memory context.) Nothing happens if
+ * we are already in one. This also arms the statement timeout if
+ * necessary.
+ */
+ start_xact_command();
+
+ /* Switch back to message context */
+ MemoryContextSwitchTo(MessageContext);
+
+ /* Get the parameter format codes */
+ numPFormats = pq_getmsgint(input_message, 2);
+ if (numPFormats > 0)
+ {
+ pformats = (int16 *) palloc(numPFormats * sizeof(int16));
+ for (int i = 0; i < numPFormats; i++)
+ pformats[i] = pq_getmsgint(input_message, 2);
+ }
+
+ /* Get the parameter value count */
+ numParams = pq_getmsgint(input_message, 2);
+
+ if (numPFormats > 1 && numPFormats != numParams)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("bind message has %d parameter formats but %d parameters",
+ numPFormats, numParams)));
+
+ if (numParams != psrc->num_params)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("bind message supplies %d parameters, but prepared statement \"%s\" requires %d",
+ numParams, stmt_name, psrc->num_params)));
+
+ /*
+ * If we are in aborted transaction state, the only portals we can
+ * actually run are those containing COMMIT or ROLLBACK commands. We
+ * disallow binding anything else to avoid problems with infrastructure
+ * that expects to run inside a valid transaction. We also disallow
+ * binding any parameters, since we can't risk calling user-defined I/O
+ * functions.
+ */
+ if (IsAbortedTransactionBlockState() &&
+ (!(psrc->raw_parse_tree &&
+ IsTransactionExitStmt(psrc->raw_parse_tree->stmt)) ||
+ numParams != 0))
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block"),
+ errdetail_abort()));
+
+ /*
+ * Create the portal. Allow silent replacement of an existing portal only
+ * if the unnamed portal is specified.
+ */
+ if (portal_name[0] == '\0')
+ portal = CreatePortal(portal_name, true, true);
+ else
+ portal = CreatePortal(portal_name, false, false);
+
+ /*
+ * Prepare to copy stuff into the portal's memory context. We do all this
+ * copying first, because it could possibly fail (out-of-memory) and we
+ * don't want a failure to occur between GetCachedPlan and
+ * PortalDefineQuery; that would result in leaking our plancache refcount.
+ */
+ oldContext = MemoryContextSwitchTo(portal->portalContext);
+
+ /* Copy the plan's query string into the portal */
+ query_string = pstrdup(psrc->query_string);
+
+ /* Likewise make a copy of the statement name, unless it's unnamed */
+ if (stmt_name[0])
+ saved_stmt_name = pstrdup(stmt_name);
+ else
+ saved_stmt_name = NULL;
+
+ /*
+ * Set a snapshot if we have parameters to fetch (since the input
+ * functions might need it) or the query isn't a utility command (and
+ * hence could require redoing parse analysis and planning). We keep the
+ * snapshot active till we're done, so that plancache.c doesn't have to
+ * take new ones.
+ */
+ if (numParams > 0 ||
+ (psrc->raw_parse_tree &&
+ analyze_requires_snapshot(psrc->raw_parse_tree)))
+ {
+ PushActiveSnapshot(GetTransactionSnapshot());
+ snapshot_set = true;
+ }
+
+ /*
+ * Fetch parameters, if any, and store in the portal's memory context.
+ */
+ if (numParams > 0)
+ {
+ char **knownTextValues = NULL; /* allocate on first use */
+ BindParamCbData one_param_data;
+
+ /*
+ * Set up an error callback so that if there's an error in this phase,
+ * we can report the specific parameter causing the problem.
+ */
+ one_param_data.portalName = portal->name;
+ one_param_data.paramno = -1;
+ one_param_data.paramval = NULL;
+ params_errcxt.previous = error_context_stack;
+ params_errcxt.callback = bind_param_error_callback;
+ params_errcxt.arg = (void *) &one_param_data;
+ error_context_stack = &params_errcxt;
+
+ params = makeParamList(numParams);
+
+ for (int paramno = 0; paramno < numParams; paramno++)
+ {
+ Oid ptype = psrc->param_types[paramno];
+ int32 plength;
+ Datum pval;
+ bool isNull;
+ StringInfoData pbuf;
+ char csave;
+ int16 pformat;
+
+ one_param_data.paramno = paramno;
+ one_param_data.paramval = NULL;
+
+ plength = pq_getmsgint(input_message, 4);
+ isNull = (plength == -1);
+
+ if (!isNull)
+ {
+ const char *pvalue = pq_getmsgbytes(input_message, plength);
+
+ /*
+ * Rather than copying data around, we just set up a phony
+ * StringInfo pointing to the correct portion of the message
+ * buffer. We assume we can scribble on the message buffer so
+ * as to maintain the convention that StringInfos have a
+ * trailing null. This is grotty but is a big win when
+ * dealing with very large parameter strings.
+ */
+ pbuf.data = unconstify(char *, pvalue);
+ pbuf.maxlen = plength + 1;
+ pbuf.len = plength;
+ pbuf.cursor = 0;
+
+ csave = pbuf.data[plength];
+ pbuf.data[plength] = '\0';
+ }
+ else
+ {
+ pbuf.data = NULL; /* keep compiler quiet */
+ csave = 0;
+ }
+
+ if (numPFormats > 1)
+ pformat = pformats[paramno];
+ else if (numPFormats > 0)
+ pformat = pformats[0];
+ else
+ pformat = 0; /* default = text */
+
+ if (pformat == 0) /* text mode */
+ {
+ Oid typinput;
+ Oid typioparam;
+ char *pstring;
+
+ getTypeInputInfo(ptype, &typinput, &typioparam);
+
+ /*
+ * We have to do encoding conversion before calling the
+ * typinput routine.
+ */
+ if (isNull)
+ pstring = NULL;
+ else
+ pstring = pg_client_to_server(pbuf.data, plength);
+
+ /* Now we can log the input string in case of error */
+ one_param_data.paramval = pstring;
+
+ pval = OidInputFunctionCall(typinput, pstring, typioparam, -1);
+
+ one_param_data.paramval = NULL;
+
+ /*
+ * If we might need to log parameters later, save a copy of
+ * the converted string in MessageContext; then free the
+ * result of encoding conversion, if any was done.
+ */
+ if (pstring)
+ {
+ if (log_parameter_max_length_on_error != 0)
+ {
+ MemoryContext oldcxt;
+
+ oldcxt = MemoryContextSwitchTo(MessageContext);
+
+ if (knownTextValues == NULL)
+ knownTextValues =
+ palloc0(numParams * sizeof(char *));
+
+ if (log_parameter_max_length_on_error < 0)
+ knownTextValues[paramno] = pstrdup(pstring);
+ else
+ {
+ /*
+ * We can trim the saved string, knowing that we
+ * won't print all of it. But we must copy at
+ * least two more full characters than
+ * BuildParamLogString wants to use; otherwise it
+ * might fail to include the trailing ellipsis.
+ */
+ knownTextValues[paramno] =
+ pnstrdup(pstring,
+ log_parameter_max_length_on_error
+ + 2 * MAX_MULTIBYTE_CHAR_LEN);
+ }
+
+ MemoryContextSwitchTo(oldcxt);
+ }
+ if (pstring != pbuf.data)
+ pfree(pstring);
+ }
+ }
+ else if (pformat == 1) /* binary mode */
+ {
+ Oid typreceive;
+ Oid typioparam;
+ StringInfo bufptr;
+
+ /*
+ * Call the parameter type's binary input converter
+ */
+ getTypeBinaryInputInfo(ptype, &typreceive, &typioparam);
+
+ if (isNull)
+ bufptr = NULL;
+ else
+ bufptr = &pbuf;
+
+ pval = OidReceiveFunctionCall(typreceive, bufptr, typioparam, -1);
+
+ /* Trouble if it didn't eat the whole buffer */
+ if (!isNull && pbuf.cursor != pbuf.len)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
+ errmsg("incorrect binary data format in bind parameter %d",
+ paramno + 1)));
+ }
+ else
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("unsupported format code: %d",
+ pformat)));
+ pval = 0; /* keep compiler quiet */
+ }
+
+ /* Restore message buffer contents */
+ if (!isNull)
+ pbuf.data[plength] = csave;
+
+ params->params[paramno].value = pval;
+ params->params[paramno].isnull = isNull;
+
+ /*
+ * We mark the params as CONST. This ensures that any custom plan
+ * makes full use of the parameter values.
+ */
+ params->params[paramno].pflags = PARAM_FLAG_CONST;
+ params->params[paramno].ptype = ptype;
+ }
+
+ /* Pop the per-parameter error callback */
+ error_context_stack = error_context_stack->previous;
+
+ /*
+ * Once all parameters have been received, prepare for printing them
+ * in future errors, if configured to do so. (This is saved in the
+ * portal, so that they'll appear when the query is executed later.)
+ */
+ if (log_parameter_max_length_on_error != 0)
+ params->paramValuesStr =
+ BuildParamLogString(params,
+ knownTextValues,
+ log_parameter_max_length_on_error);
+ }
+ else
+ params = NULL;
+
+ /* Done storing stuff in portal's context */
+ MemoryContextSwitchTo(oldContext);
+
+ /*
+ * Set up another error callback so that all the parameters are logged if
+ * we get an error during the rest of the BIND processing.
+ */
+ params_data.portalName = portal->name;
+ params_data.params = params;
+ params_errcxt.previous = error_context_stack;
+ params_errcxt.callback = ParamsErrorCallback;
+ params_errcxt.arg = (void *) &params_data;
+ error_context_stack = &params_errcxt;
+
+ /* Get the result format codes */
+ numRFormats = pq_getmsgint(input_message, 2);
+ if (numRFormats > 0)
+ {
+ rformats = (int16 *) palloc(numRFormats * sizeof(int16));
+ for (int i = 0; i < numRFormats; i++)
+ rformats[i] = pq_getmsgint(input_message, 2);
+ }
+
+ pq_getmsgend(input_message);
+
+ /*
+ * Obtain a plan from the CachedPlanSource. Any cruft from (re)planning
+ * will be generated in MessageContext. The plan refcount will be
+ * assigned to the Portal, so it will be released at portal destruction.
+ */
+ cplan = GetCachedPlan(psrc, params, NULL, NULL);
+
+ /*
+ * Now we can define the portal.
+ *
+ * DO NOT put any code that could possibly throw an error between the
+ * above GetCachedPlan call and here.
+ */
+ PortalDefineQuery(portal,
+ saved_stmt_name,
+ query_string,
+ psrc->commandTag,
+ cplan->stmt_list,
+ cplan);
+
+ /* Done with the snapshot used for parameter I/O and parsing/planning */
+ if (snapshot_set)
+ PopActiveSnapshot();
+
+ /*
+ * And we're ready to start portal execution.
+ */
+ PortalStart(portal, params, 0, InvalidSnapshot);
+
+ /*
+ * Apply the result format requests to the portal.
+ */
+ PortalSetResultFormat(portal, numRFormats, rformats);
+
+ /*
+ * Done binding; remove the parameters error callback. Entries emitted
+ * later determine independently whether to log the parameters or not.
+ */
+ error_context_stack = error_context_stack->previous;
+
+ /*
+ * Send BindComplete.
+ */
+ if (whereToSendOutput == DestRemote)
+ pq_putemptymessage('2');
+
+ /*
+ * Emit duration logging if appropriate.
+ */
+ switch (check_log_duration(msec_str, false))
+ {
+ case 1:
+ ereport(LOG,
+ (errmsg("duration: %s ms", msec_str),
+ errhidestmt(true)));
+ break;
+ case 2:
+ ereport(LOG,
+ (errmsg("duration: %s ms bind %s%s%s: %s",
+ msec_str,
+ *stmt_name ? stmt_name : "<unnamed>",
+ *portal_name ? "/" : "",
+ *portal_name ? portal_name : "",
+ psrc->query_string),
+ errhidestmt(true),
+ errdetail_params(params)));
+ break;
+ }
+
+ if (save_log_statement_stats)
+ ShowUsage("BIND MESSAGE STATISTICS");
+
+ debug_query_string = NULL;
+}
+
+/*
+ * exec_execute_message
+ *
+ * Process an "Execute" message for a portal
+ */
+static void
+exec_execute_message(const char *portal_name, long max_rows)
+{
+ CommandDest dest;
+ DestReceiver *receiver;
+ Portal portal;
+ bool completed;
+ QueryCompletion qc;
+ const char *sourceText;
+ const char *prepStmtName;
+ ParamListInfo portalParams;
+ bool save_log_statement_stats = log_statement_stats;
+ bool is_xact_command;
+ bool execute_is_fetch;
+ bool was_logged = false;
+ char msec_str[32];
+ ParamsErrorCbData params_data;
+ ErrorContextCallback params_errcxt;
+
+ /* Adjust destination to tell printtup.c what to do */
+ dest = whereToSendOutput;
+ if (dest == DestRemote)
+ dest = DestRemoteExecute;
+
+ portal = GetPortalByName(portal_name);
+ if (!PortalIsValid(portal))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_CURSOR),
+ errmsg("portal \"%s\" does not exist", portal_name)));
+
+ /*
+ * If the original query was a null string, just return
+ * EmptyQueryResponse.
+ */
+ if (portal->commandTag == CMDTAG_UNKNOWN)
+ {
+ Assert(portal->stmts == NIL);
+ NullCommand(dest);
+ return;
+ }
+
+ /* Does the portal contain a transaction command? */
+ is_xact_command = IsTransactionStmtList(portal->stmts);
+
+ /*
+ * We must copy the sourceText and prepStmtName into MessageContext in
+ * case the portal is destroyed during finish_xact_command. We do not
+ * make a copy of the portalParams though, preferring to just not print
+ * them in that case.
+ */
+ sourceText = pstrdup(portal->sourceText);
+ if (portal->prepStmtName)
+ prepStmtName = pstrdup(portal->prepStmtName);
+ else
+ prepStmtName = "<unnamed>";
+ portalParams = portal->portalParams;
+
+ /*
+ * Report query to various monitoring facilities.
+ */
+ debug_query_string = sourceText;
+
+ pgstat_report_activity(STATE_RUNNING, sourceText);
+
+ set_ps_display(GetCommandTagName(portal->commandTag));
+
+ if (save_log_statement_stats)
+ ResetUsage();
+
+ BeginCommand(portal->commandTag, dest);
+
+ /*
+ * Create dest receiver in MessageContext (we don't want it in transaction
+ * context, because that may get deleted if portal contains VACUUM).
+ */
+ receiver = CreateDestReceiver(dest);
+ if (dest == DestRemoteExecute)
+ SetRemoteDestReceiverParams(receiver, portal);
+
+ /*
+ * Ensure we are in a transaction command (this should normally be the
+ * case already due to prior BIND).
+ */
+ start_xact_command();
+
+ /*
+ * If we re-issue an Execute protocol request against an existing portal,
+ * then we are only fetching more rows rather than completely re-executing
+ * the query from the start. atStart is never reset for a v3 portal, so we
+ * are safe to use this check.
+ */
+ execute_is_fetch = !portal->atStart;
+
+ /* Log immediately if dictated by log_statement */
+ if (check_log_statement(portal->stmts))
+ {
+ ereport(LOG,
+ (errmsg("%s %s%s%s: %s",
+ execute_is_fetch ?
+ _("execute fetch from") :
+ _("execute"),
+ prepStmtName,
+ *portal_name ? "/" : "",
+ *portal_name ? portal_name : "",
+ sourceText),
+ errhidestmt(true),
+ errdetail_params(portalParams)));
+ was_logged = true;
+ }
+
+ /*
+ * If we are in aborted transaction state, the only portals we can
+ * actually run are those containing COMMIT or ROLLBACK commands.
+ */
+ if (IsAbortedTransactionBlockState() &&
+ !IsTransactionExitStmtList(portal->stmts))
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block"),
+ errdetail_abort()));
+
+ /* Check for cancel signal before we start execution */
+ CHECK_FOR_INTERRUPTS();
+
+ /*
+ * Okay to run the portal. Set the error callback so that parameters are
+ * logged. The parameters must have been saved during the bind phase.
+ */
+ params_data.portalName = portal->name;
+ params_data.params = portalParams;
+ params_errcxt.previous = error_context_stack;
+ params_errcxt.callback = ParamsErrorCallback;
+ params_errcxt.arg = (void *) &params_data;
+ error_context_stack = &params_errcxt;
+
+ if (max_rows <= 0)
+ max_rows = FETCH_ALL;
+
+ completed = PortalRun(portal,
+ max_rows,
+ true, /* always top level */
+ !execute_is_fetch && max_rows == FETCH_ALL,
+ receiver,
+ receiver,
+ &qc);
+
+ receiver->rDestroy(receiver);
+
+ /* Done executing; remove the params error callback */
+ error_context_stack = error_context_stack->previous;
+
+ if (completed)
+ {
+ if (is_xact_command || (MyXactFlags & XACT_FLAGS_NEEDIMMEDIATECOMMIT))
+ {
+ /*
+ * If this was a transaction control statement, commit it. We
+ * will start a new xact command for the next command (if any).
+ * Likewise if the statement required immediate commit. Without
+ * this provision, we wouldn't force commit until Sync is
+ * received, which creates a hazard if the client tries to
+ * pipeline immediate-commit statements.
+ */
+ finish_xact_command();
+
+ /*
+ * These commands typically don't have any parameters, and even if
+ * one did we couldn't print them now because the storage went
+ * away during finish_xact_command. So pretend there were none.
+ */
+ portalParams = NULL;
+ }
+ else
+ {
+ /*
+ * We need a CommandCounterIncrement after every query, except
+ * those that start or end a transaction block.
+ */
+ CommandCounterIncrement();
+
+ /*
+ * Set XACT_FLAGS_PIPELINING whenever we complete an Execute
+ * message without immediately committing the transaction.
+ */
+ MyXactFlags |= XACT_FLAGS_PIPELINING;
+
+ /*
+ * Disable statement timeout whenever we complete an Execute
+ * message. The next protocol message will start a fresh timeout.
+ */
+ disable_statement_timeout();
+ }
+
+ /* Send appropriate CommandComplete to client */
+ EndCommand(&qc, dest, false);
+ }
+ else
+ {
+ /* Portal run not complete, so send PortalSuspended */
+ if (whereToSendOutput == DestRemote)
+ pq_putemptymessage('s');
+
+ /*
+ * Set XACT_FLAGS_PIPELINING whenever we suspend an Execute message,
+ * too.
+ */
+ MyXactFlags |= XACT_FLAGS_PIPELINING;
+ }
+
+ /*
+ * Emit duration logging if appropriate.
+ */
+ switch (check_log_duration(msec_str, was_logged))
+ {
+ case 1:
+ ereport(LOG,
+ (errmsg("duration: %s ms", msec_str),
+ errhidestmt(true)));
+ break;
+ case 2:
+ ereport(LOG,
+ (errmsg("duration: %s ms %s %s%s%s: %s",
+ msec_str,
+ execute_is_fetch ?
+ _("execute fetch from") :
+ _("execute"),
+ prepStmtName,
+ *portal_name ? "/" : "",
+ *portal_name ? portal_name : "",
+ sourceText),
+ errhidestmt(true),
+ errdetail_params(portalParams)));
+ break;
+ }
+
+ if (save_log_statement_stats)
+ ShowUsage("EXECUTE MESSAGE STATISTICS");
+
+ debug_query_string = NULL;
+}
+
+/*
+ * check_log_statement
+ * Determine whether command should be logged because of log_statement
+ *
+ * stmt_list can be either raw grammar output or a list of planned
+ * statements
+ */
+static bool
+check_log_statement(List *stmt_list)
+{
+ ListCell *stmt_item;
+
+ if (log_statement == LOGSTMT_NONE)
+ return false;
+ if (log_statement == LOGSTMT_ALL)
+ return true;
+
+ /* Else we have to inspect the statement(s) to see whether to log */
+ foreach(stmt_item, stmt_list)
+ {
+ Node *stmt = (Node *) lfirst(stmt_item);
+
+ if (GetCommandLogLevel(stmt) <= log_statement)
+ return true;
+ }
+
+ return false;
+}
+
+/*
+ * check_log_duration
+ * Determine whether current command's duration should be logged
+ * We also check if this statement in this transaction must be logged
+ * (regardless of its duration).
+ *
+ * Returns:
+ * 0 if no logging is needed
+ * 1 if just the duration should be logged
+ * 2 if duration and query details should be logged
+ *
+ * If logging is needed, the duration in msec is formatted into msec_str[],
+ * which must be a 32-byte buffer.
+ *
+ * was_logged should be true if caller already logged query details (this
+ * essentially prevents 2 from being returned).
+ */
+int
+check_log_duration(char *msec_str, bool was_logged)
+{
+ if (log_duration || log_min_duration_sample >= 0 ||
+ log_min_duration_statement >= 0 || xact_is_sampled)
+ {
+ long secs;
+ int usecs;
+ int msecs;
+ bool exceeded_duration;
+ bool exceeded_sample_duration;
+ bool in_sample = false;
+
+ TimestampDifference(GetCurrentStatementStartTimestamp(),
+ GetCurrentTimestamp(),
+ &secs, &usecs);
+ msecs = usecs / 1000;
+
+ /*
+ * This odd-looking test for log_min_duration_* being exceeded is
+ * designed to avoid integer overflow with very long durations: don't
+ * compute secs * 1000 until we've verified it will fit in int.
+ */
+ exceeded_duration = (log_min_duration_statement == 0 ||
+ (log_min_duration_statement > 0 &&
+ (secs > log_min_duration_statement / 1000 ||
+ secs * 1000 + msecs >= log_min_duration_statement)));
+
+ exceeded_sample_duration = (log_min_duration_sample == 0 ||
+ (log_min_duration_sample > 0 &&
+ (secs > log_min_duration_sample / 1000 ||
+ secs * 1000 + msecs >= log_min_duration_sample)));
+
+ /*
+ * Do not log if log_statement_sample_rate = 0. Log a sample if
+ * log_statement_sample_rate <= 1 and avoid unnecessary PRNG call if
+ * log_statement_sample_rate = 1.
+ */
+ if (exceeded_sample_duration)
+ in_sample = log_statement_sample_rate != 0 &&
+ (log_statement_sample_rate == 1 ||
+ pg_prng_double(&pg_global_prng_state) <= log_statement_sample_rate);
+
+ if (exceeded_duration || in_sample || log_duration || xact_is_sampled)
+ {
+ snprintf(msec_str, 32, "%ld.%03d",
+ secs * 1000 + msecs, usecs % 1000);
+ if ((exceeded_duration || in_sample || xact_is_sampled) && !was_logged)
+ return 2;
+ else
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * errdetail_execute
+ *
+ * Add an errdetail() line showing the query referenced by an EXECUTE, if any.
+ * The argument is the raw parsetree list.
+ */
+static int
+errdetail_execute(List *raw_parsetree_list)
+{
+ ListCell *parsetree_item;
+
+ foreach(parsetree_item, raw_parsetree_list)
+ {
+ RawStmt *parsetree = lfirst_node(RawStmt, parsetree_item);
+
+ if (IsA(parsetree->stmt, ExecuteStmt))
+ {
+ ExecuteStmt *stmt = (ExecuteStmt *) parsetree->stmt;
+ PreparedStatement *pstmt;
+
+ pstmt = FetchPreparedStatement(stmt->name, false);
+ if (pstmt)
+ {
+ errdetail("prepare: %s", pstmt->plansource->query_string);
+ return 0;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * errdetail_params
+ *
+ * Add an errdetail() line showing bind-parameter data, if available.
+ * Note that this is only used for statement logging, so it is controlled
+ * by log_parameter_max_length not log_parameter_max_length_on_error.
+ */
+static int
+errdetail_params(ParamListInfo params)
+{
+ if (params && params->numParams > 0 && log_parameter_max_length != 0)
+ {
+ char *str;
+
+ str = BuildParamLogString(params, NULL, log_parameter_max_length);
+ if (str && str[0] != '\0')
+ errdetail("parameters: %s", str);
+ }
+
+ return 0;
+}
+
+/*
+ * errdetail_abort
+ *
+ * Add an errdetail() line showing abort reason, if any.
+ */
+static int
+errdetail_abort(void)
+{
+ if (MyProc->recoveryConflictPending)
+ errdetail("abort reason: recovery conflict");
+
+ return 0;
+}
+
+/*
+ * errdetail_recovery_conflict
+ *
+ * Add an errdetail() line showing conflict source.
+ */
+static int
+errdetail_recovery_conflict(void)
+{
+ switch (RecoveryConflictReason)
+ {
+ case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
+ errdetail("User was holding shared buffer pin for too long.");
+ break;
+ case PROCSIG_RECOVERY_CONFLICT_LOCK:
+ errdetail("User was holding a relation lock for too long.");
+ break;
+ case PROCSIG_RECOVERY_CONFLICT_TABLESPACE:
+ errdetail("User was or might have been using tablespace that must be dropped.");
+ break;
+ case PROCSIG_RECOVERY_CONFLICT_SNAPSHOT:
+ errdetail("User query might have needed to see row versions that must be removed.");
+ break;
+ case PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK:
+ errdetail("User transaction caused buffer deadlock with recovery.");
+ break;
+ case PROCSIG_RECOVERY_CONFLICT_DATABASE:
+ errdetail("User was connected to a database that must be dropped.");
+ break;
+ default:
+ break;
+ /* no errdetail */
+ }
+
+ return 0;
+}
+
+/*
+ * bind_param_error_callback
+ *
+ * Error context callback used while parsing parameters in a Bind message
+ */
+static void
+bind_param_error_callback(void *arg)
+{
+ BindParamCbData *data = (BindParamCbData *) arg;
+ StringInfoData buf;
+ char *quotedval;
+
+ if (data->paramno < 0)
+ return;
+
+ /* If we have a textual value, quote it, and trim if necessary */
+ if (data->paramval)
+ {
+ initStringInfo(&buf);
+ appendStringInfoStringQuoted(&buf, data->paramval,
+ log_parameter_max_length_on_error);
+ quotedval = buf.data;
+ }
+ else
+ quotedval = NULL;
+
+ if (data->portalName && data->portalName[0] != '\0')
+ {
+ if (quotedval)
+ errcontext("portal \"%s\" parameter $%d = %s",
+ data->portalName, data->paramno + 1, quotedval);
+ else
+ errcontext("portal \"%s\" parameter $%d",
+ data->portalName, data->paramno + 1);
+ }
+ else
+ {
+ if (quotedval)
+ errcontext("unnamed portal parameter $%d = %s",
+ data->paramno + 1, quotedval);
+ else
+ errcontext("unnamed portal parameter $%d",
+ data->paramno + 1);
+ }
+
+ if (quotedval)
+ pfree(quotedval);
+}
+
+/*
+ * exec_describe_statement_message
+ *
+ * Process a "Describe" message for a prepared statement
+ */
+static void
+exec_describe_statement_message(const char *stmt_name)
+{
+ CachedPlanSource *psrc;
+
+ /*
+ * Start up a transaction command. (Note that this will normally change
+ * current memory context.) Nothing happens if we are already in one.
+ */
+ start_xact_command();
+
+ /* Switch back to message context */
+ MemoryContextSwitchTo(MessageContext);
+
+ /* Find prepared statement */
+ if (stmt_name[0] != '\0')
+ {
+ PreparedStatement *pstmt;
+
+ pstmt = FetchPreparedStatement(stmt_name, true);
+ psrc = pstmt->plansource;
+ }
+ else
+ {
+ /* special-case the unnamed statement */
+ psrc = unnamed_stmt_psrc;
+ if (!psrc)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
+ errmsg("unnamed prepared statement does not exist")));
+ }
+
+ /* Prepared statements shouldn't have changeable result descs */
+ Assert(psrc->fixed_result);
+
+ /*
+ * If we are in aborted transaction state, we can't run
+ * SendRowDescriptionMessage(), because that needs catalog accesses.
+ * Hence, refuse to Describe statements that return data. (We shouldn't
+ * just refuse all Describes, since that might break the ability of some
+ * clients to issue COMMIT or ROLLBACK commands, if they use code that
+ * blindly Describes whatever it does.) We can Describe parameters
+ * without doing anything dangerous, so we don't restrict that.
+ */
+ if (IsAbortedTransactionBlockState() &&
+ psrc->resultDesc)
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block"),
+ errdetail_abort()));
+
+ if (whereToSendOutput != DestRemote)
+ return; /* can't actually do anything... */
+
+ /*
+ * First describe the parameters...
+ */
+ pq_beginmessage_reuse(&row_description_buf, 't'); /* parameter description
+ * message type */
+ pq_sendint16(&row_description_buf, psrc->num_params);
+
+ for (int i = 0; i < psrc->num_params; i++)
+ {
+ Oid ptype = psrc->param_types[i];
+
+ pq_sendint32(&row_description_buf, (int) ptype);
+ }
+ pq_endmessage_reuse(&row_description_buf);
+
+ /*
+ * Next send RowDescription or NoData to describe the result...
+ */
+ if (psrc->resultDesc)
+ {
+ List *tlist;
+
+ /* Get the plan's primary targetlist */
+ tlist = CachedPlanGetTargetList(psrc, NULL);
+
+ SendRowDescriptionMessage(&row_description_buf,
+ psrc->resultDesc,
+ tlist,
+ NULL);
+ }
+ else
+ pq_putemptymessage('n'); /* NoData */
+}
+
+/*
+ * exec_describe_portal_message
+ *
+ * Process a "Describe" message for a portal
+ */
+static void
+exec_describe_portal_message(const char *portal_name)
+{
+ Portal portal;
+
+ /*
+ * Start up a transaction command. (Note that this will normally change
+ * current memory context.) Nothing happens if we are already in one.
+ */
+ start_xact_command();
+
+ /* Switch back to message context */
+ MemoryContextSwitchTo(MessageContext);
+
+ portal = GetPortalByName(portal_name);
+ if (!PortalIsValid(portal))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_CURSOR),
+ errmsg("portal \"%s\" does not exist", portal_name)));
+
+ /*
+ * If we are in aborted transaction state, we can't run
+ * SendRowDescriptionMessage(), because that needs catalog accesses.
+ * Hence, refuse to Describe portals that return data. (We shouldn't just
+ * refuse all Describes, since that might break the ability of some
+ * clients to issue COMMIT or ROLLBACK commands, if they use code that
+ * blindly Describes whatever it does.)
+ */
+ if (IsAbortedTransactionBlockState() &&
+ portal->tupDesc)
+ ereport(ERROR,
+ (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+ errmsg("current transaction is aborted, "
+ "commands ignored until end of transaction block"),
+ errdetail_abort()));
+
+ if (whereToSendOutput != DestRemote)
+ return; /* can't actually do anything... */
+
+ if (portal->tupDesc)
+ SendRowDescriptionMessage(&row_description_buf,
+ portal->tupDesc,
+ FetchPortalTargetList(portal),
+ portal->formats);
+ else
+ pq_putemptymessage('n'); /* NoData */
+}
+
+
+/*
+ * Convenience routines for starting/committing a single command.
+ */
+static void
+start_xact_command(void)
+{
+ if (!xact_started)
+ {
+ StartTransactionCommand();
+
+ xact_started = true;
+ }
+
+ /*
+ * Start statement timeout if necessary. Note that this'll intentionally
+ * not reset the clock on an already started timeout, to avoid the timing
+ * overhead when start_xact_command() is invoked repeatedly, without an
+ * interceding finish_xact_command() (e.g. parse/bind/execute). If that's
+ * not desired, the timeout has to be disabled explicitly.
+ */
+ enable_statement_timeout();
+
+ /* Start timeout for checking if the client has gone away if necessary. */
+ if (client_connection_check_interval > 0 &&
+ IsUnderPostmaster &&
+ MyProcPort &&
+ !get_timeout_active(CLIENT_CONNECTION_CHECK_TIMEOUT))
+ enable_timeout_after(CLIENT_CONNECTION_CHECK_TIMEOUT,
+ client_connection_check_interval);
+}
+
+static void
+finish_xact_command(void)
+{
+ /* cancel active statement timeout after each command */
+ disable_statement_timeout();
+
+ if (xact_started)
+ {
+ CommitTransactionCommand();
+
+#ifdef MEMORY_CONTEXT_CHECKING
+ /* Check all memory contexts that weren't freed during commit */
+ /* (those that were, were checked before being deleted) */
+ MemoryContextCheck(TopMemoryContext);
+#endif
+
+#ifdef SHOW_MEMORY_STATS
+ /* Print mem stats after each commit for leak tracking */
+ MemoryContextStats(TopMemoryContext);
+#endif
+
+ xact_started = false;
+ }
+}
+
+
+/*
+ * Convenience routines for checking whether a statement is one of the
+ * ones that we allow in transaction-aborted state.
+ */
+
+/* Test a bare parsetree */
+static bool
+IsTransactionExitStmt(Node *parsetree)
+{
+ if (parsetree && IsA(parsetree, TransactionStmt))
+ {
+ TransactionStmt *stmt = (TransactionStmt *) parsetree;
+
+ if (stmt->kind == TRANS_STMT_COMMIT ||
+ stmt->kind == TRANS_STMT_PREPARE ||
+ stmt->kind == TRANS_STMT_ROLLBACK ||
+ stmt->kind == TRANS_STMT_ROLLBACK_TO)
+ return true;
+ }
+ return false;
+}
+
+/* Test a list that contains PlannedStmt nodes */
+static bool
+IsTransactionExitStmtList(List *pstmts)
+{
+ if (list_length(pstmts) == 1)
+ {
+ PlannedStmt *pstmt = linitial_node(PlannedStmt, pstmts);
+
+ if (pstmt->commandType == CMD_UTILITY &&
+ IsTransactionExitStmt(pstmt->utilityStmt))
+ return true;
+ }
+ return false;
+}
+
+/* Test a list that contains PlannedStmt nodes */
+static bool
+IsTransactionStmtList(List *pstmts)
+{
+ if (list_length(pstmts) == 1)
+ {
+ PlannedStmt *pstmt = linitial_node(PlannedStmt, pstmts);
+
+ if (pstmt->commandType == CMD_UTILITY &&
+ IsA(pstmt->utilityStmt, TransactionStmt))
+ return true;
+ }
+ return false;
+}
+
+/* Release any existing unnamed prepared statement */
+static void
+drop_unnamed_stmt(void)
+{
+ /* paranoia to avoid a dangling pointer in case of error */
+ if (unnamed_stmt_psrc)
+ {
+ CachedPlanSource *psrc = unnamed_stmt_psrc;
+
+ unnamed_stmt_psrc = NULL;
+ DropCachedPlan(psrc);
+ }
+}
+
+
+/* --------------------------------
+ * signal handler routines used in PostgresMain()
+ * --------------------------------
+ */
+
+/*
+ * quickdie() occurs when signaled SIGQUIT by the postmaster.
+ *
+ * Either some backend has bought the farm, or we've been told to shut down
+ * "immediately"; so we need to stop what we're doing and exit.
+ */
+void
+quickdie(SIGNAL_ARGS)
+{
+ sigaddset(&BlockSig, SIGQUIT); /* prevent nested calls */
+ PG_SETMASK(&BlockSig);
+
+ /*
+ * Prevent interrupts while exiting; though we just blocked signals that
+ * would queue new interrupts, one may have been pending. We don't want a
+ * quickdie() downgraded to a mere query cancel.
+ */
+ HOLD_INTERRUPTS();
+
+ /*
+ * If we're aborting out of client auth, don't risk trying to send
+ * anything to the client; we will likely violate the protocol, not to
+ * mention that we may have interrupted the guts of OpenSSL or some
+ * authentication library.
+ */
+ if (ClientAuthInProgress && whereToSendOutput == DestRemote)
+ whereToSendOutput = DestNone;
+
+ /*
+ * Notify the client before exiting, to give a clue on what happened.
+ *
+ * It's dubious to call ereport() from a signal handler. It is certainly
+ * not async-signal safe. But it seems better to try, than to disconnect
+ * abruptly and leave the client wondering what happened. It's remotely
+ * possible that we crash or hang while trying to send the message, but
+ * receiving a SIGQUIT is a sign that something has already gone badly
+ * wrong, so there's not much to lose. Assuming the postmaster is still
+ * running, it will SIGKILL us soon if we get stuck for some reason.
+ *
+ * One thing we can do to make this a tad safer is to clear the error
+ * context stack, so that context callbacks are not called. That's a lot
+ * less code that could be reached here, and the context info is unlikely
+ * to be very relevant to a SIGQUIT report anyway.
+ */
+ error_context_stack = NULL;
+
+ /*
+ * When responding to a postmaster-issued signal, we send the message only
+ * to the client; sending to the server log just creates log spam, plus
+ * it's more code that we need to hope will work in a signal handler.
+ *
+ * Ideally these should be ereport(FATAL), but then we'd not get control
+ * back to force the correct type of process exit.
+ */
+ switch (GetQuitSignalReason())
+ {
+ case PMQUIT_NOT_SENT:
+ /* Hmm, SIGQUIT arrived out of the blue */
+ ereport(WARNING,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating connection because of unexpected SIGQUIT signal")));
+ break;
+ case PMQUIT_FOR_CRASH:
+ /* A crash-and-restart cycle is in progress */
+ ereport(WARNING_CLIENT_ONLY,
+ (errcode(ERRCODE_CRASH_SHUTDOWN),
+ errmsg("terminating connection because of crash of another server process"),
+ errdetail("The postmaster has commanded this server process to roll back"
+ " the current transaction and exit, because another"
+ " server process exited abnormally and possibly corrupted"
+ " shared memory."),
+ errhint("In a moment you should be able to reconnect to the"
+ " database and repeat your command.")));
+ break;
+ case PMQUIT_FOR_STOP:
+ /* Immediate-mode stop */
+ ereport(WARNING_CLIENT_ONLY,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating connection due to immediate shutdown command")));
+ break;
+ }
+
+ /*
+ * We DO NOT want to run proc_exit() or atexit() callbacks -- we're here
+ * because shared memory may be corrupted, so we don't want to try to
+ * clean up our transaction. Just nail the windows shut and get out of
+ * town. The callbacks wouldn't be safe to run from a signal handler,
+ * anyway.
+ *
+ * Note we do _exit(2) not _exit(0). This is to force the postmaster into
+ * a system reset cycle if someone sends a manual SIGQUIT to a random
+ * backend. This is necessary precisely because we don't clean up our
+ * shared memory state. (The "dead man switch" mechanism in pmsignal.c
+ * should ensure the postmaster sees this as a crash, too, but no harm in
+ * being doubly sure.)
+ */
+ _exit(2);
+}
+
+/*
+ * Shutdown signal from postmaster: abort transaction and exit
+ * at soonest convenient time
+ */
+void
+die(SIGNAL_ARGS)
+{
+ int save_errno = errno;
+
+ /* Don't joggle the elbow of proc_exit */
+ if (!proc_exit_inprogress)
+ {
+ InterruptPending = true;
+ ProcDiePending = true;
+ }
+
+ /* for the cumulative stats system */
+ pgStatSessionEndCause = DISCONNECT_KILLED;
+
+ /* If we're still here, waken anything waiting on the process latch */
+ SetLatch(MyLatch);
+
+ /*
+ * If we're in single user mode, we want to quit immediately - we can't
+ * rely on latches as they wouldn't work when stdin/stdout is a file.
+ * Rather ugly, but it's unlikely to be worthwhile to invest much more
+ * effort just for the benefit of single user mode.
+ */
+ if (DoingCommandRead && whereToSendOutput != DestRemote)
+ ProcessInterrupts();
+
+ errno = save_errno;
+}
+
+/*
+ * Query-cancel signal from postmaster: abort current transaction
+ * at soonest convenient time
+ */
+void
+StatementCancelHandler(SIGNAL_ARGS)
+{
+ int save_errno = errno;
+
+ /*
+ * Don't joggle the elbow of proc_exit
+ */
+ if (!proc_exit_inprogress)
+ {
+ InterruptPending = true;
+ QueryCancelPending = true;
+ }
+
+ /* If we're still here, waken anything waiting on the process latch */
+ SetLatch(MyLatch);
+
+ errno = save_errno;
+}
+
+/* signal handler for floating point exception */
+void
+FloatExceptionHandler(SIGNAL_ARGS)
+{
+ /* We're not returning, so no need to save errno */
+ ereport(ERROR,
+ (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+ errmsg("floating-point exception"),
+ errdetail("An invalid floating-point operation was signaled. "
+ "This probably means an out-of-range result or an "
+ "invalid operation, such as division by zero.")));
+}
+
+/*
+ * RecoveryConflictInterrupt: out-of-line portion of recovery conflict
+ * handling following receipt of SIGUSR1. Designed to be similar to die()
+ * and StatementCancelHandler(). Called only by a normal user backend
+ * that begins a transaction during recovery.
+ */
+void
+RecoveryConflictInterrupt(ProcSignalReason reason)
+{
+ int save_errno = errno;
+
+ /*
+ * Don't joggle the elbow of proc_exit
+ */
+ if (!proc_exit_inprogress)
+ {
+ RecoveryConflictReason = reason;
+ switch (reason)
+ {
+ case PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK:
+
+ /*
+ * If we aren't waiting for a lock we can never deadlock.
+ */
+ if (!IsWaitingForLock())
+ return;
+
+ /* Intentional fall through to check wait for pin */
+ /* FALLTHROUGH */
+
+ case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
+
+ /*
+ * If PROCSIG_RECOVERY_CONFLICT_BUFFERPIN is requested but we
+ * aren't blocking the Startup process there is nothing more
+ * to do.
+ *
+ * When PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK is
+ * requested, if we're waiting for locks and the startup
+ * process is not waiting for buffer pin (i.e., also waiting
+ * for locks), we set the flag so that ProcSleep() will check
+ * for deadlocks.
+ */
+ if (!HoldingBufferPinThatDelaysRecovery())
+ {
+ if (reason == PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK &&
+ GetStartupBufferPinWaitBufId() < 0)
+ CheckDeadLockAlert();
+ return;
+ }
+
+ MyProc->recoveryConflictPending = true;
+
+ /* Intentional fall through to error handling */
+ /* FALLTHROUGH */
+
+ case PROCSIG_RECOVERY_CONFLICT_LOCK:
+ case PROCSIG_RECOVERY_CONFLICT_TABLESPACE:
+ case PROCSIG_RECOVERY_CONFLICT_SNAPSHOT:
+
+ /*
+ * If we aren't in a transaction any longer then ignore.
+ */
+ if (!IsTransactionOrTransactionBlock())
+ return;
+
+ /*
+ * If we can abort just the current subtransaction then we are
+ * OK to throw an ERROR to resolve the conflict. Otherwise
+ * drop through to the FATAL case.
+ *
+ * XXX other times that we can throw just an ERROR *may* be
+ * PROCSIG_RECOVERY_CONFLICT_LOCK if no locks are held in
+ * parent transactions
+ *
+ * PROCSIG_RECOVERY_CONFLICT_SNAPSHOT if no snapshots are held
+ * by parent transactions and the transaction is not
+ * transaction-snapshot mode
+ *
+ * PROCSIG_RECOVERY_CONFLICT_TABLESPACE if no temp files or
+ * cursors open in parent transactions
+ */
+ if (!IsSubTransaction())
+ {
+ /*
+ * If we already aborted then we no longer need to cancel.
+ * We do this here since we do not wish to ignore aborted
+ * subtransactions, which must cause FATAL, currently.
+ */
+ if (IsAbortedTransactionBlockState())
+ return;
+
+ RecoveryConflictPending = true;
+ QueryCancelPending = true;
+ InterruptPending = true;
+ break;
+ }
+
+ /* Intentional fall through to session cancel */
+ /* FALLTHROUGH */
+
+ case PROCSIG_RECOVERY_CONFLICT_DATABASE:
+ RecoveryConflictPending = true;
+ ProcDiePending = true;
+ InterruptPending = true;
+ break;
+
+ default:
+ elog(FATAL, "unrecognized conflict mode: %d",
+ (int) reason);
+ }
+
+ Assert(RecoveryConflictPending && (QueryCancelPending || ProcDiePending));
+
+ /*
+ * All conflicts apart from database cause dynamic errors where the
+ * command or transaction can be retried at a later point with some
+ * potential for success. No need to reset this, since non-retryable
+ * conflict errors are currently FATAL.
+ */
+ if (reason == PROCSIG_RECOVERY_CONFLICT_DATABASE)
+ RecoveryConflictRetryable = false;
+ }
+
+ /*
+ * Set the process latch. This function essentially emulates signal
+ * handlers like die() and StatementCancelHandler() and it seems prudent
+ * to behave similarly as they do.
+ */
+ SetLatch(MyLatch);
+
+ errno = save_errno;
+}
+
+/*
+ * ProcessInterrupts: out-of-line portion of CHECK_FOR_INTERRUPTS() macro
+ *
+ * If an interrupt condition is pending, and it's safe to service it,
+ * then clear the flag and accept the interrupt. Called only when
+ * InterruptPending is true.
+ *
+ * Note: if INTERRUPTS_CAN_BE_PROCESSED() is true, then ProcessInterrupts
+ * is guaranteed to clear the InterruptPending flag before returning.
+ * (This is not the same as guaranteeing that it's still clear when we
+ * return; another interrupt could have arrived. But we promise that
+ * any pre-existing one will have been serviced.)
+ */
+void
+ProcessInterrupts(void)
+{
+ /* OK to accept any interrupts now? */
+ if (InterruptHoldoffCount != 0 || CritSectionCount != 0)
+ return;
+ InterruptPending = false;
+
+ if (ProcDiePending)
+ {
+ ProcDiePending = false;
+ QueryCancelPending = false; /* ProcDie trumps QueryCancel */
+ LockErrorCleanup();
+ /* As in quickdie, don't risk sending to client during auth */
+ if (ClientAuthInProgress && whereToSendOutput == DestRemote)
+ whereToSendOutput = DestNone;
+ if (ClientAuthInProgress)
+ ereport(FATAL,
+ (errcode(ERRCODE_QUERY_CANCELED),
+ errmsg("canceling authentication due to timeout")));
+ else if (IsAutoVacuumWorkerProcess())
+ ereport(FATAL,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating autovacuum process due to administrator command")));
+ else if (IsLogicalWorker())
+ ereport(FATAL,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating logical replication worker due to administrator command")));
+ else if (IsLogicalLauncher())
+ {
+ ereport(DEBUG1,
+ (errmsg_internal("logical replication launcher shutting down")));
+
+ /*
+ * The logical replication launcher can be stopped at any time.
+ * Use exit status 1 so the background worker is restarted.
+ */
+ proc_exit(1);
+ }
+ else if (RecoveryConflictPending && RecoveryConflictRetryable)
+ {
+ pgstat_report_recovery_conflict(RecoveryConflictReason);
+ ereport(FATAL,
+ (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
+ errmsg("terminating connection due to conflict with recovery"),
+ errdetail_recovery_conflict()));
+ }
+ else if (RecoveryConflictPending)
+ {
+ /* Currently there is only one non-retryable recovery conflict */
+ Assert(RecoveryConflictReason == PROCSIG_RECOVERY_CONFLICT_DATABASE);
+ pgstat_report_recovery_conflict(RecoveryConflictReason);
+ ereport(FATAL,
+ (errcode(ERRCODE_DATABASE_DROPPED),
+ errmsg("terminating connection due to conflict with recovery"),
+ errdetail_recovery_conflict()));
+ }
+ else if (IsBackgroundWorker)
+ ereport(FATAL,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating background worker \"%s\" due to administrator command",
+ MyBgworkerEntry->bgw_type)));
+ else
+ ereport(FATAL,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating connection due to administrator command")));
+ }
+
+ if (CheckClientConnectionPending)
+ {
+ CheckClientConnectionPending = false;
+
+ /*
+ * Check for lost connection and re-arm, if still configured, but not
+ * if we've arrived back at DoingCommandRead state. We don't want to
+ * wake up idle sessions, and they already know how to detect lost
+ * connections.
+ */
+ if (!DoingCommandRead && client_connection_check_interval > 0)
+ {
+ if (!pq_check_connection())
+ ClientConnectionLost = true;
+ else
+ enable_timeout_after(CLIENT_CONNECTION_CHECK_TIMEOUT,
+ client_connection_check_interval);
+ }
+ }
+
+ if (ClientConnectionLost)
+ {
+ QueryCancelPending = false; /* lost connection trumps QueryCancel */
+ LockErrorCleanup();
+ /* don't send to client, we already know the connection to be dead. */
+ whereToSendOutput = DestNone;
+ ereport(FATAL,
+ (errcode(ERRCODE_CONNECTION_FAILURE),
+ errmsg("connection to client lost")));
+ }
+
+ /*
+ * If a recovery conflict happens while we are waiting for input from the
+ * client, the client is presumably just sitting idle in a transaction,
+ * preventing recovery from making progress. Terminate the connection to
+ * dislodge it.
+ */
+ if (RecoveryConflictPending && DoingCommandRead)
+ {
+ QueryCancelPending = false; /* this trumps QueryCancel */
+ RecoveryConflictPending = false;
+ LockErrorCleanup();
+ pgstat_report_recovery_conflict(RecoveryConflictReason);
+ ereport(FATAL,
+ (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
+ errmsg("terminating connection due to conflict with recovery"),
+ errdetail_recovery_conflict(),
+ errhint("In a moment you should be able to reconnect to the"
+ " database and repeat your command.")));
+ }
+
+ /*
+ * Don't allow query cancel interrupts while reading input from the
+ * client, because we might lose sync in the FE/BE protocol. (Die
+ * interrupts are OK, because we won't read any further messages from the
+ * client in that case.)
+ */
+ if (QueryCancelPending && QueryCancelHoldoffCount != 0)
+ {
+ /*
+ * Re-arm InterruptPending so that we process the cancel request as
+ * soon as we're done reading the message. (XXX this is seriously
+ * ugly: it complicates INTERRUPTS_CAN_BE_PROCESSED(), and it means we
+ * can't use that macro directly as the initial test in this function,
+ * meaning that this code also creates opportunities for other bugs to
+ * appear.)
+ */
+ InterruptPending = true;
+ }
+ else if (QueryCancelPending)
+ {
+ bool lock_timeout_occurred;
+ bool stmt_timeout_occurred;
+
+ QueryCancelPending = false;
+
+ /*
+ * If LOCK_TIMEOUT and STATEMENT_TIMEOUT indicators are both set, we
+ * need to clear both, so always fetch both.
+ */
+ lock_timeout_occurred = get_timeout_indicator(LOCK_TIMEOUT, true);
+ stmt_timeout_occurred = get_timeout_indicator(STATEMENT_TIMEOUT, true);
+
+ /*
+ * If both were set, we want to report whichever timeout completed
+ * earlier; this ensures consistent behavior if the machine is slow
+ * enough that the second timeout triggers before we get here. A tie
+ * is arbitrarily broken in favor of reporting a lock timeout.
+ */
+ if (lock_timeout_occurred && stmt_timeout_occurred &&
+ get_timeout_finish_time(STATEMENT_TIMEOUT) < get_timeout_finish_time(LOCK_TIMEOUT))
+ lock_timeout_occurred = false; /* report stmt timeout */
+
+ if (lock_timeout_occurred)
+ {
+ LockErrorCleanup();
+ ereport(ERROR,
+ (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
+ errmsg("canceling statement due to lock timeout")));
+ }
+ if (stmt_timeout_occurred)
+ {
+ LockErrorCleanup();
+ ereport(ERROR,
+ (errcode(ERRCODE_QUERY_CANCELED),
+ errmsg("canceling statement due to statement timeout")));
+ }
+ if (IsAutoVacuumWorkerProcess())
+ {
+ LockErrorCleanup();
+ ereport(ERROR,
+ (errcode(ERRCODE_QUERY_CANCELED),
+ errmsg("canceling autovacuum task")));
+ }
+ if (RecoveryConflictPending)
+ {
+ RecoveryConflictPending = false;
+ LockErrorCleanup();
+ pgstat_report_recovery_conflict(RecoveryConflictReason);
+ ereport(ERROR,
+ (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
+ errmsg("canceling statement due to conflict with recovery"),
+ errdetail_recovery_conflict()));
+ }
+
+ /*
+ * If we are reading a command from the client, just ignore the cancel
+ * request --- sending an extra error message won't accomplish
+ * anything. Otherwise, go ahead and throw the error.
+ */
+ if (!DoingCommandRead)
+ {
+ LockErrorCleanup();
+ ereport(ERROR,
+ (errcode(ERRCODE_QUERY_CANCELED),
+ errmsg("canceling statement due to user request")));
+ }
+ }
+
+ if (IdleInTransactionSessionTimeoutPending)
+ {
+ /*
+ * If the GUC has been reset to zero, ignore the signal. This is
+ * important because the GUC update itself won't disable any pending
+ * interrupt.
+ */
+ if (IdleInTransactionSessionTimeout > 0)
+ ereport(FATAL,
+ (errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT),
+ errmsg("terminating connection due to idle-in-transaction timeout")));
+ else
+ IdleInTransactionSessionTimeoutPending = false;
+ }
+
+ if (IdleSessionTimeoutPending)
+ {
+ /* As above, ignore the signal if the GUC has been reset to zero. */
+ if (IdleSessionTimeout > 0)
+ ereport(FATAL,
+ (errcode(ERRCODE_IDLE_SESSION_TIMEOUT),
+ errmsg("terminating connection due to idle-session timeout")));
+ else
+ IdleSessionTimeoutPending = false;
+ }
+
+ /*
+ * If there are pending stats updates and we currently are truly idle
+ * (matching the conditions in PostgresMain(), report stats now.
+ */
+ if (IdleStatsUpdateTimeoutPending &&
+ DoingCommandRead && !IsTransactionOrTransactionBlock())
+ {
+ IdleStatsUpdateTimeoutPending = false;
+ pgstat_report_stat(true);
+ }
+
+ if (ProcSignalBarrierPending)
+ ProcessProcSignalBarrier();
+
+ if (ParallelMessagePending)
+ HandleParallelMessages();
+
+ if (LogMemoryContextPending)
+ ProcessLogMemoryContextInterrupt();
+}
+
+
+/*
+ * IA64-specific code to fetch the AR.BSP register for stack depth checks.
+ *
+ * We currently support gcc, icc, and HP-UX's native compiler here.
+ *
+ * Note: while icc accepts gcc asm blocks on x86[_64], this is not true on
+ * ia64 (at least not in icc versions before 12.x). So we have to carry a
+ * separate implementation for it.
+ */
+#if defined(__ia64__) || defined(__ia64)
+
+#if defined(__hpux) && !defined(__GNUC__) && !defined(__INTEL_COMPILER)
+/* Assume it's HP-UX native compiler */
+#include <ia64/sys/inline.h>
+#define ia64_get_bsp() ((char *) (_Asm_mov_from_ar(_AREG_BSP, _NO_FENCE)))
+#elif defined(__INTEL_COMPILER)
+/* icc */
+#include <asm/ia64regs.h>
+#define ia64_get_bsp() ((char *) __getReg(_IA64_REG_AR_BSP))
+#else
+/* gcc */
+static __inline__ char *
+ia64_get_bsp(void)
+{
+ char *ret;
+
+ /* the ;; is a "stop", seems to be required before fetching BSP */
+ __asm__ __volatile__(
+ ";;\n"
+ " mov %0=ar.bsp \n"
+: "=r"(ret));
+
+ return ret;
+}
+#endif
+#endif /* IA64 */
+
+
+/*
+ * set_stack_base: set up reference point for stack depth checking
+ *
+ * Returns the old reference point, if any.
+ */
+pg_stack_base_t
+set_stack_base(void)
+{
+#ifndef HAVE__BUILTIN_FRAME_ADDRESS
+ char stack_base;
+#endif
+ pg_stack_base_t old;
+
+#if defined(__ia64__) || defined(__ia64)
+ old.stack_base_ptr = stack_base_ptr;
+ old.register_stack_base_ptr = register_stack_base_ptr;
+#else
+ old = stack_base_ptr;
+#endif
+
+ /*
+ * Set up reference point for stack depth checking. On recent gcc we use
+ * __builtin_frame_address() to avoid a warning about storing a local
+ * variable's address in a long-lived variable.
+ */
+#ifdef HAVE__BUILTIN_FRAME_ADDRESS
+ stack_base_ptr = __builtin_frame_address(0);
+#else
+ stack_base_ptr = &stack_base;
+#endif
+#if defined(__ia64__) || defined(__ia64)
+ register_stack_base_ptr = ia64_get_bsp();
+#endif
+
+ return old;
+}
+
+/*
+ * restore_stack_base: restore reference point for stack depth checking
+ *
+ * This can be used after set_stack_base() to restore the old value. This
+ * is currently only used in PL/Java. When PL/Java calls a backend function
+ * from different thread, the thread's stack is at a different location than
+ * the main thread's stack, so it sets the base pointer before the call, and
+ * restores it afterwards.
+ */
+void
+restore_stack_base(pg_stack_base_t base)
+{
+#if defined(__ia64__) || defined(__ia64)
+ stack_base_ptr = base.stack_base_ptr;
+ register_stack_base_ptr = base.register_stack_base_ptr;
+#else
+ stack_base_ptr = base;
+#endif
+}
+
+/*
+ * check_stack_depth/stack_is_too_deep: check for excessively deep recursion
+ *
+ * This should be called someplace in any recursive routine that might possibly
+ * recurse deep enough to overflow the stack. Most Unixen treat stack
+ * overflow as an unrecoverable SIGSEGV, so we want to error out ourselves
+ * before hitting the hardware limit.
+ *
+ * check_stack_depth() just throws an error summarily. stack_is_too_deep()
+ * can be used by code that wants to handle the error condition itself.
+ */
+void
+check_stack_depth(void)
+{
+ if (stack_is_too_deep())
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
+ errmsg("stack depth limit exceeded"),
+ errhint("Increase the configuration parameter \"max_stack_depth\" (currently %dkB), "
+ "after ensuring the platform's stack depth limit is adequate.",
+ max_stack_depth)));
+ }
+}
+
+bool
+stack_is_too_deep(void)
+{
+ char stack_top_loc;
+ long stack_depth;
+
+ /*
+ * Compute distance from reference point to my local variables
+ */
+ stack_depth = (long) (stack_base_ptr - &stack_top_loc);
+
+ /*
+ * Take abs value, since stacks grow up on some machines, down on others
+ */
+ if (stack_depth < 0)
+ stack_depth = -stack_depth;
+
+ /*
+ * Trouble?
+ *
+ * The test on stack_base_ptr prevents us from erroring out if called
+ * during process setup or in a non-backend process. Logically it should
+ * be done first, but putting it here avoids wasting cycles during normal
+ * cases.
+ */
+ if (stack_depth > max_stack_depth_bytes &&
+ stack_base_ptr != NULL)
+ return true;
+
+ /*
+ * On IA64 there is a separate "register" stack that requires its own
+ * independent check. For this, we have to measure the change in the
+ * "BSP" pointer from PostgresMain to here. Logic is just as above,
+ * except that we know IA64's register stack grows up.
+ *
+ * Note we assume that the same max_stack_depth applies to both stacks.
+ */
+#if defined(__ia64__) || defined(__ia64)
+ stack_depth = (long) (ia64_get_bsp() - register_stack_base_ptr);
+
+ if (stack_depth > max_stack_depth_bytes &&
+ register_stack_base_ptr != NULL)
+ return true;
+#endif /* IA64 */
+
+ return false;
+}
+
+/* GUC check hook for max_stack_depth */
+bool
+check_max_stack_depth(int *newval, void **extra, GucSource source)
+{
+ long newval_bytes = *newval * 1024L;
+ long stack_rlimit = get_stack_depth_rlimit();
+
+ if (stack_rlimit > 0 && newval_bytes > stack_rlimit - STACK_DEPTH_SLOP)
+ {
+ GUC_check_errdetail("\"max_stack_depth\" must not exceed %ldkB.",
+ (stack_rlimit - STACK_DEPTH_SLOP) / 1024L);
+ GUC_check_errhint("Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent.");
+ return false;
+ }
+ return true;
+}
+
+/* GUC assign hook for max_stack_depth */
+void
+assign_max_stack_depth(int newval, void *extra)
+{
+ long newval_bytes = newval * 1024L;
+
+ max_stack_depth_bytes = newval_bytes;
+}
+
+
+/*
+ * set_debug_options --- apply "-d N" command line option
+ *
+ * -d is not quite the same as setting log_min_messages because it enables
+ * other output options.
+ */
+void
+set_debug_options(int debug_flag, GucContext context, GucSource source)
+{
+ if (debug_flag > 0)
+ {
+ char debugstr[64];
+
+ sprintf(debugstr, "debug%d", debug_flag);
+ SetConfigOption("log_min_messages", debugstr, context, source);
+ }
+ else
+ SetConfigOption("log_min_messages", "notice", context, source);
+
+ if (debug_flag >= 1 && context == PGC_POSTMASTER)
+ {
+ SetConfigOption("log_connections", "true", context, source);
+ SetConfigOption("log_disconnections", "true", context, source);
+ }
+ if (debug_flag >= 2)
+ SetConfigOption("log_statement", "all", context, source);
+ if (debug_flag >= 3)
+ SetConfigOption("debug_print_parse", "true", context, source);
+ if (debug_flag >= 4)
+ SetConfigOption("debug_print_plan", "true", context, source);
+ if (debug_flag >= 5)
+ SetConfigOption("debug_print_rewritten", "true", context, source);
+}
+
+
+bool
+set_plan_disabling_options(const char *arg, GucContext context, GucSource source)
+{
+ const char *tmp = NULL;
+
+ switch (arg[0])
+ {
+ case 's': /* seqscan */
+ tmp = "enable_seqscan";
+ break;
+ case 'i': /* indexscan */
+ tmp = "enable_indexscan";
+ break;
+ case 'o': /* indexonlyscan */
+ tmp = "enable_indexonlyscan";
+ break;
+ case 'b': /* bitmapscan */
+ tmp = "enable_bitmapscan";
+ break;
+ case 't': /* tidscan */
+ tmp = "enable_tidscan";
+ break;
+ case 'n': /* nestloop */
+ tmp = "enable_nestloop";
+ break;
+ case 'm': /* mergejoin */
+ tmp = "enable_mergejoin";
+ break;
+ case 'h': /* hashjoin */
+ tmp = "enable_hashjoin";
+ break;
+ }
+ if (tmp)
+ {
+ SetConfigOption(tmp, "false", context, source);
+ return true;
+ }
+ else
+ return false;
+}
+
+
+const char *
+get_stats_option_name(const char *arg)
+{
+ switch (arg[0])
+ {
+ case 'p':
+ if (optarg[1] == 'a') /* "parser" */
+ return "log_parser_stats";
+ else if (optarg[1] == 'l') /* "planner" */
+ return "log_planner_stats";
+ break;
+
+ case 'e': /* "executor" */
+ return "log_executor_stats";
+ break;
+ }
+
+ return NULL;
+}
+
+
+/* ----------------------------------------------------------------
+ * process_postgres_switches
+ * Parse command line arguments for backends
+ *
+ * This is called twice, once for the "secure" options coming from the
+ * postmaster or command line, and once for the "insecure" options coming
+ * from the client's startup packet. The latter have the same syntax but
+ * may be restricted in what they can do.
+ *
+ * argv[0] is ignored in either case (it's assumed to be the program name).
+ *
+ * ctx is PGC_POSTMASTER for secure options, PGC_BACKEND for insecure options
+ * coming from the client, or PGC_SU_BACKEND for insecure options coming from
+ * a superuser client.
+ *
+ * If a database name is present in the command line arguments, it's
+ * returned into *dbname (this is allowed only if *dbname is initially NULL).
+ * ----------------------------------------------------------------
+ */
+void
+process_postgres_switches(int argc, char *argv[], GucContext ctx,
+ const char **dbname)
+{
+ bool secure = (ctx == PGC_POSTMASTER);
+ int errs = 0;
+ GucSource gucsource;
+ int flag;
+
+ if (secure)
+ {
+ gucsource = PGC_S_ARGV; /* switches came from command line */
+
+ /* Ignore the initial --single argument, if present */
+ if (argc > 1 && strcmp(argv[1], "--single") == 0)
+ {
+ argv++;
+ argc--;
+ }
+ }
+ else
+ {
+ gucsource = PGC_S_CLIENT; /* switches came from client */
+ }
+
+#ifdef HAVE_INT_OPTERR
+
+ /*
+ * Turn this off because it's either printed to stderr and not the log
+ * where we'd want it, or argv[0] is now "--single", which would make for
+ * a weird error message. We print our own error message below.
+ */
+ opterr = 0;
+#endif
+
+ /*
+ * Parse command-line options. CAUTION: keep this in sync with
+ * postmaster/postmaster.c (the option sets should not conflict) and with
+ * the common help() function in main/main.c.
+ */
+ while ((flag = getopt(argc, argv, "B:bc:C:D:d:EeFf:h:ijk:lN:nOPp:r:S:sTt:v:W:-:")) != -1)
+ {
+ switch (flag)
+ {
+ case 'B':
+ SetConfigOption("shared_buffers", optarg, ctx, gucsource);
+ break;
+
+ case 'b':
+ /* Undocumented flag used for binary upgrades */
+ if (secure)
+ IsBinaryUpgrade = true;
+ break;
+
+ case 'C':
+ /* ignored for consistency with the postmaster */
+ break;
+
+ case 'D':
+ if (secure)
+ userDoption = strdup(optarg);
+ break;
+
+ case 'd':
+ set_debug_options(atoi(optarg), ctx, gucsource);
+ break;
+
+ case 'E':
+ if (secure)
+ EchoQuery = true;
+ break;
+
+ case 'e':
+ SetConfigOption("datestyle", "euro", ctx, gucsource);
+ break;
+
+ case 'F':
+ SetConfigOption("fsync", "false", ctx, gucsource);
+ break;
+
+ case 'f':
+ if (!set_plan_disabling_options(optarg, ctx, gucsource))
+ errs++;
+ break;
+
+ case 'h':
+ SetConfigOption("listen_addresses", optarg, ctx, gucsource);
+ break;
+
+ case 'i':
+ SetConfigOption("listen_addresses", "*", ctx, gucsource);
+ break;
+
+ case 'j':
+ if (secure)
+ UseSemiNewlineNewline = true;
+ break;
+
+ case 'k':
+ SetConfigOption("unix_socket_directories", optarg, ctx, gucsource);
+ break;
+
+ case 'l':
+ SetConfigOption("ssl", "true", ctx, gucsource);
+ break;
+
+ case 'N':
+ SetConfigOption("max_connections", optarg, ctx, gucsource);
+ break;
+
+ case 'n':
+ /* ignored for consistency with postmaster */
+ break;
+
+ case 'O':
+ SetConfigOption("allow_system_table_mods", "true", ctx, gucsource);
+ break;
+
+ case 'P':
+ SetConfigOption("ignore_system_indexes", "true", ctx, gucsource);
+ break;
+
+ case 'p':
+ SetConfigOption("port", optarg, ctx, gucsource);
+ break;
+
+ case 'r':
+ /* send output (stdout and stderr) to the given file */
+ if (secure)
+ strlcpy(OutputFileName, optarg, MAXPGPATH);
+ break;
+
+ case 'S':
+ SetConfigOption("work_mem", optarg, ctx, gucsource);
+ break;
+
+ case 's':
+ SetConfigOption("log_statement_stats", "true", ctx, gucsource);
+ break;
+
+ case 'T':
+ /* ignored for consistency with the postmaster */
+ break;
+
+ case 't':
+ {
+ const char *tmp = get_stats_option_name(optarg);
+
+ if (tmp)
+ SetConfigOption(tmp, "true", ctx, gucsource);
+ else
+ errs++;
+ break;
+ }
+
+ case 'v':
+
+ /*
+ * -v is no longer used in normal operation, since
+ * FrontendProtocol is already set before we get here. We keep
+ * the switch only for possible use in standalone operation,
+ * in case we ever support using normal FE/BE protocol with a
+ * standalone backend.
+ */
+ if (secure)
+ FrontendProtocol = (ProtocolVersion) atoi(optarg);
+ break;
+
+ case 'W':
+ SetConfigOption("post_auth_delay", optarg, ctx, gucsource);
+ break;
+
+ case 'c':
+ case '-':
+ {
+ char *name,
+ *value;
+
+ ParseLongOption(optarg, &name, &value);
+ if (!value)
+ {
+ if (flag == '-')
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("--%s requires a value",
+ optarg)));
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("-c %s requires a value",
+ optarg)));
+ }
+ SetConfigOption(name, value, ctx, gucsource);
+ free(name);
+ if (value)
+ free(value);
+ break;
+ }
+
+ default:
+ errs++;
+ break;
+ }
+
+ if (errs)
+ break;
+ }
+
+ /*
+ * Optional database name should be there only if *dbname is NULL.
+ */
+ if (!errs && dbname && *dbname == NULL && argc - optind >= 1)
+ *dbname = strdup(argv[optind++]);
+
+ if (errs || argc != optind)
+ {
+ if (errs)
+ optind--; /* complain about the previous argument */
+
+ /* spell the error message a bit differently depending on context */
+ if (IsUnderPostmaster)
+ ereport(FATAL,
+ errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("invalid command-line argument for server process: %s", argv[optind]),
+ errhint("Try \"%s --help\" for more information.", progname));
+ else
+ ereport(FATAL,
+ errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("%s: invalid command-line argument: %s",
+ progname, argv[optind]),
+ errhint("Try \"%s --help\" for more information.", progname));
+ }
+
+ /*
+ * Reset getopt(3) library so that it will work correctly in subprocesses
+ * or when this function is called a second time with another array.
+ */
+ optind = 1;
+#ifdef HAVE_INT_OPTRESET
+ optreset = 1; /* some systems need this too */
+#endif
+}
+
+
+/*
+ * PostgresSingleUserMain
+ * Entry point for single user mode. argc/argv are the command line
+ * arguments to be used.
+ *
+ * Performs single user specific setup then calls PostgresMain() to actually
+ * process queries. Single user mode specific setup should go here, rather
+ * than PostgresMain() or InitPostgres() when reasonably possible.
+ */
+void
+PostgresSingleUserMain(int argc, char *argv[],
+ const char *username)
+{
+ const char *dbname = NULL;
+
+ Assert(!IsUnderPostmaster);
+
+ /* Initialize startup process environment. */
+ InitStandaloneProcess(argv[0]);
+
+ /*
+ * Set default values for command-line options.
+ */
+ InitializeGUCOptions();
+
+ /*
+ * Parse command-line options.
+ */
+ process_postgres_switches(argc, argv, PGC_POSTMASTER, &dbname);
+
+ /* Must have gotten a database name, or have a default (the username) */
+ if (dbname == NULL)
+ {
+ dbname = username;
+ if (dbname == NULL)
+ ereport(FATAL,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("%s: no database nor user name specified",
+ progname)));
+ }
+
+ /* Acquire configuration parameters */
+ if (!SelectConfigFiles(userDoption, progname))
+ proc_exit(1);
+
+ /*
+ * Validate we have been given a reasonable-looking DataDir and change
+ * into it.
+ */
+ checkDataDir();
+ ChangeToDataDir();
+
+ /*
+ * Create lockfile for data directory.
+ */
+ CreateDataDirLockFile(false);
+
+ /* read control file (error checking and contains config ) */
+ LocalProcessControlFile(false);
+
+ /*
+ * process any libraries that should be preloaded at postmaster start
+ */
+ process_shared_preload_libraries();
+
+ /* Initialize MaxBackends */
+ InitializeMaxBackends();
+
+ /*
+ * Give preloaded libraries a chance to request additional shared memory.
+ */
+ process_shmem_requests();
+
+ /*
+ * Now that loadable modules have had their chance to request additional
+ * shared memory, determine the value of any runtime-computed GUCs that
+ * depend on the amount of shared memory required.
+ */
+ InitializeShmemGUCs();
+
+ /*
+ * Now that modules have been loaded, we can process any custom resource
+ * managers specified in the wal_consistency_checking GUC.
+ */
+ InitializeWalConsistencyChecking();
+
+ CreateSharedMemoryAndSemaphores();
+
+ /*
+ * Remember stand-alone backend startup time,roughly at the same point
+ * during startup that postmaster does so.
+ */
+ PgStartTime = GetCurrentTimestamp();
+
+ /*
+ * Create a per-backend PGPROC struct in shared memory. We must do this
+ * before we can use LWLocks.
+ */
+ InitProcess();
+
+ /*
+ * Now that sufficient infrastructure has been initialized, PostgresMain()
+ * can do the rest.
+ */
+ PostgresMain(dbname, username);
+}
+
+
+/* ----------------------------------------------------------------
+ * PostgresMain
+ * postgres main loop -- all backends, interactive or otherwise loop here
+ *
+ * dbname is the name of the database to connect to, username is the
+ * PostgreSQL user name to be used for the session.
+ *
+ * NB: Single user mode specific setup should go to PostgresSingleUserMain()
+ * if reasonably possible.
+ * ----------------------------------------------------------------
+ */
+void
+PostgresMain(const char *dbname, const char *username)
+{
+ sigjmp_buf local_sigjmp_buf;
+
+ /* these must be volatile to ensure state is preserved across longjmp: */
+ volatile bool send_ready_for_query = true;
+ volatile bool idle_in_transaction_timeout_enabled = false;
+ volatile bool idle_session_timeout_enabled = false;
+
+ AssertArg(dbname != NULL);
+ AssertArg(username != NULL);
+
+ SetProcessingMode(InitProcessing);
+
+ /*
+ * Set up signal handlers. (InitPostmasterChild or InitStandaloneProcess
+ * has already set up BlockSig and made that the active signal mask.)
+ *
+ * Note that postmaster blocked all signals before forking child process,
+ * so there is no race condition whereby we might receive a signal before
+ * we have set up the handler.
+ *
+ * Also note: it's best not to use any signals that are SIG_IGNored in the
+ * postmaster. If such a signal arrives before we are able to change the
+ * handler to non-SIG_IGN, it'll get dropped. Instead, make a dummy
+ * handler in the postmaster to reserve the signal. (Of course, this isn't
+ * an issue for signals that are locally generated, such as SIGALRM and
+ * SIGPIPE.)
+ */
+ if (am_walsender)
+ WalSndSignals();
+ else
+ {
+ pqsignal(SIGHUP, SignalHandlerForConfigReload);
+ pqsignal(SIGINT, StatementCancelHandler); /* cancel current query */
+ pqsignal(SIGTERM, die); /* cancel current query and exit */
+
+ /*
+ * In a postmaster child backend, replace SignalHandlerForCrashExit
+ * with quickdie, so we can tell the client we're dying.
+ *
+ * In a standalone backend, SIGQUIT can be generated from the keyboard
+ * easily, while SIGTERM cannot, so we make both signals do die()
+ * rather than quickdie().
+ */
+ if (IsUnderPostmaster)
+ pqsignal(SIGQUIT, quickdie); /* hard crash time */
+ else
+ pqsignal(SIGQUIT, die); /* cancel current query and exit */
+ InitializeTimeouts(); /* establishes SIGALRM handler */
+
+ /*
+ * Ignore failure to write to frontend. Note: if frontend closes
+ * connection, we will notice it and exit cleanly when control next
+ * returns to outer loop. This seems safer than forcing exit in the
+ * midst of output during who-knows-what operation...
+ */
+ pqsignal(SIGPIPE, SIG_IGN);
+ pqsignal(SIGUSR1, procsignal_sigusr1_handler);
+ pqsignal(SIGUSR2, SIG_IGN);
+ pqsignal(SIGFPE, FloatExceptionHandler);
+
+ /*
+ * Reset some signals that are accepted by postmaster but not by
+ * backend
+ */
+ pqsignal(SIGCHLD, SIG_DFL); /* system() requires this on some
+ * platforms */
+ }
+
+ /* Early initialization */
+ BaseInit();
+
+ /* We need to allow SIGINT, etc during the initial transaction */
+ PG_SETMASK(&UnBlockSig);
+
+ /*
+ * General initialization.
+ *
+ * NOTE: if you are tempted to add code in this vicinity, consider putting
+ * it inside InitPostgres() instead. In particular, anything that
+ * involves database access should be there, not here.
+ */
+ InitPostgres(dbname, InvalidOid, /* database to connect to */
+ username, InvalidOid, /* role to connect as */
+ !am_walsender, /* honor session_preload_libraries? */
+ false, /* don't ignore datallowconn */
+ NULL); /* no out_dbname */
+
+ /*
+ * If the PostmasterContext is still around, recycle the space; we don't
+ * need it anymore after InitPostgres completes. Note this does not trash
+ * *MyProcPort, because ConnCreate() allocated that space with malloc()
+ * ... else we'd need to copy the Port data first. Also, subsidiary data
+ * such as the username isn't lost either; see ProcessStartupPacket().
+ */
+ if (PostmasterContext)
+ {
+ MemoryContextDelete(PostmasterContext);
+ PostmasterContext = NULL;
+ }
+
+ SetProcessingMode(NormalProcessing);
+
+ /*
+ * Now all GUC states are fully set up. Report them to client if
+ * appropriate.
+ */
+ BeginReportingGUCOptions();
+
+ /*
+ * Also set up handler to log session end; we have to wait till now to be
+ * sure Log_disconnections has its final value.
+ */
+ if (IsUnderPostmaster && Log_disconnections)
+ on_proc_exit(log_disconnections, 0);
+
+ pgstat_report_connect(MyDatabaseId);
+
+ /* Perform initialization specific to a WAL sender process. */
+ if (am_walsender)
+ InitWalSender();
+
+ /*
+ * Send this backend's cancellation info to the frontend.
+ */
+ if (whereToSendOutput == DestRemote)
+ {
+ StringInfoData buf;
+
+ pq_beginmessage(&buf, 'K');
+ pq_sendint32(&buf, (int32) MyProcPid);
+ pq_sendint32(&buf, (int32) MyCancelKey);
+ pq_endmessage(&buf);
+ /* Need not flush since ReadyForQuery will do it. */
+ }
+
+ /* Welcome banner for standalone case */
+ if (whereToSendOutput == DestDebug)
+ printf("\nPostgreSQL stand-alone backend %s\n", PG_VERSION);
+
+ /*
+ * Create the memory context we will use in the main loop.
+ *
+ * MessageContext is reset once per iteration of the main loop, ie, upon
+ * completion of processing of each command message from the client.
+ */
+ MessageContext = AllocSetContextCreate(TopMemoryContext,
+ "MessageContext",
+ ALLOCSET_DEFAULT_SIZES);
+
+ /*
+ * Create memory context and buffer used for RowDescription messages. As
+ * SendRowDescriptionMessage(), via exec_describe_statement_message(), is
+ * frequently executed for ever single statement, we don't want to
+ * allocate a separate buffer every time.
+ */
+ row_description_context = AllocSetContextCreate(TopMemoryContext,
+ "RowDescriptionContext",
+ ALLOCSET_DEFAULT_SIZES);
+ MemoryContextSwitchTo(row_description_context);
+ initStringInfo(&row_description_buf);
+ MemoryContextSwitchTo(TopMemoryContext);
+
+ /*
+ * POSTGRES main processing loop begins here
+ *
+ * If an exception is encountered, processing resumes here so we abort the
+ * current transaction and start a new one.
+ *
+ * You might wonder why this isn't coded as an infinite loop around a
+ * PG_TRY construct. The reason is that this is the bottom of the
+ * exception stack, and so with PG_TRY there would be no exception handler
+ * in force at all during the CATCH part. By leaving the outermost setjmp
+ * always active, we have at least some chance of recovering from an error
+ * during error recovery. (If we get into an infinite loop thereby, it
+ * will soon be stopped by overflow of elog.c's internal state stack.)
+ *
+ * Note that we use sigsetjmp(..., 1), so that this function's signal mask
+ * (to wit, UnBlockSig) will be restored when longjmp'ing to here. This
+ * is essential in case we longjmp'd out of a signal handler on a platform
+ * where that leaves the signal blocked. It's not redundant with the
+ * unblock in AbortTransaction() because the latter is only called if we
+ * were inside a transaction.
+ */
+
+ if (sigsetjmp(local_sigjmp_buf, 1) != 0)
+ {
+ /*
+ * NOTE: if you are tempted to add more code in this if-block,
+ * consider the high probability that it should be in
+ * AbortTransaction() instead. The only stuff done directly here
+ * should be stuff that is guaranteed to apply *only* for outer-level
+ * error recovery, such as adjusting the FE/BE protocol status.
+ */
+
+ /* Since not using PG_TRY, must reset error stack by hand */
+ error_context_stack = NULL;
+
+ /* Prevent interrupts while cleaning up */
+ HOLD_INTERRUPTS();
+
+ /*
+ * Forget any pending QueryCancel request, since we're returning to
+ * the idle loop anyway, and cancel any active timeout requests. (In
+ * future we might want to allow some timeout requests to survive, but
+ * at minimum it'd be necessary to do reschedule_timeouts(), in case
+ * we got here because of a query cancel interrupting the SIGALRM
+ * interrupt handler.) Note in particular that we must clear the
+ * statement and lock timeout indicators, to prevent any future plain
+ * query cancels from being misreported as timeouts in case we're
+ * forgetting a timeout cancel.
+ */
+ disable_all_timeouts(false); /* do first to avoid race condition */
+ QueryCancelPending = false;
+ idle_in_transaction_timeout_enabled = false;
+ idle_session_timeout_enabled = false;
+
+ /* Not reading from the client anymore. */
+ DoingCommandRead = false;
+
+ /* Make sure libpq is in a good state */
+ pq_comm_reset();
+
+ /* Report the error to the client and/or server log */
+ EmitErrorReport();
+
+ /*
+ * Make sure debug_query_string gets reset before we possibly clobber
+ * the storage it points at.
+ */
+ debug_query_string = NULL;
+
+ /*
+ * Abort the current transaction in order to recover.
+ */
+ AbortCurrentTransaction();
+
+ if (am_walsender)
+ WalSndErrorCleanup();
+
+ PortalErrorCleanup();
+
+ /*
+ * We can't release replication slots inside AbortTransaction() as we
+ * need to be able to start and abort transactions while having a slot
+ * acquired. But we never need to hold them across top level errors,
+ * so releasing here is fine. There also is a before_shmem_exit()
+ * callback ensuring correct cleanup on FATAL errors.
+ */
+ if (MyReplicationSlot != NULL)
+ ReplicationSlotRelease();
+
+ /* We also want to cleanup temporary slots on error. */
+ ReplicationSlotCleanup();
+
+ jit_reset_after_error();
+
+ /*
+ * Now return to normal top-level context and clear ErrorContext for
+ * next time.
+ */
+ MemoryContextSwitchTo(TopMemoryContext);
+ FlushErrorState();
+
+ /*
+ * If we were handling an extended-query-protocol message, initiate
+ * skip till next Sync. This also causes us not to issue
+ * ReadyForQuery (until we get Sync).
+ */
+ if (doing_extended_query_message)
+ ignore_till_sync = true;
+
+ /* We don't have a transaction command open anymore */
+ xact_started = false;
+
+ /*
+ * If an error occurred while we were reading a message from the
+ * client, we have potentially lost track of where the previous
+ * message ends and the next one begins. Even though we have
+ * otherwise recovered from the error, we cannot safely read any more
+ * messages from the client, so there isn't much we can do with the
+ * connection anymore.
+ */
+ if (pq_is_reading_msg())
+ ereport(FATAL,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("terminating connection because protocol synchronization was lost")));
+
+ /* Now we can allow interrupts again */
+ RESUME_INTERRUPTS();
+ }
+
+ /* We can now handle ereport(ERROR) */
+ PG_exception_stack = &local_sigjmp_buf;
+
+ if (!ignore_till_sync)
+ send_ready_for_query = true; /* initially, or after error */
+
+ /*
+ * Non-error queries loop here.
+ */
+
+ for (;;)
+ {
+ int firstchar;
+ StringInfoData input_message;
+
+ /*
+ * At top of loop, reset extended-query-message flag, so that any
+ * errors encountered in "idle" state don't provoke skip.
+ */
+ doing_extended_query_message = false;
+
+ /*
+ * Release storage left over from prior query cycle, and create a new
+ * query input buffer in the cleared MessageContext.
+ */
+ MemoryContextSwitchTo(MessageContext);
+ MemoryContextResetAndDeleteChildren(MessageContext);
+
+ initStringInfo(&input_message);
+
+ /*
+ * Also consider releasing our catalog snapshot if any, so that it's
+ * not preventing advance of global xmin while we wait for the client.
+ */
+ InvalidateCatalogSnapshotConditionally();
+
+ /*
+ * (1) If we've reached idle state, tell the frontend we're ready for
+ * a new query.
+ *
+ * Note: this includes fflush()'ing the last of the prior output.
+ *
+ * This is also a good time to flush out collected statistics to the
+ * cumulative stats system, and to update the PS stats display. We
+ * avoid doing those every time through the message loop because it'd
+ * slow down processing of batched messages, and because we don't want
+ * to report uncommitted updates (that confuses autovacuum). The
+ * notification processor wants a call too, if we are not in a
+ * transaction block.
+ *
+ * Also, if an idle timeout is enabled, start the timer for that.
+ */
+ if (send_ready_for_query)
+ {
+ if (IsAbortedTransactionBlockState())
+ {
+ set_ps_display("idle in transaction (aborted)");
+ pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL);
+
+ /* Start the idle-in-transaction timer */
+ if (IdleInTransactionSessionTimeout > 0)
+ {
+ idle_in_transaction_timeout_enabled = true;
+ enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
+ IdleInTransactionSessionTimeout);
+ }
+ }
+ else if (IsTransactionOrTransactionBlock())
+ {
+ set_ps_display("idle in transaction");
+ pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL);
+
+ /* Start the idle-in-transaction timer */
+ if (IdleInTransactionSessionTimeout > 0)
+ {
+ idle_in_transaction_timeout_enabled = true;
+ enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
+ IdleInTransactionSessionTimeout);
+ }
+ }
+ else
+ {
+ long stats_timeout;
+
+ /*
+ * Process incoming notifies (including self-notifies), if
+ * any, and send relevant messages to the client. Doing it
+ * here helps ensure stable behavior in tests: if any notifies
+ * were received during the just-finished transaction, they'll
+ * be seen by the client before ReadyForQuery is.
+ */
+ if (notifyInterruptPending)
+ ProcessNotifyInterrupt(false);
+
+ /*
+ * Check if we need to report stats. If pgstat_report_stat()
+ * decides it's too soon to flush out pending stats / lock
+ * contention prevented reporting, it'll tell us when we
+ * should try to report stats again (so that stats updates
+ * aren't unduly delayed if the connection goes idle for a
+ * long time). We only enable the timeout if we don't already
+ * have a timeout in progress, because we don't disable the
+ * timeout below. enable_timeout_after() needs to determine
+ * the current timestamp, which can have a negative
+ * performance impact. That's OK because pgstat_report_stat()
+ * won't have us wake up sooner than a prior call.
+ */
+ stats_timeout = pgstat_report_stat(false);
+ if (stats_timeout > 0)
+ {
+ if (!get_timeout_active(IDLE_STATS_UPDATE_TIMEOUT))
+ enable_timeout_after(IDLE_STATS_UPDATE_TIMEOUT,
+ stats_timeout);
+ }
+ else
+ {
+ /* all stats flushed, no need for the timeout */
+ if (get_timeout_active(IDLE_STATS_UPDATE_TIMEOUT))
+ disable_timeout(IDLE_STATS_UPDATE_TIMEOUT, false);
+ }
+
+ set_ps_display("idle");
+ pgstat_report_activity(STATE_IDLE, NULL);
+
+ /* Start the idle-session timer */
+ if (IdleSessionTimeout > 0)
+ {
+ idle_session_timeout_enabled = true;
+ enable_timeout_after(IDLE_SESSION_TIMEOUT,
+ IdleSessionTimeout);
+ }
+ }
+
+ /* Report any recently-changed GUC options */
+ ReportChangedGUCOptions();
+
+ ReadyForQuery(whereToSendOutput);
+ send_ready_for_query = false;
+ }
+
+ /*
+ * (2) Allow asynchronous signals to be executed immediately if they
+ * come in while we are waiting for client input. (This must be
+ * conditional since we don't want, say, reads on behalf of COPY FROM
+ * STDIN doing the same thing.)
+ */
+ DoingCommandRead = true;
+
+ /*
+ * (3) read a command (loop blocks here)
+ */
+ firstchar = ReadCommand(&input_message);
+
+ /*
+ * (4) turn off the idle-in-transaction and idle-session timeouts if
+ * active. We do this before step (5) so that any last-moment timeout
+ * is certain to be detected in step (5).
+ *
+ * At most one of these timeouts will be active, so there's no need to
+ * worry about combining the timeout.c calls into one.
+ */
+ if (idle_in_transaction_timeout_enabled)
+ {
+ disable_timeout(IDLE_IN_TRANSACTION_SESSION_TIMEOUT, false);
+ idle_in_transaction_timeout_enabled = false;
+ }
+ if (idle_session_timeout_enabled)
+ {
+ disable_timeout(IDLE_SESSION_TIMEOUT, false);
+ idle_session_timeout_enabled = false;
+ }
+
+ /*
+ * (5) disable async signal conditions again.
+ *
+ * Query cancel is supposed to be a no-op when there is no query in
+ * progress, so if a query cancel arrived while we were idle, just
+ * reset QueryCancelPending. ProcessInterrupts() has that effect when
+ * it's called when DoingCommandRead is set, so check for interrupts
+ * before resetting DoingCommandRead.
+ */
+ CHECK_FOR_INTERRUPTS();
+ DoingCommandRead = false;
+
+ /*
+ * (6) check for any other interesting events that happened while we
+ * slept.
+ */
+ if (ConfigReloadPending)
+ {
+ ConfigReloadPending = false;
+ ProcessConfigFile(PGC_SIGHUP);
+ }
+
+ /*
+ * (7) process the command. But ignore it if we're skipping till
+ * Sync.
+ */
+ if (ignore_till_sync && firstchar != EOF)
+ continue;
+
+ switch (firstchar)
+ {
+ case 'Q': /* simple query */
+ {
+ const char *query_string;
+
+ /* Set statement_timestamp() */
+ SetCurrentStatementStartTimestamp();
+
+ query_string = pq_getmsgstring(&input_message);
+ pq_getmsgend(&input_message);
+
+ if (am_walsender)
+ {
+ if (!exec_replication_command(query_string))
+ exec_simple_query(query_string);
+ }
+ else
+ exec_simple_query(query_string);
+
+ send_ready_for_query = true;
+ }
+ break;
+
+ case 'P': /* parse */
+ {
+ const char *stmt_name;
+ const char *query_string;
+ int numParams;
+ Oid *paramTypes = NULL;
+
+ forbidden_in_wal_sender(firstchar);
+
+ /* Set statement_timestamp() */
+ SetCurrentStatementStartTimestamp();
+
+ stmt_name = pq_getmsgstring(&input_message);
+ query_string = pq_getmsgstring(&input_message);
+ numParams = pq_getmsgint(&input_message, 2);
+ if (numParams > 0)
+ {
+ paramTypes = (Oid *) palloc(numParams * sizeof(Oid));
+ for (int i = 0; i < numParams; i++)
+ paramTypes[i] = pq_getmsgint(&input_message, 4);
+ }
+ pq_getmsgend(&input_message);
+
+ exec_parse_message(query_string, stmt_name,
+ paramTypes, numParams);
+ }
+ break;
+
+ case 'B': /* bind */
+ forbidden_in_wal_sender(firstchar);
+
+ /* Set statement_timestamp() */
+ SetCurrentStatementStartTimestamp();
+
+ /*
+ * this message is complex enough that it seems best to put
+ * the field extraction out-of-line
+ */
+ exec_bind_message(&input_message);
+ break;
+
+ case 'E': /* execute */
+ {
+ const char *portal_name;
+ int max_rows;
+
+ forbidden_in_wal_sender(firstchar);
+
+ /* Set statement_timestamp() */
+ SetCurrentStatementStartTimestamp();
+
+ portal_name = pq_getmsgstring(&input_message);
+ max_rows = pq_getmsgint(&input_message, 4);
+ pq_getmsgend(&input_message);
+
+ exec_execute_message(portal_name, max_rows);
+ }
+ break;
+
+ case 'F': /* fastpath function call */
+ forbidden_in_wal_sender(firstchar);
+
+ /* Set statement_timestamp() */
+ SetCurrentStatementStartTimestamp();
+
+ /* Report query to various monitoring facilities. */
+ pgstat_report_activity(STATE_FASTPATH, NULL);
+ set_ps_display("<FASTPATH>");
+
+ /* start an xact for this function invocation */
+ start_xact_command();
+
+ /*
+ * Note: we may at this point be inside an aborted
+ * transaction. We can't throw error for that until we've
+ * finished reading the function-call message, so
+ * HandleFunctionRequest() must check for it after doing so.
+ * Be careful not to do anything that assumes we're inside a
+ * valid transaction here.
+ */
+
+ /* switch back to message context */
+ MemoryContextSwitchTo(MessageContext);
+
+ HandleFunctionRequest(&input_message);
+
+ /* commit the function-invocation transaction */
+ finish_xact_command();
+
+ send_ready_for_query = true;
+ break;
+
+ case 'C': /* close */
+ {
+ int close_type;
+ const char *close_target;
+
+ forbidden_in_wal_sender(firstchar);
+
+ close_type = pq_getmsgbyte(&input_message);
+ close_target = pq_getmsgstring(&input_message);
+ pq_getmsgend(&input_message);
+
+ switch (close_type)
+ {
+ case 'S':
+ if (close_target[0] != '\0')
+ DropPreparedStatement(close_target, false);
+ else
+ {
+ /* special-case the unnamed statement */
+ drop_unnamed_stmt();
+ }
+ break;
+ case 'P':
+ {
+ Portal portal;
+
+ portal = GetPortalByName(close_target);
+ if (PortalIsValid(portal))
+ PortalDrop(portal, false);
+ }
+ break;
+ default:
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("invalid CLOSE message subtype %d",
+ close_type)));
+ break;
+ }
+
+ if (whereToSendOutput == DestRemote)
+ pq_putemptymessage('3'); /* CloseComplete */
+ }
+ break;
+
+ case 'D': /* describe */
+ {
+ int describe_type;
+ const char *describe_target;
+
+ forbidden_in_wal_sender(firstchar);
+
+ /* Set statement_timestamp() (needed for xact) */
+ SetCurrentStatementStartTimestamp();
+
+ describe_type = pq_getmsgbyte(&input_message);
+ describe_target = pq_getmsgstring(&input_message);
+ pq_getmsgend(&input_message);
+
+ switch (describe_type)
+ {
+ case 'S':
+ exec_describe_statement_message(describe_target);
+ break;
+ case 'P':
+ exec_describe_portal_message(describe_target);
+ break;
+ default:
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("invalid DESCRIBE message subtype %d",
+ describe_type)));
+ break;
+ }
+ }
+ break;
+
+ case 'H': /* flush */
+ pq_getmsgend(&input_message);
+ if (whereToSendOutput == DestRemote)
+ pq_flush();
+ break;
+
+ case 'S': /* sync */
+ pq_getmsgend(&input_message);
+ finish_xact_command();
+ send_ready_for_query = true;
+ break;
+
+ /*
+ * 'X' means that the frontend is closing down the socket. EOF
+ * means unexpected loss of frontend connection. Either way,
+ * perform normal shutdown.
+ */
+ case EOF:
+
+ /* for the cumulative statistics system */
+ pgStatSessionEndCause = DISCONNECT_CLIENT_EOF;
+
+ /* FALLTHROUGH */
+
+ case 'X':
+
+ /*
+ * Reset whereToSendOutput to prevent ereport from attempting
+ * to send any more messages to client.
+ */
+ if (whereToSendOutput == DestRemote)
+ whereToSendOutput = DestNone;
+
+ /*
+ * NOTE: if you are tempted to add more code here, DON'T!
+ * Whatever you had in mind to do should be set up as an
+ * on_proc_exit or on_shmem_exit callback, instead. Otherwise
+ * it will fail to be called during other backend-shutdown
+ * scenarios.
+ */
+ proc_exit(0);
+
+ case 'd': /* copy data */
+ case 'c': /* copy done */
+ case 'f': /* copy fail */
+
+ /*
+ * Accept but ignore these messages, per protocol spec; we
+ * probably got here because a COPY failed, and the frontend
+ * is still sending data.
+ */
+ break;
+
+ default:
+ ereport(FATAL,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("invalid frontend message type %d",
+ firstchar)));
+ }
+ } /* end of input-reading loop */
+}
+
+/*
+ * Throw an error if we're a WAL sender process.
+ *
+ * This is used to forbid anything else than simple query protocol messages
+ * in a WAL sender process. 'firstchar' specifies what kind of a forbidden
+ * message was received, and is used to construct the error message.
+ */
+static void
+forbidden_in_wal_sender(char firstchar)
+{
+ if (am_walsender)
+ {
+ if (firstchar == 'F')
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("fastpath function calls not supported in a replication connection")));
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("extended query protocol not supported in a replication connection")));
+ }
+}
+
+
+/*
+ * Obtain platform stack depth limit (in bytes)
+ *
+ * Return -1 if unknown
+ */
+long
+get_stack_depth_rlimit(void)
+{
+#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_STACK)
+ static long val = 0;
+
+ /* This won't change after process launch, so check just once */
+ if (val == 0)
+ {
+ struct rlimit rlim;
+
+ if (getrlimit(RLIMIT_STACK, &rlim) < 0)
+ val = -1;
+ else if (rlim.rlim_cur == RLIM_INFINITY)
+ val = LONG_MAX;
+ /* rlim_cur is probably of an unsigned type, so check for overflow */
+ else if (rlim.rlim_cur >= LONG_MAX)
+ val = LONG_MAX;
+ else
+ val = rlim.rlim_cur;
+ }
+ return val;
+#else /* no getrlimit */
+#if defined(WIN32) || defined(__CYGWIN__)
+ /* On Windows we set the backend stack size in src/backend/Makefile */
+ return WIN32_STACK_RLIMIT;
+#else /* not windows ... give up */
+ return -1;
+#endif
+#endif
+}
+
+
+static struct rusage Save_r;
+static struct timeval Save_t;
+
+void
+ResetUsage(void)
+{
+ getrusage(RUSAGE_SELF, &Save_r);
+ gettimeofday(&Save_t, NULL);
+}
+
+void
+ShowUsage(const char *title)
+{
+ StringInfoData str;
+ struct timeval user,
+ sys;
+ struct timeval elapse_t;
+ struct rusage r;
+
+ getrusage(RUSAGE_SELF, &r);
+ gettimeofday(&elapse_t, NULL);
+ memcpy((char *) &user, (char *) &r.ru_utime, sizeof(user));
+ memcpy((char *) &sys, (char *) &r.ru_stime, sizeof(sys));
+ if (elapse_t.tv_usec < Save_t.tv_usec)
+ {
+ elapse_t.tv_sec--;
+ elapse_t.tv_usec += 1000000;
+ }
+ if (r.ru_utime.tv_usec < Save_r.ru_utime.tv_usec)
+ {
+ r.ru_utime.tv_sec--;
+ r.ru_utime.tv_usec += 1000000;
+ }
+ if (r.ru_stime.tv_usec < Save_r.ru_stime.tv_usec)
+ {
+ r.ru_stime.tv_sec--;
+ r.ru_stime.tv_usec += 1000000;
+ }
+
+ /*
+ * The only stats we don't show here are ixrss, idrss, isrss. It takes
+ * some work to interpret them, and most platforms don't fill them in.
+ */
+ initStringInfo(&str);
+
+ appendStringInfoString(&str, "! system usage stats:\n");
+ appendStringInfo(&str,
+ "!\t%ld.%06ld s user, %ld.%06ld s system, %ld.%06ld s elapsed\n",
+ (long) (r.ru_utime.tv_sec - Save_r.ru_utime.tv_sec),
+ (long) (r.ru_utime.tv_usec - Save_r.ru_utime.tv_usec),
+ (long) (r.ru_stime.tv_sec - Save_r.ru_stime.tv_sec),
+ (long) (r.ru_stime.tv_usec - Save_r.ru_stime.tv_usec),
+ (long) (elapse_t.tv_sec - Save_t.tv_sec),
+ (long) (elapse_t.tv_usec - Save_t.tv_usec));
+ appendStringInfo(&str,
+ "!\t[%ld.%06ld s user, %ld.%06ld s system total]\n",
+ (long) user.tv_sec,
+ (long) user.tv_usec,
+ (long) sys.tv_sec,
+ (long) sys.tv_usec);
+#if defined(HAVE_GETRUSAGE)
+ appendStringInfo(&str,
+ "!\t%ld kB max resident size\n",
+#if defined(__darwin__)
+ /* in bytes on macOS */
+ r.ru_maxrss / 1024
+#else
+ /* in kilobytes on most other platforms */
+ r.ru_maxrss
+#endif
+ );
+ appendStringInfo(&str,
+ "!\t%ld/%ld [%ld/%ld] filesystem blocks in/out\n",
+ r.ru_inblock - Save_r.ru_inblock,
+ /* they only drink coffee at dec */
+ r.ru_oublock - Save_r.ru_oublock,
+ r.ru_inblock, r.ru_oublock);
+ appendStringInfo(&str,
+ "!\t%ld/%ld [%ld/%ld] page faults/reclaims, %ld [%ld] swaps\n",
+ r.ru_majflt - Save_r.ru_majflt,
+ r.ru_minflt - Save_r.ru_minflt,
+ r.ru_majflt, r.ru_minflt,
+ r.ru_nswap - Save_r.ru_nswap,
+ r.ru_nswap);
+ appendStringInfo(&str,
+ "!\t%ld [%ld] signals rcvd, %ld/%ld [%ld/%ld] messages rcvd/sent\n",
+ r.ru_nsignals - Save_r.ru_nsignals,
+ r.ru_nsignals,
+ r.ru_msgrcv - Save_r.ru_msgrcv,
+ r.ru_msgsnd - Save_r.ru_msgsnd,
+ r.ru_msgrcv, r.ru_msgsnd);
+ appendStringInfo(&str,
+ "!\t%ld/%ld [%ld/%ld] voluntary/involuntary context switches\n",
+ r.ru_nvcsw - Save_r.ru_nvcsw,
+ r.ru_nivcsw - Save_r.ru_nivcsw,
+ r.ru_nvcsw, r.ru_nivcsw);
+#endif /* HAVE_GETRUSAGE */
+
+ /* remove trailing newline */
+ if (str.data[str.len - 1] == '\n')
+ str.data[--str.len] = '\0';
+
+ ereport(LOG,
+ (errmsg_internal("%s", title),
+ errdetail_internal("%s", str.data)));
+
+ pfree(str.data);
+}
+
+/*
+ * on_proc_exit handler to log end of session
+ */
+static void
+log_disconnections(int code, Datum arg)
+{
+ Port *port = MyProcPort;
+ long secs;
+ int usecs;
+ int msecs;
+ int hours,
+ minutes,
+ seconds;
+
+ TimestampDifference(MyStartTimestamp,
+ GetCurrentTimestamp(),
+ &secs, &usecs);
+ msecs = usecs / 1000;
+
+ hours = secs / SECS_PER_HOUR;
+ secs %= SECS_PER_HOUR;
+ minutes = secs / SECS_PER_MINUTE;
+ seconds = secs % SECS_PER_MINUTE;
+
+ ereport(LOG,
+ (errmsg("disconnection: session time: %d:%02d:%02d.%03d "
+ "user=%s database=%s host=%s%s%s",
+ hours, minutes, seconds, msecs,
+ port->user_name, port->database_name, port->remote_host,
+ port->remote_port[0] ? " port=" : "", port->remote_port)));
+}
+
+/*
+ * Start statement timeout timer, if enabled.
+ *
+ * If there's already a timeout running, don't restart the timer. That
+ * enables compromises between accuracy of timeouts and cost of starting a
+ * timeout.
+ */
+static void
+enable_statement_timeout(void)
+{
+ /* must be within an xact */
+ Assert(xact_started);
+
+ if (StatementTimeout > 0)
+ {
+ if (!get_timeout_active(STATEMENT_TIMEOUT))
+ enable_timeout_after(STATEMENT_TIMEOUT, StatementTimeout);
+ }
+ else
+ {
+ if (get_timeout_active(STATEMENT_TIMEOUT))
+ disable_timeout(STATEMENT_TIMEOUT, false);
+ }
+}
+
+/*
+ * Disable statement timeout, if active.
+ */
+static void
+disable_statement_timeout(void)
+{
+ if (get_timeout_active(STATEMENT_TIMEOUT))
+ disable_timeout(STATEMENT_TIMEOUT, false);
+}
diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c
new file mode 100644
index 0000000..5aa5a35
--- /dev/null
+++ b/src/backend/tcop/pquery.c
@@ -0,0 +1,1807 @@
+/*-------------------------------------------------------------------------
+ *
+ * pquery.c
+ * POSTGRES process query command code
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/tcop/pquery.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include <limits.h>
+
+#include "access/xact.h"
+#include "commands/prepare.h"
+#include "executor/tstoreReceiver.h"
+#include "miscadmin.h"
+#include "pg_trace.h"
+#include "tcop/pquery.h"
+#include "tcop/utility.h"
+#include "utils/memutils.h"
+#include "utils/snapmgr.h"
+
+
+/*
+ * ActivePortal is the currently executing Portal (the most closely nested,
+ * if there are several).
+ */
+Portal ActivePortal = NULL;
+
+
+static void ProcessQuery(PlannedStmt *plan,
+ const char *sourceText,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ DestReceiver *dest,
+ QueryCompletion *qc);
+static void FillPortalStore(Portal portal, bool isTopLevel);
+static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count,
+ DestReceiver *dest);
+static uint64 PortalRunSelect(Portal portal, bool forward, long count,
+ DestReceiver *dest);
+static void PortalRunUtility(Portal portal, PlannedStmt *pstmt,
+ bool isTopLevel, bool setHoldSnapshot,
+ DestReceiver *dest, QueryCompletion *qc);
+static void PortalRunMulti(Portal portal,
+ bool isTopLevel, bool setHoldSnapshot,
+ DestReceiver *dest, DestReceiver *altdest,
+ QueryCompletion *qc);
+static uint64 DoPortalRunFetch(Portal portal,
+ FetchDirection fdirection,
+ long count,
+ DestReceiver *dest);
+static void DoPortalRewind(Portal portal);
+
+
+/*
+ * CreateQueryDesc
+ */
+QueryDesc *
+CreateQueryDesc(PlannedStmt *plannedstmt,
+ const char *sourceText,
+ Snapshot snapshot,
+ Snapshot crosscheck_snapshot,
+ DestReceiver *dest,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ int instrument_options)
+{
+ QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
+
+ qd->operation = plannedstmt->commandType; /* operation */
+ qd->plannedstmt = plannedstmt; /* plan */
+ qd->sourceText = sourceText; /* query text */
+ qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
+ /* RI check snapshot */
+ qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
+ qd->dest = dest; /* output dest */
+ qd->params = params; /* parameter values passed into query */
+ qd->queryEnv = queryEnv;
+ qd->instrument_options = instrument_options; /* instrumentation wanted? */
+
+ /* null these fields until set by ExecutorStart */
+ qd->tupDesc = NULL;
+ qd->estate = NULL;
+ qd->planstate = NULL;
+ qd->totaltime = NULL;
+
+ /* not yet executed */
+ qd->already_executed = false;
+
+ return qd;
+}
+
+/*
+ * FreeQueryDesc
+ */
+void
+FreeQueryDesc(QueryDesc *qdesc)
+{
+ /* Can't be a live query */
+ Assert(qdesc->estate == NULL);
+
+ /* forget our snapshots */
+ UnregisterSnapshot(qdesc->snapshot);
+ UnregisterSnapshot(qdesc->crosscheck_snapshot);
+
+ /* Only the QueryDesc itself need be freed */
+ pfree(qdesc);
+}
+
+
+/*
+ * ProcessQuery
+ * Execute a single plannable query within a PORTAL_MULTI_QUERY,
+ * PORTAL_ONE_RETURNING, or PORTAL_ONE_MOD_WITH portal
+ *
+ * plan: the plan tree for the query
+ * sourceText: the source text of the query
+ * params: any parameters needed
+ * dest: where to send results
+ * qc: where to store the command completion status data.
+ *
+ * qc may be NULL if caller doesn't want a status string.
+ *
+ * Must be called in a memory context that will be reset or deleted on
+ * error; otherwise the executor's memory usage will be leaked.
+ */
+static void
+ProcessQuery(PlannedStmt *plan,
+ const char *sourceText,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ DestReceiver *dest,
+ QueryCompletion *qc)
+{
+ QueryDesc *queryDesc;
+
+ /*
+ * Create the QueryDesc object
+ */
+ queryDesc = CreateQueryDesc(plan, sourceText,
+ GetActiveSnapshot(), InvalidSnapshot,
+ dest, params, queryEnv, 0);
+
+ /*
+ * Call ExecutorStart to prepare the plan for execution
+ */
+ ExecutorStart(queryDesc, 0);
+
+ /*
+ * Run the plan to completion.
+ */
+ ExecutorRun(queryDesc, ForwardScanDirection, 0L, true);
+
+ /*
+ * Build command completion status data, if caller wants one.
+ */
+ if (qc)
+ {
+ switch (queryDesc->operation)
+ {
+ case CMD_SELECT:
+ SetQueryCompletion(qc, CMDTAG_SELECT, queryDesc->estate->es_processed);
+ break;
+ case CMD_INSERT:
+ SetQueryCompletion(qc, CMDTAG_INSERT, queryDesc->estate->es_processed);
+ break;
+ case CMD_UPDATE:
+ SetQueryCompletion(qc, CMDTAG_UPDATE, queryDesc->estate->es_processed);
+ break;
+ case CMD_DELETE:
+ SetQueryCompletion(qc, CMDTAG_DELETE, queryDesc->estate->es_processed);
+ break;
+ case CMD_MERGE:
+ SetQueryCompletion(qc, CMDTAG_MERGE, queryDesc->estate->es_processed);
+ break;
+ default:
+ SetQueryCompletion(qc, CMDTAG_UNKNOWN, queryDesc->estate->es_processed);
+ break;
+ }
+ }
+
+ /*
+ * Now, we close down all the scans and free allocated resources.
+ */
+ ExecutorFinish(queryDesc);
+ ExecutorEnd(queryDesc);
+
+ FreeQueryDesc(queryDesc);
+}
+
+/*
+ * ChoosePortalStrategy
+ * Select portal execution strategy given the intended statement list.
+ *
+ * The list elements can be Querys or PlannedStmts.
+ * That's more general than portals need, but plancache.c uses this too.
+ *
+ * See the comments in portal.h.
+ */
+PortalStrategy
+ChoosePortalStrategy(List *stmts)
+{
+ int nSetTag;
+ ListCell *lc;
+
+ /*
+ * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
+ * single-statement case, since there are no rewrite rules that can add
+ * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
+ * likewise allows only one top-level statement.
+ */
+ if (list_length(stmts) == 1)
+ {
+ Node *stmt = (Node *) linitial(stmts);
+
+ if (IsA(stmt, Query))
+ {
+ Query *query = (Query *) stmt;
+
+ if (query->canSetTag)
+ {
+ if (query->commandType == CMD_SELECT)
+ {
+ if (query->hasModifyingCTE)
+ return PORTAL_ONE_MOD_WITH;
+ else
+ return PORTAL_ONE_SELECT;
+ }
+ if (query->commandType == CMD_UTILITY)
+ {
+ if (UtilityReturnsTuples(query->utilityStmt))
+ return PORTAL_UTIL_SELECT;
+ /* it can't be ONE_RETURNING, so give up */
+ return PORTAL_MULTI_QUERY;
+ }
+ }
+ }
+ else if (IsA(stmt, PlannedStmt))
+ {
+ PlannedStmt *pstmt = (PlannedStmt *) stmt;
+
+ if (pstmt->canSetTag)
+ {
+ if (pstmt->commandType == CMD_SELECT)
+ {
+ if (pstmt->hasModifyingCTE)
+ return PORTAL_ONE_MOD_WITH;
+ else
+ return PORTAL_ONE_SELECT;
+ }
+ if (pstmt->commandType == CMD_UTILITY)
+ {
+ if (UtilityReturnsTuples(pstmt->utilityStmt))
+ return PORTAL_UTIL_SELECT;
+ /* it can't be ONE_RETURNING, so give up */
+ return PORTAL_MULTI_QUERY;
+ }
+ }
+ }
+ else
+ elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
+ }
+
+ /*
+ * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
+ * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
+ * it has a RETURNING list.
+ */
+ nSetTag = 0;
+ foreach(lc, stmts)
+ {
+ Node *stmt = (Node *) lfirst(lc);
+
+ if (IsA(stmt, Query))
+ {
+ Query *query = (Query *) stmt;
+
+ if (query->canSetTag)
+ {
+ if (++nSetTag > 1)
+ return PORTAL_MULTI_QUERY; /* no need to look further */
+ if (query->commandType == CMD_UTILITY ||
+ query->returningList == NIL)
+ return PORTAL_MULTI_QUERY; /* no need to look further */
+ }
+ }
+ else if (IsA(stmt, PlannedStmt))
+ {
+ PlannedStmt *pstmt = (PlannedStmt *) stmt;
+
+ if (pstmt->canSetTag)
+ {
+ if (++nSetTag > 1)
+ return PORTAL_MULTI_QUERY; /* no need to look further */
+ if (pstmt->commandType == CMD_UTILITY ||
+ !pstmt->hasReturning)
+ return PORTAL_MULTI_QUERY; /* no need to look further */
+ }
+ }
+ else
+ elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
+ }
+ if (nSetTag == 1)
+ return PORTAL_ONE_RETURNING;
+
+ /* Else, it's the general case... */
+ return PORTAL_MULTI_QUERY;
+}
+
+/*
+ * FetchPortalTargetList
+ * Given a portal that returns tuples, extract the query targetlist.
+ * Returns NIL if the portal doesn't have a determinable targetlist.
+ *
+ * Note: do not modify the result.
+ */
+List *
+FetchPortalTargetList(Portal portal)
+{
+ /* no point in looking if we determined it doesn't return tuples */
+ if (portal->strategy == PORTAL_MULTI_QUERY)
+ return NIL;
+ /* get the primary statement and find out what it returns */
+ return FetchStatementTargetList((Node *) PortalGetPrimaryStmt(portal));
+}
+
+/*
+ * FetchStatementTargetList
+ * Given a statement that returns tuples, extract the query targetlist.
+ * Returns NIL if the statement doesn't have a determinable targetlist.
+ *
+ * This can be applied to a Query or a PlannedStmt.
+ * That's more general than portals need, but plancache.c uses this too.
+ *
+ * Note: do not modify the result.
+ *
+ * XXX be careful to keep this in sync with UtilityReturnsTuples.
+ */
+List *
+FetchStatementTargetList(Node *stmt)
+{
+ if (stmt == NULL)
+ return NIL;
+ if (IsA(stmt, Query))
+ {
+ Query *query = (Query *) stmt;
+
+ if (query->commandType == CMD_UTILITY)
+ {
+ /* transfer attention to utility statement */
+ stmt = query->utilityStmt;
+ }
+ else
+ {
+ if (query->commandType == CMD_SELECT)
+ return query->targetList;
+ if (query->returningList)
+ return query->returningList;
+ return NIL;
+ }
+ }
+ if (IsA(stmt, PlannedStmt))
+ {
+ PlannedStmt *pstmt = (PlannedStmt *) stmt;
+
+ if (pstmt->commandType == CMD_UTILITY)
+ {
+ /* transfer attention to utility statement */
+ stmt = pstmt->utilityStmt;
+ }
+ else
+ {
+ if (pstmt->commandType == CMD_SELECT)
+ return pstmt->planTree->targetlist;
+ if (pstmt->hasReturning)
+ return pstmt->planTree->targetlist;
+ return NIL;
+ }
+ }
+ if (IsA(stmt, FetchStmt))
+ {
+ FetchStmt *fstmt = (FetchStmt *) stmt;
+ Portal subportal;
+
+ Assert(!fstmt->ismove);
+ subportal = GetPortalByName(fstmt->portalname);
+ Assert(PortalIsValid(subportal));
+ return FetchPortalTargetList(subportal);
+ }
+ if (IsA(stmt, ExecuteStmt))
+ {
+ ExecuteStmt *estmt = (ExecuteStmt *) stmt;
+ PreparedStatement *entry;
+
+ entry = FetchPreparedStatement(estmt->name, true);
+ return FetchPreparedStatementTargetList(entry);
+ }
+ return NIL;
+}
+
+/*
+ * PortalStart
+ * Prepare a portal for execution.
+ *
+ * Caller must already have created the portal, done PortalDefineQuery(),
+ * and adjusted portal options if needed.
+ *
+ * If parameters are needed by the query, they must be passed in "params"
+ * (caller is responsible for giving them appropriate lifetime).
+ *
+ * The caller can also provide an initial set of "eflags" to be passed to
+ * ExecutorStart (but note these can be modified internally, and they are
+ * currently only honored for PORTAL_ONE_SELECT portals). Most callers
+ * should simply pass zero.
+ *
+ * The caller can optionally pass a snapshot to be used; pass InvalidSnapshot
+ * for the normal behavior of setting a new snapshot. This parameter is
+ * presently ignored for non-PORTAL_ONE_SELECT portals (it's only intended
+ * to be used for cursors).
+ *
+ * On return, portal is ready to accept PortalRun() calls, and the result
+ * tupdesc (if any) is known.
+ */
+void
+PortalStart(Portal portal, ParamListInfo params,
+ int eflags, Snapshot snapshot)
+{
+ Portal saveActivePortal;
+ ResourceOwner saveResourceOwner;
+ MemoryContext savePortalContext;
+ MemoryContext oldContext;
+ QueryDesc *queryDesc;
+ int myeflags;
+
+ AssertArg(PortalIsValid(portal));
+ AssertState(portal->status == PORTAL_DEFINED);
+
+ /*
+ * Set up global portal context pointers.
+ */
+ saveActivePortal = ActivePortal;
+ saveResourceOwner = CurrentResourceOwner;
+ savePortalContext = PortalContext;
+ PG_TRY();
+ {
+ ActivePortal = portal;
+ if (portal->resowner)
+ CurrentResourceOwner = portal->resowner;
+ PortalContext = portal->portalContext;
+
+ oldContext = MemoryContextSwitchTo(PortalContext);
+
+ /* Must remember portal param list, if any */
+ portal->portalParams = params;
+
+ /*
+ * Determine the portal execution strategy
+ */
+ portal->strategy = ChoosePortalStrategy(portal->stmts);
+
+ /*
+ * Fire her up according to the strategy
+ */
+ switch (portal->strategy)
+ {
+ case PORTAL_ONE_SELECT:
+
+ /* Must set snapshot before starting executor. */
+ if (snapshot)
+ PushActiveSnapshot(snapshot);
+ else
+ PushActiveSnapshot(GetTransactionSnapshot());
+
+ /*
+ * We could remember the snapshot in portal->portalSnapshot,
+ * but presently there seems no need to, as this code path
+ * cannot be used for non-atomic execution. Hence there can't
+ * be any commit/abort that might destroy the snapshot. Since
+ * we don't do that, there's also no need to force a
+ * non-default nesting level for the snapshot.
+ */
+
+ /*
+ * Create QueryDesc in portal's context; for the moment, set
+ * the destination to DestNone.
+ */
+ queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
+ portal->sourceText,
+ GetActiveSnapshot(),
+ InvalidSnapshot,
+ None_Receiver,
+ params,
+ portal->queryEnv,
+ 0);
+
+ /*
+ * If it's a scrollable cursor, executor needs to support
+ * REWIND and backwards scan, as well as whatever the caller
+ * might've asked for.
+ */
+ if (portal->cursorOptions & CURSOR_OPT_SCROLL)
+ myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
+ else
+ myeflags = eflags;
+
+ /*
+ * Call ExecutorStart to prepare the plan for execution
+ */
+ ExecutorStart(queryDesc, myeflags);
+
+ /*
+ * This tells PortalCleanup to shut down the executor
+ */
+ portal->queryDesc = queryDesc;
+
+ /*
+ * Remember tuple descriptor (computed by ExecutorStart)
+ */
+ portal->tupDesc = queryDesc->tupDesc;
+
+ /*
+ * Reset cursor position data to "start of query"
+ */
+ portal->atStart = true;
+ portal->atEnd = false; /* allow fetches */
+ portal->portalPos = 0;
+
+ PopActiveSnapshot();
+ break;
+
+ case PORTAL_ONE_RETURNING:
+ case PORTAL_ONE_MOD_WITH:
+
+ /*
+ * We don't start the executor until we are told to run the
+ * portal. We do need to set up the result tupdesc.
+ */
+ {
+ PlannedStmt *pstmt;
+
+ pstmt = PortalGetPrimaryStmt(portal);
+ portal->tupDesc =
+ ExecCleanTypeFromTL(pstmt->planTree->targetlist);
+ }
+
+ /*
+ * Reset cursor position data to "start of query"
+ */
+ portal->atStart = true;
+ portal->atEnd = false; /* allow fetches */
+ portal->portalPos = 0;
+ break;
+
+ case PORTAL_UTIL_SELECT:
+
+ /*
+ * We don't set snapshot here, because PortalRunUtility will
+ * take care of it if needed.
+ */
+ {
+ PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
+
+ Assert(pstmt->commandType == CMD_UTILITY);
+ portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
+ }
+
+ /*
+ * Reset cursor position data to "start of query"
+ */
+ portal->atStart = true;
+ portal->atEnd = false; /* allow fetches */
+ portal->portalPos = 0;
+ break;
+
+ case PORTAL_MULTI_QUERY:
+ /* Need do nothing now */
+ portal->tupDesc = NULL;
+ break;
+ }
+ }
+ PG_CATCH();
+ {
+ /* Uncaught error while executing portal: mark it dead */
+ MarkPortalFailed(portal);
+
+ /* Restore global vars and propagate error */
+ ActivePortal = saveActivePortal;
+ CurrentResourceOwner = saveResourceOwner;
+ PortalContext = savePortalContext;
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ MemoryContextSwitchTo(oldContext);
+
+ ActivePortal = saveActivePortal;
+ CurrentResourceOwner = saveResourceOwner;
+ PortalContext = savePortalContext;
+
+ portal->status = PORTAL_READY;
+}
+
+/*
+ * PortalSetResultFormat
+ * Select the format codes for a portal's output.
+ *
+ * This must be run after PortalStart for a portal that will be read by
+ * a DestRemote or DestRemoteExecute destination. It is not presently needed
+ * for other destination types.
+ *
+ * formats[] is the client format request, as per Bind message conventions.
+ */
+void
+PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
+{
+ int natts;
+ int i;
+
+ /* Do nothing if portal won't return tuples */
+ if (portal->tupDesc == NULL)
+ return;
+ natts = portal->tupDesc->natts;
+ portal->formats = (int16 *)
+ MemoryContextAlloc(portal->portalContext,
+ natts * sizeof(int16));
+ if (nFormats > 1)
+ {
+ /* format specified for each column */
+ if (nFormats != natts)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("bind message has %d result formats but query has %d columns",
+ nFormats, natts)));
+ memcpy(portal->formats, formats, natts * sizeof(int16));
+ }
+ else if (nFormats > 0)
+ {
+ /* single format specified, use for all columns */
+ int16 format1 = formats[0];
+
+ for (i = 0; i < natts; i++)
+ portal->formats[i] = format1;
+ }
+ else
+ {
+ /* use default format for all columns */
+ for (i = 0; i < natts; i++)
+ portal->formats[i] = 0;
+ }
+}
+
+/*
+ * PortalRun
+ * Run a portal's query or queries.
+ *
+ * count <= 0 is interpreted as a no-op: the destination gets started up
+ * and shut down, but nothing else happens. Also, count == FETCH_ALL is
+ * interpreted as "all rows". Note that count is ignored in multi-query
+ * situations, where we always run the portal to completion.
+ *
+ * isTopLevel: true if query is being executed at backend "top level"
+ * (that is, directly from a client command message)
+ *
+ * dest: where to send output of primary (canSetTag) query
+ *
+ * altdest: where to send output of non-primary queries
+ *
+ * qc: where to store command completion status data.
+ * May be NULL if caller doesn't want status data.
+ *
+ * Returns true if the portal's execution is complete, false if it was
+ * suspended due to exhaustion of the count parameter.
+ */
+bool
+PortalRun(Portal portal, long count, bool isTopLevel, bool run_once,
+ DestReceiver *dest, DestReceiver *altdest,
+ QueryCompletion *qc)
+{
+ bool result;
+ uint64 nprocessed;
+ ResourceOwner saveTopTransactionResourceOwner;
+ MemoryContext saveTopTransactionContext;
+ Portal saveActivePortal;
+ ResourceOwner saveResourceOwner;
+ MemoryContext savePortalContext;
+ MemoryContext saveMemoryContext;
+
+ AssertArg(PortalIsValid(portal));
+
+ TRACE_POSTGRESQL_QUERY_EXECUTE_START();
+
+ /* Initialize empty completion data */
+ if (qc)
+ InitializeQueryCompletion(qc);
+
+ if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
+ {
+ elog(DEBUG3, "PortalRun");
+ /* PORTAL_MULTI_QUERY logs its own stats per query */
+ ResetUsage();
+ }
+
+ /*
+ * Check for improper portal use, and mark portal active.
+ */
+ MarkPortalActive(portal);
+
+ /* Set run_once flag. Shouldn't be clear if previously set. */
+ Assert(!portal->run_once || run_once);
+ portal->run_once = run_once;
+
+ /*
+ * Set up global portal context pointers.
+ *
+ * We have to play a special game here to support utility commands like
+ * VACUUM and CLUSTER, which internally start and commit transactions.
+ * When we are called to execute such a command, CurrentResourceOwner will
+ * be pointing to the TopTransactionResourceOwner --- which will be
+ * destroyed and replaced in the course of the internal commit and
+ * restart. So we need to be prepared to restore it as pointing to the
+ * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
+ * internally starting whole new transactions is not good.)
+ * CurrentMemoryContext has a similar problem, but the other pointers we
+ * save here will be NULL or pointing to longer-lived objects.
+ */
+ saveTopTransactionResourceOwner = TopTransactionResourceOwner;
+ saveTopTransactionContext = TopTransactionContext;
+ saveActivePortal = ActivePortal;
+ saveResourceOwner = CurrentResourceOwner;
+ savePortalContext = PortalContext;
+ saveMemoryContext = CurrentMemoryContext;
+ PG_TRY();
+ {
+ ActivePortal = portal;
+ if (portal->resowner)
+ CurrentResourceOwner = portal->resowner;
+ PortalContext = portal->portalContext;
+
+ MemoryContextSwitchTo(PortalContext);
+
+ switch (portal->strategy)
+ {
+ case PORTAL_ONE_SELECT:
+ case PORTAL_ONE_RETURNING:
+ case PORTAL_ONE_MOD_WITH:
+ case PORTAL_UTIL_SELECT:
+
+ /*
+ * If we have not yet run the command, do so, storing its
+ * results in the portal's tuplestore. But we don't do that
+ * for the PORTAL_ONE_SELECT case.
+ */
+ if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
+ FillPortalStore(portal, isTopLevel);
+
+ /*
+ * Now fetch desired portion of results.
+ */
+ nprocessed = PortalRunSelect(portal, true, count, dest);
+
+ /*
+ * If the portal result contains a command tag and the caller
+ * gave us a pointer to store it, copy it and update the
+ * rowcount.
+ */
+ if (qc && portal->qc.commandTag != CMDTAG_UNKNOWN)
+ {
+ CopyQueryCompletion(qc, &portal->qc);
+ qc->nprocessed = nprocessed;
+ }
+
+ /* Mark portal not active */
+ portal->status = PORTAL_READY;
+
+ /*
+ * Since it's a forward fetch, say DONE iff atEnd is now true.
+ */
+ result = portal->atEnd;
+ break;
+
+ case PORTAL_MULTI_QUERY:
+ PortalRunMulti(portal, isTopLevel, false,
+ dest, altdest, qc);
+
+ /* Prevent portal's commands from being re-executed */
+ MarkPortalDone(portal);
+
+ /* Always complete at end of RunMulti */
+ result = true;
+ break;
+
+ default:
+ elog(ERROR, "unrecognized portal strategy: %d",
+ (int) portal->strategy);
+ result = false; /* keep compiler quiet */
+ break;
+ }
+ }
+ PG_CATCH();
+ {
+ /* Uncaught error while executing portal: mark it dead */
+ MarkPortalFailed(portal);
+
+ /* Restore global vars and propagate error */
+ if (saveMemoryContext == saveTopTransactionContext)
+ MemoryContextSwitchTo(TopTransactionContext);
+ else
+ MemoryContextSwitchTo(saveMemoryContext);
+ ActivePortal = saveActivePortal;
+ if (saveResourceOwner == saveTopTransactionResourceOwner)
+ CurrentResourceOwner = TopTransactionResourceOwner;
+ else
+ CurrentResourceOwner = saveResourceOwner;
+ PortalContext = savePortalContext;
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (saveMemoryContext == saveTopTransactionContext)
+ MemoryContextSwitchTo(TopTransactionContext);
+ else
+ MemoryContextSwitchTo(saveMemoryContext);
+ ActivePortal = saveActivePortal;
+ if (saveResourceOwner == saveTopTransactionResourceOwner)
+ CurrentResourceOwner = TopTransactionResourceOwner;
+ else
+ CurrentResourceOwner = saveResourceOwner;
+ PortalContext = savePortalContext;
+
+ if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
+ ShowUsage("EXECUTOR STATISTICS");
+
+ TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
+
+ return result;
+}
+
+/*
+ * PortalRunSelect
+ * Execute a portal's query in PORTAL_ONE_SELECT mode, and also
+ * when fetching from a completed holdStore in PORTAL_ONE_RETURNING,
+ * PORTAL_ONE_MOD_WITH, and PORTAL_UTIL_SELECT cases.
+ *
+ * This handles simple N-rows-forward-or-backward cases. For more complex
+ * nonsequential access to a portal, see PortalRunFetch.
+ *
+ * count <= 0 is interpreted as a no-op: the destination gets started up
+ * and shut down, but nothing else happens. Also, count == FETCH_ALL is
+ * interpreted as "all rows". (cf FetchStmt.howMany)
+ *
+ * Caller must already have validated the Portal and done appropriate
+ * setup (cf. PortalRun).
+ *
+ * Returns number of rows processed (suitable for use in result tag)
+ */
+static uint64
+PortalRunSelect(Portal portal,
+ bool forward,
+ long count,
+ DestReceiver *dest)
+{
+ QueryDesc *queryDesc;
+ ScanDirection direction;
+ uint64 nprocessed;
+
+ /*
+ * NB: queryDesc will be NULL if we are fetching from a held cursor or a
+ * completed utility query; can't use it in that path.
+ */
+ queryDesc = portal->queryDesc;
+
+ /* Caller messed up if we have neither a ready query nor held data. */
+ Assert(queryDesc || portal->holdStore);
+
+ /*
+ * Force the queryDesc destination to the right thing. This supports
+ * MOVE, for example, which will pass in dest = DestNone. This is okay to
+ * change as long as we do it on every fetch. (The Executor must not
+ * assume that dest never changes.)
+ */
+ if (queryDesc)
+ queryDesc->dest = dest;
+
+ /*
+ * Determine which direction to go in, and check to see if we're already
+ * at the end of the available tuples in that direction. If so, set the
+ * direction to NoMovement to avoid trying to fetch any tuples. (This
+ * check exists because not all plan node types are robust about being
+ * called again if they've already returned NULL once.) Then call the
+ * executor (we must not skip this, because the destination needs to see a
+ * setup and shutdown even if no tuples are available). Finally, update
+ * the portal position state depending on the number of tuples that were
+ * retrieved.
+ */
+ if (forward)
+ {
+ if (portal->atEnd || count <= 0)
+ {
+ direction = NoMovementScanDirection;
+ count = 0; /* don't pass negative count to executor */
+ }
+ else
+ direction = ForwardScanDirection;
+
+ /* In the executor, zero count processes all rows */
+ if (count == FETCH_ALL)
+ count = 0;
+
+ if (portal->holdStore)
+ nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
+ else
+ {
+ PushActiveSnapshot(queryDesc->snapshot);
+ ExecutorRun(queryDesc, direction, (uint64) count,
+ portal->run_once);
+ nprocessed = queryDesc->estate->es_processed;
+ PopActiveSnapshot();
+ }
+
+ if (!ScanDirectionIsNoMovement(direction))
+ {
+ if (nprocessed > 0)
+ portal->atStart = false; /* OK to go backward now */
+ if (count == 0 || nprocessed < (uint64) count)
+ portal->atEnd = true; /* we retrieved 'em all */
+ portal->portalPos += nprocessed;
+ }
+ }
+ else
+ {
+ if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("cursor can only scan forward"),
+ errhint("Declare it with SCROLL option to enable backward scan.")));
+
+ if (portal->atStart || count <= 0)
+ {
+ direction = NoMovementScanDirection;
+ count = 0; /* don't pass negative count to executor */
+ }
+ else
+ direction = BackwardScanDirection;
+
+ /* In the executor, zero count processes all rows */
+ if (count == FETCH_ALL)
+ count = 0;
+
+ if (portal->holdStore)
+ nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
+ else
+ {
+ PushActiveSnapshot(queryDesc->snapshot);
+ ExecutorRun(queryDesc, direction, (uint64) count,
+ portal->run_once);
+ nprocessed = queryDesc->estate->es_processed;
+ PopActiveSnapshot();
+ }
+
+ if (!ScanDirectionIsNoMovement(direction))
+ {
+ if (nprocessed > 0 && portal->atEnd)
+ {
+ portal->atEnd = false; /* OK to go forward now */
+ portal->portalPos++; /* adjust for endpoint case */
+ }
+ if (count == 0 || nprocessed < (uint64) count)
+ {
+ portal->atStart = true; /* we retrieved 'em all */
+ portal->portalPos = 0;
+ }
+ else
+ {
+ portal->portalPos -= nprocessed;
+ }
+ }
+ }
+
+ return nprocessed;
+}
+
+/*
+ * FillPortalStore
+ * Run the query and load result tuples into the portal's tuple store.
+ *
+ * This is used for PORTAL_ONE_RETURNING, PORTAL_ONE_MOD_WITH, and
+ * PORTAL_UTIL_SELECT cases only.
+ */
+static void
+FillPortalStore(Portal portal, bool isTopLevel)
+{
+ DestReceiver *treceiver;
+ QueryCompletion qc;
+
+ InitializeQueryCompletion(&qc);
+ PortalCreateHoldStore(portal);
+ treceiver = CreateDestReceiver(DestTuplestore);
+ SetTuplestoreDestReceiverParams(treceiver,
+ portal->holdStore,
+ portal->holdContext,
+ false,
+ NULL,
+ NULL);
+
+ switch (portal->strategy)
+ {
+ case PORTAL_ONE_RETURNING:
+ case PORTAL_ONE_MOD_WITH:
+
+ /*
+ * Run the portal to completion just as for the default
+ * PORTAL_MULTI_QUERY case, but send the primary query's output to
+ * the tuplestore. Auxiliary query outputs are discarded. Set the
+ * portal's holdSnapshot to the snapshot used (or a copy of it).
+ */
+ PortalRunMulti(portal, isTopLevel, true,
+ treceiver, None_Receiver, &qc);
+ break;
+
+ case PORTAL_UTIL_SELECT:
+ PortalRunUtility(portal, linitial_node(PlannedStmt, portal->stmts),
+ isTopLevel, true, treceiver, &qc);
+ break;
+
+ default:
+ elog(ERROR, "unsupported portal strategy: %d",
+ (int) portal->strategy);
+ break;
+ }
+
+ /* Override portal completion data with actual command results */
+ if (qc.commandTag != CMDTAG_UNKNOWN)
+ CopyQueryCompletion(&portal->qc, &qc);
+
+ treceiver->rDestroy(treceiver);
+}
+
+/*
+ * RunFromStore
+ * Fetch tuples from the portal's tuple store.
+ *
+ * Calling conventions are similar to ExecutorRun, except that we
+ * do not depend on having a queryDesc or estate. Therefore we return the
+ * number of tuples processed as the result, not in estate->es_processed.
+ *
+ * One difference from ExecutorRun is that the destination receiver functions
+ * are run in the caller's memory context (since we have no estate). Watch
+ * out for memory leaks.
+ */
+static uint64
+RunFromStore(Portal portal, ScanDirection direction, uint64 count,
+ DestReceiver *dest)
+{
+ uint64 current_tuple_count = 0;
+ TupleTableSlot *slot;
+
+ slot = MakeSingleTupleTableSlot(portal->tupDesc, &TTSOpsMinimalTuple);
+
+ dest->rStartup(dest, CMD_SELECT, portal->tupDesc);
+
+ if (ScanDirectionIsNoMovement(direction))
+ {
+ /* do nothing except start/stop the destination */
+ }
+ else
+ {
+ bool forward = ScanDirectionIsForward(direction);
+
+ for (;;)
+ {
+ MemoryContext oldcontext;
+ bool ok;
+
+ oldcontext = MemoryContextSwitchTo(portal->holdContext);
+
+ ok = tuplestore_gettupleslot(portal->holdStore, forward, false,
+ slot);
+
+ MemoryContextSwitchTo(oldcontext);
+
+ if (!ok)
+ break;
+
+ /*
+ * If we are not able to send the tuple, we assume the destination
+ * has closed and no more tuples can be sent. If that's the case,
+ * end the loop.
+ */
+ if (!dest->receiveSlot(slot, dest))
+ break;
+
+ ExecClearTuple(slot);
+
+ /*
+ * check our tuple count.. if we've processed the proper number
+ * then quit, else loop again and process more tuples. Zero count
+ * means no limit.
+ */
+ current_tuple_count++;
+ if (count && count == current_tuple_count)
+ break;
+ }
+ }
+
+ dest->rShutdown(dest);
+
+ ExecDropSingleTupleTableSlot(slot);
+
+ return current_tuple_count;
+}
+
+/*
+ * PortalRunUtility
+ * Execute a utility statement inside a portal.
+ */
+static void
+PortalRunUtility(Portal portal, PlannedStmt *pstmt,
+ bool isTopLevel, bool setHoldSnapshot,
+ DestReceiver *dest, QueryCompletion *qc)
+{
+ /*
+ * Set snapshot if utility stmt needs one.
+ */
+ if (PlannedStmtRequiresSnapshot(pstmt))
+ {
+ Snapshot snapshot = GetTransactionSnapshot();
+
+ /* If told to, register the snapshot we're using and save in portal */
+ if (setHoldSnapshot)
+ {
+ snapshot = RegisterSnapshot(snapshot);
+ portal->holdSnapshot = snapshot;
+ }
+
+ /*
+ * In any case, make the snapshot active and remember it in portal.
+ * Because the portal now references the snapshot, we must tell
+ * snapmgr.c that the snapshot belongs to the portal's transaction
+ * level, else we risk portalSnapshot becoming a dangling pointer.
+ */
+ PushActiveSnapshotWithLevel(snapshot, portal->createLevel);
+ /* PushActiveSnapshotWithLevel might have copied the snapshot */
+ portal->portalSnapshot = GetActiveSnapshot();
+ }
+ else
+ portal->portalSnapshot = NULL;
+
+ ProcessUtility(pstmt,
+ portal->sourceText,
+ (portal->cplan != NULL), /* protect tree if in plancache */
+ isTopLevel ? PROCESS_UTILITY_TOPLEVEL : PROCESS_UTILITY_QUERY,
+ portal->portalParams,
+ portal->queryEnv,
+ dest,
+ qc);
+
+ /* Some utility statements may change context on us */
+ MemoryContextSwitchTo(portal->portalContext);
+
+ /*
+ * Some utility commands (e.g., VACUUM) pop the ActiveSnapshot stack from
+ * under us, so don't complain if it's now empty. Otherwise, our snapshot
+ * should be the top one; pop it. Note that this could be a different
+ * snapshot from the one we made above; see EnsurePortalSnapshotExists.
+ */
+ if (portal->portalSnapshot != NULL && ActiveSnapshotSet())
+ {
+ Assert(portal->portalSnapshot == GetActiveSnapshot());
+ PopActiveSnapshot();
+ }
+ portal->portalSnapshot = NULL;
+}
+
+/*
+ * PortalRunMulti
+ * Execute a portal's queries in the general case (multi queries
+ * or non-SELECT-like queries)
+ */
+static void
+PortalRunMulti(Portal portal,
+ bool isTopLevel, bool setHoldSnapshot,
+ DestReceiver *dest, DestReceiver *altdest,
+ QueryCompletion *qc)
+{
+ bool active_snapshot_set = false;
+ ListCell *stmtlist_item;
+
+ /*
+ * If the destination is DestRemoteExecute, change to DestNone. The
+ * reason is that the client won't be expecting any tuples, and indeed has
+ * no way to know what they are, since there is no provision for Describe
+ * to send a RowDescription message when this portal execution strategy is
+ * in effect. This presently will only affect SELECT commands added to
+ * non-SELECT queries by rewrite rules: such commands will be executed,
+ * but the results will be discarded unless you use "simple Query"
+ * protocol.
+ */
+ if (dest->mydest == DestRemoteExecute)
+ dest = None_Receiver;
+ if (altdest->mydest == DestRemoteExecute)
+ altdest = None_Receiver;
+
+ /*
+ * Loop to handle the individual queries generated from a single parsetree
+ * by analysis and rewrite.
+ */
+ foreach(stmtlist_item, portal->stmts)
+ {
+ PlannedStmt *pstmt = lfirst_node(PlannedStmt, stmtlist_item);
+
+ /*
+ * If we got a cancel signal in prior command, quit
+ */
+ CHECK_FOR_INTERRUPTS();
+
+ if (pstmt->utilityStmt == NULL)
+ {
+ /*
+ * process a plannable query.
+ */
+ TRACE_POSTGRESQL_QUERY_EXECUTE_START();
+
+ if (log_executor_stats)
+ ResetUsage();
+
+ /*
+ * Must always have a snapshot for plannable queries. First time
+ * through, take a new snapshot; for subsequent queries in the
+ * same portal, just update the snapshot's copy of the command
+ * counter.
+ */
+ if (!active_snapshot_set)
+ {
+ Snapshot snapshot = GetTransactionSnapshot();
+
+ /* If told to, register the snapshot and save in portal */
+ if (setHoldSnapshot)
+ {
+ snapshot = RegisterSnapshot(snapshot);
+ portal->holdSnapshot = snapshot;
+ }
+
+ /*
+ * We can't have the holdSnapshot also be the active one,
+ * because UpdateActiveSnapshotCommandId would complain. So
+ * force an extra snapshot copy. Plain PushActiveSnapshot
+ * would have copied the transaction snapshot anyway, so this
+ * only adds a copy step when setHoldSnapshot is true. (It's
+ * okay for the command ID of the active snapshot to diverge
+ * from what holdSnapshot has.)
+ */
+ PushCopiedSnapshot(snapshot);
+
+ /*
+ * As for PORTAL_ONE_SELECT portals, it does not seem
+ * necessary to maintain portal->portalSnapshot here.
+ */
+
+ active_snapshot_set = true;
+ }
+ else
+ UpdateActiveSnapshotCommandId();
+
+ if (pstmt->canSetTag)
+ {
+ /* statement can set tag string */
+ ProcessQuery(pstmt,
+ portal->sourceText,
+ portal->portalParams,
+ portal->queryEnv,
+ dest, qc);
+ }
+ else
+ {
+ /* stmt added by rewrite cannot set tag */
+ ProcessQuery(pstmt,
+ portal->sourceText,
+ portal->portalParams,
+ portal->queryEnv,
+ altdest, NULL);
+ }
+
+ if (log_executor_stats)
+ ShowUsage("EXECUTOR STATISTICS");
+
+ TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
+ }
+ else
+ {
+ /*
+ * process utility functions (create, destroy, etc..)
+ *
+ * We must not set a snapshot here for utility commands (if one is
+ * needed, PortalRunUtility will do it). If a utility command is
+ * alone in a portal then everything's fine. The only case where
+ * a utility command can be part of a longer list is that rules
+ * are allowed to include NotifyStmt. NotifyStmt doesn't care
+ * whether it has a snapshot or not, so we just leave the current
+ * snapshot alone if we have one.
+ */
+ if (pstmt->canSetTag)
+ {
+ Assert(!active_snapshot_set);
+ /* statement can set tag string */
+ PortalRunUtility(portal, pstmt, isTopLevel, false,
+ dest, qc);
+ }
+ else
+ {
+ Assert(IsA(pstmt->utilityStmt, NotifyStmt));
+ /* stmt added by rewrite cannot set tag */
+ PortalRunUtility(portal, pstmt, isTopLevel, false,
+ altdest, NULL);
+ }
+ }
+
+ /*
+ * Clear subsidiary contexts to recover temporary memory.
+ */
+ Assert(portal->portalContext == CurrentMemoryContext);
+
+ MemoryContextDeleteChildren(portal->portalContext);
+
+ /*
+ * Avoid crashing if portal->stmts has been reset. This can only
+ * occur if a CALL or DO utility statement executed an internal
+ * COMMIT/ROLLBACK (cf PortalReleaseCachedPlan). The CALL or DO must
+ * have been the only statement in the portal, so there's nothing left
+ * for us to do; but we don't want to dereference a now-dangling list
+ * pointer.
+ */
+ if (portal->stmts == NIL)
+ break;
+
+ /*
+ * Increment command counter between queries, but not after the last
+ * one.
+ */
+ if (lnext(portal->stmts, stmtlist_item) != NULL)
+ CommandCounterIncrement();
+ }
+
+ /* Pop the snapshot if we pushed one. */
+ if (active_snapshot_set)
+ PopActiveSnapshot();
+
+ /*
+ * If a query completion data was supplied, use it. Otherwise use the
+ * portal's query completion data.
+ *
+ * Exception: Clients expect INSERT/UPDATE/DELETE tags to have counts, so
+ * fake them with zeros. This can happen with DO INSTEAD rules if there
+ * is no replacement query of the same type as the original. We print "0
+ * 0" here because technically there is no query of the matching tag type,
+ * and printing a non-zero count for a different query type seems wrong,
+ * e.g. an INSERT that does an UPDATE instead should not print "0 1" if
+ * one row was updated. See QueryRewrite(), step 3, for details.
+ */
+ if (qc && qc->commandTag == CMDTAG_UNKNOWN)
+ {
+ if (portal->qc.commandTag != CMDTAG_UNKNOWN)
+ CopyQueryCompletion(qc, &portal->qc);
+ /* If the caller supplied a qc, we should have set it by now. */
+ Assert(qc->commandTag != CMDTAG_UNKNOWN);
+ }
+}
+
+/*
+ * PortalRunFetch
+ * Variant form of PortalRun that supports SQL FETCH directions.
+ *
+ * Note: we presently assume that no callers of this want isTopLevel = true.
+ *
+ * count <= 0 is interpreted as a no-op: the destination gets started up
+ * and shut down, but nothing else happens. Also, count == FETCH_ALL is
+ * interpreted as "all rows". (cf FetchStmt.howMany)
+ *
+ * Returns number of rows processed (suitable for use in result tag)
+ */
+uint64
+PortalRunFetch(Portal portal,
+ FetchDirection fdirection,
+ long count,
+ DestReceiver *dest)
+{
+ uint64 result;
+ Portal saveActivePortal;
+ ResourceOwner saveResourceOwner;
+ MemoryContext savePortalContext;
+ MemoryContext oldContext;
+
+ AssertArg(PortalIsValid(portal));
+
+ /*
+ * Check for improper portal use, and mark portal active.
+ */
+ MarkPortalActive(portal);
+
+ /* If supporting FETCH, portal can't be run-once. */
+ Assert(!portal->run_once);
+
+ /*
+ * Set up global portal context pointers.
+ */
+ saveActivePortal = ActivePortal;
+ saveResourceOwner = CurrentResourceOwner;
+ savePortalContext = PortalContext;
+ PG_TRY();
+ {
+ ActivePortal = portal;
+ if (portal->resowner)
+ CurrentResourceOwner = portal->resowner;
+ PortalContext = portal->portalContext;
+
+ oldContext = MemoryContextSwitchTo(PortalContext);
+
+ switch (portal->strategy)
+ {
+ case PORTAL_ONE_SELECT:
+ result = DoPortalRunFetch(portal, fdirection, count, dest);
+ break;
+
+ case PORTAL_ONE_RETURNING:
+ case PORTAL_ONE_MOD_WITH:
+ case PORTAL_UTIL_SELECT:
+
+ /*
+ * If we have not yet run the command, do so, storing its
+ * results in the portal's tuplestore.
+ */
+ if (!portal->holdStore)
+ FillPortalStore(portal, false /* isTopLevel */ );
+
+ /*
+ * Now fetch desired portion of results.
+ */
+ result = DoPortalRunFetch(portal, fdirection, count, dest);
+ break;
+
+ default:
+ elog(ERROR, "unsupported portal strategy");
+ result = 0; /* keep compiler quiet */
+ break;
+ }
+ }
+ PG_CATCH();
+ {
+ /* Uncaught error while executing portal: mark it dead */
+ MarkPortalFailed(portal);
+
+ /* Restore global vars and propagate error */
+ ActivePortal = saveActivePortal;
+ CurrentResourceOwner = saveResourceOwner;
+ PortalContext = savePortalContext;
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ MemoryContextSwitchTo(oldContext);
+
+ /* Mark portal not active */
+ portal->status = PORTAL_READY;
+
+ ActivePortal = saveActivePortal;
+ CurrentResourceOwner = saveResourceOwner;
+ PortalContext = savePortalContext;
+
+ return result;
+}
+
+/*
+ * DoPortalRunFetch
+ * Guts of PortalRunFetch --- the portal context is already set up
+ *
+ * Here, count < 0 typically reverses the direction. Also, count == FETCH_ALL
+ * is interpreted as "all rows". (cf FetchStmt.howMany)
+ *
+ * Returns number of rows processed (suitable for use in result tag)
+ */
+static uint64
+DoPortalRunFetch(Portal portal,
+ FetchDirection fdirection,
+ long count,
+ DestReceiver *dest)
+{
+ bool forward;
+
+ Assert(portal->strategy == PORTAL_ONE_SELECT ||
+ portal->strategy == PORTAL_ONE_RETURNING ||
+ portal->strategy == PORTAL_ONE_MOD_WITH ||
+ portal->strategy == PORTAL_UTIL_SELECT);
+
+ /*
+ * Note: we disallow backwards fetch (including re-fetch of current row)
+ * for NO SCROLL cursors, but we interpret that very loosely: you can use
+ * any of the FetchDirection options, so long as the end result is to move
+ * forwards by at least one row. Currently it's sufficient to check for
+ * NO SCROLL in DoPortalRewind() and in the forward == false path in
+ * PortalRunSelect(); but someday we might prefer to account for that
+ * restriction explicitly here.
+ */
+ switch (fdirection)
+ {
+ case FETCH_FORWARD:
+ if (count < 0)
+ {
+ fdirection = FETCH_BACKWARD;
+ count = -count;
+ }
+ /* fall out of switch to share code with FETCH_BACKWARD */
+ break;
+ case FETCH_BACKWARD:
+ if (count < 0)
+ {
+ fdirection = FETCH_FORWARD;
+ count = -count;
+ }
+ /* fall out of switch to share code with FETCH_FORWARD */
+ break;
+ case FETCH_ABSOLUTE:
+ if (count > 0)
+ {
+ /*
+ * Definition: Rewind to start, advance count-1 rows, return
+ * next row (if any).
+ *
+ * In practice, if the goal is less than halfway back to the
+ * start, it's better to scan from where we are.
+ *
+ * Also, if current portalPos is outside the range of "long",
+ * do it the hard way to avoid possible overflow of the count
+ * argument to PortalRunSelect. We must exclude exactly
+ * LONG_MAX, as well, lest the count look like FETCH_ALL.
+ *
+ * In any case, we arrange to fetch the target row going
+ * forwards.
+ */
+ if ((uint64) (count - 1) <= portal->portalPos / 2 ||
+ portal->portalPos >= (uint64) LONG_MAX)
+ {
+ DoPortalRewind(portal);
+ if (count > 1)
+ PortalRunSelect(portal, true, count - 1,
+ None_Receiver);
+ }
+ else
+ {
+ long pos = (long) portal->portalPos;
+
+ if (portal->atEnd)
+ pos++; /* need one extra fetch if off end */
+ if (count <= pos)
+ PortalRunSelect(portal, false, pos - count + 1,
+ None_Receiver);
+ else if (count > pos + 1)
+ PortalRunSelect(portal, true, count - pos - 1,
+ None_Receiver);
+ }
+ return PortalRunSelect(portal, true, 1L, dest);
+ }
+ else if (count < 0)
+ {
+ /*
+ * Definition: Advance to end, back up abs(count)-1 rows,
+ * return prior row (if any). We could optimize this if we
+ * knew in advance where the end was, but typically we won't.
+ * (Is it worth considering case where count > half of size of
+ * query? We could rewind once we know the size ...)
+ */
+ PortalRunSelect(portal, true, FETCH_ALL, None_Receiver);
+ if (count < -1)
+ PortalRunSelect(portal, false, -count - 1, None_Receiver);
+ return PortalRunSelect(portal, false, 1L, dest);
+ }
+ else
+ {
+ /* count == 0 */
+ /* Rewind to start, return zero rows */
+ DoPortalRewind(portal);
+ return PortalRunSelect(portal, true, 0L, dest);
+ }
+ break;
+ case FETCH_RELATIVE:
+ if (count > 0)
+ {
+ /*
+ * Definition: advance count-1 rows, return next row (if any).
+ */
+ if (count > 1)
+ PortalRunSelect(portal, true, count - 1, None_Receiver);
+ return PortalRunSelect(portal, true, 1L, dest);
+ }
+ else if (count < 0)
+ {
+ /*
+ * Definition: back up abs(count)-1 rows, return prior row (if
+ * any).
+ */
+ if (count < -1)
+ PortalRunSelect(portal, false, -count - 1, None_Receiver);
+ return PortalRunSelect(portal, false, 1L, dest);
+ }
+ else
+ {
+ /* count == 0 */
+ /* Same as FETCH FORWARD 0, so fall out of switch */
+ fdirection = FETCH_FORWARD;
+ }
+ break;
+ default:
+ elog(ERROR, "bogus direction");
+ break;
+ }
+
+ /*
+ * Get here with fdirection == FETCH_FORWARD or FETCH_BACKWARD, and count
+ * >= 0.
+ */
+ forward = (fdirection == FETCH_FORWARD);
+
+ /*
+ * Zero count means to re-fetch the current row, if any (per SQL)
+ */
+ if (count == 0)
+ {
+ bool on_row;
+
+ /* Are we sitting on a row? */
+ on_row = (!portal->atStart && !portal->atEnd);
+
+ if (dest->mydest == DestNone)
+ {
+ /* MOVE 0 returns 0/1 based on if FETCH 0 would return a row */
+ return on_row ? 1 : 0;
+ }
+ else
+ {
+ /*
+ * If we are sitting on a row, back up one so we can re-fetch it.
+ * If we are not sitting on a row, we still have to start up and
+ * shut down the executor so that the destination is initialized
+ * and shut down correctly; so keep going. To PortalRunSelect,
+ * count == 0 means we will retrieve no row.
+ */
+ if (on_row)
+ {
+ PortalRunSelect(portal, false, 1L, None_Receiver);
+ /* Set up to fetch one row forward */
+ count = 1;
+ forward = true;
+ }
+ }
+ }
+
+ /*
+ * Optimize MOVE BACKWARD ALL into a Rewind.
+ */
+ if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
+ {
+ uint64 result = portal->portalPos;
+
+ if (result > 0 && !portal->atEnd)
+ result--;
+ DoPortalRewind(portal);
+ return result;
+ }
+
+ return PortalRunSelect(portal, forward, count, dest);
+}
+
+/*
+ * DoPortalRewind - rewind a Portal to starting point
+ */
+static void
+DoPortalRewind(Portal portal)
+{
+ QueryDesc *queryDesc;
+
+ /*
+ * No work is needed if we've not advanced nor attempted to advance the
+ * cursor (and we don't want to throw a NO SCROLL error in this case).
+ */
+ if (portal->atStart && !portal->atEnd)
+ return;
+
+ /* Otherwise, cursor must allow scrolling */
+ if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("cursor can only scan forward"),
+ errhint("Declare it with SCROLL option to enable backward scan.")));
+
+ /* Rewind holdStore, if we have one */
+ if (portal->holdStore)
+ {
+ MemoryContext oldcontext;
+
+ oldcontext = MemoryContextSwitchTo(portal->holdContext);
+ tuplestore_rescan(portal->holdStore);
+ MemoryContextSwitchTo(oldcontext);
+ }
+
+ /* Rewind executor, if active */
+ queryDesc = portal->queryDesc;
+ if (queryDesc)
+ {
+ PushActiveSnapshot(queryDesc->snapshot);
+ ExecutorRewind(queryDesc);
+ PopActiveSnapshot();
+ }
+
+ portal->atStart = true;
+ portal->atEnd = false;
+ portal->portalPos = 0;
+}
+
+/*
+ * PlannedStmtRequiresSnapshot - what it says on the tin
+ */
+bool
+PlannedStmtRequiresSnapshot(PlannedStmt *pstmt)
+{
+ Node *utilityStmt = pstmt->utilityStmt;
+
+ /* If it's not a utility statement, it definitely needs a snapshot */
+ if (utilityStmt == NULL)
+ return true;
+
+ /*
+ * Most utility statements need a snapshot, and the default presumption
+ * about new ones should be that they do too. Hence, enumerate those that
+ * do not need one.
+ *
+ * Transaction control, LOCK, and SET must *not* set a snapshot, since
+ * they need to be executable at the start of a transaction-snapshot-mode
+ * transaction without freezing a snapshot. By extension we allow SHOW
+ * not to set a snapshot. The other stmts listed are just efficiency
+ * hacks. Beware of listing anything that can modify the database --- if,
+ * say, it has to update an index with expressions that invoke
+ * user-defined functions, then it had better have a snapshot.
+ */
+ if (IsA(utilityStmt, TransactionStmt) ||
+ IsA(utilityStmt, LockStmt) ||
+ IsA(utilityStmt, VariableSetStmt) ||
+ IsA(utilityStmt, VariableShowStmt) ||
+ IsA(utilityStmt, ConstraintsSetStmt) ||
+ /* efficiency hacks from here down */
+ IsA(utilityStmt, FetchStmt) ||
+ IsA(utilityStmt, ListenStmt) ||
+ IsA(utilityStmt, NotifyStmt) ||
+ IsA(utilityStmt, UnlistenStmt) ||
+ IsA(utilityStmt, CheckPointStmt))
+ return false;
+
+ return true;
+}
+
+/*
+ * EnsurePortalSnapshotExists - recreate Portal-level snapshot, if needed
+ *
+ * Generally, we will have an active snapshot whenever we are executing
+ * inside a Portal, unless the Portal's query is one of the utility
+ * statements exempted from that rule (see PlannedStmtRequiresSnapshot).
+ * However, procedures and DO blocks can commit or abort the transaction,
+ * and thereby destroy all snapshots. This function can be called to
+ * re-establish the Portal-level snapshot when none exists.
+ */
+void
+EnsurePortalSnapshotExists(void)
+{
+ Portal portal;
+
+ /*
+ * Nothing to do if a snapshot is set. (We take it on faith that the
+ * outermost active snapshot belongs to some Portal; or if there is no
+ * Portal, it's somebody else's responsibility to manage things.)
+ */
+ if (ActiveSnapshotSet())
+ return;
+
+ /* Otherwise, we'd better have an active Portal */
+ portal = ActivePortal;
+ if (unlikely(portal == NULL))
+ elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
+ Assert(portal->portalSnapshot == NULL);
+
+ /*
+ * Create a new snapshot, make it active, and remember it in portal.
+ * Because the portal now references the snapshot, we must tell snapmgr.c
+ * that the snapshot belongs to the portal's transaction level, else we
+ * risk portalSnapshot becoming a dangling pointer.
+ */
+ PushActiveSnapshotWithLevel(GetTransactionSnapshot(), portal->createLevel);
+ /* PushActiveSnapshotWithLevel might have copied the snapshot */
+ portal->portalSnapshot = GetActiveSnapshot();
+}
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
new file mode 100644
index 0000000..6b0a865
--- /dev/null
+++ b/src/backend/tcop/utility.c
@@ -0,0 +1,3761 @@
+/*-------------------------------------------------------------------------
+ *
+ * utility.c
+ * Contains functions which control the execution of the POSTGRES utility
+ * commands. At one time acted as an interface between the Lisp and C
+ * systems.
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/tcop/utility.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "access/reloptions.h"
+#include "access/twophase.h"
+#include "access/xact.h"
+#include "access/xlog.h"
+#include "catalog/catalog.h"
+#include "catalog/index.h"
+#include "catalog/namespace.h"
+#include "catalog/pg_authid.h"
+#include "catalog/pg_inherits.h"
+#include "catalog/toasting.h"
+#include "commands/alter.h"
+#include "commands/async.h"
+#include "commands/cluster.h"
+#include "commands/collationcmds.h"
+#include "commands/comment.h"
+#include "commands/conversioncmds.h"
+#include "commands/copy.h"
+#include "commands/createas.h"
+#include "commands/dbcommands.h"
+#include "commands/defrem.h"
+#include "commands/discard.h"
+#include "commands/event_trigger.h"
+#include "commands/explain.h"
+#include "commands/extension.h"
+#include "commands/lockcmds.h"
+#include "commands/matview.h"
+#include "commands/policy.h"
+#include "commands/portalcmds.h"
+#include "commands/prepare.h"
+#include "commands/proclang.h"
+#include "commands/publicationcmds.h"
+#include "commands/schemacmds.h"
+#include "commands/seclabel.h"
+#include "commands/sequence.h"
+#include "commands/subscriptioncmds.h"
+#include "commands/tablecmds.h"
+#include "commands/tablespace.h"
+#include "commands/trigger.h"
+#include "commands/typecmds.h"
+#include "commands/user.h"
+#include "commands/vacuum.h"
+#include "commands/view.h"
+#include "miscadmin.h"
+#include "parser/parse_utilcmd.h"
+#include "postmaster/bgwriter.h"
+#include "rewrite/rewriteDefine.h"
+#include "rewrite/rewriteRemove.h"
+#include "storage/fd.h"
+#include "tcop/pquery.h"
+#include "tcop/utility.h"
+#include "utils/acl.h"
+#include "utils/guc.h"
+#include "utils/lsyscache.h"
+#include "utils/rel.h"
+#include "utils/syscache.h"
+
+/* Hook for plugins to get control in ProcessUtility() */
+ProcessUtility_hook_type ProcessUtility_hook = NULL;
+
+/* local function declarations */
+static int ClassifyUtilityCommandAsReadOnly(Node *parsetree);
+static void ProcessUtilitySlow(ParseState *pstate,
+ PlannedStmt *pstmt,
+ const char *queryString,
+ ProcessUtilityContext context,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ DestReceiver *dest,
+ QueryCompletion *qc);
+static void ExecDropStmt(DropStmt *stmt, bool isTopLevel);
+
+/*
+ * CommandIsReadOnly: is an executable query read-only?
+ *
+ * This is a much stricter test than we apply for XactReadOnly mode;
+ * the query must be *in truth* read-only, because the caller wishes
+ * not to do CommandCounterIncrement for it.
+ *
+ * Note: currently no need to support raw or analyzed queries here
+ */
+bool
+CommandIsReadOnly(PlannedStmt *pstmt)
+{
+ Assert(IsA(pstmt, PlannedStmt));
+ switch (pstmt->commandType)
+ {
+ case CMD_SELECT:
+ if (pstmt->rowMarks != NIL)
+ return false; /* SELECT FOR [KEY] UPDATE/SHARE */
+ else if (pstmt->hasModifyingCTE)
+ return false; /* data-modifying CTE */
+ else
+ return true;
+ case CMD_UPDATE:
+ case CMD_INSERT:
+ case CMD_DELETE:
+ case CMD_MERGE:
+ return false;
+ case CMD_UTILITY:
+ /* For now, treat all utility commands as read/write */
+ return false;
+ default:
+ elog(WARNING, "unrecognized commandType: %d",
+ (int) pstmt->commandType);
+ break;
+ }
+ return false;
+}
+
+/*
+ * Determine the degree to which a utility command is read only.
+ *
+ * Note the definitions of the relevant flags in src/include/utility/tcop.h.
+ */
+static int
+ClassifyUtilityCommandAsReadOnly(Node *parsetree)
+{
+ switch (nodeTag(parsetree))
+ {
+ case T_AlterCollationStmt:
+ case T_AlterDatabaseRefreshCollStmt:
+ case T_AlterDatabaseSetStmt:
+ case T_AlterDatabaseStmt:
+ case T_AlterDefaultPrivilegesStmt:
+ case T_AlterDomainStmt:
+ case T_AlterEnumStmt:
+ case T_AlterEventTrigStmt:
+ case T_AlterExtensionContentsStmt:
+ case T_AlterExtensionStmt:
+ case T_AlterFdwStmt:
+ case T_AlterForeignServerStmt:
+ case T_AlterFunctionStmt:
+ case T_AlterObjectDependsStmt:
+ case T_AlterObjectSchemaStmt:
+ case T_AlterOpFamilyStmt:
+ case T_AlterOperatorStmt:
+ case T_AlterOwnerStmt:
+ case T_AlterPolicyStmt:
+ case T_AlterPublicationStmt:
+ case T_AlterRoleSetStmt:
+ case T_AlterRoleStmt:
+ case T_AlterSeqStmt:
+ case T_AlterStatsStmt:
+ case T_AlterSubscriptionStmt:
+ case T_AlterTSConfigurationStmt:
+ case T_AlterTSDictionaryStmt:
+ case T_AlterTableMoveAllStmt:
+ case T_AlterTableSpaceOptionsStmt:
+ case T_AlterTableStmt:
+ case T_AlterTypeStmt:
+ case T_AlterUserMappingStmt:
+ case T_CommentStmt:
+ case T_CompositeTypeStmt:
+ case T_CreateAmStmt:
+ case T_CreateCastStmt:
+ case T_CreateConversionStmt:
+ case T_CreateDomainStmt:
+ case T_CreateEnumStmt:
+ case T_CreateEventTrigStmt:
+ case T_CreateExtensionStmt:
+ case T_CreateFdwStmt:
+ case T_CreateForeignServerStmt:
+ case T_CreateForeignTableStmt:
+ case T_CreateFunctionStmt:
+ case T_CreateOpClassStmt:
+ case T_CreateOpFamilyStmt:
+ case T_CreatePLangStmt:
+ case T_CreatePolicyStmt:
+ case T_CreatePublicationStmt:
+ case T_CreateRangeStmt:
+ case T_CreateRoleStmt:
+ case T_CreateSchemaStmt:
+ case T_CreateSeqStmt:
+ case T_CreateStatsStmt:
+ case T_CreateStmt:
+ case T_CreateSubscriptionStmt:
+ case T_CreateTableAsStmt:
+ case T_CreateTableSpaceStmt:
+ case T_CreateTransformStmt:
+ case T_CreateTrigStmt:
+ case T_CreateUserMappingStmt:
+ case T_CreatedbStmt:
+ case T_DefineStmt:
+ case T_DropOwnedStmt:
+ case T_DropRoleStmt:
+ case T_DropStmt:
+ case T_DropSubscriptionStmt:
+ case T_DropTableSpaceStmt:
+ case T_DropUserMappingStmt:
+ case T_DropdbStmt:
+ case T_GrantRoleStmt:
+ case T_GrantStmt:
+ case T_ImportForeignSchemaStmt:
+ case T_IndexStmt:
+ case T_ReassignOwnedStmt:
+ case T_RefreshMatViewStmt:
+ case T_RenameStmt:
+ case T_RuleStmt:
+ case T_SecLabelStmt:
+ case T_TruncateStmt:
+ case T_ViewStmt:
+ {
+ /* DDL is not read-only, and neither is TRUNCATE. */
+ return COMMAND_IS_NOT_READ_ONLY;
+ }
+
+ case T_AlterSystemStmt:
+ {
+ /*
+ * Surprisingly, ALTER SYSTEM meets all our definitions of
+ * read-only: it changes nothing that affects the output of
+ * pg_dump, it doesn't write WAL or imperil the application of
+ * future WAL, and it doesn't depend on any state that needs
+ * to be synchronized with parallel workers.
+ *
+ * So, despite the fact that it writes to a file, it's read
+ * only!
+ */
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+ }
+
+ case T_CallStmt:
+ case T_DoStmt:
+ {
+ /*
+ * Commands inside the DO block or the called procedure might
+ * not be read only, but they'll be checked separately when we
+ * try to execute them. Here we only need to worry about the
+ * DO or CALL command itself.
+ */
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+ }
+
+ case T_CheckPointStmt:
+ {
+ /*
+ * You might think that this should not be permitted in
+ * recovery, but we interpret a CHECKPOINT command during
+ * recovery as a request for a restartpoint instead. We allow
+ * this since it can be a useful way of reducing switchover
+ * time when using various forms of replication.
+ */
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+ }
+
+ case T_ClosePortalStmt:
+ case T_ConstraintsSetStmt:
+ case T_DeallocateStmt:
+ case T_DeclareCursorStmt:
+ case T_DiscardStmt:
+ case T_ExecuteStmt:
+ case T_FetchStmt:
+ case T_LoadStmt:
+ case T_PrepareStmt:
+ case T_UnlistenStmt:
+ case T_VariableSetStmt:
+ {
+ /*
+ * These modify only backend-local state, so they're OK to run
+ * in a read-only transaction or on a standby. However, they
+ * are disallowed in parallel mode, because they either rely
+ * upon or modify backend-local state that might not be
+ * synchronized among cooperating backends.
+ */
+ return COMMAND_OK_IN_RECOVERY | COMMAND_OK_IN_READ_ONLY_TXN;
+ }
+
+ case T_ClusterStmt:
+ case T_ReindexStmt:
+ case T_VacuumStmt:
+ {
+ /*
+ * These commands write WAL, so they're not strictly
+ * read-only, and running them in parallel workers isn't
+ * supported.
+ *
+ * However, they don't change the database state in a way that
+ * would affect pg_dump output, so it's fine to run them in a
+ * read-only transaction. (CLUSTER might change the order of
+ * rows on disk, which could affect the ordering of pg_dump
+ * output, but that's not semantically significant.)
+ */
+ return COMMAND_OK_IN_READ_ONLY_TXN;
+ }
+
+ case T_CopyStmt:
+ {
+ CopyStmt *stmt = (CopyStmt *) parsetree;
+
+ /*
+ * You might think that COPY FROM is not at all read only, but
+ * it's OK to copy into a temporary table, because that
+ * wouldn't change the output of pg_dump. If the target table
+ * turns out to be non-temporary, DoCopy itself will call
+ * PreventCommandIfReadOnly.
+ */
+ if (stmt->is_from)
+ return COMMAND_OK_IN_READ_ONLY_TXN;
+ else
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+ }
+
+ case T_ExplainStmt:
+ case T_VariableShowStmt:
+ {
+ /*
+ * These commands don't modify any data and are safe to run in
+ * a parallel worker.
+ */
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+ }
+
+ case T_ListenStmt:
+ case T_NotifyStmt:
+ {
+ /*
+ * NOTIFY requires an XID assignment, so it can't be permitted
+ * on a standby. Perhaps LISTEN could, since without NOTIFY it
+ * would be OK to just do nothing, at least until promotion,
+ * but we currently prohibit it lest the user get the wrong
+ * idea.
+ *
+ * (We do allow T_UnlistenStmt on a standby, though, because
+ * it's a no-op.)
+ */
+ return COMMAND_OK_IN_READ_ONLY_TXN;
+ }
+
+ case T_LockStmt:
+ {
+ LockStmt *stmt = (LockStmt *) parsetree;
+
+ /*
+ * Only weaker locker modes are allowed during recovery. The
+ * restrictions here must match those in
+ * LockAcquireExtended().
+ */
+ if (stmt->mode > RowExclusiveLock)
+ return COMMAND_OK_IN_READ_ONLY_TXN;
+ else
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+ }
+
+ case T_TransactionStmt:
+ {
+ TransactionStmt *stmt = (TransactionStmt *) parsetree;
+
+ /*
+ * PREPARE, COMMIT PREPARED, and ROLLBACK PREPARED all write
+ * WAL, so they're not read-only in the strict sense; but the
+ * first and third do not change pg_dump output, so they're OK
+ * in a read-only transactions.
+ *
+ * We also consider COMMIT PREPARED to be OK in a read-only
+ * transaction environment, by way of exception.
+ */
+ switch (stmt->kind)
+ {
+ case TRANS_STMT_BEGIN:
+ case TRANS_STMT_START:
+ case TRANS_STMT_COMMIT:
+ case TRANS_STMT_ROLLBACK:
+ case TRANS_STMT_SAVEPOINT:
+ case TRANS_STMT_RELEASE:
+ case TRANS_STMT_ROLLBACK_TO:
+ return COMMAND_IS_STRICTLY_READ_ONLY;
+
+ case TRANS_STMT_PREPARE:
+ case TRANS_STMT_COMMIT_PREPARED:
+ case TRANS_STMT_ROLLBACK_PREPARED:
+ return COMMAND_OK_IN_READ_ONLY_TXN;
+ }
+ elog(ERROR, "unrecognized TransactionStmtKind: %d",
+ (int) stmt->kind);
+ return 0; /* silence stupider compilers */
+ }
+
+ default:
+ elog(ERROR, "unrecognized node type: %d",
+ (int) nodeTag(parsetree));
+ return 0; /* silence stupider compilers */
+ }
+}
+
+/*
+ * PreventCommandIfReadOnly: throw error if XactReadOnly
+ *
+ * This is useful partly to ensure consistency of the error message wording;
+ * some callers have checked XactReadOnly for themselves.
+ */
+void
+PreventCommandIfReadOnly(const char *cmdname)
+{
+ if (XactReadOnly)
+ ereport(ERROR,
+ (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
+ /* translator: %s is name of a SQL command, eg CREATE */
+ errmsg("cannot execute %s in a read-only transaction",
+ cmdname)));
+}
+
+/*
+ * PreventCommandIfParallelMode: throw error if current (sub)transaction is
+ * in parallel mode.
+ *
+ * This is useful partly to ensure consistency of the error message wording;
+ * some callers have checked IsInParallelMode() for themselves.
+ */
+void
+PreventCommandIfParallelMode(const char *cmdname)
+{
+ if (IsInParallelMode())
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
+ /* translator: %s is name of a SQL command, eg CREATE */
+ errmsg("cannot execute %s during a parallel operation",
+ cmdname)));
+}
+
+/*
+ * PreventCommandDuringRecovery: throw error if RecoveryInProgress
+ *
+ * The majority of operations that are unsafe in a Hot Standby
+ * will be rejected by XactReadOnly tests. However there are a few
+ * commands that are allowed in "read-only" xacts but cannot be allowed
+ * in Hot Standby mode. Those commands should call this function.
+ */
+void
+PreventCommandDuringRecovery(const char *cmdname)
+{
+ if (RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
+ /* translator: %s is name of a SQL command, eg CREATE */
+ errmsg("cannot execute %s during recovery",
+ cmdname)));
+}
+
+/*
+ * CheckRestrictedOperation: throw error for hazardous command if we're
+ * inside a security restriction context.
+ *
+ * This is needed to protect session-local state for which there is not any
+ * better-defined protection mechanism, such as ownership.
+ */
+static void
+CheckRestrictedOperation(const char *cmdname)
+{
+ if (InSecurityRestrictedOperation())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ /* translator: %s is name of a SQL command, eg PREPARE */
+ errmsg("cannot execute %s within security-restricted operation",
+ cmdname)));
+}
+
+/*
+ * ProcessUtility
+ * general utility function invoker
+ *
+ * pstmt: PlannedStmt wrapper for the utility statement
+ * queryString: original source text of command
+ * readOnlyTree: if true, pstmt's node tree must not be modified
+ * context: identifies source of statement (toplevel client command,
+ * non-toplevel client command, subcommand of a larger utility command)
+ * params: parameters to use during execution
+ * queryEnv: environment for parse through execution (e.g., ephemeral named
+ * tables like trigger transition tables). May be NULL.
+ * dest: where to send results
+ * qc: where to store command completion status data. May be NULL,
+ * but if not, then caller must have initialized it.
+ *
+ * Caller MUST supply a queryString; it is not allowed (anymore) to pass NULL.
+ * If you really don't have source text, you can pass a constant string,
+ * perhaps "(query not available)".
+ *
+ * Note for users of ProcessUtility_hook: the same queryString may be passed
+ * to multiple invocations of ProcessUtility when processing a query string
+ * containing multiple semicolon-separated statements. One should use
+ * pstmt->stmt_location and pstmt->stmt_len to identify the substring
+ * containing the current statement. Keep in mind also that some utility
+ * statements (e.g., CREATE SCHEMA) will recurse to ProcessUtility to process
+ * sub-statements, often passing down the same queryString, stmt_location,
+ * and stmt_len that were given for the whole statement.
+ */
+void
+ProcessUtility(PlannedStmt *pstmt,
+ const char *queryString,
+ bool readOnlyTree,
+ ProcessUtilityContext context,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ DestReceiver *dest,
+ QueryCompletion *qc)
+{
+ Assert(IsA(pstmt, PlannedStmt));
+ Assert(pstmt->commandType == CMD_UTILITY);
+ Assert(queryString != NULL); /* required as of 8.4 */
+ Assert(qc == NULL || qc->commandTag == CMDTAG_UNKNOWN);
+
+ /*
+ * We provide a function hook variable that lets loadable plugins get
+ * control when ProcessUtility is called. Such a plugin would normally
+ * call standard_ProcessUtility().
+ */
+ if (ProcessUtility_hook)
+ (*ProcessUtility_hook) (pstmt, queryString, readOnlyTree,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ standard_ProcessUtility(pstmt, queryString, readOnlyTree,
+ context, params, queryEnv,
+ dest, qc);
+}
+
+/*
+ * standard_ProcessUtility itself deals only with utility commands for
+ * which we do not provide event trigger support. Commands that do have
+ * such support are passed down to ProcessUtilitySlow, which contains the
+ * necessary infrastructure for such triggers.
+ *
+ * This division is not just for performance: it's critical that the
+ * event trigger code not be invoked when doing START TRANSACTION for
+ * example, because we might need to refresh the event trigger cache,
+ * which requires being in a valid transaction.
+ */
+void
+standard_ProcessUtility(PlannedStmt *pstmt,
+ const char *queryString,
+ bool readOnlyTree,
+ ProcessUtilityContext context,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ DestReceiver *dest,
+ QueryCompletion *qc)
+{
+ Node *parsetree;
+ bool isTopLevel = (context == PROCESS_UTILITY_TOPLEVEL);
+ bool isAtomicContext = (!(context == PROCESS_UTILITY_TOPLEVEL || context == PROCESS_UTILITY_QUERY_NONATOMIC) || IsTransactionBlock());
+ ParseState *pstate;
+ int readonly_flags;
+
+ /* This can recurse, so check for excessive recursion */
+ check_stack_depth();
+
+ /*
+ * If the given node tree is read-only, make a copy to ensure that parse
+ * transformations don't damage the original tree. This could be
+ * refactored to avoid making unnecessary copies in more cases, but it's
+ * not clear that it's worth a great deal of trouble over. Statements
+ * that are complex enough to be expensive to copy are exactly the ones
+ * we'd need to copy, so that only marginal savings seem possible.
+ */
+ if (readOnlyTree)
+ pstmt = copyObject(pstmt);
+ parsetree = pstmt->utilityStmt;
+
+ /* Prohibit read/write commands in read-only states. */
+ readonly_flags = ClassifyUtilityCommandAsReadOnly(parsetree);
+ if (readonly_flags != COMMAND_IS_STRICTLY_READ_ONLY &&
+ (XactReadOnly || IsInParallelMode()))
+ {
+ CommandTag commandtag = CreateCommandTag(parsetree);
+
+ if ((readonly_flags & COMMAND_OK_IN_READ_ONLY_TXN) == 0)
+ PreventCommandIfReadOnly(GetCommandTagName(commandtag));
+ if ((readonly_flags & COMMAND_OK_IN_PARALLEL_MODE) == 0)
+ PreventCommandIfParallelMode(GetCommandTagName(commandtag));
+ if ((readonly_flags & COMMAND_OK_IN_RECOVERY) == 0)
+ PreventCommandDuringRecovery(GetCommandTagName(commandtag));
+ }
+
+ pstate = make_parsestate(NULL);
+ pstate->p_sourcetext = queryString;
+ pstate->p_queryEnv = queryEnv;
+
+ switch (nodeTag(parsetree))
+ {
+ /*
+ * ******************** transactions ********************
+ */
+ case T_TransactionStmt:
+ {
+ TransactionStmt *stmt = (TransactionStmt *) parsetree;
+
+ switch (stmt->kind)
+ {
+ /*
+ * START TRANSACTION, as defined by SQL99: Identical
+ * to BEGIN. Same code for both.
+ */
+ case TRANS_STMT_BEGIN:
+ case TRANS_STMT_START:
+ {
+ ListCell *lc;
+
+ BeginTransactionBlock();
+ foreach(lc, stmt->options)
+ {
+ DefElem *item = (DefElem *) lfirst(lc);
+
+ if (strcmp(item->defname, "transaction_isolation") == 0)
+ SetPGVariable("transaction_isolation",
+ list_make1(item->arg),
+ true);
+ else if (strcmp(item->defname, "transaction_read_only") == 0)
+ SetPGVariable("transaction_read_only",
+ list_make1(item->arg),
+ true);
+ else if (strcmp(item->defname, "transaction_deferrable") == 0)
+ SetPGVariable("transaction_deferrable",
+ list_make1(item->arg),
+ true);
+ }
+ }
+ break;
+
+ case TRANS_STMT_COMMIT:
+ if (!EndTransactionBlock(stmt->chain))
+ {
+ /* report unsuccessful commit in qc */
+ if (qc)
+ SetQueryCompletion(qc, CMDTAG_ROLLBACK, 0);
+ }
+ break;
+
+ case TRANS_STMT_PREPARE:
+ if (!PrepareTransactionBlock(stmt->gid))
+ {
+ /* report unsuccessful commit in qc */
+ if (qc)
+ SetQueryCompletion(qc, CMDTAG_ROLLBACK, 0);
+ }
+ break;
+
+ case TRANS_STMT_COMMIT_PREPARED:
+ PreventInTransactionBlock(isTopLevel, "COMMIT PREPARED");
+ FinishPreparedTransaction(stmt->gid, true);
+ break;
+
+ case TRANS_STMT_ROLLBACK_PREPARED:
+ PreventInTransactionBlock(isTopLevel, "ROLLBACK PREPARED");
+ FinishPreparedTransaction(stmt->gid, false);
+ break;
+
+ case TRANS_STMT_ROLLBACK:
+ UserAbortTransactionBlock(stmt->chain);
+ break;
+
+ case TRANS_STMT_SAVEPOINT:
+ RequireTransactionBlock(isTopLevel, "SAVEPOINT");
+ DefineSavepoint(stmt->savepoint_name);
+ break;
+
+ case TRANS_STMT_RELEASE:
+ RequireTransactionBlock(isTopLevel, "RELEASE SAVEPOINT");
+ ReleaseSavepoint(stmt->savepoint_name);
+ break;
+
+ case TRANS_STMT_ROLLBACK_TO:
+ RequireTransactionBlock(isTopLevel, "ROLLBACK TO SAVEPOINT");
+ RollbackToSavepoint(stmt->savepoint_name);
+
+ /*
+ * CommitTransactionCommand is in charge of
+ * re-defining the savepoint again
+ */
+ break;
+ }
+ }
+ break;
+
+ /*
+ * Portal (cursor) manipulation
+ */
+ case T_DeclareCursorStmt:
+ PerformCursorOpen(pstate, (DeclareCursorStmt *) parsetree, params,
+ isTopLevel);
+ break;
+
+ case T_ClosePortalStmt:
+ {
+ ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
+
+ CheckRestrictedOperation("CLOSE");
+ PerformPortalClose(stmt->portalname);
+ }
+ break;
+
+ case T_FetchStmt:
+ PerformPortalFetch((FetchStmt *) parsetree, dest, qc);
+ break;
+
+ case T_DoStmt:
+ ExecuteDoStmt(pstate, (DoStmt *) parsetree, isAtomicContext);
+ break;
+
+ case T_CreateTableSpaceStmt:
+ /* no event triggers for global objects */
+ PreventInTransactionBlock(isTopLevel, "CREATE TABLESPACE");
+ CreateTableSpace((CreateTableSpaceStmt *) parsetree);
+ break;
+
+ case T_DropTableSpaceStmt:
+ /* no event triggers for global objects */
+ PreventInTransactionBlock(isTopLevel, "DROP TABLESPACE");
+ DropTableSpace((DropTableSpaceStmt *) parsetree);
+ break;
+
+ case T_AlterTableSpaceOptionsStmt:
+ /* no event triggers for global objects */
+ AlterTableSpaceOptions((AlterTableSpaceOptionsStmt *) parsetree);
+ break;
+
+ case T_TruncateStmt:
+ ExecuteTruncate((TruncateStmt *) parsetree);
+ break;
+
+ case T_CopyStmt:
+ {
+ uint64 processed;
+
+ DoCopy(pstate, (CopyStmt *) parsetree,
+ pstmt->stmt_location, pstmt->stmt_len,
+ &processed);
+ if (qc)
+ SetQueryCompletion(qc, CMDTAG_COPY, processed);
+ }
+ break;
+
+ case T_PrepareStmt:
+ CheckRestrictedOperation("PREPARE");
+ PrepareQuery(pstate, (PrepareStmt *) parsetree,
+ pstmt->stmt_location, pstmt->stmt_len);
+ break;
+
+ case T_ExecuteStmt:
+ ExecuteQuery(pstate,
+ (ExecuteStmt *) parsetree, NULL,
+ params,
+ dest, qc);
+ break;
+
+ case T_DeallocateStmt:
+ CheckRestrictedOperation("DEALLOCATE");
+ DeallocateQuery((DeallocateStmt *) parsetree);
+ break;
+
+ case T_GrantRoleStmt:
+ /* no event triggers for global objects */
+ GrantRole((GrantRoleStmt *) parsetree);
+ break;
+
+ case T_CreatedbStmt:
+ /* no event triggers for global objects */
+ PreventInTransactionBlock(isTopLevel, "CREATE DATABASE");
+ createdb(pstate, (CreatedbStmt *) parsetree);
+ break;
+
+ case T_AlterDatabaseStmt:
+ /* no event triggers for global objects */
+ AlterDatabase(pstate, (AlterDatabaseStmt *) parsetree, isTopLevel);
+ break;
+
+ case T_AlterDatabaseRefreshCollStmt:
+ /* no event triggers for global objects */
+ AlterDatabaseRefreshColl((AlterDatabaseRefreshCollStmt *) parsetree);
+ break;
+
+ case T_AlterDatabaseSetStmt:
+ /* no event triggers for global objects */
+ AlterDatabaseSet((AlterDatabaseSetStmt *) parsetree);
+ break;
+
+ case T_DropdbStmt:
+ /* no event triggers for global objects */
+ PreventInTransactionBlock(isTopLevel, "DROP DATABASE");
+ DropDatabase(pstate, (DropdbStmt *) parsetree);
+ break;
+
+ /* Query-level asynchronous notification */
+ case T_NotifyStmt:
+ {
+ NotifyStmt *stmt = (NotifyStmt *) parsetree;
+
+ Async_Notify(stmt->conditionname, stmt->payload);
+ }
+ break;
+
+ case T_ListenStmt:
+ {
+ ListenStmt *stmt = (ListenStmt *) parsetree;
+
+ CheckRestrictedOperation("LISTEN");
+
+ /*
+ * We don't allow LISTEN in background processes, as there is
+ * no mechanism for them to collect NOTIFY messages, so they'd
+ * just block cleanout of the async SLRU indefinitely.
+ * (Authors of custom background workers could bypass this
+ * restriction by calling Async_Listen directly, but then it's
+ * on them to provide some mechanism to process the message
+ * queue.) Note there seems no reason to forbid UNLISTEN.
+ */
+ if (MyBackendType != B_BACKEND)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ /* translator: %s is name of a SQL command, eg LISTEN */
+ errmsg("cannot execute %s within a background process",
+ "LISTEN")));
+
+ Async_Listen(stmt->conditionname);
+ }
+ break;
+
+ case T_UnlistenStmt:
+ {
+ UnlistenStmt *stmt = (UnlistenStmt *) parsetree;
+
+ CheckRestrictedOperation("UNLISTEN");
+ if (stmt->conditionname)
+ Async_Unlisten(stmt->conditionname);
+ else
+ Async_UnlistenAll();
+ }
+ break;
+
+ case T_LoadStmt:
+ {
+ LoadStmt *stmt = (LoadStmt *) parsetree;
+
+ closeAllVfds(); /* probably not necessary... */
+ /* Allowed names are restricted if you're not superuser */
+ load_file(stmt->filename, !superuser());
+ }
+ break;
+
+ case T_CallStmt:
+ ExecuteCallStmt(castNode(CallStmt, parsetree), params, isAtomicContext, dest);
+ break;
+
+ case T_ClusterStmt:
+ cluster(pstate, (ClusterStmt *) parsetree, isTopLevel);
+ break;
+
+ case T_VacuumStmt:
+ ExecVacuum(pstate, (VacuumStmt *) parsetree, isTopLevel);
+ break;
+
+ case T_ExplainStmt:
+ ExplainQuery(pstate, (ExplainStmt *) parsetree, params, dest);
+ break;
+
+ case T_AlterSystemStmt:
+ PreventInTransactionBlock(isTopLevel, "ALTER SYSTEM");
+ AlterSystemSetConfigFile((AlterSystemStmt *) parsetree);
+ break;
+
+ case T_VariableSetStmt:
+ ExecSetVariableStmt((VariableSetStmt *) parsetree, isTopLevel);
+ break;
+
+ case T_VariableShowStmt:
+ {
+ VariableShowStmt *n = (VariableShowStmt *) parsetree;
+
+ GetPGVariable(n->name, dest);
+ }
+ break;
+
+ case T_DiscardStmt:
+ /* should we allow DISCARD PLANS? */
+ CheckRestrictedOperation("DISCARD");
+ DiscardCommand((DiscardStmt *) parsetree, isTopLevel);
+ break;
+
+ case T_CreateEventTrigStmt:
+ /* no event triggers on event triggers */
+ CreateEventTrigger((CreateEventTrigStmt *) parsetree);
+ break;
+
+ case T_AlterEventTrigStmt:
+ /* no event triggers on event triggers */
+ AlterEventTrigger((AlterEventTrigStmt *) parsetree);
+ break;
+
+ /*
+ * ******************************** ROLE statements ****
+ */
+ case T_CreateRoleStmt:
+ /* no event triggers for global objects */
+ CreateRole(pstate, (CreateRoleStmt *) parsetree);
+ break;
+
+ case T_AlterRoleStmt:
+ /* no event triggers for global objects */
+ AlterRole(pstate, (AlterRoleStmt *) parsetree);
+ break;
+
+ case T_AlterRoleSetStmt:
+ /* no event triggers for global objects */
+ AlterRoleSet((AlterRoleSetStmt *) parsetree);
+ break;
+
+ case T_DropRoleStmt:
+ /* no event triggers for global objects */
+ DropRole((DropRoleStmt *) parsetree);
+ break;
+
+ case T_ReassignOwnedStmt:
+ /* no event triggers for global objects */
+ ReassignOwnedObjects((ReassignOwnedStmt *) parsetree);
+ break;
+
+ case T_LockStmt:
+
+ /*
+ * Since the lock would just get dropped immediately, LOCK TABLE
+ * outside a transaction block is presumed to be user error.
+ */
+ RequireTransactionBlock(isTopLevel, "LOCK TABLE");
+ LockTableCommand((LockStmt *) parsetree);
+ break;
+
+ case T_ConstraintsSetStmt:
+ WarnNoTransactionBlock(isTopLevel, "SET CONSTRAINTS");
+ AfterTriggerSetState((ConstraintsSetStmt *) parsetree);
+ break;
+
+ case T_CheckPointStmt:
+ if (!has_privs_of_role(GetUserId(), ROLE_PG_CHECKPOINT))
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ errmsg("must be superuser or have privileges of pg_checkpoint to do CHECKPOINT")));
+
+ RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_WAIT |
+ (RecoveryInProgress() ? 0 : CHECKPOINT_FORCE));
+ break;
+
+ case T_ReindexStmt:
+ ExecReindex(pstate, (ReindexStmt *) parsetree, isTopLevel);
+ break;
+
+ /*
+ * The following statements are supported by Event Triggers only
+ * in some cases, so we "fast path" them in the other cases.
+ */
+
+ case T_GrantStmt:
+ {
+ GrantStmt *stmt = (GrantStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->objtype))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecuteGrantStmt(stmt);
+ }
+ break;
+
+ case T_DropStmt:
+ {
+ DropStmt *stmt = (DropStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->removeType))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecDropStmt(stmt, isTopLevel);
+ }
+ break;
+
+ case T_RenameStmt:
+ {
+ RenameStmt *stmt = (RenameStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->renameType))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecRenameStmt(stmt);
+ }
+ break;
+
+ case T_AlterObjectDependsStmt:
+ {
+ AlterObjectDependsStmt *stmt = (AlterObjectDependsStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->objectType))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecAlterObjectDependsStmt(stmt, NULL);
+ }
+ break;
+
+ case T_AlterObjectSchemaStmt:
+ {
+ AlterObjectSchemaStmt *stmt = (AlterObjectSchemaStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->objectType))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecAlterObjectSchemaStmt(stmt, NULL);
+ }
+ break;
+
+ case T_AlterOwnerStmt:
+ {
+ AlterOwnerStmt *stmt = (AlterOwnerStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->objectType))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecAlterOwnerStmt(stmt);
+ }
+ break;
+
+ case T_CommentStmt:
+ {
+ CommentStmt *stmt = (CommentStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->objtype))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ CommentObject(stmt);
+ break;
+ }
+
+ case T_SecLabelStmt:
+ {
+ SecLabelStmt *stmt = (SecLabelStmt *) parsetree;
+
+ if (EventTriggerSupportsObjectType(stmt->objtype))
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ else
+ ExecSecLabelStmt(stmt);
+ break;
+ }
+
+ default:
+ /* All other statement types have event trigger support */
+ ProcessUtilitySlow(pstate, pstmt, queryString,
+ context, params, queryEnv,
+ dest, qc);
+ break;
+ }
+
+ free_parsestate(pstate);
+
+ /*
+ * Make effects of commands visible, for instance so that
+ * PreCommit_on_commit_actions() can see them (see for example bug
+ * #15631).
+ */
+ CommandCounterIncrement();
+}
+
+/*
+ * The "Slow" variant of ProcessUtility should only receive statements
+ * supported by the event triggers facility. Therefore, we always
+ * perform the trigger support calls if the context allows it.
+ */
+static void
+ProcessUtilitySlow(ParseState *pstate,
+ PlannedStmt *pstmt,
+ const char *queryString,
+ ProcessUtilityContext context,
+ ParamListInfo params,
+ QueryEnvironment *queryEnv,
+ DestReceiver *dest,
+ QueryCompletion *qc)
+{
+ Node *parsetree = pstmt->utilityStmt;
+ bool isTopLevel = (context == PROCESS_UTILITY_TOPLEVEL);
+ bool isCompleteQuery = (context != PROCESS_UTILITY_SUBCOMMAND);
+ bool needCleanup;
+ bool commandCollected = false;
+ ObjectAddress address;
+ ObjectAddress secondaryObject = InvalidObjectAddress;
+
+ /* All event trigger calls are done only when isCompleteQuery is true */
+ needCleanup = isCompleteQuery && EventTriggerBeginCompleteQuery();
+
+ /* PG_TRY block is to ensure we call EventTriggerEndCompleteQuery */
+ PG_TRY();
+ {
+ if (isCompleteQuery)
+ EventTriggerDDLCommandStart(parsetree);
+
+ switch (nodeTag(parsetree))
+ {
+ /*
+ * relation and attribute manipulation
+ */
+ case T_CreateSchemaStmt:
+ CreateSchemaCommand((CreateSchemaStmt *) parsetree,
+ queryString,
+ pstmt->stmt_location,
+ pstmt->stmt_len);
+
+ /*
+ * EventTriggerCollectSimpleCommand called by
+ * CreateSchemaCommand
+ */
+ commandCollected = true;
+ break;
+
+ case T_CreateStmt:
+ case T_CreateForeignTableStmt:
+ {
+ List *stmts;
+ RangeVar *table_rv = NULL;
+
+ /* Run parse analysis ... */
+ stmts = transformCreateStmt((CreateStmt *) parsetree,
+ queryString);
+
+ /*
+ * ... and do it. We can't use foreach() because we may
+ * modify the list midway through, so pick off the
+ * elements one at a time, the hard way.
+ */
+ while (stmts != NIL)
+ {
+ Node *stmt = (Node *) linitial(stmts);
+
+ stmts = list_delete_first(stmts);
+
+ if (IsA(stmt, CreateStmt))
+ {
+ CreateStmt *cstmt = (CreateStmt *) stmt;
+ Datum toast_options;
+ static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
+
+ /* Remember transformed RangeVar for LIKE */
+ table_rv = cstmt->relation;
+
+ /* Create the table itself */
+ address = DefineRelation(cstmt,
+ RELKIND_RELATION,
+ InvalidOid, NULL,
+ queryString);
+ EventTriggerCollectSimpleCommand(address,
+ secondaryObject,
+ stmt);
+
+ /*
+ * Let NewRelationCreateToastTable decide if this
+ * one needs a secondary relation too.
+ */
+ CommandCounterIncrement();
+
+ /*
+ * parse and validate reloptions for the toast
+ * table
+ */
+ toast_options = transformRelOptions((Datum) 0,
+ cstmt->options,
+ "toast",
+ validnsps,
+ true,
+ false);
+ (void) heap_reloptions(RELKIND_TOASTVALUE,
+ toast_options,
+ true);
+
+ NewRelationCreateToastTable(address.objectId,
+ toast_options);
+ }
+ else if (IsA(stmt, CreateForeignTableStmt))
+ {
+ CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) stmt;
+
+ /* Remember transformed RangeVar for LIKE */
+ table_rv = cstmt->base.relation;
+
+ /* Create the table itself */
+ address = DefineRelation(&cstmt->base,
+ RELKIND_FOREIGN_TABLE,
+ InvalidOid, NULL,
+ queryString);
+ CreateForeignTable(cstmt,
+ address.objectId);
+ EventTriggerCollectSimpleCommand(address,
+ secondaryObject,
+ stmt);
+ }
+ else if (IsA(stmt, TableLikeClause))
+ {
+ /*
+ * Do delayed processing of LIKE options. This
+ * will result in additional sub-statements for us
+ * to process. Those should get done before any
+ * remaining actions, so prepend them to "stmts".
+ */
+ TableLikeClause *like = (TableLikeClause *) stmt;
+ List *morestmts;
+
+ Assert(table_rv != NULL);
+
+ morestmts = expandTableLikeClause(table_rv, like);
+ stmts = list_concat(morestmts, stmts);
+ }
+ else
+ {
+ /*
+ * Recurse for anything else. Note the recursive
+ * call will stash the objects so created into our
+ * event trigger context.
+ */
+ PlannedStmt *wrapper;
+
+ wrapper = makeNode(PlannedStmt);
+ wrapper->commandType = CMD_UTILITY;
+ wrapper->canSetTag = false;
+ wrapper->utilityStmt = stmt;
+ wrapper->stmt_location = pstmt->stmt_location;
+ wrapper->stmt_len = pstmt->stmt_len;
+
+ ProcessUtility(wrapper,
+ queryString,
+ false,
+ PROCESS_UTILITY_SUBCOMMAND,
+ params,
+ NULL,
+ None_Receiver,
+ NULL);
+ }
+
+ /* Need CCI between commands */
+ if (stmts != NIL)
+ CommandCounterIncrement();
+ }
+
+ /*
+ * The multiple commands generated here are stashed
+ * individually, so disable collection below.
+ */
+ commandCollected = true;
+ }
+ break;
+
+ case T_AlterTableStmt:
+ {
+ AlterTableStmt *atstmt = (AlterTableStmt *) parsetree;
+ Oid relid;
+ LOCKMODE lockmode;
+ ListCell *cell;
+
+ /*
+ * Disallow ALTER TABLE .. DETACH CONCURRENTLY in a
+ * transaction block or function. (Perhaps it could be
+ * allowed in a procedure, but don't hold your breath.)
+ */
+ foreach(cell, atstmt->cmds)
+ {
+ AlterTableCmd *cmd = (AlterTableCmd *) lfirst(cell);
+
+ /* Disallow DETACH CONCURRENTLY in a transaction block */
+ if (cmd->subtype == AT_DetachPartition)
+ {
+ if (((PartitionCmd *) cmd->def)->concurrent)
+ PreventInTransactionBlock(isTopLevel,
+ "ALTER TABLE ... DETACH CONCURRENTLY");
+ }
+ }
+
+ /*
+ * Figure out lock mode, and acquire lock. This also does
+ * basic permissions checks, so that we won't wait for a
+ * lock on (for example) a relation on which we have no
+ * permissions.
+ */
+ lockmode = AlterTableGetLockLevel(atstmt->cmds);
+ relid = AlterTableLookupRelation(atstmt, lockmode);
+
+ if (OidIsValid(relid))
+ {
+ AlterTableUtilityContext atcontext;
+
+ /* Set up info needed for recursive callbacks ... */
+ atcontext.pstmt = pstmt;
+ atcontext.queryString = queryString;
+ atcontext.relid = relid;
+ atcontext.params = params;
+ atcontext.queryEnv = queryEnv;
+
+ /* ... ensure we have an event trigger context ... */
+ EventTriggerAlterTableStart(parsetree);
+ EventTriggerAlterTableRelid(relid);
+
+ /* ... and do it */
+ AlterTable(atstmt, lockmode, &atcontext);
+
+ /* done */
+ EventTriggerAlterTableEnd();
+ }
+ else
+ ereport(NOTICE,
+ (errmsg("relation \"%s\" does not exist, skipping",
+ atstmt->relation->relname)));
+ }
+
+ /* ALTER TABLE stashes commands internally */
+ commandCollected = true;
+ break;
+
+ case T_AlterDomainStmt:
+ {
+ AlterDomainStmt *stmt = (AlterDomainStmt *) parsetree;
+
+ /*
+ * Some or all of these functions are recursive to cover
+ * inherited things, so permission checks are done there.
+ */
+ switch (stmt->subtype)
+ {
+ case 'T': /* ALTER DOMAIN DEFAULT */
+
+ /*
+ * Recursively alter column default for table and,
+ * if requested, for descendants
+ */
+ address =
+ AlterDomainDefault(stmt->typeName,
+ stmt->def);
+ break;
+ case 'N': /* ALTER DOMAIN DROP NOT NULL */
+ address =
+ AlterDomainNotNull(stmt->typeName,
+ false);
+ break;
+ case 'O': /* ALTER DOMAIN SET NOT NULL */
+ address =
+ AlterDomainNotNull(stmt->typeName,
+ true);
+ break;
+ case 'C': /* ADD CONSTRAINT */
+ address =
+ AlterDomainAddConstraint(stmt->typeName,
+ stmt->def,
+ &secondaryObject);
+ break;
+ case 'X': /* DROP CONSTRAINT */
+ address =
+ AlterDomainDropConstraint(stmt->typeName,
+ stmt->name,
+ stmt->behavior,
+ stmt->missing_ok);
+ break;
+ case 'V': /* VALIDATE CONSTRAINT */
+ address =
+ AlterDomainValidateConstraint(stmt->typeName,
+ stmt->name);
+ break;
+ default: /* oops */
+ elog(ERROR, "unrecognized alter domain type: %d",
+ (int) stmt->subtype);
+ break;
+ }
+ }
+ break;
+
+ /*
+ * ************* object creation / destruction **************
+ */
+ case T_DefineStmt:
+ {
+ DefineStmt *stmt = (DefineStmt *) parsetree;
+
+ switch (stmt->kind)
+ {
+ case OBJECT_AGGREGATE:
+ address =
+ DefineAggregate(pstate, stmt->defnames, stmt->args,
+ stmt->oldstyle,
+ stmt->definition,
+ stmt->replace);
+ break;
+ case OBJECT_OPERATOR:
+ Assert(stmt->args == NIL);
+ address = DefineOperator(stmt->defnames,
+ stmt->definition);
+ break;
+ case OBJECT_TYPE:
+ Assert(stmt->args == NIL);
+ address = DefineType(pstate,
+ stmt->defnames,
+ stmt->definition);
+ break;
+ case OBJECT_TSPARSER:
+ Assert(stmt->args == NIL);
+ address = DefineTSParser(stmt->defnames,
+ stmt->definition);
+ break;
+ case OBJECT_TSDICTIONARY:
+ Assert(stmt->args == NIL);
+ address = DefineTSDictionary(stmt->defnames,
+ stmt->definition);
+ break;
+ case OBJECT_TSTEMPLATE:
+ Assert(stmt->args == NIL);
+ address = DefineTSTemplate(stmt->defnames,
+ stmt->definition);
+ break;
+ case OBJECT_TSCONFIGURATION:
+ Assert(stmt->args == NIL);
+ address = DefineTSConfiguration(stmt->defnames,
+ stmt->definition,
+ &secondaryObject);
+ break;
+ case OBJECT_COLLATION:
+ Assert(stmt->args == NIL);
+ address = DefineCollation(pstate,
+ stmt->defnames,
+ stmt->definition,
+ stmt->if_not_exists);
+ break;
+ default:
+ elog(ERROR, "unrecognized define stmt type: %d",
+ (int) stmt->kind);
+ break;
+ }
+ }
+ break;
+
+ case T_IndexStmt: /* CREATE INDEX */
+ {
+ IndexStmt *stmt = (IndexStmt *) parsetree;
+ Oid relid;
+ LOCKMODE lockmode;
+ bool is_alter_table;
+
+ if (stmt->concurrent)
+ PreventInTransactionBlock(isTopLevel,
+ "CREATE INDEX CONCURRENTLY");
+
+ /*
+ * Look up the relation OID just once, right here at the
+ * beginning, so that we don't end up repeating the name
+ * lookup later and latching onto a different relation
+ * partway through. To avoid lock upgrade hazards, it's
+ * important that we take the strongest lock that will
+ * eventually be needed here, so the lockmode calculation
+ * needs to match what DefineIndex() does.
+ */
+ lockmode = stmt->concurrent ? ShareUpdateExclusiveLock
+ : ShareLock;
+ relid =
+ RangeVarGetRelidExtended(stmt->relation, lockmode,
+ 0,
+ RangeVarCallbackOwnsRelation,
+ NULL);
+
+ /*
+ * CREATE INDEX on partitioned tables (but not regular
+ * inherited tables) recurses to partitions, so we must
+ * acquire locks early to avoid deadlocks.
+ *
+ * We also take the opportunity to verify that all
+ * partitions are something we can put an index on, to
+ * avoid building some indexes only to fail later.
+ */
+ if (stmt->relation->inh &&
+ get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE)
+ {
+ ListCell *lc;
+ List *inheritors = NIL;
+
+ inheritors = find_all_inheritors(relid, lockmode, NULL);
+ foreach(lc, inheritors)
+ {
+ char relkind = get_rel_relkind(lfirst_oid(lc));
+
+ if (relkind != RELKIND_RELATION &&
+ relkind != RELKIND_MATVIEW &&
+ relkind != RELKIND_PARTITIONED_TABLE &&
+ relkind != RELKIND_FOREIGN_TABLE)
+ elog(ERROR, "unexpected relkind \"%c\" on partition \"%s\"",
+ relkind, stmt->relation->relname);
+
+ if (relkind == RELKIND_FOREIGN_TABLE &&
+ (stmt->unique || stmt->primary))
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("cannot create unique index on partitioned table \"%s\"",
+ stmt->relation->relname),
+ errdetail("Table \"%s\" contains partitions that are foreign tables.",
+ stmt->relation->relname)));
+ }
+ list_free(inheritors);
+ }
+
+ /*
+ * If the IndexStmt is already transformed, it must have
+ * come from generateClonedIndexStmt, which in current
+ * usage means it came from expandTableLikeClause rather
+ * than from original parse analysis. And that means we
+ * must treat it like ALTER TABLE ADD INDEX, not CREATE.
+ * (This is a bit grotty, but currently it doesn't seem
+ * worth adding a separate bool field for the purpose.)
+ */
+ is_alter_table = stmt->transformed;
+
+ /* Run parse analysis ... */
+ stmt = transformIndexStmt(relid, stmt, queryString);
+
+ /* ... and do it */
+ EventTriggerAlterTableStart(parsetree);
+ address =
+ DefineIndex(relid, /* OID of heap relation */
+ stmt,
+ InvalidOid, /* no predefined OID */
+ InvalidOid, /* no parent index */
+ InvalidOid, /* no parent constraint */
+ is_alter_table,
+ true, /* check_rights */
+ true, /* check_not_in_use */
+ false, /* skip_build */
+ false); /* quiet */
+
+ /*
+ * Add the CREATE INDEX node itself to stash right away;
+ * if there were any commands stashed in the ALTER TABLE
+ * code, we need them to appear after this one.
+ */
+ EventTriggerCollectSimpleCommand(address, secondaryObject,
+ parsetree);
+ commandCollected = true;
+ EventTriggerAlterTableEnd();
+ }
+ break;
+
+ case T_CreateExtensionStmt:
+ address = CreateExtension(pstate, (CreateExtensionStmt *) parsetree);
+ break;
+
+ case T_AlterExtensionStmt:
+ address = ExecAlterExtensionStmt(pstate, (AlterExtensionStmt *) parsetree);
+ break;
+
+ case T_AlterExtensionContentsStmt:
+ address = ExecAlterExtensionContentsStmt((AlterExtensionContentsStmt *) parsetree,
+ &secondaryObject);
+ break;
+
+ case T_CreateFdwStmt:
+ address = CreateForeignDataWrapper(pstate, (CreateFdwStmt *) parsetree);
+ break;
+
+ case T_AlterFdwStmt:
+ address = AlterForeignDataWrapper(pstate, (AlterFdwStmt *) parsetree);
+ break;
+
+ case T_CreateForeignServerStmt:
+ address = CreateForeignServer((CreateForeignServerStmt *) parsetree);
+ break;
+
+ case T_AlterForeignServerStmt:
+ address = AlterForeignServer((AlterForeignServerStmt *) parsetree);
+ break;
+
+ case T_CreateUserMappingStmt:
+ address = CreateUserMapping((CreateUserMappingStmt *) parsetree);
+ break;
+
+ case T_AlterUserMappingStmt:
+ address = AlterUserMapping((AlterUserMappingStmt *) parsetree);
+ break;
+
+ case T_DropUserMappingStmt:
+ RemoveUserMapping((DropUserMappingStmt *) parsetree);
+ /* no commands stashed for DROP */
+ commandCollected = true;
+ break;
+
+ case T_ImportForeignSchemaStmt:
+ ImportForeignSchema((ImportForeignSchemaStmt *) parsetree);
+ /* commands are stashed inside ImportForeignSchema */
+ commandCollected = true;
+ break;
+
+ case T_CompositeTypeStmt: /* CREATE TYPE (composite) */
+ {
+ CompositeTypeStmt *stmt = (CompositeTypeStmt *) parsetree;
+
+ address = DefineCompositeType(stmt->typevar,
+ stmt->coldeflist);
+ }
+ break;
+
+ case T_CreateEnumStmt: /* CREATE TYPE AS ENUM */
+ address = DefineEnum((CreateEnumStmt *) parsetree);
+ break;
+
+ case T_CreateRangeStmt: /* CREATE TYPE AS RANGE */
+ address = DefineRange(pstate, (CreateRangeStmt *) parsetree);
+ break;
+
+ case T_AlterEnumStmt: /* ALTER TYPE (enum) */
+ address = AlterEnum((AlterEnumStmt *) parsetree);
+ break;
+
+ case T_ViewStmt: /* CREATE VIEW */
+ EventTriggerAlterTableStart(parsetree);
+ address = DefineView((ViewStmt *) parsetree, queryString,
+ pstmt->stmt_location, pstmt->stmt_len);
+ EventTriggerCollectSimpleCommand(address, secondaryObject,
+ parsetree);
+ /* stashed internally */
+ commandCollected = true;
+ EventTriggerAlterTableEnd();
+ break;
+
+ case T_CreateFunctionStmt: /* CREATE FUNCTION */
+ address = CreateFunction(pstate, (CreateFunctionStmt *) parsetree);
+ break;
+
+ case T_AlterFunctionStmt: /* ALTER FUNCTION */
+ address = AlterFunction(pstate, (AlterFunctionStmt *) parsetree);
+ break;
+
+ case T_RuleStmt: /* CREATE RULE */
+ address = DefineRule((RuleStmt *) parsetree, queryString);
+ break;
+
+ case T_CreateSeqStmt:
+ address = DefineSequence(pstate, (CreateSeqStmt *) parsetree);
+ break;
+
+ case T_AlterSeqStmt:
+ address = AlterSequence(pstate, (AlterSeqStmt *) parsetree);
+ break;
+
+ case T_CreateTableAsStmt:
+ address = ExecCreateTableAs(pstate, (CreateTableAsStmt *) parsetree,
+ params, queryEnv, qc);
+ break;
+
+ case T_RefreshMatViewStmt:
+
+ /*
+ * REFRESH CONCURRENTLY executes some DDL commands internally.
+ * Inhibit DDL command collection here to avoid those commands
+ * from showing up in the deparsed command queue. The refresh
+ * command itself is queued, which is enough.
+ */
+ EventTriggerInhibitCommandCollection();
+ PG_TRY();
+ {
+ address = ExecRefreshMatView((RefreshMatViewStmt *) parsetree,
+ queryString, params, qc);
+ }
+ PG_FINALLY();
+ {
+ EventTriggerUndoInhibitCommandCollection();
+ }
+ PG_END_TRY();
+ break;
+
+ case T_CreateTrigStmt:
+ address = CreateTrigger((CreateTrigStmt *) parsetree,
+ queryString, InvalidOid, InvalidOid,
+ InvalidOid, InvalidOid, InvalidOid,
+ InvalidOid, NULL, false, false);
+ break;
+
+ case T_CreatePLangStmt:
+ address = CreateProceduralLanguage((CreatePLangStmt *) parsetree);
+ break;
+
+ case T_CreateDomainStmt:
+ address = DefineDomain((CreateDomainStmt *) parsetree);
+ break;
+
+ case T_CreateConversionStmt:
+ address = CreateConversionCommand((CreateConversionStmt *) parsetree);
+ break;
+
+ case T_CreateCastStmt:
+ address = CreateCast((CreateCastStmt *) parsetree);
+ break;
+
+ case T_CreateOpClassStmt:
+ DefineOpClass((CreateOpClassStmt *) parsetree);
+ /* command is stashed in DefineOpClass */
+ commandCollected = true;
+ break;
+
+ case T_CreateOpFamilyStmt:
+ address = DefineOpFamily((CreateOpFamilyStmt *) parsetree);
+
+ /*
+ * DefineOpFamily calls EventTriggerCollectSimpleCommand
+ * directly.
+ */
+ commandCollected = true;
+ break;
+
+ case T_CreateTransformStmt:
+ address = CreateTransform((CreateTransformStmt *) parsetree);
+ break;
+
+ case T_AlterOpFamilyStmt:
+ AlterOpFamily((AlterOpFamilyStmt *) parsetree);
+ /* commands are stashed in AlterOpFamily */
+ commandCollected = true;
+ break;
+
+ case T_AlterTSDictionaryStmt:
+ address = AlterTSDictionary((AlterTSDictionaryStmt *) parsetree);
+ break;
+
+ case T_AlterTSConfigurationStmt:
+ AlterTSConfiguration((AlterTSConfigurationStmt *) parsetree);
+
+ /*
+ * Commands are stashed in MakeConfigurationMapping and
+ * DropConfigurationMapping, which are called from
+ * AlterTSConfiguration
+ */
+ commandCollected = true;
+ break;
+
+ case T_AlterTableMoveAllStmt:
+ AlterTableMoveAll((AlterTableMoveAllStmt *) parsetree);
+ /* commands are stashed in AlterTableMoveAll */
+ commandCollected = true;
+ break;
+
+ case T_DropStmt:
+ ExecDropStmt((DropStmt *) parsetree, isTopLevel);
+ /* no commands stashed for DROP */
+ commandCollected = true;
+ break;
+
+ case T_RenameStmt:
+ address = ExecRenameStmt((RenameStmt *) parsetree);
+ break;
+
+ case T_AlterObjectDependsStmt:
+ address =
+ ExecAlterObjectDependsStmt((AlterObjectDependsStmt *) parsetree,
+ &secondaryObject);
+ break;
+
+ case T_AlterObjectSchemaStmt:
+ address =
+ ExecAlterObjectSchemaStmt((AlterObjectSchemaStmt *) parsetree,
+ &secondaryObject);
+ break;
+
+ case T_AlterOwnerStmt:
+ address = ExecAlterOwnerStmt((AlterOwnerStmt *) parsetree);
+ break;
+
+ case T_AlterOperatorStmt:
+ address = AlterOperator((AlterOperatorStmt *) parsetree);
+ break;
+
+ case T_AlterTypeStmt:
+ address = AlterType((AlterTypeStmt *) parsetree);
+ break;
+
+ case T_CommentStmt:
+ address = CommentObject((CommentStmt *) parsetree);
+ break;
+
+ case T_GrantStmt:
+ ExecuteGrantStmt((GrantStmt *) parsetree);
+ /* commands are stashed in ExecGrantStmt_oids */
+ commandCollected = true;
+ break;
+
+ case T_DropOwnedStmt:
+ DropOwnedObjects((DropOwnedStmt *) parsetree);
+ /* no commands stashed for DROP */
+ commandCollected = true;
+ break;
+
+ case T_AlterDefaultPrivilegesStmt:
+ ExecAlterDefaultPrivilegesStmt(pstate, (AlterDefaultPrivilegesStmt *) parsetree);
+ EventTriggerCollectAlterDefPrivs((AlterDefaultPrivilegesStmt *) parsetree);
+ commandCollected = true;
+ break;
+
+ case T_CreatePolicyStmt: /* CREATE POLICY */
+ address = CreatePolicy((CreatePolicyStmt *) parsetree);
+ break;
+
+ case T_AlterPolicyStmt: /* ALTER POLICY */
+ address = AlterPolicy((AlterPolicyStmt *) parsetree);
+ break;
+
+ case T_SecLabelStmt:
+ address = ExecSecLabelStmt((SecLabelStmt *) parsetree);
+ break;
+
+ case T_CreateAmStmt:
+ address = CreateAccessMethod((CreateAmStmt *) parsetree);
+ break;
+
+ case T_CreatePublicationStmt:
+ address = CreatePublication(pstate, (CreatePublicationStmt *) parsetree);
+ break;
+
+ case T_AlterPublicationStmt:
+ AlterPublication(pstate, (AlterPublicationStmt *) parsetree);
+
+ /*
+ * AlterPublication calls EventTriggerCollectSimpleCommand
+ * directly
+ */
+ commandCollected = true;
+ break;
+
+ case T_CreateSubscriptionStmt:
+ address = CreateSubscription(pstate,
+ (CreateSubscriptionStmt *) parsetree,
+ isTopLevel);
+ break;
+
+ case T_AlterSubscriptionStmt:
+ address = AlterSubscription(pstate,
+ (AlterSubscriptionStmt *) parsetree,
+ isTopLevel);
+ break;
+
+ case T_DropSubscriptionStmt:
+ DropSubscription((DropSubscriptionStmt *) parsetree, isTopLevel);
+ /* no commands stashed for DROP */
+ commandCollected = true;
+ break;
+
+ case T_CreateStatsStmt:
+ {
+ Oid relid;
+ CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree;
+ RangeVar *rel = (RangeVar *) linitial(stmt->relations);
+
+ if (!IsA(rel, RangeVar))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("only a single relation is allowed in CREATE STATISTICS")));
+
+ /*
+ * CREATE STATISTICS will influence future execution plans
+ * but does not interfere with currently executing plans.
+ * So it should be enough to take ShareUpdateExclusiveLock
+ * on relation, conflicting with ANALYZE and other DDL
+ * that sets statistical information, but not with normal
+ * queries.
+ *
+ * XXX RangeVarCallbackOwnsRelation not needed here, to
+ * keep the same behavior as before.
+ */
+ relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false);
+
+ /* Run parse analysis ... */
+ stmt = transformStatsStmt(relid, stmt, queryString);
+
+ address = CreateStatistics(stmt);
+ }
+ break;
+
+ case T_AlterStatsStmt:
+ address = AlterStatistics((AlterStatsStmt *) parsetree);
+ break;
+
+ case T_AlterCollationStmt:
+ address = AlterCollation((AlterCollationStmt *) parsetree);
+ break;
+
+ default:
+ elog(ERROR, "unrecognized node type: %d",
+ (int) nodeTag(parsetree));
+ break;
+ }
+
+ /*
+ * Remember the object so that ddl_command_end event triggers have
+ * access to it.
+ */
+ if (!commandCollected)
+ EventTriggerCollectSimpleCommand(address, secondaryObject,
+ parsetree);
+
+ if (isCompleteQuery)
+ {
+ EventTriggerSQLDrop(parsetree);
+ EventTriggerDDLCommandEnd(parsetree);
+ }
+ }
+ PG_FINALLY();
+ {
+ if (needCleanup)
+ EventTriggerEndCompleteQuery();
+ }
+ PG_END_TRY();
+}
+
+/*
+ * ProcessUtilityForAlterTable
+ * Recursive entry from ALTER TABLE
+ *
+ * ALTER TABLE sometimes generates subcommands such as CREATE INDEX.
+ * It calls this, not the main entry point ProcessUtility, to execute
+ * such subcommands.
+ *
+ * stmt: the utility command to execute
+ * context: opaque passthrough struct with the info we need
+ *
+ * It's caller's responsibility to do CommandCounterIncrement after
+ * calling this, if needed.
+ */
+void
+ProcessUtilityForAlterTable(Node *stmt, AlterTableUtilityContext *context)
+{
+ PlannedStmt *wrapper;
+
+ /*
+ * For event triggers, we must "close" the current complex-command set,
+ * and start a new one afterwards; this is needed to ensure the ordering
+ * of command events is consistent with the way they were executed.
+ */
+ EventTriggerAlterTableEnd();
+
+ /* Create a suitable wrapper */
+ wrapper = makeNode(PlannedStmt);
+ wrapper->commandType = CMD_UTILITY;
+ wrapper->canSetTag = false;
+ wrapper->utilityStmt = stmt;
+ wrapper->stmt_location = context->pstmt->stmt_location;
+ wrapper->stmt_len = context->pstmt->stmt_len;
+
+ ProcessUtility(wrapper,
+ context->queryString,
+ false,
+ PROCESS_UTILITY_SUBCOMMAND,
+ context->params,
+ context->queryEnv,
+ None_Receiver,
+ NULL);
+
+ EventTriggerAlterTableStart(context->pstmt->utilityStmt);
+ EventTriggerAlterTableRelid(context->relid);
+}
+
+/*
+ * Dispatch function for DropStmt
+ */
+static void
+ExecDropStmt(DropStmt *stmt, bool isTopLevel)
+{
+ switch (stmt->removeType)
+ {
+ case OBJECT_INDEX:
+ if (stmt->concurrent)
+ PreventInTransactionBlock(isTopLevel,
+ "DROP INDEX CONCURRENTLY");
+ /* fall through */
+
+ case OBJECT_TABLE:
+ case OBJECT_SEQUENCE:
+ case OBJECT_VIEW:
+ case OBJECT_MATVIEW:
+ case OBJECT_FOREIGN_TABLE:
+ RemoveRelations(stmt);
+ break;
+ default:
+ RemoveObjects(stmt);
+ break;
+ }
+}
+
+
+/*
+ * UtilityReturnsTuples
+ * Return "true" if this utility statement will send output to the
+ * destination.
+ *
+ * Generally, there should be a case here for each case in ProcessUtility
+ * where "dest" is passed on.
+ */
+bool
+UtilityReturnsTuples(Node *parsetree)
+{
+ switch (nodeTag(parsetree))
+ {
+ case T_CallStmt:
+ {
+ CallStmt *stmt = (CallStmt *) parsetree;
+
+ return (stmt->funcexpr->funcresulttype == RECORDOID);
+ }
+ case T_FetchStmt:
+ {
+ FetchStmt *stmt = (FetchStmt *) parsetree;
+ Portal portal;
+
+ if (stmt->ismove)
+ return false;
+ portal = GetPortalByName(stmt->portalname);
+ if (!PortalIsValid(portal))
+ return false; /* not our business to raise error */
+ return portal->tupDesc ? true : false;
+ }
+
+ case T_ExecuteStmt:
+ {
+ ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
+ PreparedStatement *entry;
+
+ entry = FetchPreparedStatement(stmt->name, false);
+ if (!entry)
+ return false; /* not our business to raise error */
+ if (entry->plansource->resultDesc)
+ return true;
+ return false;
+ }
+
+ case T_ExplainStmt:
+ return true;
+
+ case T_VariableShowStmt:
+ return true;
+
+ default:
+ return false;
+ }
+}
+
+/*
+ * UtilityTupleDescriptor
+ * Fetch the actual output tuple descriptor for a utility statement
+ * for which UtilityReturnsTuples() previously returned "true".
+ *
+ * The returned descriptor is created in (or copied into) the current memory
+ * context.
+ */
+TupleDesc
+UtilityTupleDescriptor(Node *parsetree)
+{
+ switch (nodeTag(parsetree))
+ {
+ case T_CallStmt:
+ return CallStmtResultDesc((CallStmt *) parsetree);
+
+ case T_FetchStmt:
+ {
+ FetchStmt *stmt = (FetchStmt *) parsetree;
+ Portal portal;
+
+ if (stmt->ismove)
+ return NULL;
+ portal = GetPortalByName(stmt->portalname);
+ if (!PortalIsValid(portal))
+ return NULL; /* not our business to raise error */
+ return CreateTupleDescCopy(portal->tupDesc);
+ }
+
+ case T_ExecuteStmt:
+ {
+ ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
+ PreparedStatement *entry;
+
+ entry = FetchPreparedStatement(stmt->name, false);
+ if (!entry)
+ return NULL; /* not our business to raise error */
+ return FetchPreparedStatementResultDesc(entry);
+ }
+
+ case T_ExplainStmt:
+ return ExplainResultDesc((ExplainStmt *) parsetree);
+
+ case T_VariableShowStmt:
+ {
+ VariableShowStmt *n = (VariableShowStmt *) parsetree;
+
+ return GetPGVariableResultDesc(n->name);
+ }
+
+ default:
+ return NULL;
+ }
+}
+
+
+/*
+ * QueryReturnsTuples
+ * Return "true" if this Query will send output to the destination.
+ */
+#ifdef NOT_USED
+bool
+QueryReturnsTuples(Query *parsetree)
+{
+ switch (parsetree->commandType)
+ {
+ case CMD_SELECT:
+ /* returns tuples */
+ return true;
+ case CMD_MERGE:
+ return false;
+ case CMD_INSERT:
+ case CMD_UPDATE:
+ case CMD_DELETE:
+ /* the forms with RETURNING return tuples */
+ if (parsetree->returningList)
+ return true;
+ break;
+ case CMD_UTILITY:
+ return UtilityReturnsTuples(parsetree->utilityStmt);
+ case CMD_UNKNOWN:
+ case CMD_NOTHING:
+ /* probably shouldn't get here */
+ break;
+ }
+ return false; /* default */
+}
+#endif
+
+
+/*
+ * UtilityContainsQuery
+ * Return the contained Query, or NULL if there is none
+ *
+ * Certain utility statements, such as EXPLAIN, contain a plannable Query.
+ * This function encapsulates knowledge of exactly which ones do.
+ * We assume it is invoked only on already-parse-analyzed statements
+ * (else the contained parsetree isn't a Query yet).
+ *
+ * In some cases (currently, only EXPLAIN of CREATE TABLE AS/SELECT INTO and
+ * CREATE MATERIALIZED VIEW), potentially Query-containing utility statements
+ * can be nested. This function will drill down to a non-utility Query, or
+ * return NULL if none.
+ */
+Query *
+UtilityContainsQuery(Node *parsetree)
+{
+ Query *qry;
+
+ switch (nodeTag(parsetree))
+ {
+ case T_DeclareCursorStmt:
+ qry = castNode(Query, ((DeclareCursorStmt *) parsetree)->query);
+ if (qry->commandType == CMD_UTILITY)
+ return UtilityContainsQuery(qry->utilityStmt);
+ return qry;
+
+ case T_ExplainStmt:
+ qry = castNode(Query, ((ExplainStmt *) parsetree)->query);
+ if (qry->commandType == CMD_UTILITY)
+ return UtilityContainsQuery(qry->utilityStmt);
+ return qry;
+
+ case T_CreateTableAsStmt:
+ qry = castNode(Query, ((CreateTableAsStmt *) parsetree)->query);
+ if (qry->commandType == CMD_UTILITY)
+ return UtilityContainsQuery(qry->utilityStmt);
+ return qry;
+
+ default:
+ return NULL;
+ }
+}
+
+
+/*
+ * AlterObjectTypeCommandTag
+ * helper function for CreateCommandTag
+ *
+ * This covers most cases where ALTER is used with an ObjectType enum.
+ */
+static CommandTag
+AlterObjectTypeCommandTag(ObjectType objtype)
+{
+ CommandTag tag;
+
+ switch (objtype)
+ {
+ case OBJECT_AGGREGATE:
+ tag = CMDTAG_ALTER_AGGREGATE;
+ break;
+ case OBJECT_ATTRIBUTE:
+ tag = CMDTAG_ALTER_TYPE;
+ break;
+ case OBJECT_CAST:
+ tag = CMDTAG_ALTER_CAST;
+ break;
+ case OBJECT_COLLATION:
+ tag = CMDTAG_ALTER_COLLATION;
+ break;
+ case OBJECT_COLUMN:
+ tag = CMDTAG_ALTER_TABLE;
+ break;
+ case OBJECT_CONVERSION:
+ tag = CMDTAG_ALTER_CONVERSION;
+ break;
+ case OBJECT_DATABASE:
+ tag = CMDTAG_ALTER_DATABASE;
+ break;
+ case OBJECT_DOMAIN:
+ case OBJECT_DOMCONSTRAINT:
+ tag = CMDTAG_ALTER_DOMAIN;
+ break;
+ case OBJECT_EXTENSION:
+ tag = CMDTAG_ALTER_EXTENSION;
+ break;
+ case OBJECT_FDW:
+ tag = CMDTAG_ALTER_FOREIGN_DATA_WRAPPER;
+ break;
+ case OBJECT_FOREIGN_SERVER:
+ tag = CMDTAG_ALTER_SERVER;
+ break;
+ case OBJECT_FOREIGN_TABLE:
+ tag = CMDTAG_ALTER_FOREIGN_TABLE;
+ break;
+ case OBJECT_FUNCTION:
+ tag = CMDTAG_ALTER_FUNCTION;
+ break;
+ case OBJECT_INDEX:
+ tag = CMDTAG_ALTER_INDEX;
+ break;
+ case OBJECT_LANGUAGE:
+ tag = CMDTAG_ALTER_LANGUAGE;
+ break;
+ case OBJECT_LARGEOBJECT:
+ tag = CMDTAG_ALTER_LARGE_OBJECT;
+ break;
+ case OBJECT_OPCLASS:
+ tag = CMDTAG_ALTER_OPERATOR_CLASS;
+ break;
+ case OBJECT_OPERATOR:
+ tag = CMDTAG_ALTER_OPERATOR;
+ break;
+ case OBJECT_OPFAMILY:
+ tag = CMDTAG_ALTER_OPERATOR_FAMILY;
+ break;
+ case OBJECT_POLICY:
+ tag = CMDTAG_ALTER_POLICY;
+ break;
+ case OBJECT_PROCEDURE:
+ tag = CMDTAG_ALTER_PROCEDURE;
+ break;
+ case OBJECT_ROLE:
+ tag = CMDTAG_ALTER_ROLE;
+ break;
+ case OBJECT_ROUTINE:
+ tag = CMDTAG_ALTER_ROUTINE;
+ break;
+ case OBJECT_RULE:
+ tag = CMDTAG_ALTER_RULE;
+ break;
+ case OBJECT_SCHEMA:
+ tag = CMDTAG_ALTER_SCHEMA;
+ break;
+ case OBJECT_SEQUENCE:
+ tag = CMDTAG_ALTER_SEQUENCE;
+ break;
+ case OBJECT_TABLE:
+ case OBJECT_TABCONSTRAINT:
+ tag = CMDTAG_ALTER_TABLE;
+ break;
+ case OBJECT_TABLESPACE:
+ tag = CMDTAG_ALTER_TABLESPACE;
+ break;
+ case OBJECT_TRIGGER:
+ tag = CMDTAG_ALTER_TRIGGER;
+ break;
+ case OBJECT_EVENT_TRIGGER:
+ tag = CMDTAG_ALTER_EVENT_TRIGGER;
+ break;
+ case OBJECT_TSCONFIGURATION:
+ tag = CMDTAG_ALTER_TEXT_SEARCH_CONFIGURATION;
+ break;
+ case OBJECT_TSDICTIONARY:
+ tag = CMDTAG_ALTER_TEXT_SEARCH_DICTIONARY;
+ break;
+ case OBJECT_TSPARSER:
+ tag = CMDTAG_ALTER_TEXT_SEARCH_PARSER;
+ break;
+ case OBJECT_TSTEMPLATE:
+ tag = CMDTAG_ALTER_TEXT_SEARCH_TEMPLATE;
+ break;
+ case OBJECT_TYPE:
+ tag = CMDTAG_ALTER_TYPE;
+ break;
+ case OBJECT_VIEW:
+ tag = CMDTAG_ALTER_VIEW;
+ break;
+ case OBJECT_MATVIEW:
+ tag = CMDTAG_ALTER_MATERIALIZED_VIEW;
+ break;
+ case OBJECT_PUBLICATION:
+ tag = CMDTAG_ALTER_PUBLICATION;
+ break;
+ case OBJECT_SUBSCRIPTION:
+ tag = CMDTAG_ALTER_SUBSCRIPTION;
+ break;
+ case OBJECT_STATISTIC_EXT:
+ tag = CMDTAG_ALTER_STATISTICS;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ break;
+ }
+
+ return tag;
+}
+
+/*
+ * CreateCommandTag
+ * utility to get a CommandTag for the command operation,
+ * given either a raw (un-analyzed) parsetree, an analyzed Query,
+ * or a PlannedStmt.
+ *
+ * This must handle all command types, but since the vast majority
+ * of 'em are utility commands, it seems sensible to keep it here.
+ */
+CommandTag
+CreateCommandTag(Node *parsetree)
+{
+ CommandTag tag;
+
+ switch (nodeTag(parsetree))
+ {
+ /* recurse if we're given a RawStmt */
+ case T_RawStmt:
+ tag = CreateCommandTag(((RawStmt *) parsetree)->stmt);
+ break;
+
+ /* raw plannable queries */
+ case T_InsertStmt:
+ tag = CMDTAG_INSERT;
+ break;
+
+ case T_DeleteStmt:
+ tag = CMDTAG_DELETE;
+ break;
+
+ case T_UpdateStmt:
+ tag = CMDTAG_UPDATE;
+ break;
+
+ case T_MergeStmt:
+ tag = CMDTAG_MERGE;
+ break;
+
+ case T_SelectStmt:
+ tag = CMDTAG_SELECT;
+ break;
+
+ case T_PLAssignStmt:
+ tag = CMDTAG_SELECT;
+ break;
+
+ /* utility statements --- same whether raw or cooked */
+ case T_TransactionStmt:
+ {
+ TransactionStmt *stmt = (TransactionStmt *) parsetree;
+
+ switch (stmt->kind)
+ {
+ case TRANS_STMT_BEGIN:
+ tag = CMDTAG_BEGIN;
+ break;
+
+ case TRANS_STMT_START:
+ tag = CMDTAG_START_TRANSACTION;
+ break;
+
+ case TRANS_STMT_COMMIT:
+ tag = CMDTAG_COMMIT;
+ break;
+
+ case TRANS_STMT_ROLLBACK:
+ case TRANS_STMT_ROLLBACK_TO:
+ tag = CMDTAG_ROLLBACK;
+ break;
+
+ case TRANS_STMT_SAVEPOINT:
+ tag = CMDTAG_SAVEPOINT;
+ break;
+
+ case TRANS_STMT_RELEASE:
+ tag = CMDTAG_RELEASE;
+ break;
+
+ case TRANS_STMT_PREPARE:
+ tag = CMDTAG_PREPARE_TRANSACTION;
+ break;
+
+ case TRANS_STMT_COMMIT_PREPARED:
+ tag = CMDTAG_COMMIT_PREPARED;
+ break;
+
+ case TRANS_STMT_ROLLBACK_PREPARED:
+ tag = CMDTAG_ROLLBACK_PREPARED;
+ break;
+
+ default:
+ tag = CMDTAG_UNKNOWN;
+ break;
+ }
+ }
+ break;
+
+ case T_DeclareCursorStmt:
+ tag = CMDTAG_DECLARE_CURSOR;
+ break;
+
+ case T_ClosePortalStmt:
+ {
+ ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
+
+ if (stmt->portalname == NULL)
+ tag = CMDTAG_CLOSE_CURSOR_ALL;
+ else
+ tag = CMDTAG_CLOSE_CURSOR;
+ }
+ break;
+
+ case T_FetchStmt:
+ {
+ FetchStmt *stmt = (FetchStmt *) parsetree;
+
+ tag = (stmt->ismove) ? CMDTAG_MOVE : CMDTAG_FETCH;
+ }
+ break;
+
+ case T_CreateDomainStmt:
+ tag = CMDTAG_CREATE_DOMAIN;
+ break;
+
+ case T_CreateSchemaStmt:
+ tag = CMDTAG_CREATE_SCHEMA;
+ break;
+
+ case T_CreateStmt:
+ tag = CMDTAG_CREATE_TABLE;
+ break;
+
+ case T_CreateTableSpaceStmt:
+ tag = CMDTAG_CREATE_TABLESPACE;
+ break;
+
+ case T_DropTableSpaceStmt:
+ tag = CMDTAG_DROP_TABLESPACE;
+ break;
+
+ case T_AlterTableSpaceOptionsStmt:
+ tag = CMDTAG_ALTER_TABLESPACE;
+ break;
+
+ case T_CreateExtensionStmt:
+ tag = CMDTAG_CREATE_EXTENSION;
+ break;
+
+ case T_AlterExtensionStmt:
+ tag = CMDTAG_ALTER_EXTENSION;
+ break;
+
+ case T_AlterExtensionContentsStmt:
+ tag = CMDTAG_ALTER_EXTENSION;
+ break;
+
+ case T_CreateFdwStmt:
+ tag = CMDTAG_CREATE_FOREIGN_DATA_WRAPPER;
+ break;
+
+ case T_AlterFdwStmt:
+ tag = CMDTAG_ALTER_FOREIGN_DATA_WRAPPER;
+ break;
+
+ case T_CreateForeignServerStmt:
+ tag = CMDTAG_CREATE_SERVER;
+ break;
+
+ case T_AlterForeignServerStmt:
+ tag = CMDTAG_ALTER_SERVER;
+ break;
+
+ case T_CreateUserMappingStmt:
+ tag = CMDTAG_CREATE_USER_MAPPING;
+ break;
+
+ case T_AlterUserMappingStmt:
+ tag = CMDTAG_ALTER_USER_MAPPING;
+ break;
+
+ case T_DropUserMappingStmt:
+ tag = CMDTAG_DROP_USER_MAPPING;
+ break;
+
+ case T_CreateForeignTableStmt:
+ tag = CMDTAG_CREATE_FOREIGN_TABLE;
+ break;
+
+ case T_ImportForeignSchemaStmt:
+ tag = CMDTAG_IMPORT_FOREIGN_SCHEMA;
+ break;
+
+ case T_DropStmt:
+ switch (((DropStmt *) parsetree)->removeType)
+ {
+ case OBJECT_TABLE:
+ tag = CMDTAG_DROP_TABLE;
+ break;
+ case OBJECT_SEQUENCE:
+ tag = CMDTAG_DROP_SEQUENCE;
+ break;
+ case OBJECT_VIEW:
+ tag = CMDTAG_DROP_VIEW;
+ break;
+ case OBJECT_MATVIEW:
+ tag = CMDTAG_DROP_MATERIALIZED_VIEW;
+ break;
+ case OBJECT_INDEX:
+ tag = CMDTAG_DROP_INDEX;
+ break;
+ case OBJECT_TYPE:
+ tag = CMDTAG_DROP_TYPE;
+ break;
+ case OBJECT_DOMAIN:
+ tag = CMDTAG_DROP_DOMAIN;
+ break;
+ case OBJECT_COLLATION:
+ tag = CMDTAG_DROP_COLLATION;
+ break;
+ case OBJECT_CONVERSION:
+ tag = CMDTAG_DROP_CONVERSION;
+ break;
+ case OBJECT_SCHEMA:
+ tag = CMDTAG_DROP_SCHEMA;
+ break;
+ case OBJECT_TSPARSER:
+ tag = CMDTAG_DROP_TEXT_SEARCH_PARSER;
+ break;
+ case OBJECT_TSDICTIONARY:
+ tag = CMDTAG_DROP_TEXT_SEARCH_DICTIONARY;
+ break;
+ case OBJECT_TSTEMPLATE:
+ tag = CMDTAG_DROP_TEXT_SEARCH_TEMPLATE;
+ break;
+ case OBJECT_TSCONFIGURATION:
+ tag = CMDTAG_DROP_TEXT_SEARCH_CONFIGURATION;
+ break;
+ case OBJECT_FOREIGN_TABLE:
+ tag = CMDTAG_DROP_FOREIGN_TABLE;
+ break;
+ case OBJECT_EXTENSION:
+ tag = CMDTAG_DROP_EXTENSION;
+ break;
+ case OBJECT_FUNCTION:
+ tag = CMDTAG_DROP_FUNCTION;
+ break;
+ case OBJECT_PROCEDURE:
+ tag = CMDTAG_DROP_PROCEDURE;
+ break;
+ case OBJECT_ROUTINE:
+ tag = CMDTAG_DROP_ROUTINE;
+ break;
+ case OBJECT_AGGREGATE:
+ tag = CMDTAG_DROP_AGGREGATE;
+ break;
+ case OBJECT_OPERATOR:
+ tag = CMDTAG_DROP_OPERATOR;
+ break;
+ case OBJECT_LANGUAGE:
+ tag = CMDTAG_DROP_LANGUAGE;
+ break;
+ case OBJECT_CAST:
+ tag = CMDTAG_DROP_CAST;
+ break;
+ case OBJECT_TRIGGER:
+ tag = CMDTAG_DROP_TRIGGER;
+ break;
+ case OBJECT_EVENT_TRIGGER:
+ tag = CMDTAG_DROP_EVENT_TRIGGER;
+ break;
+ case OBJECT_RULE:
+ tag = CMDTAG_DROP_RULE;
+ break;
+ case OBJECT_FDW:
+ tag = CMDTAG_DROP_FOREIGN_DATA_WRAPPER;
+ break;
+ case OBJECT_FOREIGN_SERVER:
+ tag = CMDTAG_DROP_SERVER;
+ break;
+ case OBJECT_OPCLASS:
+ tag = CMDTAG_DROP_OPERATOR_CLASS;
+ break;
+ case OBJECT_OPFAMILY:
+ tag = CMDTAG_DROP_OPERATOR_FAMILY;
+ break;
+ case OBJECT_POLICY:
+ tag = CMDTAG_DROP_POLICY;
+ break;
+ case OBJECT_TRANSFORM:
+ tag = CMDTAG_DROP_TRANSFORM;
+ break;
+ case OBJECT_ACCESS_METHOD:
+ tag = CMDTAG_DROP_ACCESS_METHOD;
+ break;
+ case OBJECT_PUBLICATION:
+ tag = CMDTAG_DROP_PUBLICATION;
+ break;
+ case OBJECT_STATISTIC_EXT:
+ tag = CMDTAG_DROP_STATISTICS;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ }
+ break;
+
+ case T_TruncateStmt:
+ tag = CMDTAG_TRUNCATE_TABLE;
+ break;
+
+ case T_CommentStmt:
+ tag = CMDTAG_COMMENT;
+ break;
+
+ case T_SecLabelStmt:
+ tag = CMDTAG_SECURITY_LABEL;
+ break;
+
+ case T_CopyStmt:
+ tag = CMDTAG_COPY;
+ break;
+
+ case T_RenameStmt:
+
+ /*
+ * When the column is renamed, the command tag is created from its
+ * relation type
+ */
+ tag = AlterObjectTypeCommandTag(((RenameStmt *) parsetree)->renameType == OBJECT_COLUMN ?
+ ((RenameStmt *) parsetree)->relationType :
+ ((RenameStmt *) parsetree)->renameType);
+ break;
+
+ case T_AlterObjectDependsStmt:
+ tag = AlterObjectTypeCommandTag(((AlterObjectDependsStmt *) parsetree)->objectType);
+ break;
+
+ case T_AlterObjectSchemaStmt:
+ tag = AlterObjectTypeCommandTag(((AlterObjectSchemaStmt *) parsetree)->objectType);
+ break;
+
+ case T_AlterOwnerStmt:
+ tag = AlterObjectTypeCommandTag(((AlterOwnerStmt *) parsetree)->objectType);
+ break;
+
+ case T_AlterTableMoveAllStmt:
+ tag = AlterObjectTypeCommandTag(((AlterTableMoveAllStmt *) parsetree)->objtype);
+ break;
+
+ case T_AlterTableStmt:
+ tag = AlterObjectTypeCommandTag(((AlterTableStmt *) parsetree)->objtype);
+ break;
+
+ case T_AlterDomainStmt:
+ tag = CMDTAG_ALTER_DOMAIN;
+ break;
+
+ case T_AlterFunctionStmt:
+ switch (((AlterFunctionStmt *) parsetree)->objtype)
+ {
+ case OBJECT_FUNCTION:
+ tag = CMDTAG_ALTER_FUNCTION;
+ break;
+ case OBJECT_PROCEDURE:
+ tag = CMDTAG_ALTER_PROCEDURE;
+ break;
+ case OBJECT_ROUTINE:
+ tag = CMDTAG_ALTER_ROUTINE;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ }
+ break;
+
+ case T_GrantStmt:
+ {
+ GrantStmt *stmt = (GrantStmt *) parsetree;
+
+ tag = (stmt->is_grant) ? CMDTAG_GRANT : CMDTAG_REVOKE;
+ }
+ break;
+
+ case T_GrantRoleStmt:
+ {
+ GrantRoleStmt *stmt = (GrantRoleStmt *) parsetree;
+
+ tag = (stmt->is_grant) ? CMDTAG_GRANT_ROLE : CMDTAG_REVOKE_ROLE;
+ }
+ break;
+
+ case T_AlterDefaultPrivilegesStmt:
+ tag = CMDTAG_ALTER_DEFAULT_PRIVILEGES;
+ break;
+
+ case T_DefineStmt:
+ switch (((DefineStmt *) parsetree)->kind)
+ {
+ case OBJECT_AGGREGATE:
+ tag = CMDTAG_CREATE_AGGREGATE;
+ break;
+ case OBJECT_OPERATOR:
+ tag = CMDTAG_CREATE_OPERATOR;
+ break;
+ case OBJECT_TYPE:
+ tag = CMDTAG_CREATE_TYPE;
+ break;
+ case OBJECT_TSPARSER:
+ tag = CMDTAG_CREATE_TEXT_SEARCH_PARSER;
+ break;
+ case OBJECT_TSDICTIONARY:
+ tag = CMDTAG_CREATE_TEXT_SEARCH_DICTIONARY;
+ break;
+ case OBJECT_TSTEMPLATE:
+ tag = CMDTAG_CREATE_TEXT_SEARCH_TEMPLATE;
+ break;
+ case OBJECT_TSCONFIGURATION:
+ tag = CMDTAG_CREATE_TEXT_SEARCH_CONFIGURATION;
+ break;
+ case OBJECT_COLLATION:
+ tag = CMDTAG_CREATE_COLLATION;
+ break;
+ case OBJECT_ACCESS_METHOD:
+ tag = CMDTAG_CREATE_ACCESS_METHOD;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ }
+ break;
+
+ case T_CompositeTypeStmt:
+ tag = CMDTAG_CREATE_TYPE;
+ break;
+
+ case T_CreateEnumStmt:
+ tag = CMDTAG_CREATE_TYPE;
+ break;
+
+ case T_CreateRangeStmt:
+ tag = CMDTAG_CREATE_TYPE;
+ break;
+
+ case T_AlterEnumStmt:
+ tag = CMDTAG_ALTER_TYPE;
+ break;
+
+ case T_ViewStmt:
+ tag = CMDTAG_CREATE_VIEW;
+ break;
+
+ case T_CreateFunctionStmt:
+ if (((CreateFunctionStmt *) parsetree)->is_procedure)
+ tag = CMDTAG_CREATE_PROCEDURE;
+ else
+ tag = CMDTAG_CREATE_FUNCTION;
+ break;
+
+ case T_IndexStmt:
+ tag = CMDTAG_CREATE_INDEX;
+ break;
+
+ case T_RuleStmt:
+ tag = CMDTAG_CREATE_RULE;
+ break;
+
+ case T_CreateSeqStmt:
+ tag = CMDTAG_CREATE_SEQUENCE;
+ break;
+
+ case T_AlterSeqStmt:
+ tag = CMDTAG_ALTER_SEQUENCE;
+ break;
+
+ case T_DoStmt:
+ tag = CMDTAG_DO;
+ break;
+
+ case T_CreatedbStmt:
+ tag = CMDTAG_CREATE_DATABASE;
+ break;
+
+ case T_AlterDatabaseStmt:
+ case T_AlterDatabaseRefreshCollStmt:
+ case T_AlterDatabaseSetStmt:
+ tag = CMDTAG_ALTER_DATABASE;
+ break;
+
+ case T_DropdbStmt:
+ tag = CMDTAG_DROP_DATABASE;
+ break;
+
+ case T_NotifyStmt:
+ tag = CMDTAG_NOTIFY;
+ break;
+
+ case T_ListenStmt:
+ tag = CMDTAG_LISTEN;
+ break;
+
+ case T_UnlistenStmt:
+ tag = CMDTAG_UNLISTEN;
+ break;
+
+ case T_LoadStmt:
+ tag = CMDTAG_LOAD;
+ break;
+
+ case T_CallStmt:
+ tag = CMDTAG_CALL;
+ break;
+
+ case T_ClusterStmt:
+ tag = CMDTAG_CLUSTER;
+ break;
+
+ case T_VacuumStmt:
+ if (((VacuumStmt *) parsetree)->is_vacuumcmd)
+ tag = CMDTAG_VACUUM;
+ else
+ tag = CMDTAG_ANALYZE;
+ break;
+
+ case T_ExplainStmt:
+ tag = CMDTAG_EXPLAIN;
+ break;
+
+ case T_CreateTableAsStmt:
+ switch (((CreateTableAsStmt *) parsetree)->objtype)
+ {
+ case OBJECT_TABLE:
+ if (((CreateTableAsStmt *) parsetree)->is_select_into)
+ tag = CMDTAG_SELECT_INTO;
+ else
+ tag = CMDTAG_CREATE_TABLE_AS;
+ break;
+ case OBJECT_MATVIEW:
+ tag = CMDTAG_CREATE_MATERIALIZED_VIEW;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ }
+ break;
+
+ case T_RefreshMatViewStmt:
+ tag = CMDTAG_REFRESH_MATERIALIZED_VIEW;
+ break;
+
+ case T_AlterSystemStmt:
+ tag = CMDTAG_ALTER_SYSTEM;
+ break;
+
+ case T_VariableSetStmt:
+ switch (((VariableSetStmt *) parsetree)->kind)
+ {
+ case VAR_SET_VALUE:
+ case VAR_SET_CURRENT:
+ case VAR_SET_DEFAULT:
+ case VAR_SET_MULTI:
+ tag = CMDTAG_SET;
+ break;
+ case VAR_RESET:
+ case VAR_RESET_ALL:
+ tag = CMDTAG_RESET;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ }
+ break;
+
+ case T_VariableShowStmt:
+ tag = CMDTAG_SHOW;
+ break;
+
+ case T_DiscardStmt:
+ switch (((DiscardStmt *) parsetree)->target)
+ {
+ case DISCARD_ALL:
+ tag = CMDTAG_DISCARD_ALL;
+ break;
+ case DISCARD_PLANS:
+ tag = CMDTAG_DISCARD_PLANS;
+ break;
+ case DISCARD_TEMP:
+ tag = CMDTAG_DISCARD_TEMP;
+ break;
+ case DISCARD_SEQUENCES:
+ tag = CMDTAG_DISCARD_SEQUENCES;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ }
+ break;
+
+ case T_CreateTransformStmt:
+ tag = CMDTAG_CREATE_TRANSFORM;
+ break;
+
+ case T_CreateTrigStmt:
+ tag = CMDTAG_CREATE_TRIGGER;
+ break;
+
+ case T_CreateEventTrigStmt:
+ tag = CMDTAG_CREATE_EVENT_TRIGGER;
+ break;
+
+ case T_AlterEventTrigStmt:
+ tag = CMDTAG_ALTER_EVENT_TRIGGER;
+ break;
+
+ case T_CreatePLangStmt:
+ tag = CMDTAG_CREATE_LANGUAGE;
+ break;
+
+ case T_CreateRoleStmt:
+ tag = CMDTAG_CREATE_ROLE;
+ break;
+
+ case T_AlterRoleStmt:
+ tag = CMDTAG_ALTER_ROLE;
+ break;
+
+ case T_AlterRoleSetStmt:
+ tag = CMDTAG_ALTER_ROLE;
+ break;
+
+ case T_DropRoleStmt:
+ tag = CMDTAG_DROP_ROLE;
+ break;
+
+ case T_DropOwnedStmt:
+ tag = CMDTAG_DROP_OWNED;
+ break;
+
+ case T_ReassignOwnedStmt:
+ tag = CMDTAG_REASSIGN_OWNED;
+ break;
+
+ case T_LockStmt:
+ tag = CMDTAG_LOCK_TABLE;
+ break;
+
+ case T_ConstraintsSetStmt:
+ tag = CMDTAG_SET_CONSTRAINTS;
+ break;
+
+ case T_CheckPointStmt:
+ tag = CMDTAG_CHECKPOINT;
+ break;
+
+ case T_ReindexStmt:
+ tag = CMDTAG_REINDEX;
+ break;
+
+ case T_CreateConversionStmt:
+ tag = CMDTAG_CREATE_CONVERSION;
+ break;
+
+ case T_CreateCastStmt:
+ tag = CMDTAG_CREATE_CAST;
+ break;
+
+ case T_CreateOpClassStmt:
+ tag = CMDTAG_CREATE_OPERATOR_CLASS;
+ break;
+
+ case T_CreateOpFamilyStmt:
+ tag = CMDTAG_CREATE_OPERATOR_FAMILY;
+ break;
+
+ case T_AlterOpFamilyStmt:
+ tag = CMDTAG_ALTER_OPERATOR_FAMILY;
+ break;
+
+ case T_AlterOperatorStmt:
+ tag = CMDTAG_ALTER_OPERATOR;
+ break;
+
+ case T_AlterTypeStmt:
+ tag = CMDTAG_ALTER_TYPE;
+ break;
+
+ case T_AlterTSDictionaryStmt:
+ tag = CMDTAG_ALTER_TEXT_SEARCH_DICTIONARY;
+ break;
+
+ case T_AlterTSConfigurationStmt:
+ tag = CMDTAG_ALTER_TEXT_SEARCH_CONFIGURATION;
+ break;
+
+ case T_CreatePolicyStmt:
+ tag = CMDTAG_CREATE_POLICY;
+ break;
+
+ case T_AlterPolicyStmt:
+ tag = CMDTAG_ALTER_POLICY;
+ break;
+
+ case T_CreateAmStmt:
+ tag = CMDTAG_CREATE_ACCESS_METHOD;
+ break;
+
+ case T_CreatePublicationStmt:
+ tag = CMDTAG_CREATE_PUBLICATION;
+ break;
+
+ case T_AlterPublicationStmt:
+ tag = CMDTAG_ALTER_PUBLICATION;
+ break;
+
+ case T_CreateSubscriptionStmt:
+ tag = CMDTAG_CREATE_SUBSCRIPTION;
+ break;
+
+ case T_AlterSubscriptionStmt:
+ tag = CMDTAG_ALTER_SUBSCRIPTION;
+ break;
+
+ case T_DropSubscriptionStmt:
+ tag = CMDTAG_DROP_SUBSCRIPTION;
+ break;
+
+ case T_AlterCollationStmt:
+ tag = CMDTAG_ALTER_COLLATION;
+ break;
+
+ case T_PrepareStmt:
+ tag = CMDTAG_PREPARE;
+ break;
+
+ case T_ExecuteStmt:
+ tag = CMDTAG_EXECUTE;
+ break;
+
+ case T_CreateStatsStmt:
+ tag = CMDTAG_CREATE_STATISTICS;
+ break;
+
+ case T_AlterStatsStmt:
+ tag = CMDTAG_ALTER_STATISTICS;
+ break;
+
+ case T_DeallocateStmt:
+ {
+ DeallocateStmt *stmt = (DeallocateStmt *) parsetree;
+
+ if (stmt->name == NULL)
+ tag = CMDTAG_DEALLOCATE_ALL;
+ else
+ tag = CMDTAG_DEALLOCATE;
+ }
+ break;
+
+ /* already-planned queries */
+ case T_PlannedStmt:
+ {
+ PlannedStmt *stmt = (PlannedStmt *) parsetree;
+
+ switch (stmt->commandType)
+ {
+ case CMD_SELECT:
+
+ /*
+ * We take a little extra care here so that the result
+ * will be useful for complaints about read-only
+ * statements
+ */
+ if (stmt->rowMarks != NIL)
+ {
+ /* not 100% but probably close enough */
+ switch (((PlanRowMark *) linitial(stmt->rowMarks))->strength)
+ {
+ case LCS_FORKEYSHARE:
+ tag = CMDTAG_SELECT_FOR_KEY_SHARE;
+ break;
+ case LCS_FORSHARE:
+ tag = CMDTAG_SELECT_FOR_SHARE;
+ break;
+ case LCS_FORNOKEYUPDATE:
+ tag = CMDTAG_SELECT_FOR_NO_KEY_UPDATE;
+ break;
+ case LCS_FORUPDATE:
+ tag = CMDTAG_SELECT_FOR_UPDATE;
+ break;
+ default:
+ tag = CMDTAG_SELECT;
+ break;
+ }
+ }
+ else
+ tag = CMDTAG_SELECT;
+ break;
+ case CMD_UPDATE:
+ tag = CMDTAG_UPDATE;
+ break;
+ case CMD_INSERT:
+ tag = CMDTAG_INSERT;
+ break;
+ case CMD_DELETE:
+ tag = CMDTAG_DELETE;
+ break;
+ case CMD_MERGE:
+ tag = CMDTAG_MERGE;
+ break;
+ case CMD_UTILITY:
+ tag = CreateCommandTag(stmt->utilityStmt);
+ break;
+ default:
+ elog(WARNING, "unrecognized commandType: %d",
+ (int) stmt->commandType);
+ tag = CMDTAG_UNKNOWN;
+ break;
+ }
+ }
+ break;
+
+ /* parsed-and-rewritten-but-not-planned queries */
+ case T_Query:
+ {
+ Query *stmt = (Query *) parsetree;
+
+ switch (stmt->commandType)
+ {
+ case CMD_SELECT:
+
+ /*
+ * We take a little extra care here so that the result
+ * will be useful for complaints about read-only
+ * statements
+ */
+ if (stmt->rowMarks != NIL)
+ {
+ /* not 100% but probably close enough */
+ switch (((RowMarkClause *) linitial(stmt->rowMarks))->strength)
+ {
+ case LCS_FORKEYSHARE:
+ tag = CMDTAG_SELECT_FOR_KEY_SHARE;
+ break;
+ case LCS_FORSHARE:
+ tag = CMDTAG_SELECT_FOR_SHARE;
+ break;
+ case LCS_FORNOKEYUPDATE:
+ tag = CMDTAG_SELECT_FOR_NO_KEY_UPDATE;
+ break;
+ case LCS_FORUPDATE:
+ tag = CMDTAG_SELECT_FOR_UPDATE;
+ break;
+ default:
+ tag = CMDTAG_UNKNOWN;
+ break;
+ }
+ }
+ else
+ tag = CMDTAG_SELECT;
+ break;
+ case CMD_UPDATE:
+ tag = CMDTAG_UPDATE;
+ break;
+ case CMD_INSERT:
+ tag = CMDTAG_INSERT;
+ break;
+ case CMD_DELETE:
+ tag = CMDTAG_DELETE;
+ break;
+ case CMD_MERGE:
+ tag = CMDTAG_MERGE;
+ break;
+ case CMD_UTILITY:
+ tag = CreateCommandTag(stmt->utilityStmt);
+ break;
+ default:
+ elog(WARNING, "unrecognized commandType: %d",
+ (int) stmt->commandType);
+ tag = CMDTAG_UNKNOWN;
+ break;
+ }
+ }
+ break;
+
+ default:
+ elog(WARNING, "unrecognized node type: %d",
+ (int) nodeTag(parsetree));
+ tag = CMDTAG_UNKNOWN;
+ break;
+ }
+
+ return tag;
+}
+
+
+/*
+ * GetCommandLogLevel
+ * utility to get the minimum log_statement level for a command,
+ * given either a raw (un-analyzed) parsetree, an analyzed Query,
+ * or a PlannedStmt.
+ *
+ * This must handle all command types, but since the vast majority
+ * of 'em are utility commands, it seems sensible to keep it here.
+ */
+LogStmtLevel
+GetCommandLogLevel(Node *parsetree)
+{
+ LogStmtLevel lev;
+
+ switch (nodeTag(parsetree))
+ {
+ /* recurse if we're given a RawStmt */
+ case T_RawStmt:
+ lev = GetCommandLogLevel(((RawStmt *) parsetree)->stmt);
+ break;
+
+ /* raw plannable queries */
+ case T_InsertStmt:
+ case T_DeleteStmt:
+ case T_UpdateStmt:
+ case T_MergeStmt:
+ lev = LOGSTMT_MOD;
+ break;
+
+ case T_SelectStmt:
+ if (((SelectStmt *) parsetree)->intoClause)
+ lev = LOGSTMT_DDL; /* SELECT INTO */
+ else
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_PLAssignStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ /* utility statements --- same whether raw or cooked */
+ case T_TransactionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_DeclareCursorStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_ClosePortalStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_FetchStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_CreateSchemaStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateStmt:
+ case T_CreateForeignTableStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateTableSpaceStmt:
+ case T_DropTableSpaceStmt:
+ case T_AlterTableSpaceOptionsStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateExtensionStmt:
+ case T_AlterExtensionStmt:
+ case T_AlterExtensionContentsStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateFdwStmt:
+ case T_AlterFdwStmt:
+ case T_CreateForeignServerStmt:
+ case T_AlterForeignServerStmt:
+ case T_CreateUserMappingStmt:
+ case T_AlterUserMappingStmt:
+ case T_DropUserMappingStmt:
+ case T_ImportForeignSchemaStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DropStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_TruncateStmt:
+ lev = LOGSTMT_MOD;
+ break;
+
+ case T_CommentStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_SecLabelStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CopyStmt:
+ if (((CopyStmt *) parsetree)->is_from)
+ lev = LOGSTMT_MOD;
+ else
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_PrepareStmt:
+ {
+ PrepareStmt *stmt = (PrepareStmt *) parsetree;
+
+ /* Look through a PREPARE to the contained stmt */
+ lev = GetCommandLogLevel(stmt->query);
+ }
+ break;
+
+ case T_ExecuteStmt:
+ {
+ ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
+ PreparedStatement *ps;
+
+ /* Look through an EXECUTE to the referenced stmt */
+ ps = FetchPreparedStatement(stmt->name, false);
+ if (ps && ps->plansource->raw_parse_tree)
+ lev = GetCommandLogLevel(ps->plansource->raw_parse_tree->stmt);
+ else
+ lev = LOGSTMT_ALL;
+ }
+ break;
+
+ case T_DeallocateStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_RenameStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterObjectDependsStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterObjectSchemaStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterOwnerStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterOperatorStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterTypeStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterTableMoveAllStmt:
+ case T_AlterTableStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterDomainStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_GrantStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_GrantRoleStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterDefaultPrivilegesStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DefineStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CompositeTypeStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateEnumStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateRangeStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterEnumStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_ViewStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateFunctionStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterFunctionStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_IndexStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_RuleStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateSeqStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterSeqStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DoStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_CreatedbStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterDatabaseStmt:
+ case T_AlterDatabaseRefreshCollStmt:
+ case T_AlterDatabaseSetStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DropdbStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_NotifyStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_ListenStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_UnlistenStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_LoadStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_CallStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_ClusterStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_VacuumStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_ExplainStmt:
+ {
+ ExplainStmt *stmt = (ExplainStmt *) parsetree;
+ bool analyze = false;
+ ListCell *lc;
+
+ /* Look through an EXPLAIN ANALYZE to the contained stmt */
+ foreach(lc, stmt->options)
+ {
+ DefElem *opt = (DefElem *) lfirst(lc);
+
+ if (strcmp(opt->defname, "analyze") == 0)
+ analyze = defGetBoolean(opt);
+ /* don't "break", as explain.c will use the last value */
+ }
+ if (analyze)
+ return GetCommandLogLevel(stmt->query);
+
+ /* Plain EXPLAIN isn't so interesting */
+ lev = LOGSTMT_ALL;
+ }
+ break;
+
+ case T_CreateTableAsStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_RefreshMatViewStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterSystemStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_VariableSetStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_VariableShowStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_DiscardStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_CreateTrigStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateEventTrigStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterEventTrigStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreatePLangStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateDomainStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateRoleStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterRoleStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterRoleSetStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DropRoleStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DropOwnedStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_ReassignOwnedStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_LockStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_ConstraintsSetStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_CheckPointStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case T_ReindexStmt:
+ lev = LOGSTMT_ALL; /* should this be DDL? */
+ break;
+
+ case T_CreateConversionStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateCastStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateOpClassStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateOpFamilyStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateTransformStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterOpFamilyStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreatePolicyStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterPolicyStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterTSDictionaryStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterTSConfigurationStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateAmStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreatePublicationStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterPublicationStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateSubscriptionStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterSubscriptionStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_DropSubscriptionStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_CreateStatsStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterStatsStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ case T_AlterCollationStmt:
+ lev = LOGSTMT_DDL;
+ break;
+
+ /* already-planned queries */
+ case T_PlannedStmt:
+ {
+ PlannedStmt *stmt = (PlannedStmt *) parsetree;
+
+ switch (stmt->commandType)
+ {
+ case CMD_SELECT:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case CMD_UPDATE:
+ case CMD_INSERT:
+ case CMD_DELETE:
+ case CMD_MERGE:
+ lev = LOGSTMT_MOD;
+ break;
+
+ case CMD_UTILITY:
+ lev = GetCommandLogLevel(stmt->utilityStmt);
+ break;
+
+ default:
+ elog(WARNING, "unrecognized commandType: %d",
+ (int) stmt->commandType);
+ lev = LOGSTMT_ALL;
+ break;
+ }
+ }
+ break;
+
+ /* parsed-and-rewritten-but-not-planned queries */
+ case T_Query:
+ {
+ Query *stmt = (Query *) parsetree;
+
+ switch (stmt->commandType)
+ {
+ case CMD_SELECT:
+ lev = LOGSTMT_ALL;
+ break;
+
+ case CMD_UPDATE:
+ case CMD_INSERT:
+ case CMD_DELETE:
+ case CMD_MERGE:
+ lev = LOGSTMT_MOD;
+ break;
+
+ case CMD_UTILITY:
+ lev = GetCommandLogLevel(stmt->utilityStmt);
+ break;
+
+ default:
+ elog(WARNING, "unrecognized commandType: %d",
+ (int) stmt->commandType);
+ lev = LOGSTMT_ALL;
+ break;
+ }
+ }
+ break;
+
+ default:
+ elog(WARNING, "unrecognized node type: %d",
+ (int) nodeTag(parsetree));
+ lev = LOGSTMT_ALL;
+ break;
+ }
+
+ return lev;
+}