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
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/tstoreReceiver.h"
36 #include "libpq/libpq.h"
37 #include "libpq/pqformat.h"
38 #include "utils/portal.h"
42 * dummy DestReceiver functions
46 donothingReceive(TupleTableSlot
*slot
, DestReceiver
*self
)
51 donothingStartup(DestReceiver
*self
, int operation
, TupleDesc typeinfo
)
56 donothingCleanup(DestReceiver
*self
)
58 /* this is used for both shutdown and destroy methods */
62 * static DestReceiver structs for dest types needing no local state
65 static DestReceiver donothingDR
= {
66 donothingReceive
, donothingStartup
, donothingCleanup
, donothingCleanup
,
70 static DestReceiver debugtupDR
= {
71 debugtup
, debugStartup
, donothingCleanup
, donothingCleanup
,
75 static DestReceiver spi_printtupDR
= {
76 spi_printtup
, spi_dest_startup
, donothingCleanup
, donothingCleanup
,
80 /* Globally available receiver for DestNone */
81 DestReceiver
*None_Receiver
= &donothingDR
;
85 * BeginCommand - initialize the destination at start of command
89 BeginCommand(const char *commandTag
, CommandDest dest
)
91 /* Nothing to do at present */
95 * CreateDestReceiver - return appropriate receiver function set for dest
97 * Note: a Portal must be specified for destinations DestRemote,
98 * DestRemoteExecute, and DestTuplestore. It can be NULL for the others.
102 CreateDestReceiver(CommandDest dest
, Portal portal
)
107 case DestRemoteExecute
:
109 elog(ERROR
, "no portal specified for DestRemote receiver");
110 return printtup_create_DR(dest
, portal
);
119 return &spi_printtupDR
;
123 elog(ERROR
, "no portal specified for DestTuplestore receiver");
124 if (portal
->holdStore
== NULL
||
125 portal
->holdContext
== NULL
)
126 elog(ERROR
, "portal has no holdStore");
127 return CreateTuplestoreDestReceiver(portal
->holdStore
,
128 portal
->holdContext
);
131 return CreateIntoRelDestReceiver();
134 return CreateCopyDestReceiver();
137 /* should never get here */
142 * EndCommand - clean up the destination at end of command
146 EndCommand(const char *commandTag
, CommandDest dest
)
151 case DestRemoteExecute
:
152 pq_puttextmessage('C', commandTag
);
166 * NullCommand - tell dest that an empty query string was recognized
168 * In FE/BE protocol version 1.0, this hack is necessary to support
169 * libpq's crufty way of determining whether a multiple-command
170 * query string is done. In protocol 2.0 it's probably not really
171 * necessary to distinguish empty queries anymore, but we still do it
172 * for backwards compatibility with 1.0. In protocol 3.0 it has some
173 * use again, since it ensures that there will be a recognizable end
174 * to the response to an Execute message.
178 NullCommand(CommandDest dest
)
183 case DestRemoteExecute
:
186 * tell the fe that we saw an empty query string. In protocols
187 * before 3.0 this has a useless empty-string message body.
189 if (PG_PROTOCOL_MAJOR(FrontendProtocol
) >= 3)
190 pq_putemptymessage('I');
192 pq_puttextmessage('I', "");
206 * ReadyForQuery - tell dest that we are ready for a new query
208 * The ReadyForQuery message is sent in protocol versions 2.0 and up
209 * so that the FE can tell when we are done processing a query string.
210 * In versions 3.0 and up, it also carries a transaction state indicator.
212 * Note that by flushing the stdio buffer here, we can avoid doing it
213 * most other places and thus reduce the number of separate packets sent.
217 ReadyForQuery(CommandDest dest
)
222 case DestRemoteExecute
:
223 if (PG_PROTOCOL_MAJOR(FrontendProtocol
) >= 3)
227 pq_beginmessage(&buf
, 'Z');
228 pq_sendbyte(&buf
, TransactionBlockStatusCode());
231 else if (PG_PROTOCOL_MAJOR(FrontendProtocol
) >= 2)
232 pq_putemptymessage('Z');
233 /* Flush output at end of cycle in any case. */