PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pqmq.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pqmq.c
4 * Use the frontend/backend protocol for communication over a shm_mq
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/backend/libpq/pqmq.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include "access/parallel.h"
17#include "libpq/libpq.h"
18#include "libpq/pqformat.h"
19#include "libpq/pqmq.h"
20#include "miscadmin.h"
21#include "pgstat.h"
23#include "storage/latch.h"
24#include "tcop/tcopprot.h"
25#include "utils/builtins.h"
26#include "utils/wait_event.h"
27
29static bool pq_mq_busy = false;
32
34static void mq_comm_reset(void);
35static int mq_flush(void);
36static int mq_flush_if_writable(void);
37static bool mq_is_send_pending(void);
38static int mq_putmessage(char msgtype, const char *s, size_t len);
39static void mq_putmessage_noblock(char msgtype, const char *s, size_t len);
40
43 .flush = mq_flush,
44 .flush_if_writable = mq_flush_if_writable,
45 .is_send_pending = mq_is_send_pending,
46 .putmessage = mq_putmessage,
47 .putmessage_noblock = mq_putmessage_noblock
48};
49
50/*
51 * Arrange to redirect frontend/backend protocol messages to a shared-memory
52 * message queue.
53 */
54void
63
64/*
65 * When the DSM that contains our shm_mq goes away, we need to stop sending
66 * messages to it.
67 */
68static void
78
79/*
80 * Arrange to SendProcSignal() to the parallel leader each time we transmit
81 * message data via the shm_mq.
82 */
83void
90
91static void
93{
94 /* Nothing to do. */
95}
96
97static int
99{
100 /* Nothing to do. */
101 return 0;
102}
103
104static int
106{
107 /* Nothing to do. */
108 return 0;
109}
110
111static bool
113{
114 /* There's never anything pending. */
115 return 0;
116}
117
118/*
119 * Transmit a libpq protocol message to the shared memory message queue
120 * selected via pq_mq_handle. We don't include a length word, because the
121 * receiver will know the length of the message from shm_mq_receive().
122 */
123static int
124mq_putmessage(char msgtype, const char *s, size_t len)
125{
126 shm_mq_iovec iov[2];
127 shm_mq_result result;
128
129 /*
130 * If we're sending a message, and we have to wait because the queue is
131 * full, and then we get interrupted, and that interrupt results in trying
132 * to send another message, we respond by detaching the queue. There's no
133 * way to return to the original context, but even if there were, just
134 * queueing the message would amount to indefinitely postponing the
135 * response to the interrupt. So we do this instead.
136 */
137 if (pq_mq_busy)
138 {
139 if (pq_mq_handle != NULL)
140 {
144 }
145 return EOF;
146 }
147
148 /*
149 * If the message queue is already gone, just ignore the message. This
150 * doesn't necessarily indicate a problem; for example, DEBUG messages can
151 * be generated late in the shutdown sequence, after all DSMs have already
152 * been detached.
153 */
154 if (pq_mq_handle == NULL)
155 return 0;
156
157 pq_mq_busy = true;
158
159 iov[0].data = &msgtype;
160 iov[0].len = 1;
161 iov[1].data = s;
162 iov[1].len = len;
163
164 for (;;)
165 {
166 /*
167 * Immediately notify the receiver by passing force_flush as true so
168 * that the shared memory value is updated before we send the parallel
169 * message signal right after this.
170 */
172 result = shm_mq_sendv(pq_mq_handle, iov, 2, true, true);
173
175 {
180 else
181 {
186 }
187 }
188
189 if (result != SHM_MQ_WOULD_BLOCK)
190 break;
191
196 }
197
198 pq_mq_busy = false;
199
200 Assert(result == SHM_MQ_SUCCESS || result == SHM_MQ_DETACHED);
201 if (result != SHM_MQ_SUCCESS)
202 return EOF;
203 return 0;
204}
205
206static void
207mq_putmessage_noblock(char msgtype, const char *s, size_t len)
208{
209 /*
210 * While the shm_mq machinery does support sending a message in
211 * non-blocking mode, there's currently no way to try sending beginning to
212 * send the message that doesn't also commit us to completing the
213 * transmission. This could be improved in the future, but for now we
214 * don't need it.
215 */
216 elog(ERROR, "not currently supported");
217}
218
219/*
220 * Parse an ErrorResponse or NoticeResponse payload and populate an ErrorData
221 * structure with the results.
222 */
223void
225{
226 /* Initialize edata with reasonable defaults. */
227 MemSet(edata, 0, sizeof(ErrorData));
228 edata->elevel = ERROR;
229 edata->assoc_context = CurrentMemoryContext;
230
231 /* Loop over fields and extract each one. */
232 for (;;)
233 {
234 char code = pq_getmsgbyte(msg);
235 const char *value;
236
237 if (code == '\0')
238 {
239 pq_getmsgend(msg);
240 break;
241 }
243
244 switch (code)
245 {
246 case PG_DIAG_SEVERITY:
247 /* ignore, trusting we'll get a nonlocalized version */
248 break;
250 if (strcmp(value, "DEBUG") == 0)
251 {
252 /*
253 * We can't reconstruct the exact DEBUG level, but
254 * presumably it was >= client_min_messages, so select
255 * DEBUG1 to ensure we'll pass it on to the client.
256 */
257 edata->elevel = DEBUG1;
258 }
259 else if (strcmp(value, "LOG") == 0)
260 {
261 /*
262 * It can't be LOG_SERVER_ONLY, or the worker wouldn't
263 * have sent it to us; so LOG is the correct value.
264 */
265 edata->elevel = LOG;
266 }
267 else if (strcmp(value, "INFO") == 0)
268 edata->elevel = INFO;
269 else if (strcmp(value, "NOTICE") == 0)
270 edata->elevel = NOTICE;
271 else if (strcmp(value, "WARNING") == 0)
272 edata->elevel = WARNING;
273 else if (strcmp(value, "ERROR") == 0)
274 edata->elevel = ERROR;
275 else if (strcmp(value, "FATAL") == 0)
276 edata->elevel = FATAL;
277 else if (strcmp(value, "PANIC") == 0)
278 edata->elevel = PANIC;
279 else
280 elog(ERROR, "unrecognized error severity: \"%s\"", value);
281 break;
282 case PG_DIAG_SQLSTATE:
283 if (strlen(value) != 5)
284 elog(ERROR, "invalid SQLSTATE: \"%s\"", value);
285 edata->sqlerrcode = MAKE_SQLSTATE(value[0], value[1], value[2],
286 value[3], value[4]);
287 break;
289 edata->message = pstrdup(value);
290 break;
292 edata->detail = pstrdup(value);
293 break;
295 edata->hint = pstrdup(value);
296 break;
298 edata->cursorpos = pg_strtoint32(value);
299 break;
301 edata->internalpos = pg_strtoint32(value);
302 break;
304 edata->internalquery = pstrdup(value);
305 break;
306 case PG_DIAG_CONTEXT:
307 edata->context = pstrdup(value);
308 break;
310 edata->schema_name = pstrdup(value);
311 break;
313 edata->table_name = pstrdup(value);
314 break;
316 edata->column_name = pstrdup(value);
317 break;
319 edata->datatype_name = pstrdup(value);
320 break;
322 edata->constraint_name = pstrdup(value);
323 break;
325 edata->filename = pstrdup(value);
326 break;
328 edata->lineno = pg_strtoint32(value);
329 break;
331 edata->funcname = pstrdup(value);
332 break;
333 default:
334 elog(ERROR, "unrecognized error field code: %d", code);
335 break;
336 }
337 }
338}
bool IsLogicalParallelApplyWorker(void)
Definition worker.c:6073
#define Assert(condition)
Definition c.h:942
#define MemSet(start, val, len)
Definition c.h:1106
@ DestRemote
Definition dest.h:89
@ DestNone
Definition dest.h:87
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition dsm.c:1132
Datum arg
Definition elog.c:1322
#define LOG
Definition elog.h:31
#define FATAL
Definition elog.h:41
#define WARNING
Definition elog.h:36
#define PANIC
Definition elog.h:42
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition elog.h:56
#define elog(elevel,...)
Definition elog.h:226
#define NOTICE
Definition elog.h:35
#define INFO
Definition elog.h:34
ProtocolVersion FrontendProtocol
Definition globals.c:30
struct Latch * MyLatch
Definition globals.c:63
#define IsParallelWorker()
Definition parallel.h:62
static struct @174 value
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
int32 pg_strtoint32(const char *s)
Definition numutils.c:382
const void size_t len
CommandDest whereToSendOutput
Definition postgres.c:94
uint64_t Datum
Definition postgres.h:70
#define PG_DIAG_INTERNAL_QUERY
#define PG_DIAG_SCHEMA_NAME
#define PG_DIAG_CONSTRAINT_NAME
#define PG_DIAG_DATATYPE_NAME
#define PG_DIAG_SOURCE_LINE
#define PG_DIAG_STATEMENT_POSITION
#define PG_DIAG_SOURCE_FILE
#define PG_DIAG_MESSAGE_HINT
#define PG_DIAG_SQLSTATE
#define PG_DIAG_SEVERITY_NONLOCALIZED
#define PG_DIAG_TABLE_NAME
#define PG_DIAG_MESSAGE_PRIMARY
#define PG_DIAG_COLUMN_NAME
#define PG_DIAG_MESSAGE_DETAIL
#define PG_DIAG_CONTEXT
#define PG_DIAG_SEVERITY
#define PG_DIAG_SOURCE_FUNCTION
#define PG_DIAG_INTERNAL_POSITION
const PQcommMethods * PqCommMethods
Definition pqcomm.c:165
#define PG_PROTOCOL_LATEST
Definition pqcomm.h:95
void pq_getmsgend(StringInfo msg)
Definition pqformat.c:634
int pq_getmsgbyte(StringInfo msg)
Definition pqformat.c:398
const char * pq_getmsgrawstring(StringInfo msg)
Definition pqformat.c:607
static int mq_flush(void)
Definition pqmq.c:98
static bool pq_mq_busy
Definition pqmq.c:29
static void pq_cleanup_redirect_to_shm_mq(dsm_segment *seg, Datum arg)
Definition pqmq.c:69
static const PQcommMethods PqCommMqMethods
Definition pqmq.c:41
static ProcNumber pq_mq_parallel_leader_proc_number
Definition pqmq.c:31
void pq_set_parallel_leader(pid_t pid, ProcNumber procNumber)
Definition pqmq.c:84
static int mq_putmessage(char msgtype, const char *s, size_t len)
Definition pqmq.c:124
static bool mq_is_send_pending(void)
Definition pqmq.c:112
static pid_t pq_mq_parallel_leader_pid
Definition pqmq.c:30
void pq_parse_errornotice(StringInfo msg, ErrorData *edata)
Definition pqmq.c:224
static shm_mq_handle * pq_mq_handle
Definition pqmq.c:28
static void mq_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition pqmq.c:207
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition pqmq.c:55
static void mq_comm_reset(void)
Definition pqmq.c:92
static int mq_flush_if_writable(void)
Definition pqmq.c:105
static int fb(int x)
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
int ProcNumber
Definition procnumber.h:24
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition procsignal.c:287
@ PROCSIG_PARALLEL_MESSAGE
Definition procsignal.h:34
@ PROCSIG_PARALLEL_APPLY_MESSAGE
Definition procsignal.h:38
void shm_mq_detach(shm_mq_handle *mqh)
Definition shm_mq.c:845
shm_mq_result shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait, bool force_flush)
Definition shm_mq.c:363
shm_mq_result
Definition shm_mq.h:39
@ SHM_MQ_SUCCESS
Definition shm_mq.h:40
@ SHM_MQ_WOULD_BLOCK
Definition shm_mq.h:41
@ SHM_MQ_DETACHED
Definition shm_mq.h:42
void(* comm_reset)(void)
Definition libpq.h:38
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET