PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pqmq.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "libpq/pqmq.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/logicalworker.h"
#include "storage/latch.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
Include dependency graph for pqmq.c:

Go to the source code of this file.

Functions

static void pq_cleanup_redirect_to_shm_mq (dsm_segment *seg, Datum arg)
 
static void mq_comm_reset (void)
 
static int mq_flush (void)
 
static int mq_flush_if_writable (void)
 
static bool mq_is_send_pending (void)
 
static int mq_putmessage (char msgtype, const char *s, size_t len)
 
static void mq_putmessage_noblock (char msgtype, const char *s, size_t len)
 
void pq_redirect_to_shm_mq (dsm_segment *seg, shm_mq_handle *mqh)
 
void pq_set_parallel_leader (pid_t pid, ProcNumber procNumber)
 
void pq_parse_errornotice (StringInfo msg, ErrorData *edata)
 

Variables

static shm_mq_handlepq_mq_handle = NULL
 
static bool pq_mq_busy = false
 
static pid_t pq_mq_parallel_leader_pid = 0
 
static ProcNumber pq_mq_parallel_leader_proc_number = INVALID_PROC_NUMBER
 
static const PQcommMethods PqCommMqMethods
 

Function Documentation

◆ mq_comm_reset()

static void mq_comm_reset ( void  )
static

Definition at line 91 of file pqmq.c.

92{
93 /* Nothing to do. */
94}

◆ mq_flush()

static int mq_flush ( void  )
static

Definition at line 97 of file pqmq.c.

98{
99 /* Nothing to do. */
100 return 0;
101}

◆ mq_flush_if_writable()

static int mq_flush_if_writable ( void  )
static

Definition at line 104 of file pqmq.c.

105{
106 /* Nothing to do. */
107 return 0;
108}

◆ mq_is_send_pending()

static bool mq_is_send_pending ( void  )
static

Definition at line 111 of file pqmq.c.

112{
113 /* There's never anything pending. */
114 return 0;
115}

◆ mq_putmessage()

static int mq_putmessage ( char  msgtype,
const char s,
size_t  len 
)
static

Definition at line 123 of file pqmq.c.

124{
125 shm_mq_iovec iov[2];
126 shm_mq_result result;
127
128 /*
129 * If we're sending a message, and we have to wait because the queue is
130 * full, and then we get interrupted, and that interrupt results in trying
131 * to send another message, we respond by detaching the queue. There's no
132 * way to return to the original context, but even if there were, just
133 * queueing the message would amount to indefinitely postponing the
134 * response to the interrupt. So we do this instead.
135 */
136 if (pq_mq_busy)
137 {
138 if (pq_mq_handle != NULL)
139 {
143 }
144 return EOF;
145 }
146
147 /*
148 * If the message queue is already gone, just ignore the message. This
149 * doesn't necessarily indicate a problem; for example, DEBUG messages can
150 * be generated late in the shutdown sequence, after all DSMs have already
151 * been detached.
152 */
153 if (pq_mq_handle == NULL)
154 return 0;
155
156 pq_mq_busy = true;
157
158 iov[0].data = &msgtype;
159 iov[0].len = 1;
160 iov[1].data = s;
161 iov[1].len = len;
162
163 for (;;)
164 {
165 /*
166 * Immediately notify the receiver by passing force_flush as true so
167 * that the shared memory value is updated before we send the parallel
168 * message signal right after this.
169 */
171 result = shm_mq_sendv(pq_mq_handle, iov, 2, true, true);
172
174 {
179 else
180 {
185 }
186 }
187
188 if (result != SHM_MQ_WOULD_BLOCK)
189 break;
190
195 }
196
197 pq_mq_busy = false;
198
199 Assert(result == SHM_MQ_SUCCESS || result == SHM_MQ_DETACHED);
200 if (result != SHM_MQ_SUCCESS)
201 return EOF;
202 return 0;
203}
bool IsLogicalParallelApplyWorker(void)
Definition worker.c:6052
#define Assert(condition)
Definition c.h:885
struct Latch * MyLatch
Definition globals.c:63
#define IsParallelWorker()
Definition parallel.h:62
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
void pfree(void *pointer)
Definition mcxt.c:1616
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
const void size_t len
static bool pq_mq_busy
Definition pqmq.c:28
static ProcNumber pq_mq_parallel_leader_proc_number
Definition pqmq.c:30
static pid_t pq_mq_parallel_leader_pid
Definition pqmq.c:29
static shm_mq_handle * pq_mq_handle
Definition pqmq.c:27
static int fb(int x)
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition procsignal.c:286
@ 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:844
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:362
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
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET

References Assert, CHECK_FOR_INTERRUPTS, fb(), IsLogicalParallelApplyWorker(), IsParallelWorker, len, MyLatch, pfree(), pq_mq_busy, pq_mq_handle, pq_mq_parallel_leader_pid, pq_mq_parallel_leader_proc_number, PROCSIG_PARALLEL_APPLY_MESSAGE, PROCSIG_PARALLEL_MESSAGE, ResetLatch(), SendProcSignal(), shm_mq_detach(), SHM_MQ_DETACHED, shm_mq_sendv(), SHM_MQ_SUCCESS, SHM_MQ_WOULD_BLOCK, WaitLatch(), WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

◆ mq_putmessage_noblock()

static void mq_putmessage_noblock ( char  msgtype,
const char s,
size_t  len 
)
static

Definition at line 206 of file pqmq.c.

207{
208 /*
209 * While the shm_mq machinery does support sending a message in
210 * non-blocking mode, there's currently no way to try sending beginning to
211 * send the message that doesn't also commit us to completing the
212 * transmission. This could be improved in the future, but for now we
213 * don't need it.
214 */
215 elog(ERROR, "not currently supported");
216}
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226

References elog, and ERROR.

◆ pq_cleanup_redirect_to_shm_mq()

static void pq_cleanup_redirect_to_shm_mq ( dsm_segment seg,
Datum  arg 
)
static

Definition at line 68 of file pqmq.c.

69{
70 if (pq_mq_handle != NULL)
71 {
74 }
76}
@ DestNone
Definition dest.h:87
CommandDest whereToSendOutput
Definition postgres.c:93

References DestNone, fb(), pfree(), pq_mq_handle, and whereToSendOutput.

Referenced by pq_redirect_to_shm_mq().

◆ pq_parse_errornotice()

void pq_parse_errornotice ( StringInfo  msg,
ErrorData edata 
)

Definition at line 223 of file pqmq.c.

224{
225 /* Initialize edata with reasonable defaults. */
226 MemSet(edata, 0, sizeof(ErrorData));
227 edata->elevel = ERROR;
228 edata->assoc_context = CurrentMemoryContext;
229
230 /* Loop over fields and extract each one. */
231 for (;;)
232 {
233 char code = pq_getmsgbyte(msg);
234 const char *value;
235
236 if (code == '\0')
237 {
238 pq_getmsgend(msg);
239 break;
240 }
242
243 switch (code)
244 {
245 case PG_DIAG_SEVERITY:
246 /* ignore, trusting we'll get a nonlocalized version */
247 break;
249 if (strcmp(value, "DEBUG") == 0)
250 {
251 /*
252 * We can't reconstruct the exact DEBUG level, but
253 * presumably it was >= client_min_messages, so select
254 * DEBUG1 to ensure we'll pass it on to the client.
255 */
256 edata->elevel = DEBUG1;
257 }
258 else if (strcmp(value, "LOG") == 0)
259 {
260 /*
261 * It can't be LOG_SERVER_ONLY, or the worker wouldn't
262 * have sent it to us; so LOG is the correct value.
263 */
264 edata->elevel = LOG;
265 }
266 else if (strcmp(value, "INFO") == 0)
267 edata->elevel = INFO;
268 else if (strcmp(value, "NOTICE") == 0)
269 edata->elevel = NOTICE;
270 else if (strcmp(value, "WARNING") == 0)
271 edata->elevel = WARNING;
272 else if (strcmp(value, "ERROR") == 0)
273 edata->elevel = ERROR;
274 else if (strcmp(value, "FATAL") == 0)
275 edata->elevel = FATAL;
276 else if (strcmp(value, "PANIC") == 0)
277 edata->elevel = PANIC;
278 else
279 elog(ERROR, "unrecognized error severity: \"%s\"", value);
280 break;
281 case PG_DIAG_SQLSTATE:
282 if (strlen(value) != 5)
283 elog(ERROR, "invalid SQLSTATE: \"%s\"", value);
284 edata->sqlerrcode = MAKE_SQLSTATE(value[0], value[1], value[2],
285 value[3], value[4]);
286 break;
288 edata->message = pstrdup(value);
289 break;
291 edata->detail = pstrdup(value);
292 break;
294 edata->hint = pstrdup(value);
295 break;
297 edata->cursorpos = pg_strtoint32(value);
298 break;
300 edata->internalpos = pg_strtoint32(value);
301 break;
303 edata->internalquery = pstrdup(value);
304 break;
305 case PG_DIAG_CONTEXT:
306 edata->context = pstrdup(value);
307 break;
309 edata->schema_name = pstrdup(value);
310 break;
312 edata->table_name = pstrdup(value);
313 break;
315 edata->column_name = pstrdup(value);
316 break;
318 edata->datatype_name = pstrdup(value);
319 break;
321 edata->constraint_name = pstrdup(value);
322 break;
324 edata->filename = pstrdup(value);
325 break;
327 edata->lineno = pg_strtoint32(value);
328 break;
330 edata->funcname = pstrdup(value);
331 break;
332 default:
333 elog(ERROR, "unrecognized error field code: %d", code);
334 break;
335 }
336 }
337}
#define MemSet(start, val, len)
Definition c.h:1035
#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 MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition elog.h:56
#define NOTICE
Definition elog.h:35
#define INFO
Definition elog.h:34
static struct @174 value
char * pstrdup(const char *in)
Definition mcxt.c:1781
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
int32 pg_strtoint32(const char *s)
Definition numutils.c:382
#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
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

References CurrentMemoryContext, DEBUG1, elog, ERROR, FATAL, fb(), INFO, LOG, MAKE_SQLSTATE, MemSet, NOTICE, PANIC, PG_DIAG_COLUMN_NAME, PG_DIAG_CONSTRAINT_NAME, PG_DIAG_CONTEXT, PG_DIAG_DATATYPE_NAME, PG_DIAG_INTERNAL_POSITION, PG_DIAG_INTERNAL_QUERY, PG_DIAG_MESSAGE_DETAIL, PG_DIAG_MESSAGE_HINT, PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SCHEMA_NAME, PG_DIAG_SEVERITY, PG_DIAG_SEVERITY_NONLOCALIZED, PG_DIAG_SOURCE_FILE, PG_DIAG_SOURCE_FUNCTION, PG_DIAG_SOURCE_LINE, PG_DIAG_SQLSTATE, PG_DIAG_STATEMENT_POSITION, PG_DIAG_TABLE_NAME, pg_strtoint32(), pq_getmsgbyte(), pq_getmsgend(), pq_getmsgrawstring(), pstrdup(), value, and WARNING.

Referenced by ProcessParallelApplyMessage(), and ProcessParallelMessage().

◆ pq_redirect_to_shm_mq()

void pq_redirect_to_shm_mq ( dsm_segment seg,
shm_mq_handle mqh 
)

Definition at line 54 of file pqmq.c.

55{
61}
@ DestRemote
Definition dest.h:89
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition dsm.c:1132
ProtocolVersion FrontendProtocol
Definition globals.c:30
uint64_t Datum
Definition postgres.h:70
const PQcommMethods * PqCommMethods
Definition pqcomm.c:165
#define PG_PROTOCOL_LATEST
Definition pqcomm.h:95
static void pq_cleanup_redirect_to_shm_mq(dsm_segment *seg, Datum arg)
Definition pqmq.c:68
static const PQcommMethods PqCommMqMethods
Definition pqmq.c:40

References DestRemote, fb(), FrontendProtocol, on_dsm_detach(), PG_PROTOCOL_LATEST, pq_cleanup_redirect_to_shm_mq(), pq_mq_handle, PqCommMethods, PqCommMqMethods, and whereToSendOutput.

Referenced by ParallelApplyWorkerMain(), and ParallelWorkerMain().

◆ pq_set_parallel_leader()

void pq_set_parallel_leader ( pid_t  pid,
ProcNumber  procNumber 
)

Variable Documentation

◆ pq_mq_busy

bool pq_mq_busy = false
static

Definition at line 28 of file pqmq.c.

Referenced by mq_putmessage().

◆ pq_mq_handle

shm_mq_handle* pq_mq_handle = NULL
static

Definition at line 27 of file pqmq.c.

Referenced by mq_putmessage(), pq_cleanup_redirect_to_shm_mq(), and pq_redirect_to_shm_mq().

◆ pq_mq_parallel_leader_pid

pid_t pq_mq_parallel_leader_pid = 0
static

Definition at line 29 of file pqmq.c.

Referenced by mq_putmessage(), and pq_set_parallel_leader().

◆ pq_mq_parallel_leader_proc_number

ProcNumber pq_mq_parallel_leader_proc_number = INVALID_PROC_NUMBER
static

Definition at line 30 of file pqmq.c.

Referenced by mq_putmessage(), and pq_set_parallel_leader().

◆ PqCommMqMethods

const PQcommMethods PqCommMqMethods
static
Initial value:
= {
.comm_reset = mq_comm_reset,
.flush = mq_flush,
.flush_if_writable = mq_flush_if_writable,
.is_send_pending = mq_is_send_pending,
.putmessage = mq_putmessage,
.putmessage_noblock = mq_putmessage_noblock
}
static int mq_flush(void)
Definition pqmq.c:97
static int mq_putmessage(char msgtype, const char *s, size_t len)
Definition pqmq.c:123
static bool mq_is_send_pending(void)
Definition pqmq.c:111
static void mq_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition pqmq.c:206
static void mq_comm_reset(void)
Definition pqmq.c:91
static int mq_flush_if_writable(void)
Definition pqmq.c:104

Definition at line 40 of file pqmq.c.

40 {
41 .comm_reset = mq_comm_reset,
42 .flush = mq_flush,
43 .flush_if_writable = mq_flush_if_writable,
44 .is_send_pending = mq_is_send_pending,
45 .putmessage = mq_putmessage,
46 .putmessage_noblock = mq_putmessage_noblock
47};

Referenced by pq_redirect_to_shm_mq(), and pq_set_parallel_leader().