PostgreSQL Source Code  git master
dest.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dest.c
4  * support for communication destinations
5  *
6  *
7  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/tcop/dest.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
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
22  *
23  * NOTES
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.
27  */
28 
29 #include "postgres.h"
30 
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"
40 #include "libpq/libpq.h"
41 #include "libpq/pqformat.h"
42 #include "utils/portal.h"
43 
44 
45 /* ----------------
46  * dummy DestReceiver functions
47  * ----------------
48  */
49 static bool
51 {
52  return true;
53 }
54 
55 static void
56 donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
57 {
58 }
59 
60 static void
62 {
63  /* this is used for both shutdown and destroy methods */
64 }
65 
66 /* ----------------
67  * static DestReceiver structs for dest types needing no local state
68  * ----------------
69  */
70 static const DestReceiver donothingDR = {
72  DestNone
73 };
74 
75 static const DestReceiver debugtupDR = {
77  DestDebug
78 };
79 
80 static const DestReceiver printsimpleDR = {
83 };
84 
85 static const DestReceiver spi_printtupDR = {
87  DestSPI
88 };
89 
90 /*
91  * Globally available receiver for DestNone.
92  *
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).
95  */
97 
98 /* ----------------
99  * BeginCommand - initialize the destination at start of command
100  * ----------------
101  */
102 void
104 {
105  /* Nothing to do at present */
106 }
107 
108 /* ----------------
109  * CreateDestReceiver - return appropriate receiver function set for dest
110  * ----------------
111  */
112 DestReceiver *
114 {
115  /*
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).
118  */
119 
120  switch (dest)
121  {
122  case DestRemote:
123  case DestRemoteExecute:
124  return printtup_create_DR(dest);
125 
126  case DestRemoteSimple:
127  return unconstify(DestReceiver *, &printsimpleDR);
128 
129  case DestNone:
130  return unconstify(DestReceiver *, &donothingDR);
131 
132  case DestDebug:
133  return unconstify(DestReceiver *, &debugtupDR);
134 
135  case DestSPI:
136  return unconstify(DestReceiver *, &spi_printtupDR);
137 
138  case DestTuplestore:
140 
141  case DestIntoRel:
142  return CreateIntoRelDestReceiver(NULL);
143 
144  case DestCopyOut:
145  return CreateCopyDestReceiver();
146 
147  case DestSQLFunction:
149 
150  case DestTransientRel:
152 
153  case DestTupleQueue:
154  return CreateTupleQueueDestReceiver(NULL);
155  }
156 
157  /* should never get here */
158  pg_unreachable();
159 }
160 
161 /* ----------------
162  * EndCommand - clean up the destination at end of command
163  * ----------------
164  */
165 void
166 EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
167 {
168  char completionTag[COMPLETION_TAG_BUFSIZE];
169  CommandTag tag;
170  const char *tagname;
171 
172  switch (dest)
173  {
174  case DestRemote:
175  case DestRemoteExecute:
176  case DestRemoteSimple:
177 
178  /*
179  * We assume the tagname is plain ASCII and therefore requires no
180  * encoding conversion.
181  *
182  * We no longer display LastOid, but to preserve the wire
183  * protocol, we write InvalidOid where the LastOid used to be
184  * written.
185  *
186  * All cases where LastOid was written also write nprocessed
187  * count, so just Assert that rather than having an extra test.
188  */
189  tag = qc->commandTag;
190  tagname = GetCommandTagName(tag);
191 
192  if (command_tag_display_rowcount(tag) && !force_undecorated_output)
193  snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
194  tag == CMDTAG_INSERT ?
195  "%s 0 " UINT64_FORMAT : "%s " UINT64_FORMAT,
196  tagname, qc->nprocessed);
197  else
198  snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s", tagname);
199  pq_putmessage('C', completionTag, strlen(completionTag) + 1);
200 
201  case DestNone:
202  case DestDebug:
203  case DestSPI:
204  case DestTuplestore:
205  case DestIntoRel:
206  case DestCopyOut:
207  case DestSQLFunction:
208  case DestTransientRel:
209  case DestTupleQueue:
210  break;
211  }
212 }
213 
214 /* ----------------
215  * EndReplicationCommand - stripped down version of EndCommand
216  *
217  * For use by replication commands.
218  * ----------------
219  */
220 void
221 EndReplicationCommand(const char *commandTag)
222 {
223  pq_putmessage('C', commandTag, strlen(commandTag) + 1);
224 }
225 
226 /* ----------------
227  * NullCommand - tell dest that an empty query string was recognized
228  *
229  * This ensures that there will be a recognizable end to the response
230  * to an Execute message in the extended query protocol.
231  * ----------------
232  */
233 void
235 {
236  switch (dest)
237  {
238  case DestRemote:
239  case DestRemoteExecute:
240  case DestRemoteSimple:
241 
242  /* Tell the FE that we saw an empty query string */
243  pq_putemptymessage('I');
244  break;
245 
246  case DestNone:
247  case DestDebug:
248  case DestSPI:
249  case DestTuplestore:
250  case DestIntoRel:
251  case DestCopyOut:
252  case DestSQLFunction:
253  case DestTransientRel:
254  case DestTupleQueue:
255  break;
256  }
257 }
258 
259 /* ----------------
260  * ReadyForQuery - tell dest that we are ready for a new query
261  *
262  * The ReadyForQuery message is sent so that the FE can tell when
263  * we are done processing a query string.
264  * In versions 3.0 and up, it also carries a transaction state indicator.
265  *
266  * Note that by flushing the stdio buffer here, we can avoid doing it
267  * most other places and thus reduce the number of separate packets sent.
268  * ----------------
269  */
270 void
272 {
273  switch (dest)
274  {
275  case DestRemote:
276  case DestRemoteExecute:
277  case DestRemoteSimple:
278  {
280 
281  pq_beginmessage(&buf, 'Z');
283  pq_endmessage(&buf);
284  }
285  /* Flush output at end of cycle in any case. */
286  pq_flush();
287  break;
288 
289  case DestNone:
290  case DestDebug:
291  case DestSPI:
292  case DestTuplestore:
293  case DestIntoRel:
294  case DestCopyOut:
295  case DestSQLFunction:
296  case DestTransientRel:
297  case DestTupleQueue:
298  break;
299  }
300 }
CommandTag
Definition: cmdtag.h:20
#define pg_unreachable()
Definition: c.h:258
#define pq_flush()
Definition: libpq.h:46
CommandDest
Definition: dest.h:87
static const DestReceiver debugtupDR
Definition: dest.c:75
Definition: dest.h:94
void pq_putemptymessage(char msgtype)
Definition: pqformat.c:390
DestReceiver * None_Receiver
Definition: dest.c:96
char TransactionBlockStatusCode(void)
Definition: xact.c:4725
bool printsimple(TupleTableSlot *slot, DestReceiver *self)
Definition: printsimple.c:58
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static bool donothingReceive(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.c:50
uint64 nprocessed
Definition: cmdtag.h:31
void EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
Definition: dest.c:166
static const DestReceiver printsimpleDR
Definition: dest.c:80
Definition: dest.h:89
static char * buf
Definition: pg_test_fsync.c:68
DestReceiver * printtup_create_DR(CommandDest dest)
Definition: printtup.c:71
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
Definition: dest.h:90
void spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: spi.c:2010
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:428
void EndReplicationCommand(const char *commandTag)
Definition: dest.c:221
void printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
Definition: printsimple.c:30
DestReceiver * CreateSQLFunctionDestReceiver(void)
Definition: functions.c:2046
#define unconstify(underlying_type, expr)
Definition: c.h:1243
#define InvalidOid
Definition: postgres_ext.h:36
CommandTag commandTag
Definition: cmdtag.h:30
#define COMPLETION_TAG_BUFSIZE
Definition: dest.h:75
static void donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: dest.c:56
bool debugtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:459
static const DestReceiver spi_printtupDR
Definition: dest.c:85
const char * GetCommandTagName(CommandTag commandTag)
Definition: cmdtag.c:45
DestReceiver * CreateTuplestoreDestReceiver(void)
DestReceiver * CreateTransientRelDestReceiver(Oid transientoid)
Definition: matview.c:442
void BeginCommand(CommandTag commandTag, CommandDest dest)
Definition: dest.c:103
void ReadyForQuery(CommandDest dest)
Definition: dest.c:271
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
DestReceiver * CreateCopyDestReceiver(void)
Definition: copyto.c:1296
static const DestReceiver donothingDR
Definition: dest.c:70
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:49
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:119
void debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:441
#define snprintf
Definition: port.h:216
#define UINT64_FORMAT
Definition: c.h:484
static void donothingCleanup(DestReceiver *self)
Definition: dest.c:61
bool spi_printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: spi.c:2058
void NullCommand(CommandDest dest)
Definition: dest.c:234
bool command_tag_display_rowcount(CommandTag commandTag)
Definition: cmdtag.c:51