PostgreSQL Source Code  git master
pqmq.c File Reference
#include "postgres.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "libpq/pqmq.h"
#include "miscadmin.h"
#include "pgstat.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)
 
static void mq_startcopyout (void)
 
static void mq_endcopyout (bool errorAbort)
 
void pq_redirect_to_shm_mq (dsm_segment *seg, shm_mq_handle *mqh)
 
void pq_set_parallel_master (pid_t pid, BackendId backend_id)
 
void pq_parse_errornotice (StringInfo msg, ErrorData *edata)
 

Variables

static shm_mq_handlepq_mq_handle
 
static bool pq_mq_busy = false
 
static pid_t pq_mq_parallel_master_pid = 0
 
static pid_t pq_mq_parallel_master_backend_id = InvalidBackendId
 
static PQcommMethods PqCommMqMethods
 

Function Documentation

◆ mq_comm_reset()

static void mq_comm_reset ( void  )
static

Definition at line 88 of file pqmq.c.

89 {
90  /* Nothing to do. */
91 }

◆ mq_endcopyout()

static void mq_endcopyout ( bool  errorAbort)
static

Definition at line 205 of file pqmq.c.

206 {
207  /* Nothing to do. */
208 }

◆ mq_flush()

static int mq_flush ( void  )
static

Definition at line 94 of file pqmq.c.

95 {
96  /* Nothing to do. */
97  return 0;
98 }

◆ mq_flush_if_writable()

static int mq_flush_if_writable ( void  )
static

Definition at line 101 of file pqmq.c.

102 {
103  /* Nothing to do. */
104  return 0;
105 }

◆ mq_is_send_pending()

static bool mq_is_send_pending ( void  )
static

Definition at line 108 of file pqmq.c.

109 {
110  /* There's never anything pending. */
111  return 0;
112 }

◆ mq_putmessage()

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

Definition at line 120 of file pqmq.c.

References Assert, CHECK_FOR_INTERRUPTS, shm_mq_iovec::data, shm_mq_iovec::len, MyLatch, pq_mq_busy, pq_mq_parallel_master_backend_id, pq_mq_parallel_master_pid, PROCSIG_PARALLEL_MESSAGE, ResetLatch(), SendProcSignal(), shm_mq_detach(), SHM_MQ_DETACHED, shm_mq_sendv(), SHM_MQ_SUCCESS, SHM_MQ_WOULD_BLOCK, WAIT_EVENT_MQ_PUT_MESSAGE, WaitLatch(), and WL_LATCH_SET.

121 {
122  shm_mq_iovec iov[2];
123  shm_mq_result result;
124 
125  /*
126  * If we're sending a message, and we have to wait because the queue is
127  * full, and then we get interrupted, and that interrupt results in trying
128  * to send another message, we respond by detaching the queue. There's no
129  * way to return to the original context, but even if there were, just
130  * queueing the message would amount to indefinitely postponing the
131  * response to the interrupt. So we do this instead.
132  */
133  if (pq_mq_busy)
134  {
135  if (pq_mq_handle != NULL)
137  pq_mq_handle = NULL;
138  return EOF;
139  }
140 
141  /*
142  * If the message queue is already gone, just ignore the message. This
143  * doesn't necessarily indicate a problem; for example, DEBUG messages can
144  * be generated late in the shutdown sequence, after all DSMs have already
145  * been detached.
146  */
147  if (pq_mq_handle == NULL)
148  return 0;
149 
150  pq_mq_busy = true;
151 
152  iov[0].data = &msgtype;
153  iov[0].len = 1;
154  iov[1].data = s;
155  iov[1].len = len;
156 
157  Assert(pq_mq_handle != NULL);
158 
159  for (;;)
160  {
161  result = shm_mq_sendv(pq_mq_handle, iov, 2, true);
162 
163  if (pq_mq_parallel_master_pid != 0)
167 
168  if (result != SHM_MQ_WOULD_BLOCK)
169  break;
170 
175  }
176 
177  pq_mq_busy = false;
178 
179  Assert(result == SHM_MQ_SUCCESS || result == SHM_MQ_DETACHED);
180  if (result != SHM_MQ_SUCCESS)
181  return EOF;
182  return 0;
183 }
static shm_mq_handle * pq_mq_handle
Definition: pqmq.c:24
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:775
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
Size len
Definition: shm_mq.h:32
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:180
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
const char * data
Definition: shm_mq.h:31
static bool pq_mq_busy
Definition: pqmq.c:25
static pid_t pq_mq_parallel_master_pid
Definition: pqmq.c:26
static pid_t pq_mq_parallel_master_backend_id
Definition: pqmq.c:27
shm_mq_result
Definition: shm_mq.h:36
#define Assert(condition)
Definition: c.h:680
shm_mq_result shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait)
Definition: shm_mq.c:351
struct Latch * MyLatch
Definition: globals.c:52
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define WL_LATCH_SET
Definition: latch.h:124

◆ mq_putmessage_noblock()

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

Definition at line 186 of file pqmq.c.

References elog, and ERROR.

187 {
188  /*
189  * While the shm_mq machinery does support sending a message in
190  * non-blocking mode, there's currently no way to try sending beginning to
191  * send the message that doesn't also commit us to completing the
192  * transmission. This could be improved in the future, but for now we
193  * don't need it.
194  */
195  elog(ERROR, "not currently supported");
196 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219

◆ mq_startcopyout()

static void mq_startcopyout ( void  )
static

Definition at line 199 of file pqmq.c.

200 {
201  /* Nothing to do. */
202 }

◆ pq_cleanup_redirect_to_shm_mq()

static void pq_cleanup_redirect_to_shm_mq ( dsm_segment seg,
Datum  arg 
)
static

Definition at line 69 of file pqmq.c.

References DestNone, and whereToSendOutput.

Referenced by pq_redirect_to_shm_mq().

70 {
71  pq_mq_handle = NULL;
73 }
static shm_mq_handle * pq_mq_handle
Definition: pqmq.c:24
Definition: dest.h:88
CommandDest whereToSendOutput
Definition: postgres.c:88

◆ pq_parse_errornotice()

void pq_parse_errornotice ( StringInfo  msg,
ErrorData edata 
)

Definition at line 215 of file pqmq.c.

References ErrorData::assoc_context, ErrorData::column_name, ErrorData::constraint_name, ErrorData::context, CurrentMemoryContext, ErrorData::cursorpos, ErrorData::datatype_name, DEBUG1, ErrorData::detail, ErrorData::elevel, elog, ERROR, FATAL, ErrorData::filename, ErrorData::funcname, ErrorData::hint, INFO, ErrorData::internalpos, ErrorData::internalquery, ErrorData::lineno, LOG, MAKE_SQLSTATE, MemSet, ErrorData::message, NOTICE, PANIC, pg_atoi(), 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, pq_getmsgbyte(), pq_getmsgend(), pq_getmsgrawstring(), pstrdup(), ErrorData::schema_name, ErrorData::sqlerrcode, ErrorData::table_name, value, and WARNING.

Referenced by HandleParallelMessage().

216 {
217  /* Initialize edata with reasonable defaults. */
218  MemSet(edata, 0, sizeof(ErrorData));
219  edata->elevel = ERROR;
221 
222  /* Loop over fields and extract each one. */
223  for (;;)
224  {
225  char code = pq_getmsgbyte(msg);
226  const char *value;
227 
228  if (code == '\0')
229  {
230  pq_getmsgend(msg);
231  break;
232  }
233  value = pq_getmsgrawstring(msg);
234 
235  switch (code)
236  {
237  case PG_DIAG_SEVERITY:
238  /* ignore, trusting we'll get a nonlocalized version */
239  break;
241  if (strcmp(value, "DEBUG") == 0)
242  {
243  /*
244  * We can't reconstruct the exact DEBUG level, but
245  * presumably it was >= client_min_messages, so select
246  * DEBUG1 to ensure we'll pass it on to the client.
247  */
248  edata->elevel = DEBUG1;
249  }
250  else if (strcmp(value, "LOG") == 0)
251  {
252  /*
253  * It can't be LOG_SERVER_ONLY, or the worker wouldn't
254  * have sent it to us; so LOG is the correct value.
255  */
256  edata->elevel = LOG;
257  }
258  else if (strcmp(value, "INFO") == 0)
259  edata->elevel = INFO;
260  else if (strcmp(value, "NOTICE") == 0)
261  edata->elevel = NOTICE;
262  else if (strcmp(value, "WARNING") == 0)
263  edata->elevel = WARNING;
264  else if (strcmp(value, "ERROR") == 0)
265  edata->elevel = ERROR;
266  else if (strcmp(value, "FATAL") == 0)
267  edata->elevel = FATAL;
268  else if (strcmp(value, "PANIC") == 0)
269  edata->elevel = PANIC;
270  else
271  elog(ERROR, "unrecognized error severity: \"%s\"", value);
272  break;
273  case PG_DIAG_SQLSTATE:
274  if (strlen(value) != 5)
275  elog(ERROR, "invalid SQLSTATE: \"%s\"", value);
276  edata->sqlerrcode = MAKE_SQLSTATE(value[0], value[1], value[2],
277  value[3], value[4]);
278  break;
280  edata->message = pstrdup(value);
281  break;
283  edata->detail = pstrdup(value);
284  break;
286  edata->hint = pstrdup(value);
287  break;
289  edata->cursorpos = pg_atoi(value, sizeof(int), '\0');
290  break;
292  edata->internalpos = pg_atoi(value, sizeof(int), '\0');
293  break;
295  edata->internalquery = pstrdup(value);
296  break;
297  case PG_DIAG_CONTEXT:
298  edata->context = pstrdup(value);
299  break;
300  case PG_DIAG_SCHEMA_NAME:
301  edata->schema_name = pstrdup(value);
302  break;
303  case PG_DIAG_TABLE_NAME:
304  edata->table_name = pstrdup(value);
305  break;
306  case PG_DIAG_COLUMN_NAME:
307  edata->column_name = pstrdup(value);
308  break;
310  edata->datatype_name = pstrdup(value);
311  break;
313  edata->constraint_name = pstrdup(value);
314  break;
315  case PG_DIAG_SOURCE_FILE:
316  edata->filename = pstrdup(value);
317  break;
318  case PG_DIAG_SOURCE_LINE:
319  edata->lineno = pg_atoi(value, sizeof(int), '\0');
320  break;
322  edata->funcname = pstrdup(value);
323  break;
324  default:
325  elog(ERROR, "unrecognized error field code: %d", (int) code);
326  break;
327  }
328  }
329 }
char * schema_name
Definition: elog.h:349
#define DEBUG1
Definition: elog.h:25
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:58
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:65
int sqlerrcode
Definition: elog.h:342
#define PG_DIAG_MESSAGE_DETAIL
Definition: postgres_ext.h:59
#define PG_DIAG_COLUMN_NAME
Definition: postgres_ext.h:67
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition: elog.h:62
const char * funcname
Definition: elog.h:339
char * pstrdup(const char *in)
Definition: mcxt.c:1063
#define PG_DIAG_TABLE_NAME
Definition: postgres_ext.h:66
int lineno
Definition: elog.h:338
#define PG_DIAG_SOURCE_LINE
Definition: postgres_ext.h:71
#define MemSet(start, val, len)
Definition: c.h:863
#define INFO
Definition: elog.h:33
char * internalquery
Definition: elog.h:356
#define LOG
Definition: elog.h:26
#define PG_DIAG_INTERNAL_POSITION
Definition: postgres_ext.h:62
const char * pq_getmsgrawstring(StringInfo msg)
Definition: pqformat.c:610
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
#define PANIC
Definition: elog.h:53
int cursorpos
Definition: elog.h:354
#define PG_DIAG_SOURCE_FILE
Definition: postgres_ext.h:70
#define ERROR
Definition: elog.h:43
const char * filename
Definition: elog.h:337
#define FATAL
Definition: elog.h:52
static struct @121 value
char * table_name
Definition: elog.h:350
int internalpos
Definition: elog.h:355
#define PG_DIAG_SEVERITY
Definition: postgres_ext.h:55
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define PG_DIAG_STATEMENT_POSITION
Definition: postgres_ext.h:61
#define WARNING
Definition: elog.h:40
char * datatype_name
Definition: elog.h:352
int elevel
Definition: elog.h:331
char * detail
Definition: elog.h:344
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:68
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:69
#define PG_DIAG_SEVERITY_NONLOCALIZED
Definition: postgres_ext.h:56
#define PG_DIAG_INTERNAL_QUERY
Definition: postgres_ext.h:63
#define PG_DIAG_MESSAGE_HINT
Definition: postgres_ext.h:60
char * column_name
Definition: elog.h:351
#define NOTICE
Definition: elog.h:37
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
char * hint
Definition: elog.h:346
char * context
Definition: elog.h:347
struct MemoryContextData * assoc_context
Definition: elog.h:360
#define elog
Definition: elog.h:219
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:637
char * constraint_name
Definition: elog.h:353
#define PG_DIAG_SOURCE_FUNCTION
Definition: postgres_ext.h:72
char * message
Definition: elog.h:343
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37
#define PG_DIAG_CONTEXT
Definition: postgres_ext.h:64

◆ pq_redirect_to_shm_mq()

void pq_redirect_to_shm_mq ( dsm_segment seg,
shm_mq_handle mqh 
)

Definition at line 55 of file pqmq.c.

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

Referenced by ParallelWorkerMain().

56 {
58  pq_mq_handle = mqh;
62 }
static shm_mq_handle * pq_mq_handle
Definition: pqmq.c:24
PQcommMethods * PqCommMethods
Definition: pqcomm.c:184
#define PG_PROTOCOL_LATEST
Definition: pqcomm.h:111
static void pq_cleanup_redirect_to_shm_mq(dsm_segment *seg, Datum arg)
Definition: pqmq.c:69
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1024
static PQcommMethods PqCommMqMethods
Definition: pqmq.c:39
uintptr_t Datum
Definition: postgres.h:372
CommandDest whereToSendOutput
Definition: postgres.c:88
ProtocolVersion FrontendProtocol
Definition: globals.c:27

◆ pq_set_parallel_master()

void pq_set_parallel_master ( pid_t  pid,
BackendId  backend_id 
)

Definition at line 80 of file pqmq.c.

References Assert, pq_mq_parallel_master_backend_id, pq_mq_parallel_master_pid, and PqCommMethods.

Referenced by ParallelWorkerMain().

81 {
85 }
PQcommMethods * PqCommMethods
Definition: pqcomm.c:184
static PQcommMethods PqCommMqMethods
Definition: pqmq.c:39
static pid_t pq_mq_parallel_master_pid
Definition: pqmq.c:26
static pid_t pq_mq_parallel_master_backend_id
Definition: pqmq.c:27
#define Assert(condition)
Definition: c.h:680

Variable Documentation

◆ pq_mq_busy

bool pq_mq_busy = false
static

Definition at line 25 of file pqmq.c.

Referenced by mq_putmessage().

◆ pq_mq_handle

shm_mq_handle* pq_mq_handle
static

Definition at line 24 of file pqmq.c.

◆ pq_mq_parallel_master_backend_id

pid_t pq_mq_parallel_master_backend_id = InvalidBackendId
static

Definition at line 27 of file pqmq.c.

Referenced by mq_putmessage(), and pq_set_parallel_master().

◆ pq_mq_parallel_master_pid

pid_t pq_mq_parallel_master_pid = 0
static

Definition at line 26 of file pqmq.c.

Referenced by mq_putmessage(), and pq_set_parallel_master().

◆ PqCommMqMethods

PQcommMethods PqCommMqMethods
static
Initial value:
= {
}
static int mq_putmessage(char msgtype, const char *s, size_t len)
Definition: pqmq.c:120
static int mq_flush_if_writable(void)
Definition: pqmq.c:101
static void mq_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition: pqmq.c:186
static void mq_comm_reset(void)
Definition: pqmq.c:88
static void mq_startcopyout(void)
Definition: pqmq.c:199
static bool mq_is_send_pending(void)
Definition: pqmq.c:108
static void mq_endcopyout(bool errorAbort)
Definition: pqmq.c:205
static int mq_flush(void)
Definition: pqmq.c:94

Definition at line 39 of file pqmq.c.

Referenced by pq_redirect_to_shm_mq().