LCOV - code coverage report
Current view: top level - src/backend/utils/activity - backend_status.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 272 302 90.1 %
Date: 2021-09-17 15:07:27 Functions: 18 19 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* ----------
       2             :  * backend_status.c
       3             :  *    Backend status reporting infrastructure.
       4             :  *
       5             :  * Copyright (c) 2001-2021, PostgreSQL Global Development Group
       6             :  *
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *    src/backend/postmaster/backend_status.c
      10             :  * ----------
      11             :  */
      12             : #include "postgres.h"
      13             : 
      14             : #include "access/xact.h"
      15             : #include "libpq/libpq.h"
      16             : #include "miscadmin.h"
      17             : #include "pg_trace.h"
      18             : #include "pgstat.h"
      19             : #include "port/atomics.h"     /* for memory barriers */
      20             : #include "storage/ipc.h"
      21             : #include "storage/proc.h"     /* for MyProc */
      22             : #include "storage/sinvaladt.h"
      23             : #include "utils/ascii.h"
      24             : #include "utils/backend_status.h"
      25             : #include "utils/guc.h"            /* for application_name */
      26             : #include "utils/memutils.h"
      27             : 
      28             : 
      29             : /* ----------
      30             :  * Total number of backends including auxiliary
      31             :  *
      32             :  * We reserve a slot for each possible BackendId, plus one for each
      33             :  * possible auxiliary process type.  (This scheme assumes there is not
      34             :  * more than one of any auxiliary process type at a time.) MaxBackends
      35             :  * includes autovacuum workers and background workers as well.
      36             :  * ----------
      37             :  */
      38             : #define NumBackendStatSlots (MaxBackends + NUM_AUXPROCTYPES)
      39             : 
      40             : 
      41             : /* ----------
      42             :  * GUC parameters
      43             :  * ----------
      44             :  */
      45             : bool        pgstat_track_activities = false;
      46             : int         pgstat_track_activity_query_size = 1024;
      47             : 
      48             : 
      49             : /* exposed so that backend_progress.c can access it */
      50             : PgBackendStatus *MyBEEntry = NULL;
      51             : 
      52             : 
      53             : static PgBackendStatus *BackendStatusArray = NULL;
      54             : static char *BackendAppnameBuffer = NULL;
      55             : static char *BackendClientHostnameBuffer = NULL;
      56             : static char *BackendActivityBuffer = NULL;
      57             : static Size BackendActivityBufferSize = 0;
      58             : #ifdef USE_SSL
      59             : static PgBackendSSLStatus *BackendSslStatusBuffer = NULL;
      60             : #endif
      61             : #ifdef ENABLE_GSS
      62             : static PgBackendGSSStatus *BackendGssStatusBuffer = NULL;
      63             : #endif
      64             : 
      65             : 
      66             : /* Status for backends including auxiliary */
      67             : static LocalPgBackendStatus *localBackendStatusTable = NULL;
      68             : 
      69             : /* Total number of backends including auxiliary */
      70             : static int  localNumBackends = 0;
      71             : 
      72             : static MemoryContext backendStatusSnapContext;
      73             : 
      74             : 
      75             : static void pgstat_beshutdown_hook(int code, Datum arg);
      76             : static void pgstat_read_current_status(void);
      77             : static void pgstat_setup_backend_status_context(void);
      78             : 
      79             : 
      80             : /*
      81             :  * Report shared-memory space needed by CreateSharedBackendStatus.
      82             :  */
      83             : Size
      84        3670 : BackendStatusShmemSize(void)
      85             : {
      86             :     Size        size;
      87             : 
      88             :     /* BackendStatusArray: */
      89        3670 :     size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
      90             :     /* BackendAppnameBuffer: */
      91        3670 :     size = add_size(size,
      92        3670 :                     mul_size(NAMEDATALEN, NumBackendStatSlots));
      93             :     /* BackendClientHostnameBuffer: */
      94        3670 :     size = add_size(size,
      95        3670 :                     mul_size(NAMEDATALEN, NumBackendStatSlots));
      96             :     /* BackendActivityBuffer: */
      97        3670 :     size = add_size(size,
      98        3670 :                     mul_size(pgstat_track_activity_query_size, NumBackendStatSlots));
      99             : #ifdef USE_SSL
     100             :     /* BackendSslStatusBuffer: */
     101        3670 :     size = add_size(size,
     102        3670 :                     mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots));
     103             : #endif
     104             : #ifdef ENABLE_GSS
     105             :     /* BackendGssStatusBuffer: */
     106             :     size = add_size(size,
     107             :                     mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots));
     108             : #endif
     109        3670 :     return size;
     110             : }
     111             : 
     112             : /*
     113             :  * Initialize the shared status array and several string buffers
     114             :  * during postmaster startup.
     115             :  */
     116             : void
     117        2760 : CreateSharedBackendStatus(void)
     118             : {
     119             :     Size        size;
     120             :     bool        found;
     121             :     int         i;
     122             :     char       *buffer;
     123             : 
     124             :     /* Create or attach to the shared array */
     125        2760 :     size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
     126        2760 :     BackendStatusArray = (PgBackendStatus *)
     127        2760 :         ShmemInitStruct("Backend Status Array", size, &found);
     128             : 
     129        2760 :     if (!found)
     130             :     {
     131             :         /*
     132             :          * We're the first - initialize.
     133             :          */
     134        2760 :         MemSet(BackendStatusArray, 0, size);
     135             :     }
     136             : 
     137             :     /* Create or attach to the shared appname buffer */
     138        2760 :     size = mul_size(NAMEDATALEN, NumBackendStatSlots);
     139        2760 :     BackendAppnameBuffer = (char *)
     140        2760 :         ShmemInitStruct("Backend Application Name Buffer", size, &found);
     141             : 
     142        2760 :     if (!found)
     143             :     {
     144        2760 :         MemSet(BackendAppnameBuffer, 0, size);
     145             : 
     146             :         /* Initialize st_appname pointers. */
     147        2760 :         buffer = BackendAppnameBuffer;
     148      308326 :         for (i = 0; i < NumBackendStatSlots; i++)
     149             :         {
     150      305566 :             BackendStatusArray[i].st_appname = buffer;
     151      305566 :             buffer += NAMEDATALEN;
     152             :         }
     153             :     }
     154             : 
     155             :     /* Create or attach to the shared client hostname buffer */
     156        2760 :     size = mul_size(NAMEDATALEN, NumBackendStatSlots);
     157        2760 :     BackendClientHostnameBuffer = (char *)
     158        2760 :         ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
     159             : 
     160        2760 :     if (!found)
     161             :     {
     162        2760 :         MemSet(BackendClientHostnameBuffer, 0, size);
     163             : 
     164             :         /* Initialize st_clienthostname pointers. */
     165        2760 :         buffer = BackendClientHostnameBuffer;
     166      308326 :         for (i = 0; i < NumBackendStatSlots; i++)
     167             :         {
     168      305566 :             BackendStatusArray[i].st_clienthostname = buffer;
     169      305566 :             buffer += NAMEDATALEN;
     170             :         }
     171             :     }
     172             : 
     173             :     /* Create or attach to the shared activity buffer */
     174        5520 :     BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
     175        2760 :                                          NumBackendStatSlots);
     176        2760 :     BackendActivityBuffer = (char *)
     177        2760 :         ShmemInitStruct("Backend Activity Buffer",
     178             :                         BackendActivityBufferSize,
     179             :                         &found);
     180             : 
     181        2760 :     if (!found)
     182             :     {
     183        2760 :         MemSet(BackendActivityBuffer, 0, BackendActivityBufferSize);
     184             : 
     185             :         /* Initialize st_activity pointers. */
     186        2760 :         buffer = BackendActivityBuffer;
     187      308326 :         for (i = 0; i < NumBackendStatSlots; i++)
     188             :         {
     189      305566 :             BackendStatusArray[i].st_activity_raw = buffer;
     190      305566 :             buffer += pgstat_track_activity_query_size;
     191             :         }
     192             :     }
     193             : 
     194             : #ifdef USE_SSL
     195             :     /* Create or attach to the shared SSL status buffer */
     196        2760 :     size = mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots);
     197        2760 :     BackendSslStatusBuffer = (PgBackendSSLStatus *)
     198        2760 :         ShmemInitStruct("Backend SSL Status Buffer", size, &found);
     199             : 
     200        2760 :     if (!found)
     201             :     {
     202             :         PgBackendSSLStatus *ptr;
     203             : 
     204        2760 :         MemSet(BackendSslStatusBuffer, 0, size);
     205             : 
     206             :         /* Initialize st_sslstatus pointers. */
     207        2760 :         ptr = BackendSslStatusBuffer;
     208      308326 :         for (i = 0; i < NumBackendStatSlots; i++)
     209             :         {
     210      305566 :             BackendStatusArray[i].st_sslstatus = ptr;
     211      305566 :             ptr++;
     212             :         }
     213             :     }
     214             : #endif
     215             : 
     216             : #ifdef ENABLE_GSS
     217             :     /* Create or attach to the shared GSSAPI status buffer */
     218             :     size = mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots);
     219             :     BackendGssStatusBuffer = (PgBackendGSSStatus *)
     220             :         ShmemInitStruct("Backend GSS Status Buffer", size, &found);
     221             : 
     222             :     if (!found)
     223             :     {
     224             :         PgBackendGSSStatus *ptr;
     225             : 
     226             :         MemSet(BackendGssStatusBuffer, 0, size);
     227             : 
     228             :         /* Initialize st_gssstatus pointers. */
     229             :         ptr = BackendGssStatusBuffer;
     230             :         for (i = 0; i < NumBackendStatSlots; i++)
     231             :         {
     232             :             BackendStatusArray[i].st_gssstatus = ptr;
     233             :             ptr++;
     234             :         }
     235             :     }
     236             : #endif
     237        2760 : }
     238             : 
     239             : /*
     240             :  * Initialize pgstats backend activity state, and set up our on-proc-exit
     241             :  * hook.  Called from InitPostgres and AuxiliaryProcessMain. For auxiliary
     242             :  * process, MyBackendId is invalid. Otherwise, MyBackendId must be set, but we
     243             :  * must not have started any transaction yet (since the exit hook must run
     244             :  * after the last transaction exit).
     245             :  *
     246             :  * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
     247             :  */
     248             : void
     249       16812 : pgstat_beinit(void)
     250             : {
     251             :     /* Initialize MyBEEntry */
     252       16812 :     if (MyBackendId != InvalidBackendId)
     253             :     {
     254             :         Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
     255       14188 :         MyBEEntry = &BackendStatusArray[MyBackendId - 1];
     256             :     }
     257             :     else
     258             :     {
     259             :         /* Must be an auxiliary process */
     260             :         Assert(MyAuxProcType != NotAnAuxProcess);
     261             : 
     262             :         /*
     263             :          * Assign the MyBEEntry for an auxiliary process.  Since it doesn't
     264             :          * have a BackendId, the slot is statically allocated based on the
     265             :          * auxiliary process type (MyAuxProcType).  Backends use slots indexed
     266             :          * in the range from 1 to MaxBackends (inclusive), so we use
     267             :          * MaxBackends + AuxBackendType + 1 as the index of the slot for an
     268             :          * auxiliary process.
     269             :          */
     270        2624 :         MyBEEntry = &BackendStatusArray[MaxBackends + MyAuxProcType];
     271             :     }
     272             : 
     273             :     /* Set up a process-exit hook to clean up */
     274       16812 :     on_shmem_exit(pgstat_beshutdown_hook, 0);
     275       16812 : }
     276             : 
     277             : 
     278             : /* ----------
     279             :  * pgstat_bestart() -
     280             :  *
     281             :  *  Initialize this backend's entry in the PgBackendStatus array.
     282             :  *  Called from InitPostgres.
     283             :  *
     284             :  *  Apart from auxiliary processes, MyBackendId, MyDatabaseId,
     285             :  *  session userid, and application_name must be set for a
     286             :  *  backend (hence, this cannot be combined with pgbestat_beinit).
     287             :  *  Note also that we must be inside a transaction if this isn't an aux
     288             :  *  process, as we may need to do encoding conversion on some strings.
     289             :  * ----------
     290             :  */
     291             : void
     292       16732 : pgstat_bestart(void)
     293             : {
     294       16732 :     volatile PgBackendStatus *vbeentry = MyBEEntry;
     295             :     PgBackendStatus lbeentry;
     296             : #ifdef USE_SSL
     297             :     PgBackendSSLStatus lsslstatus;
     298             : #endif
     299             : #ifdef ENABLE_GSS
     300             :     PgBackendGSSStatus lgssstatus;
     301             : #endif
     302             : 
     303             :     /* pgstats state must be initialized from pgstat_beinit() */
     304             :     Assert(vbeentry != NULL);
     305             : 
     306             :     /*
     307             :      * To minimize the time spent modifying the PgBackendStatus entry, and
     308             :      * avoid risk of errors inside the critical section, we first copy the
     309             :      * shared-memory struct to a local variable, then modify the data in the
     310             :      * local variable, then copy the local variable back to shared memory.
     311             :      * Only the last step has to be inside the critical section.
     312             :      *
     313             :      * Most of the data we copy from shared memory is just going to be
     314             :      * overwritten, but the struct's not so large that it's worth the
     315             :      * maintenance hassle to copy only the needful fields.
     316             :      */
     317       16732 :     memcpy(&lbeentry,
     318       16732 :            unvolatize(PgBackendStatus *, vbeentry),
     319             :            sizeof(PgBackendStatus));
     320             : 
     321             :     /* These structs can just start from zeroes each time, though */
     322             : #ifdef USE_SSL
     323       16732 :     memset(&lsslstatus, 0, sizeof(lsslstatus));
     324             : #endif
     325             : #ifdef ENABLE_GSS
     326             :     memset(&lgssstatus, 0, sizeof(lgssstatus));
     327             : #endif
     328             : 
     329             :     /*
     330             :      * Now fill in all the fields of lbeentry, except for strings that are
     331             :      * out-of-line data.  Those have to be handled separately, below.
     332             :      */
     333       16732 :     lbeentry.st_procpid = MyProcPid;
     334       16732 :     lbeentry.st_backendType = MyBackendType;
     335       16732 :     lbeentry.st_proc_start_timestamp = MyStartTimestamp;
     336       16732 :     lbeentry.st_activity_start_timestamp = 0;
     337       16732 :     lbeentry.st_state_start_timestamp = 0;
     338       16732 :     lbeentry.st_xact_start_timestamp = 0;
     339       16732 :     lbeentry.st_databaseid = MyDatabaseId;
     340             : 
     341             :     /* We have userid for client-backends, wal-sender and bgworker processes */
     342       16732 :     if (lbeentry.st_backendType == B_BACKEND
     343        6942 :         || lbeentry.st_backendType == B_WAL_SENDER
     344        6064 :         || lbeentry.st_backendType == B_BG_WORKER)
     345       13120 :         lbeentry.st_userid = GetSessionUserId();
     346             :     else
     347        3612 :         lbeentry.st_userid = InvalidOid;
     348             : 
     349             :     /*
     350             :      * We may not have a MyProcPort (eg, if this is the autovacuum process).
     351             :      * If so, use all-zeroes client address, which is dealt with specially in
     352             :      * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
     353             :      */
     354       16732 :     if (MyProcPort)
     355       10668 :         memcpy(&lbeentry.st_clientaddr, &MyProcPort->raddr,
     356             :                sizeof(lbeentry.st_clientaddr));
     357             :     else
     358      109152 :         MemSet(&lbeentry.st_clientaddr, 0, sizeof(lbeentry.st_clientaddr));
     359             : 
     360             : #ifdef USE_SSL
     361       16732 :     if (MyProcPort && MyProcPort->ssl_in_use)
     362             :     {
     363          72 :         lbeentry.st_ssl = true;
     364          72 :         lsslstatus.ssl_bits = be_tls_get_cipher_bits(MyProcPort);
     365          72 :         strlcpy(lsslstatus.ssl_version, be_tls_get_version(MyProcPort), NAMEDATALEN);
     366          72 :         strlcpy(lsslstatus.ssl_cipher, be_tls_get_cipher(MyProcPort), NAMEDATALEN);
     367          72 :         be_tls_get_peer_subject_name(MyProcPort, lsslstatus.ssl_client_dn, NAMEDATALEN);
     368          72 :         be_tls_get_peer_serial(MyProcPort, lsslstatus.ssl_client_serial, NAMEDATALEN);
     369          72 :         be_tls_get_peer_issuer_name(MyProcPort, lsslstatus.ssl_issuer_dn, NAMEDATALEN);
     370             :     }
     371             :     else
     372             :     {
     373       16660 :         lbeentry.st_ssl = false;
     374             :     }
     375             : #else
     376             :     lbeentry.st_ssl = false;
     377             : #endif
     378             : 
     379             : #ifdef ENABLE_GSS
     380             :     if (MyProcPort && MyProcPort->gss != NULL)
     381             :     {
     382             :         const char *princ = be_gssapi_get_princ(MyProcPort);
     383             : 
     384             :         lbeentry.st_gss = true;
     385             :         lgssstatus.gss_auth = be_gssapi_get_auth(MyProcPort);
     386             :         lgssstatus.gss_enc = be_gssapi_get_enc(MyProcPort);
     387             :         if (princ)
     388             :             strlcpy(lgssstatus.gss_princ, princ, NAMEDATALEN);
     389             :     }
     390             :     else
     391             :     {
     392             :         lbeentry.st_gss = false;
     393             :     }
     394             : #else
     395       16732 :     lbeentry.st_gss = false;
     396             : #endif
     397             : 
     398       16732 :     lbeentry.st_state = STATE_UNDEFINED;
     399       16732 :     lbeentry.st_progress_command = PROGRESS_COMMAND_INVALID;
     400       16732 :     lbeentry.st_progress_command_target = InvalidOid;
     401       16732 :     lbeentry.st_query_id = UINT64CONST(0);
     402             : 
     403             :     /*
     404             :      * we don't zero st_progress_param here to save cycles; nobody should
     405             :      * examine it until st_progress_command has been set to something other
     406             :      * than PROGRESS_COMMAND_INVALID
     407             :      */
     408             : 
     409             :     /*
     410             :      * We're ready to enter the critical section that fills the shared-memory
     411             :      * status entry.  We follow the protocol of bumping st_changecount before
     412             :      * and after; and make sure it's even afterwards.  We use a volatile
     413             :      * pointer here to ensure the compiler doesn't try to get cute.
     414             :      */
     415       16732 :     PGSTAT_BEGIN_WRITE_ACTIVITY(vbeentry);
     416             : 
     417             :     /* make sure we'll memcpy the same st_changecount back */
     418       16732 :     lbeentry.st_changecount = vbeentry->st_changecount;
     419             : 
     420       16732 :     memcpy(unvolatize(PgBackendStatus *, vbeentry),
     421             :            &lbeentry,
     422             :            sizeof(PgBackendStatus));
     423             : 
     424             :     /*
     425             :      * We can write the out-of-line strings and structs using the pointers
     426             :      * that are in lbeentry; this saves some de-volatilizing messiness.
     427             :      */
     428       16732 :     lbeentry.st_appname[0] = '\0';
     429       16732 :     if (MyProcPort && MyProcPort->remote_hostname)
     430          72 :         strlcpy(lbeentry.st_clienthostname, MyProcPort->remote_hostname,
     431             :                 NAMEDATALEN);
     432             :     else
     433       16660 :         lbeentry.st_clienthostname[0] = '\0';
     434       16732 :     lbeentry.st_activity_raw[0] = '\0';
     435             :     /* Also make sure the last byte in each string area is always 0 */
     436       16732 :     lbeentry.st_appname[NAMEDATALEN - 1] = '\0';
     437       16732 :     lbeentry.st_clienthostname[NAMEDATALEN - 1] = '\0';
     438       16732 :     lbeentry.st_activity_raw[pgstat_track_activity_query_size - 1] = '\0';
     439             : 
     440             : #ifdef USE_SSL
     441       16732 :     memcpy(lbeentry.st_sslstatus, &lsslstatus, sizeof(PgBackendSSLStatus));
     442             : #endif
     443             : #ifdef ENABLE_GSS
     444             :     memcpy(lbeentry.st_gssstatus, &lgssstatus, sizeof(PgBackendGSSStatus));
     445             : #endif
     446             : 
     447       16732 :     PGSTAT_END_WRITE_ACTIVITY(vbeentry);
     448             : 
     449             :     /* Update app name to current GUC setting */
     450       16732 :     if (application_name)
     451       16732 :         pgstat_report_appname(application_name);
     452       16732 : }
     453             : 
     454             : /*
     455             :  * Clear out our entry in the PgBackendStatus array.
     456             :  */
     457             : static void
     458       16812 : pgstat_beshutdown_hook(int code, Datum arg)
     459             : {
     460       16812 :     volatile PgBackendStatus *beentry = MyBEEntry;
     461             : 
     462             :     /*
     463             :      * Clear my status entry, following the protocol of bumping st_changecount
     464             :      * before and after.  We use a volatile pointer here to ensure the
     465             :      * compiler doesn't try to get cute.
     466             :      */
     467       16812 :     PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
     468             : 
     469       16812 :     beentry->st_procpid = 0; /* mark invalid */
     470             : 
     471       16812 :     PGSTAT_END_WRITE_ACTIVITY(beentry);
     472             : 
     473             :     /* so that functions can check if backend_status.c is up via MyBEEntry */
     474       16812 :     MyBEEntry = NULL;
     475       16812 : }
     476             : 
     477             : /*
     478             :  * Discard any data collected in the current transaction.  Any subsequent
     479             :  * request will cause new snapshots to be read.
     480             :  *
     481             :  * This is also invoked during transaction commit or abort to discard the
     482             :  * no-longer-wanted snapshot.
     483             :  */
     484             : void
     485      692916 : pgstat_clear_backend_activity_snapshot(void)
     486             : {
     487             :     /* Release memory, if any was allocated */
     488      692916 :     if (backendStatusSnapContext)
     489             :     {
     490         918 :         MemoryContextDelete(backendStatusSnapContext);
     491         918 :         backendStatusSnapContext = NULL;
     492             :     }
     493             : 
     494             :     /* Reset variables */
     495      692916 :     localBackendStatusTable = NULL;
     496      692916 :     localNumBackends = 0;
     497      692916 : }
     498             : 
     499             : static void
     500         918 : pgstat_setup_backend_status_context(void)
     501             : {
     502         918 :     if (!backendStatusSnapContext)
     503         918 :         backendStatusSnapContext = AllocSetContextCreate(TopMemoryContext,
     504             :                                                          "Backend Status Snapshot",
     505             :                                                          ALLOCSET_SMALL_SIZES);
     506         918 : }
     507             : 
     508             : 
     509             : /* ----------
     510             :  * pgstat_report_activity() -
     511             :  *
     512             :  *  Called from tcop/postgres.c to report what the backend is actually doing
     513             :  *  (but note cmd_str can be NULL for certain cases).
     514             :  *
     515             :  * All updates of the status entry follow the protocol of bumping
     516             :  * st_changecount before and after.  We use a volatile pointer here to
     517             :  * ensure the compiler doesn't try to get cute.
     518             :  * ----------
     519             :  */
     520             : void
     521     1364850 : pgstat_report_activity(BackendState state, const char *cmd_str)
     522             : {
     523     1364850 :     volatile PgBackendStatus *beentry = MyBEEntry;
     524             :     TimestampTz start_timestamp;
     525             :     TimestampTz current_timestamp;
     526     1364850 :     int         len = 0;
     527             : 
     528             :     TRACE_POSTGRESQL_STATEMENT_STATUS(cmd_str);
     529             : 
     530     1364850 :     if (!beentry)
     531           0 :         return;
     532             : 
     533     1364850 :     if (!pgstat_track_activities)
     534             :     {
     535           0 :         if (beentry->st_state != STATE_DISABLED)
     536             :         {
     537           0 :             volatile PGPROC *proc = MyProc;
     538             : 
     539             :             /*
     540             :              * track_activities is disabled, but we last reported a
     541             :              * non-disabled state.  As our final update, change the state and
     542             :              * clear fields we will not be updating anymore.
     543             :              */
     544           0 :             PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
     545           0 :             beentry->st_state = STATE_DISABLED;
     546           0 :             beentry->st_state_start_timestamp = 0;
     547           0 :             beentry->st_activity_raw[0] = '\0';
     548           0 :             beentry->st_activity_start_timestamp = 0;
     549             :             /* st_xact_start_timestamp and wait_event_info are also disabled */
     550           0 :             beentry->st_xact_start_timestamp = 0;
     551           0 :             beentry->st_query_id = UINT64CONST(0);
     552           0 :             proc->wait_event_info = 0;
     553           0 :             PGSTAT_END_WRITE_ACTIVITY(beentry);
     554             :         }
     555           0 :         return;
     556             :     }
     557             : 
     558             :     /*
     559             :      * To minimize the time spent modifying the entry, and avoid risk of
     560             :      * errors inside the critical section, fetch all the needed data first.
     561             :      */
     562     1364850 :     start_timestamp = GetCurrentStatementStartTimestamp();
     563     1364850 :     if (cmd_str != NULL)
     564             :     {
     565             :         /*
     566             :          * Compute length of to-be-stored string unaware of multi-byte
     567             :          * characters. For speed reasons that'll get corrected on read, rather
     568             :          * than computed every write.
     569             :          */
     570      692996 :         len = Min(strlen(cmd_str), pgstat_track_activity_query_size - 1);
     571             :     }
     572     1364850 :     current_timestamp = GetCurrentTimestamp();
     573             : 
     574             :     /*
     575             :      * If the state has changed from "active" or "idle in transaction",
     576             :      * calculate the duration.
     577             :      */
     578     1364850 :     if ((beentry->st_state == STATE_RUNNING ||
     579      672472 :          beentry->st_state == STATE_FASTPATH ||
     580      671228 :          beentry->st_state == STATE_IDLEINTRANSACTION ||
     581      581418 :          beentry->st_state == STATE_IDLEINTRANSACTION_ABORTED) &&
     582      784552 :         state != beentry->st_state)
     583             :     {
     584             :         long        secs;
     585             :         int         usecs;
     586             : 
     587      748640 :         TimestampDifference(beentry->st_state_start_timestamp,
     588             :                             current_timestamp,
     589             :                             &secs, &usecs);
     590             : 
     591      748640 :         if (beentry->st_state == STATE_RUNNING ||
     592       92168 :             beentry->st_state == STATE_FASTPATH)
     593      657716 :             pgstat_count_conn_active_time((PgStat_Counter) secs * 1000000 + usecs);
     594             :         else
     595       90924 :             pgstat_count_conn_txn_idle_time((PgStat_Counter) secs * 1000000 + usecs);
     596             :     }
     597             : 
     598             :     /*
     599             :      * Now update the status entry
     600             :      */
     601     1364850 :     PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
     602             : 
     603     1364850 :     beentry->st_state = state;
     604     1364850 :     beentry->st_state_start_timestamp = current_timestamp;
     605             : 
     606             :     /*
     607             :      * If a new query is started, we reset the query identifier as it'll only
     608             :      * be known after parse analysis, to avoid reporting last query's
     609             :      * identifier.
     610             :      */
     611     1364850 :     if (state == STATE_RUNNING)
     612      694324 :         beentry->st_query_id = UINT64CONST(0);
     613             : 
     614     1364850 :     if (cmd_str != NULL)
     615             :     {
     616      692996 :         memcpy((char *) beentry->st_activity_raw, cmd_str, len);
     617      692996 :         beentry->st_activity_raw[len] = '\0';
     618      692996 :         beentry->st_activity_start_timestamp = start_timestamp;
     619             :     }
     620             : 
     621     1364850 :     PGSTAT_END_WRITE_ACTIVITY(beentry);
     622             : }
     623             : 
     624             : /* --------
     625             :  * pgstat_report_query_id() -
     626             :  *
     627             :  * Called to update top-level query identifier.
     628             :  * --------
     629             :  */
     630             : void
     631     1902344 : pgstat_report_query_id(uint64 query_id, bool force)
     632             : {
     633     1902344 :     volatile PgBackendStatus *beentry = MyBEEntry;
     634             : 
     635             :     /*
     636             :      * if track_activities is disabled, st_query_id should already have been
     637             :      * reset
     638             :      */
     639     1902344 :     if (!beentry || !pgstat_track_activities)
     640           0 :         return;
     641             : 
     642             :     /*
     643             :      * We only report the top-level query identifiers.  The stored query_id is
     644             :      * reset when a backend calls pgstat_report_activity(STATE_RUNNING), or
     645             :      * with an explicit call to this function using the force flag.  If the
     646             :      * saved query identifier is not zero it means that it's not a top-level
     647             :      * command, so ignore the one provided unless it's an explicit call to
     648             :      * reset the identifier.
     649             :      */
     650     1902344 :     if (beentry->st_query_id != 0 && !force)
     651         366 :         return;
     652             : 
     653             :     /*
     654             :      * Update my status entry, following the protocol of bumping
     655             :      * st_changecount before and after.  We use a volatile pointer here to
     656             :      * ensure the compiler doesn't try to get cute.
     657             :      */
     658     1901978 :     PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
     659     1901978 :     beentry->st_query_id = query_id;
     660     1901978 :     PGSTAT_END_WRITE_ACTIVITY(beentry);
     661             : }
     662             : 
     663             : 
     664             : /* ----------
     665             :  * pgstat_report_appname() -
     666             :  *
     667             :  *  Called to update our application name.
     668             :  * ----------
     669             :  */
     670             : void
     671       31780 : pgstat_report_appname(const char *appname)
     672             : {
     673       31780 :     volatile PgBackendStatus *beentry = MyBEEntry;
     674             :     int         len;
     675             : 
     676       31780 :     if (!beentry)
     677        2776 :         return;
     678             : 
     679             :     /* This should be unnecessary if GUC did its job, but be safe */
     680       29004 :     len = pg_mbcliplen(appname, strlen(appname), NAMEDATALEN - 1);
     681             : 
     682             :     /*
     683             :      * Update my status entry, following the protocol of bumping
     684             :      * st_changecount before and after.  We use a volatile pointer here to
     685             :      * ensure the compiler doesn't try to get cute.
     686             :      */
     687       29004 :     PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
     688             : 
     689       29004 :     memcpy((char *) beentry->st_appname, appname, len);
     690       29004 :     beentry->st_appname[len] = '\0';
     691             : 
     692       29004 :     PGSTAT_END_WRITE_ACTIVITY(beentry);
     693             : }
     694             : 
     695             : /*
     696             :  * Report current transaction start timestamp as the specified value.
     697             :  * Zero means there is no active transaction.
     698             :  */
     699             : void
     700     1383832 : pgstat_report_xact_timestamp(TimestampTz tstamp)
     701             : {
     702     1383832 :     volatile PgBackendStatus *beentry = MyBEEntry;
     703             : 
     704     1383832 :     if (!pgstat_track_activities || !beentry)
     705         916 :         return;
     706             : 
     707             :     /*
     708             :      * Update my status entry, following the protocol of bumping
     709             :      * st_changecount before and after.  We use a volatile pointer here to
     710             :      * ensure the compiler doesn't try to get cute.
     711             :      */
     712     1382916 :     PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
     713             : 
     714     1382916 :     beentry->st_xact_start_timestamp = tstamp;
     715             : 
     716     1382916 :     PGSTAT_END_WRITE_ACTIVITY(beentry);
     717             : }
     718             : 
     719             : /* ----------
     720             :  * pgstat_read_current_status() -
     721             :  *
     722             :  *  Copy the current contents of the PgBackendStatus array to local memory,
     723             :  *  if not already done in this transaction.
     724             :  * ----------
     725             :  */
     726             : static void
     727        7876 : pgstat_read_current_status(void)
     728             : {
     729             :     volatile PgBackendStatus *beentry;
     730             :     LocalPgBackendStatus *localtable;
     731             :     LocalPgBackendStatus *localentry;
     732             :     char       *localappname,
     733             :                *localclienthostname,
     734             :                *localactivity;
     735             : #ifdef USE_SSL
     736             :     PgBackendSSLStatus *localsslstatus;
     737             : #endif
     738             : #ifdef ENABLE_GSS
     739             :     PgBackendGSSStatus *localgssstatus;
     740             : #endif
     741             :     int         i;
     742             : 
     743        7876 :     if (localBackendStatusTable)
     744        6958 :         return;                 /* already done */
     745             : 
     746         918 :     pgstat_setup_backend_status_context();
     747             : 
     748             :     /*
     749             :      * Allocate storage for local copy of state data.  We can presume that
     750             :      * none of these requests overflow size_t, because we already calculated
     751             :      * the same values using mul_size during shmem setup.  However, with
     752             :      * probably-silly values of pgstat_track_activity_query_size and
     753             :      * max_connections, the localactivity buffer could exceed 1GB, so use
     754             :      * "huge" allocation for that one.
     755             :      */
     756             :     localtable = (LocalPgBackendStatus *)
     757         918 :         MemoryContextAlloc(backendStatusSnapContext,
     758         918 :                            sizeof(LocalPgBackendStatus) * NumBackendStatSlots);
     759             :     localappname = (char *)
     760         918 :         MemoryContextAlloc(backendStatusSnapContext,
     761         918 :                            NAMEDATALEN * NumBackendStatSlots);
     762             :     localclienthostname = (char *)
     763         918 :         MemoryContextAlloc(backendStatusSnapContext,
     764         918 :                            NAMEDATALEN * NumBackendStatSlots);
     765             :     localactivity = (char *)
     766         918 :         MemoryContextAllocHuge(backendStatusSnapContext,
     767         918 :                                pgstat_track_activity_query_size * NumBackendStatSlots);
     768             : #ifdef USE_SSL
     769             :     localsslstatus = (PgBackendSSLStatus *)
     770         918 :         MemoryContextAlloc(backendStatusSnapContext,
     771         918 :                            sizeof(PgBackendSSLStatus) * NumBackendStatSlots);
     772             : #endif
     773             : #ifdef ENABLE_GSS
     774             :     localgssstatus = (PgBackendGSSStatus *)
     775             :         MemoryContextAlloc(backendStatusSnapContext,
     776             :                            sizeof(PgBackendGSSStatus) * NumBackendStatSlots);
     777             : #endif
     778             : 
     779         918 :     localNumBackends = 0;
     780             : 
     781         918 :     beentry = BackendStatusArray;
     782         918 :     localentry = localtable;
     783       38746 :     for (i = 1; i <= NumBackendStatSlots; i++)
     784             :     {
     785             :         /*
     786             :          * Follow the protocol of retrying if st_changecount changes while we
     787             :          * copy the entry, or if it's odd.  (The check for odd is needed to
     788             :          * cover the case where we are able to completely copy the entry while
     789             :          * the source backend is between increment steps.)  We use a volatile
     790             :          * pointer here to ensure the compiler doesn't try to get cute.
     791             :          */
     792             :         for (;;)
     793           0 :         {
     794             :             int         before_changecount;
     795             :             int         after_changecount;
     796             : 
     797       37828 :             pgstat_begin_read_activity(beentry, before_changecount);
     798             : 
     799       37828 :             localentry->backendStatus.st_procpid = beentry->st_procpid;
     800             :             /* Skip all the data-copying work if entry is not in use */
     801       37828 :             if (localentry->backendStatus.st_procpid > 0)
     802             :             {
     803        6958 :                 memcpy(&localentry->backendStatus, unvolatize(PgBackendStatus *, beentry), sizeof(PgBackendStatus));
     804             : 
     805             :                 /*
     806             :                  * For each PgBackendStatus field that is a pointer, copy the
     807             :                  * pointed-to data, then adjust the local copy of the pointer
     808             :                  * field to point at the local copy of the data.
     809             :                  *
     810             :                  * strcpy is safe even if the string is modified concurrently,
     811             :                  * because there's always a \0 at the end of the buffer.
     812             :                  */
     813        6958 :                 strcpy(localappname, (char *) beentry->st_appname);
     814        6958 :                 localentry->backendStatus.st_appname = localappname;
     815        6958 :                 strcpy(localclienthostname, (char *) beentry->st_clienthostname);
     816        6958 :                 localentry->backendStatus.st_clienthostname = localclienthostname;
     817        6958 :                 strcpy(localactivity, (char *) beentry->st_activity_raw);
     818        6958 :                 localentry->backendStatus.st_activity_raw = localactivity;
     819             : #ifdef USE_SSL
     820        6958 :                 if (beentry->st_ssl)
     821             :                 {
     822           4 :                     memcpy(localsslstatus, beentry->st_sslstatus, sizeof(PgBackendSSLStatus));
     823           4 :                     localentry->backendStatus.st_sslstatus = localsslstatus;
     824             :                 }
     825             : #endif
     826             : #ifdef ENABLE_GSS
     827             :                 if (beentry->st_gss)
     828             :                 {
     829             :                     memcpy(localgssstatus, beentry->st_gssstatus, sizeof(PgBackendGSSStatus));
     830             :                     localentry->backendStatus.st_gssstatus = localgssstatus;
     831             :                 }
     832             : #endif
     833             :             }
     834             : 
     835       37828 :             pgstat_end_read_activity(beentry, after_changecount);
     836             : 
     837       37828 :             if (pgstat_read_activity_complete(before_changecount,
     838             :                                               after_changecount))
     839       37828 :                 break;
     840             : 
     841             :             /* Make sure we can break out of loop if stuck... */
     842           0 :             CHECK_FOR_INTERRUPTS();
     843             :         }
     844             : 
     845       37828 :         beentry++;
     846             :         /* Only valid entries get included into the local array */
     847       37828 :         if (localentry->backendStatus.st_procpid > 0)
     848             :         {
     849        6958 :             BackendIdGetTransactionIds(i,
     850             :                                        &localentry->backend_xid,
     851             :                                        &localentry->backend_xmin);
     852             : 
     853        6958 :             localentry++;
     854        6958 :             localappname += NAMEDATALEN;
     855        6958 :             localclienthostname += NAMEDATALEN;
     856        6958 :             localactivity += pgstat_track_activity_query_size;
     857             : #ifdef USE_SSL
     858        6958 :             localsslstatus++;
     859             : #endif
     860             : #ifdef ENABLE_GSS
     861             :             localgssstatus++;
     862             : #endif
     863        6958 :             localNumBackends++;
     864             :         }
     865             :     }
     866             : 
     867             :     /* Set the pointer only after completion of a valid table */
     868         918 :     localBackendStatusTable = localtable;
     869             : }
     870             : 
     871             : 
     872             : /* ----------
     873             :  * pgstat_get_backend_current_activity() -
     874             :  *
     875             :  *  Return a string representing the current activity of the backend with
     876             :  *  the specified PID.  This looks directly at the BackendStatusArray,
     877             :  *  and so will provide current information regardless of the age of our
     878             :  *  transaction's snapshot of the status array.
     879             :  *
     880             :  *  It is the caller's responsibility to invoke this only for backends whose
     881             :  *  state is expected to remain stable while the result is in use.  The
     882             :  *  only current use is in deadlock reporting, where we can expect that
     883             :  *  the target backend is blocked on a lock.  (There are corner cases
     884             :  *  where the target's wait could get aborted while we are looking at it,
     885             :  *  but the very worst consequence is to return a pointer to a string
     886             :  *  that's been changed, so we won't worry too much.)
     887             :  *
     888             :  *  Note: return strings for special cases match pg_stat_get_backend_activity.
     889             :  * ----------
     890             :  */
     891             : const char *
     892          20 : pgstat_get_backend_current_activity(int pid, bool checkUser)
     893             : {
     894             :     PgBackendStatus *beentry;
     895             :     int         i;
     896             : 
     897          20 :     beentry = BackendStatusArray;
     898         138 :     for (i = 1; i <= MaxBackends; i++)
     899             :     {
     900             :         /*
     901             :          * Although we expect the target backend's entry to be stable, that
     902             :          * doesn't imply that anyone else's is.  To avoid identifying the
     903             :          * wrong backend, while we check for a match to the desired PID we
     904             :          * must follow the protocol of retrying if st_changecount changes
     905             :          * while we examine the entry, or if it's odd.  (This might be
     906             :          * unnecessary, since fetching or storing an int is almost certainly
     907             :          * atomic, but let's play it safe.)  We use a volatile pointer here to
     908             :          * ensure the compiler doesn't try to get cute.
     909             :          */
     910         138 :         volatile PgBackendStatus *vbeentry = beentry;
     911             :         bool        found;
     912             : 
     913             :         for (;;)
     914           0 :         {
     915             :             int         before_changecount;
     916             :             int         after_changecount;
     917             : 
     918         138 :             pgstat_begin_read_activity(vbeentry, before_changecount);
     919             : 
     920         138 :             found = (vbeentry->st_procpid == pid);
     921             : 
     922         138 :             pgstat_end_read_activity(vbeentry, after_changecount);
     923             : 
     924         138 :             if (pgstat_read_activity_complete(before_changecount,
     925             :                                               after_changecount))
     926         138 :                 break;
     927             : 
     928             :             /* Make sure we can break out of loop if stuck... */
     929           0 :             CHECK_FOR_INTERRUPTS();
     930             :         }
     931             : 
     932         138 :         if (found)
     933             :         {
     934             :             /* Now it is safe to use the non-volatile pointer */
     935          20 :             if (checkUser && !superuser() && beentry->st_userid != GetUserId())
     936           0 :                 return "<insufficient privilege>";
     937          20 :             else if (*(beentry->st_activity_raw) == '\0')
     938           0 :                 return "<command string not enabled>";
     939             :             else
     940             :             {
     941             :                 /* this'll leak a bit of memory, but that seems acceptable */
     942          20 :                 return pgstat_clip_activity(beentry->st_activity_raw);
     943             :             }
     944             :         }
     945             : 
     946         118 :         beentry++;
     947             :     }
     948             : 
     949             :     /* If we get here, caller is in error ... */
     950           0 :     return "<backend information not available>";
     951             : }
     952             : 
     953             : /* ----------
     954             :  * pgstat_get_crashed_backend_activity() -
     955             :  *
     956             :  *  Return a string representing the current activity of the backend with
     957             :  *  the specified PID.  Like the function above, but reads shared memory with
     958             :  *  the expectation that it may be corrupt.  On success, copy the string
     959             :  *  into the "buffer" argument and return that pointer.  On failure,
     960             :  *  return NULL.
     961             :  *
     962             :  *  This function is only intended to be used by the postmaster to report the
     963             :  *  query that crashed a backend.  In particular, no attempt is made to
     964             :  *  follow the correct concurrency protocol when accessing the
     965             :  *  BackendStatusArray.  But that's OK, in the worst case we'll return a
     966             :  *  corrupted message.  We also must take care not to trip on ereport(ERROR).
     967             :  * ----------
     968             :  */
     969             : const char *
     970        1030 : pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
     971             : {
     972             :     volatile PgBackendStatus *beentry;
     973             :     int         i;
     974             : 
     975        1030 :     beentry = BackendStatusArray;
     976             : 
     977             :     /*
     978             :      * We probably shouldn't get here before shared memory has been set up,
     979             :      * but be safe.
     980             :      */
     981        1030 :     if (beentry == NULL || BackendActivityBuffer == NULL)
     982           0 :         return NULL;
     983             : 
     984       77842 :     for (i = 1; i <= MaxBackends; i++)
     985             :     {
     986       76898 :         if (beentry->st_procpid == pid)
     987             :         {
     988             :             /* Read pointer just once, so it can't change after validation */
     989          86 :             const char *activity = beentry->st_activity_raw;
     990             :             const char *activity_last;
     991             : 
     992             :             /*
     993             :              * We mustn't access activity string before we verify that it
     994             :              * falls within the BackendActivityBuffer. To make sure that the
     995             :              * entire string including its ending is contained within the
     996             :              * buffer, subtract one activity length from the buffer size.
     997             :              */
     998          86 :             activity_last = BackendActivityBuffer + BackendActivityBufferSize
     999          86 :                 - pgstat_track_activity_query_size;
    1000             : 
    1001          86 :             if (activity < BackendActivityBuffer ||
    1002             :                 activity > activity_last)
    1003           0 :                 return NULL;
    1004             : 
    1005             :             /* If no string available, no point in a report */
    1006          86 :             if (activity[0] == '\0')
    1007           0 :                 return NULL;
    1008             : 
    1009             :             /*
    1010             :              * Copy only ASCII-safe characters so we don't run into encoding
    1011             :              * problems when reporting the message; and be sure not to run off
    1012             :              * the end of memory.  As only ASCII characters are reported, it
    1013             :              * doesn't seem necessary to perform multibyte aware clipping.
    1014             :              */
    1015          86 :             ascii_safe_strlcpy(buffer, activity,
    1016          86 :                                Min(buflen, pgstat_track_activity_query_size));
    1017             : 
    1018          86 :             return buffer;
    1019             :         }
    1020             : 
    1021       76812 :         beentry++;
    1022             :     }
    1023             : 
    1024             :     /* PID not found */
    1025         944 :     return NULL;
    1026             : }
    1027             : 
    1028             : /* ----------
    1029             :  * pgstat_get_my_query_id() -
    1030             :  *
    1031             :  * Return current backend's query identifier.
    1032             :  */
    1033             : uint64
    1034         442 : pgstat_get_my_query_id(void)
    1035             : {
    1036         442 :     if (!MyBEEntry)
    1037          14 :         return 0;
    1038             : 
    1039             :     /*
    1040             :      * There's no need for a lock around pgstat_begin_read_activity /
    1041             :      * pgstat_end_read_activity here as it's only called from
    1042             :      * pg_stat_get_activity which is already protected, or from the same
    1043             :      * backend which means that there won't be concurrent writes.
    1044             :      */
    1045         428 :     return MyBEEntry->st_query_id;
    1046             : }
    1047             : 
    1048             : 
    1049             : /* ----------
    1050             :  * pgstat_fetch_stat_beentry() -
    1051             :  *
    1052             :  *  Support function for the SQL-callable pgstat* functions. Returns
    1053             :  *  our local copy of the current-activity entry for one backend.
    1054             :  *
    1055             :  *  NB: caller is responsible for a check if the user is permitted to see
    1056             :  *  this info (especially the querystring).
    1057             :  * ----------
    1058             :  */
    1059             : PgBackendStatus *
    1060           0 : pgstat_fetch_stat_beentry(int beid)
    1061             : {
    1062           0 :     pgstat_read_current_status();
    1063             : 
    1064           0 :     if (beid < 1 || beid > localNumBackends)
    1065           0 :         return NULL;
    1066             : 
    1067           0 :     return &localBackendStatusTable[beid - 1].backendStatus;
    1068             : }
    1069             : 
    1070             : 
    1071             : /* ----------
    1072             :  * pgstat_fetch_stat_local_beentry() -
    1073             :  *
    1074             :  *  Like pgstat_fetch_stat_beentry() but with locally computed additions (like
    1075             :  *  xid and xmin values of the backend)
    1076             :  *
    1077             :  *  NB: caller is responsible for a check if the user is permitted to see
    1078             :  *  this info (especially the querystring).
    1079             :  * ----------
    1080             :  */
    1081             : LocalPgBackendStatus *
    1082        6958 : pgstat_fetch_stat_local_beentry(int beid)
    1083             : {
    1084        6958 :     pgstat_read_current_status();
    1085             : 
    1086        6958 :     if (beid < 1 || beid > localNumBackends)
    1087           0 :         return NULL;
    1088             : 
    1089        6958 :     return &localBackendStatusTable[beid - 1];
    1090             : }
    1091             : 
    1092             : 
    1093             : /* ----------
    1094             :  * pgstat_fetch_stat_numbackends() -
    1095             :  *
    1096             :  *  Support function for the SQL-callable pgstat* functions. Returns
    1097             :  *  the maximum current backend id.
    1098             :  * ----------
    1099             :  */
    1100             : int
    1101         918 : pgstat_fetch_stat_numbackends(void)
    1102             : {
    1103         918 :     pgstat_read_current_status();
    1104             : 
    1105         918 :     return localNumBackends;
    1106             : }
    1107             : 
    1108             : /*
    1109             :  * Convert a potentially unsafely truncated activity string (see
    1110             :  * PgBackendStatus.st_activity_raw's documentation) into a correctly truncated
    1111             :  * one.
    1112             :  *
    1113             :  * The returned string is allocated in the caller's memory context and may be
    1114             :  * freed.
    1115             :  */
    1116             : char *
    1117        6894 : pgstat_clip_activity(const char *raw_activity)
    1118             : {
    1119             :     char       *activity;
    1120             :     int         rawlen;
    1121             :     int         cliplen;
    1122             : 
    1123             :     /*
    1124             :      * Some callers, like pgstat_get_backend_current_activity(), do not
    1125             :      * guarantee that the buffer isn't concurrently modified. We try to take
    1126             :      * care that the buffer is always terminated by a NUL byte regardless, but
    1127             :      * let's still be paranoid about the string's length. In those cases the
    1128             :      * underlying buffer is guaranteed to be pgstat_track_activity_query_size
    1129             :      * large.
    1130             :      */
    1131        6894 :     activity = pnstrdup(raw_activity, pgstat_track_activity_query_size - 1);
    1132             : 
    1133             :     /* now double-guaranteed to be NUL terminated */
    1134        6894 :     rawlen = strlen(activity);
    1135             : 
    1136             :     /*
    1137             :      * All supported server-encodings make it possible to determine the length
    1138             :      * of a multi-byte character from its first byte (this is not the case for
    1139             :      * client encodings, see GB18030). As st_activity is always stored using
    1140             :      * server encoding, this allows us to perform multi-byte aware truncation,
    1141             :      * even if the string earlier was truncated in the middle of a multi-byte
    1142             :      * character.
    1143             :      */
    1144        6894 :     cliplen = pg_mbcliplen(activity, rawlen,
    1145             :                            pgstat_track_activity_query_size - 1);
    1146             : 
    1147        6894 :     activity[cliplen] = '\0';
    1148             : 
    1149        6894 :     return activity;
    1150             : }

Generated by: LCOV version 1.13