LCOV - code coverage report
Current view: top level - src/backend/libpq - pqmq.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 62.7 % 142 89
Test Date: 2026-12-04 04:16:15 Functions: 60.0 % 10 6
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 48.1 % 77 37

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

Generated by: LCOV version 2.0-1