From 6e11f39c1510060b6d48eaad48440346f4ebe228 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Thu, 19 Jan 2017 10:32:43 -0500
Subject: [PATCH 1/2] Add new DestReceiver type for use when we lack catalog
 access.

printtup.c's DestReceiver requires both a portal and catalog access;
sometimes, we may want to send a simple result set to the client
without needing those things.  For example, this code can be used in
a walsender not connected to a specific database.

The only real problem here is that we have to hardcode information
about each type to be supported -- currently, just text, but we
could easily extend this to a few other builtin types.  However,
such hard-coding is inevitable if we want database-less walsenders
(or other database-less processes) to be able to send result sets,
and this might at least centralize it to some degree.

Patch by me.
---
 src/backend/access/common/Makefile      |   4 +-
 src/backend/access/common/printsimple.c | 110 ++++++++++++++++++++++++++++++++
 src/backend/tcop/dest.c                 |  12 ++++
 src/include/access/printsimple.h        |  23 +++++++
 src/include/tcop/dest.h                 |   1 +
 5 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 src/backend/access/common/printsimple.c
 create mode 100644 src/include/access/printsimple.h

diff --git a/src/backend/access/common/Makefile b/src/backend/access/common/Makefile
index 1fa6de0..d4b8132 100644
--- a/src/backend/access/common/Makefile
+++ b/src/backend/access/common/Makefile
@@ -12,7 +12,7 @@ subdir = src/backend/access/common
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = heaptuple.o indextuple.o printtup.o reloptions.o scankey.o \
-	tupconvert.o tupdesc.o
+OBJS = heaptuple.o indextuple.o printsimple.o printtup.o reloptions.o \
+	scankey.o tupconvert.o tupdesc.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/access/common/printsimple.c b/src/backend/access/common/printsimple.c
new file mode 100644
index 0000000..420de65
--- /dev/null
+++ b/src/backend/access/common/printsimple.c
@@ -0,0 +1,110 @@
+/*-------------------------------------------------------------------------
+ *
+ * printsimple.c
+ *	  Routines to print out tuples containing only a limited range of
+ *	  builtin types without catalog access.  This is intended for
+ *	  backends that don't have catalog access because they are not bound
+ *	  to a specific database, such as some walsender processes.  It
+ *	  doesn't handle standalone backends or protocol versions other than
+ *	  3.0, because we don't need such handling for current applications.
+ *
+ * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *	  src/backend/access/common/printsimple.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/printsimple.h"
+#include "catalog/pg_type.h"
+#include "fmgr.h"
+#include "libpq/pqformat.h"
+
+/*
+ * At startup time, send a RowDescription message.
+ */
+void
+printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
+{
+	StringInfoData buf;
+	int			i;
+
+	pq_beginmessage(&buf, 'T'); /* RowDescription */
+	pq_sendint(&buf, tupdesc->natts, 2);
+
+	for (i = 0; i < tupdesc->natts; ++i)
+	{
+		Form_pg_attribute attr = tupdesc->attrs[i];
+
+		pq_sendstring(&buf, NameStr(attr->attname));
+		pq_sendint(&buf, 0, 4); /* table oid */
+		pq_sendint(&buf, 0, 2); /* attnum */
+		pq_sendint(&buf, (int) attr->atttypid, 4);
+		pq_sendint(&buf, attr->attlen, 2);
+		pq_sendint(&buf, attr->atttypmod, 4);
+		pq_sendint(&buf, 0, 2); /* format code */
+	}
+
+	pq_endmessage(&buf);
+}
+
+/*
+ * For each tuple, send a DataRow message.
+ */
+bool
+printsimple(TupleTableSlot *slot, DestReceiver *self)
+{
+	TupleDesc	tupdesc = slot->tts_tupleDescriptor;
+	StringInfoData buf;
+	int			i;
+
+	/* Make sure the tuple is fully deconstructed */
+	slot_getallattrs(slot);
+
+	/* Prepare and send message */
+	pq_beginmessage(&buf, 'D');
+	pq_sendint(&buf, tupdesc->natts, 2);
+
+	for (i = 0; i < tupdesc->natts; ++i)
+	{
+		Form_pg_attribute attr = tupdesc->attrs[i];
+		Datum		value;
+
+		if (slot->tts_isnull[i])
+		{
+			pq_sendint(&buf, -1, 4);
+			continue;
+		}
+
+		value = slot->tts_values[i];
+
+		/*
+		 * We can't call the regular type output functions here because we
+		 * might not have catalog access.  Instead, we must hard-wire
+		 * knowledge of the required types.
+		 */
+		switch (attr->atttypid)
+		{
+			case TEXTOID:
+				{
+					text	   *t = DatumGetTextPP(value);
+
+					pq_sendcountedtext(&buf,
+									   VARDATA_ANY(t),
+									   VARSIZE_ANY_EXHDR(t),
+									   false);
+				}
+				break;
+
+			default:
+				elog(ERROR, "unsupported type OID: %u", attr->atttypid);
+		}
+	}
+
+	pq_endmessage(&buf);
+
+	return true;
+}
diff --git a/src/backend/tcop/dest.c b/src/backend/tcop/dest.c
index da39f43..28081c3 100644
--- a/src/backend/tcop/dest.c
+++ b/src/backend/tcop/dest.c
@@ -28,6 +28,7 @@
 
 #include "postgres.h"
 
+#include "access/printsimple.h"
 #include "access/printtup.h"
 #include "access/xact.h"
 #include "commands/copy.h"
@@ -76,6 +77,11 @@ static DestReceiver debugtupDR = {
 	DestDebug
 };
 
+static DestReceiver printsimpleDR = {
+	printsimple, printsimple_startup, donothingCleanup, donothingCleanup,
+	DestRemoteSimple
+};
+
 static DestReceiver spi_printtupDR = {
 	spi_printtup, spi_dest_startup, donothingCleanup, donothingCleanup,
 	DestSPI
@@ -108,6 +114,9 @@ CreateDestReceiver(CommandDest dest)
 		case DestRemoteExecute:
 			return printtup_create_DR(dest);
 
+		case DestRemoteSimple:
+			return &printsimpleDR;
+
 		case DestNone:
 			return &donothingDR;
 
@@ -151,6 +160,7 @@ EndCommand(const char *commandTag, CommandDest dest)
 	{
 		case DestRemote:
 		case DestRemoteExecute:
+		case DestRemoteSimple:
 
 			/*
 			 * We assume the commandTag is plain ASCII and therefore requires
@@ -191,6 +201,7 @@ NullCommand(CommandDest dest)
 	{
 		case DestRemote:
 		case DestRemoteExecute:
+		case DestRemoteSimple:
 
 			/*
 			 * tell the fe that we saw an empty query string.  In protocols
@@ -233,6 +244,7 @@ ReadyForQuery(CommandDest dest)
 	{
 		case DestRemote:
 		case DestRemoteExecute:
+		case DestRemoteSimple:
 			if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
 			{
 				StringInfoData buf;
diff --git a/src/include/access/printsimple.h b/src/include/access/printsimple.h
new file mode 100644
index 0000000..3f3e7a3
--- /dev/null
+++ b/src/include/access/printsimple.h
@@ -0,0 +1,23 @@
+/*-------------------------------------------------------------------------
+ *
+ * printsimple.h
+ *	  print simple tuples without catalog access
+ *
+ * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/access/printsimple.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef PRINTSIMPLE_H
+#define PRINTSIMPLE_H
+
+#include "tcop/dest.h"
+
+extern bool printsimple(TupleTableSlot *slot, DestReceiver *self);
+extern void printsimple_startup(DestReceiver *self, int operation,
+					TupleDesc tupdesc);
+
+#endif   /* PRINTSIMPLE_H */
diff --git a/src/include/tcop/dest.h b/src/include/tcop/dest.h
index 93f9b74..c459af2 100644
--- a/src/include/tcop/dest.h
+++ b/src/include/tcop/dest.h
@@ -89,6 +89,7 @@ typedef enum
 	DestDebug,					/* results go to debugging output */
 	DestRemote,					/* results sent to frontend process */
 	DestRemoteExecute,			/* sent to frontend, in Execute command */
+	DestRemoteSimple,			/* sent to frontend, w/no catalog access */
 	DestSPI,					/* results sent to SPI manager */
 	DestTuplestore,				/* results sent to Tuplestore */
 	DestIntoRel,				/* results sent to relation (SELECT INTO) */
-- 
2.5.4 (Apple Git-61)

