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-2024, 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 
43 
44 /* ----------------
45  * dummy DestReceiver functions
46  * ----------------
47  */
48 static bool
50 {
51  return true;
52 }
53 
54 static void
55 donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
56 {
57 }
58 
59 static void
61 {
62  /* this is used for both shutdown and destroy methods */
63 }
64 
65 /* ----------------
66  * static DestReceiver structs for dest types needing no local state
67  * ----------------
68  */
69 static const DestReceiver donothingDR = {
71  DestNone
72 };
73 
74 static const DestReceiver debugtupDR = {
76  DestDebug
77 };
78 
79 static const DestReceiver printsimpleDR = {
82 };
83 
84 static const DestReceiver spi_printtupDR = {
86  DestSPI
87 };
88 
89 /*
90  * Globally available receiver for DestNone.
91  *
92  * It's ok to cast the constness away as any modification of the none receiver
93  * would be a bug (which gets easier to catch this way).
94  */
96 
97 /* ----------------
98  * BeginCommand - initialize the destination at start of command
99  * ----------------
100  */
101 void
103 {
104  /* Nothing to do at present */
105 }
106 
107 /* ----------------
108  * CreateDestReceiver - return appropriate receiver function set for dest
109  * ----------------
110  */
111 DestReceiver *
113 {
114  /*
115  * It's ok to cast the constness away as any modification of the none
116  * receiver would be a bug (which gets easier to catch this way).
117  */
118 
119  switch (dest)
120  {
121  case DestRemote:
122  case DestRemoteExecute:
123  return printtup_create_DR(dest);
124 
125  case DestRemoteSimple:
127 
128  case DestNone:
129  return unconstify(DestReceiver *, &donothingDR);
130 
131  case DestDebug:
132  return unconstify(DestReceiver *, &debugtupDR);
133 
134  case DestSPI:
136 
137  case DestTuplestore:
139 
140  case DestIntoRel:
141  return CreateIntoRelDestReceiver(NULL);
142 
143  case DestCopyOut:
144  return CreateCopyDestReceiver();
145 
146  case DestSQLFunction:
148 
149  case DestTransientRel:
151 
152  case DestTupleQueue:
153  return CreateTupleQueueDestReceiver(NULL);
154  }
155 
156  /* should never get here */
157  pg_unreachable();
158 }
159 
160 /* ----------------
161  * EndCommand - clean up the destination at end of command
162  * ----------------
163  */
164 void
165 EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
166 {
167  char completionTag[COMPLETION_TAG_BUFSIZE];
168  Size len;
169 
170  switch (dest)
171  {
172  case DestRemote:
173  case DestRemoteExecute:
174  case DestRemoteSimple:
175 
176  len = BuildQueryCompletionString(completionTag, qc,
177  force_undecorated_output);
178  pq_putmessage(PqMsg_CommandComplete, completionTag, len + 1);
179 
180  case DestNone:
181  case DestDebug:
182  case DestSPI:
183  case DestTuplestore:
184  case DestIntoRel:
185  case DestCopyOut:
186  case DestSQLFunction:
187  case DestTransientRel:
188  case DestTupleQueue:
189  break;
190  }
191 }
192 
193 /* ----------------
194  * EndReplicationCommand - stripped down version of EndCommand
195  *
196  * For use by replication commands.
197  * ----------------
198  */
199 void
200 EndReplicationCommand(const char *commandTag)
201 {
202  pq_putmessage(PqMsg_CommandComplete, commandTag, strlen(commandTag) + 1);
203 }
204 
205 /* ----------------
206  * NullCommand - tell dest that an empty query string was recognized
207  *
208  * This ensures that there will be a recognizable end to the response
209  * to an Execute message in the extended query protocol.
210  * ----------------
211  */
212 void
214 {
215  switch (dest)
216  {
217  case DestRemote:
218  case DestRemoteExecute:
219  case DestRemoteSimple:
220 
221  /* Tell the FE that we saw an empty query string */
223  break;
224 
225  case DestNone:
226  case DestDebug:
227  case DestSPI:
228  case DestTuplestore:
229  case DestIntoRel:
230  case DestCopyOut:
231  case DestSQLFunction:
232  case DestTransientRel:
233  case DestTupleQueue:
234  break;
235  }
236 }
237 
238 /* ----------------
239  * ReadyForQuery - tell dest that we are ready for a new query
240  *
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.
244  *
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.
247  * ----------------
248  */
249 void
251 {
252  switch (dest)
253  {
254  case DestRemote:
255  case DestRemoteExecute:
256  case DestRemoteSimple:
257  {
259 
262  pq_endmessage(&buf);
263  }
264  /* Flush output at end of cycle in any case. */
265  pq_flush();
266  break;
267 
268  case DestNone:
269  case DestDebug:
270  case DestSPI:
271  case DestTuplestore:
272  case DestIntoRel:
273  case DestCopyOut:
274  case DestSQLFunction:
275  case DestTransientRel:
276  case DestTupleQueue:
277  break;
278  }
279 }
#define unconstify(underlying_type, expr)
Definition: c.h:1232
#define pg_unreachable()
Definition: c.h:283
size_t Size
Definition: c.h:592
Size BuildQueryCompletionString(char *buff, const QueryCompletion *qc, bool nameonly)
Definition: cmdtag.c:121
#define COMPLETION_TAG_BUFSIZE
Definition: cmdtag.h:17
CommandTag
Definition: cmdtag.h:23
DestReceiver * CreateCopyDestReceiver(void)
Definition: copyto.c:1267
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:435
void EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
Definition: dest.c:165
static const DestReceiver spi_printtupDR
Definition: dest.c:84
static void donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: dest.c:55
static void donothingCleanup(DestReceiver *self)
Definition: dest.c:60
void BeginCommand(CommandTag commandTag, CommandDest dest)
Definition: dest.c:102
static const DestReceiver debugtupDR
Definition: dest.c:74
static const DestReceiver donothingDR
Definition: dest.c:69
void ReadyForQuery(CommandDest dest)
Definition: dest.c:250
static bool donothingReceive(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.c:49
void EndReplicationCommand(const char *commandTag)
Definition: dest.c:200
DestReceiver * None_Receiver
Definition: dest.c:95
static const DestReceiver printsimpleDR
Definition: dest.c:79
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:112
void NullCommand(CommandDest dest)
Definition: dest.c:213
CommandDest
Definition: dest.h:86
@ DestSQLFunction
Definition: dest.h:96
@ DestTupleQueue
Definition: dest.h:98
@ DestTuplestore
Definition: dest.h:93
@ DestRemote
Definition: dest.h:89
@ DestDebug
Definition: dest.h:88
@ DestRemoteSimple
Definition: dest.h:91
@ DestCopyOut
Definition: dest.h:95
@ DestTransientRel
Definition: dest.h:97
@ DestSPI
Definition: dest.h:92
@ DestIntoRel
Definition: dest.h:94
@ DestRemoteExecute
Definition: dest.h:90
@ DestNone
Definition: dest.h:87
DestReceiver * CreateSQLFunctionDestReceiver(void)
Definition: functions.c:2062
#define pq_flush()
Definition: libpq.h:46
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:49
DestReceiver * CreateTransientRelDestReceiver(Oid transientoid)
Definition: matview.c:432
const void size_t len
static char * buf
Definition: pg_test_fsync.c:73
#define InvalidOid
Definition: postgres_ext.h:36
void pq_putemptymessage(char msgtype)
Definition: pqformat.c:388
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:160
void printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
Definition: printsimple.c:31
bool printsimple(TupleTableSlot *slot, DestReceiver *self)
Definition: printsimple.c:59
bool debugtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:458
void debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:440
DestReceiver * printtup_create_DR(CommandDest dest)
Definition: printtup.c:70
#define PqMsg_ReadyForQuery
Definition: protocol.h:55
#define PqMsg_EmptyQueryResponse
Definition: protocol.h:47
#define PqMsg_CommandComplete
Definition: protocol.h:42
bool spi_printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: spi.c:2166
void spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: spi.c:2118
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:119
DestReceiver * CreateTuplestoreDestReceiver(void)
char TransactionBlockStatusCode(void)
Definition: xact.c:4960