1 /*-------------------------------------------------------------------------
4 * support for communication destinations
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/tcop/dest.c,v 1.74 2008/11/30 20:51:25 tgl Exp $
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/printtup.h"
32 #include "access/xact.h"
33 #include "commands/copy.h"
34 #include "executor/executor.h"
35 #include "executor/functions.h"
36 #include "executor/tstoreReceiver.h"
37 #include "libpq/libpq.h"
38 #include "libpq/pqformat.h"
39 #include "utils/portal.h"
43 * dummy DestReceiver functions
47 donothingReceive(TupleTableSlot *slot, DestReceiver *self)
52 donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
57 donothingCleanup(DestReceiver *self)
59 /* this is used for both shutdown and destroy methods */
63 * static DestReceiver structs for dest types needing no local state
66 static DestReceiver donothingDR = {
67 donothingReceive, donothingStartup, donothingCleanup, donothingCleanup,
71 static DestReceiver debugtupDR = {
72 debugtup, debugStartup, donothingCleanup, donothingCleanup,
76 static DestReceiver spi_printtupDR = {
77 spi_printtup, spi_dest_startup, donothingCleanup, donothingCleanup,
81 /* Globally available receiver for DestNone */
82 DestReceiver *None_Receiver = &donothingDR;
86 * BeginCommand - initialize the destination at start of command
90 BeginCommand(const char *commandTag, CommandDest dest)
92 /* Nothing to do at present */
96 * CreateDestReceiver - return appropriate receiver function set for dest
100 CreateDestReceiver(CommandDest dest)
105 case DestRemoteExecute:
106 return printtup_create_DR(dest);
115 return &spi_printtupDR;
118 return CreateTuplestoreDestReceiver();
121 return CreateIntoRelDestReceiver();
124 return CreateCopyDestReceiver();
126 case DestSQLFunction:
127 return CreateSQLFunctionDestReceiver();
130 /* should never get here */
135 * EndCommand - clean up the destination at end of command
139 EndCommand(const char *commandTag, CommandDest dest)
144 case DestRemoteExecute:
145 pq_puttextmessage('C', commandTag);
154 case DestSQLFunction:
160 * NullCommand - tell dest that an empty query string was recognized
162 * In FE/BE protocol version 1.0, this hack is necessary to support
163 * libpq's crufty way of determining whether a multiple-command
164 * query string is done. In protocol 2.0 it's probably not really
165 * necessary to distinguish empty queries anymore, but we still do it
166 * for backwards compatibility with 1.0. In protocol 3.0 it has some
167 * use again, since it ensures that there will be a recognizable end
168 * to the response to an Execute message.
172 NullCommand(CommandDest dest)
177 case DestRemoteExecute:
180 * tell the fe that we saw an empty query string. In protocols
181 * before 3.0 this has a useless empty-string message body.
183 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
184 pq_putemptymessage('I');
186 pq_puttextmessage('I', "");
195 case DestSQLFunction:
201 * ReadyForQuery - tell dest that we are ready for a new query
203 * The ReadyForQuery message is sent in protocol versions 2.0 and up
204 * so that the FE can tell when we are done processing a query string.
205 * In versions 3.0 and up, it also carries a transaction state indicator.
207 * Note that by flushing the stdio buffer here, we can avoid doing it
208 * most other places and thus reduce the number of separate packets sent.
212 ReadyForQuery(CommandDest dest)
217 case DestRemoteExecute:
218 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
222 pq_beginmessage(&buf, 'Z');
223 pq_sendbyte(&buf, TransactionBlockStatusCode());
226 else if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
227 pq_putemptymessage('Z');
228 /* Flush output at end of cycle in any case. */
238 case DestSQLFunction: