1 /*-------------------------------------------------------------------------
4 * support for communication destinations
7 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/tcop/dest.c
13 *-------------------------------------------------------------------------
17 * BeginCommand - initialize the destination at start of command
18 * CreateDestReceiver - create tuple receiver object for destination
19 * EndCommand - clean up the destination at end of command
20 * NullCommand - tell dest that an empty query string was recognized
21 * ReadyForQuery - tell dest that we are ready for a new query
24 * These routines do the appropriate work before and after
25 * tuples are returned by a query to keep the backend and the
26 * "destination" portals synchronized.
31 #include "access/printsimple.h"
32 #include "access/printtup.h"
33 #include "access/xact.h"
34 #include "commands/copy.h"
35 #include "commands/createas.h"
36 #include "commands/matview.h"
37 #include "executor/functions.h"
38 #include "executor/tqueue.h"
39 #include "executor/tstoreReceiver.h"
40 #include "libpq/libpq.h"
41 #include "libpq/pqformat.h"
42 #include "utils/portal.h"
46 * dummy DestReceiver functions
50 donothingReceive(TupleTableSlot *slot, DestReceiver *self)
56 donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
61 donothingCleanup(DestReceiver *self)
63 /* this is used for both shutdown and destroy methods */
67 * static DestReceiver structs for dest types needing no local state
70 static const DestReceiver donothingDR = {
71 donothingReceive, donothingStartup, donothingCleanup, donothingCleanup,
75 static const DestReceiver debugtupDR = {
76 debugtup, debugStartup, donothingCleanup, donothingCleanup,
80 static const DestReceiver printsimpleDR = {
81 printsimple, printsimple_startup, donothingCleanup, donothingCleanup,
85 static const DestReceiver spi_printtupDR = {
86 spi_printtup, spi_dest_startup, donothingCleanup, donothingCleanup,
91 * Globally available receiver for DestNone.
93 * It's ok to cast the constness away as any modification of the none receiver
94 * would be a bug (which gets easier to catch this way).
96 DestReceiver *None_Receiver = (DestReceiver *) &donothingDR;
99 * BeginCommand - initialize the destination at start of command
103 BeginCommand(const char *commandTag, CommandDest dest)
105 /* Nothing to do at present */
109 * CreateDestReceiver - return appropriate receiver function set for dest
113 CreateDestReceiver(CommandDest dest)
116 * It's ok to cast the constness away as any modification of the none
117 * receiver would be a bug (which gets easier to catch this way).
123 case DestRemoteExecute:
124 return printtup_create_DR(dest);
126 case DestRemoteSimple:
127 return unconstify(DestReceiver *, &printsimpleDR);
130 return unconstify(DestReceiver *, &donothingDR);
133 return unconstify(DestReceiver *, &debugtupDR);
136 return unconstify(DestReceiver *, &spi_printtupDR);
139 return CreateTuplestoreDestReceiver();
142 return CreateIntoRelDestReceiver(NULL);
145 return CreateCopyDestReceiver();
147 case DestSQLFunction:
148 return CreateSQLFunctionDestReceiver();
150 case DestTransientRel:
151 return CreateTransientRelDestReceiver(InvalidOid);
154 return CreateTupleQueueDestReceiver(NULL);
157 /* should never get here */
162 * EndCommand - clean up the destination at end of command
166 EndCommand(const char *commandTag, CommandDest dest)
171 case DestRemoteExecute:
172 case DestRemoteSimple:
175 * We assume the commandTag is plain ASCII and therefore requires
176 * no encoding conversion.
178 pq_putmessage('C', commandTag, strlen(commandTag) + 1);
187 case DestSQLFunction:
188 case DestTransientRel:
195 * NullCommand - tell dest that an empty query string was recognized
197 * In FE/BE protocol version 1.0, this hack is necessary to support
198 * libpq's crufty way of determining whether a multiple-command
199 * query string is done. In protocol 2.0 it's probably not really
200 * necessary to distinguish empty queries anymore, but we still do it
201 * for backwards compatibility with 1.0. In protocol 3.0 it has some
202 * use again, since it ensures that there will be a recognizable end
203 * to the response to an Execute message.
207 NullCommand(CommandDest dest)
212 case DestRemoteExecute:
213 case DestRemoteSimple:
216 * tell the fe that we saw an empty query string. In protocols
217 * before 3.0 this has a useless empty-string message body.
219 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
220 pq_putemptymessage('I');
222 pq_putmessage('I', "", 1);
231 case DestSQLFunction:
232 case DestTransientRel:
239 * ReadyForQuery - tell dest that we are ready for a new query
241 * The ReadyForQuery message is sent so that the FE can tell when
242 * we are done processing a query string.
243 * In versions 3.0 and up, it also carries a transaction state indicator.
245 * Note that by flushing the stdio buffer here, we can avoid doing it
246 * most other places and thus reduce the number of separate packets sent.
250 ReadyForQuery(CommandDest dest)
255 case DestRemoteExecute:
256 case DestRemoteSimple:
257 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
261 pq_beginmessage(&buf, 'Z');
262 pq_sendbyte(&buf, TransactionBlockStatusCode());
266 pq_putemptymessage('Z');
267 /* Flush output at end of cycle in any case. */
277 case DestSQLFunction:
278 case DestTransientRel: