LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pgstatfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 293 933 31.4 %
Date: 2020-11-27 11:06:40 Functions: 18 98 18.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pgstatfuncs.c
       4             :  *    Functions for accessing the statistics collector data
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/utils/adt/pgstatfuncs.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/htup_details.h"
      18             : #include "access/xlog.h"
      19             : #include "catalog/pg_authid.h"
      20             : #include "catalog/pg_type.h"
      21             : #include "common/ip.h"
      22             : #include "funcapi.h"
      23             : #include "miscadmin.h"
      24             : #include "pgstat.h"
      25             : #include "postmaster/bgworker_internals.h"
      26             : #include "postmaster/postmaster.h"
      27             : #include "storage/proc.h"
      28             : #include "storage/procarray.h"
      29             : #include "utils/acl.h"
      30             : #include "utils/builtins.h"
      31             : #include "utils/inet.h"
      32             : #include "utils/timestamp.h"
      33             : 
      34             : #define UINT32_ACCESS_ONCE(var)      ((uint32)(*((volatile uint32 *)&(var))))
      35             : 
      36             : #define HAS_PGSTAT_PERMISSIONS(role)     (is_member_of_role(GetUserId(), DEFAULT_ROLE_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
      37             : 
      38             : /* Global bgwriter statistics, from bgwriter.c */
      39             : extern PgStat_MsgBgWriter bgwriterStats;
      40             : 
      41             : Datum
      42         112 : pg_stat_get_numscans(PG_FUNCTION_ARGS)
      43             : {
      44         112 :     Oid         relid = PG_GETARG_OID(0);
      45             :     int64       result;
      46             :     PgStat_StatTabEntry *tabentry;
      47             : 
      48         112 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
      49           0 :         result = 0;
      50             :     else
      51         112 :         result = (int64) (tabentry->numscans);
      52             : 
      53         112 :     PG_RETURN_INT64(result);
      54             : }
      55             : 
      56             : 
      57             : Datum
      58           8 : pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
      59             : {
      60           8 :     Oid         relid = PG_GETARG_OID(0);
      61             :     int64       result;
      62             :     PgStat_StatTabEntry *tabentry;
      63             : 
      64           8 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
      65           0 :         result = 0;
      66             :     else
      67           8 :         result = (int64) (tabentry->tuples_returned);
      68             : 
      69           8 :     PG_RETURN_INT64(result);
      70             : }
      71             : 
      72             : 
      73             : Datum
      74          32 : pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
      75             : {
      76          32 :     Oid         relid = PG_GETARG_OID(0);
      77             :     int64       result;
      78             :     PgStat_StatTabEntry *tabentry;
      79             : 
      80          32 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
      81           0 :         result = 0;
      82             :     else
      83          32 :         result = (int64) (tabentry->tuples_fetched);
      84             : 
      85          32 :     PG_RETURN_INT64(result);
      86             : }
      87             : 
      88             : 
      89             : Datum
      90          40 : pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
      91             : {
      92          40 :     Oid         relid = PG_GETARG_OID(0);
      93             :     int64       result;
      94             :     PgStat_StatTabEntry *tabentry;
      95             : 
      96          40 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
      97          16 :         result = 0;
      98             :     else
      99          24 :         result = (int64) (tabentry->tuples_inserted);
     100             : 
     101          40 :     PG_RETURN_INT64(result);
     102             : }
     103             : 
     104             : 
     105             : Datum
     106          20 : pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
     107             : {
     108          20 :     Oid         relid = PG_GETARG_OID(0);
     109             :     int64       result;
     110             :     PgStat_StatTabEntry *tabentry;
     111             : 
     112          20 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     113           0 :         result = 0;
     114             :     else
     115          20 :         result = (int64) (tabentry->tuples_updated);
     116             : 
     117          20 :     PG_RETURN_INT64(result);
     118             : }
     119             : 
     120             : 
     121             : Datum
     122          20 : pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
     123             : {
     124          20 :     Oid         relid = PG_GETARG_OID(0);
     125             :     int64       result;
     126             :     PgStat_StatTabEntry *tabentry;
     127             : 
     128          20 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     129           0 :         result = 0;
     130             :     else
     131          20 :         result = (int64) (tabentry->tuples_deleted);
     132             : 
     133          20 :     PG_RETURN_INT64(result);
     134             : }
     135             : 
     136             : 
     137             : Datum
     138           0 : pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
     139             : {
     140           0 :     Oid         relid = PG_GETARG_OID(0);
     141             :     int64       result;
     142             :     PgStat_StatTabEntry *tabentry;
     143             : 
     144           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     145           0 :         result = 0;
     146             :     else
     147           0 :         result = (int64) (tabentry->tuples_hot_updated);
     148             : 
     149           0 :     PG_RETURN_INT64(result);
     150             : }
     151             : 
     152             : 
     153             : Datum
     154          20 : pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
     155             : {
     156          20 :     Oid         relid = PG_GETARG_OID(0);
     157             :     int64       result;
     158             :     PgStat_StatTabEntry *tabentry;
     159             : 
     160          20 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     161           0 :         result = 0;
     162             :     else
     163          20 :         result = (int64) (tabentry->n_live_tuples);
     164             : 
     165          20 :     PG_RETURN_INT64(result);
     166             : }
     167             : 
     168             : 
     169             : Datum
     170          20 : pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
     171             : {
     172          20 :     Oid         relid = PG_GETARG_OID(0);
     173             :     int64       result;
     174             :     PgStat_StatTabEntry *tabentry;
     175             : 
     176          20 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     177           0 :         result = 0;
     178             :     else
     179          20 :         result = (int64) (tabentry->n_dead_tuples);
     180             : 
     181          20 :     PG_RETURN_INT64(result);
     182             : }
     183             : 
     184             : 
     185             : Datum
     186           0 : pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS)
     187             : {
     188           0 :     Oid         relid = PG_GETARG_OID(0);
     189             :     int64       result;
     190             :     PgStat_StatTabEntry *tabentry;
     191             : 
     192           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     193           0 :         result = 0;
     194             :     else
     195           0 :         result = (int64) (tabentry->changes_since_analyze);
     196             : 
     197           0 :     PG_RETURN_INT64(result);
     198             : }
     199             : 
     200             : 
     201             : Datum
     202           0 : pg_stat_get_ins_since_vacuum(PG_FUNCTION_ARGS)
     203             : {
     204           0 :     Oid         relid = PG_GETARG_OID(0);
     205             :     int64       result;
     206             :     PgStat_StatTabEntry *tabentry;
     207             : 
     208           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     209           0 :         result = 0;
     210             :     else
     211           0 :         result = (int64) (tabentry->inserts_since_vacuum);
     212             : 
     213           0 :     PG_RETURN_INT64(result);
     214             : }
     215             : 
     216             : 
     217             : Datum
     218          32 : pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
     219             : {
     220          32 :     Oid         relid = PG_GETARG_OID(0);
     221             :     int64       result;
     222             :     PgStat_StatTabEntry *tabentry;
     223             : 
     224          32 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     225           0 :         result = 0;
     226             :     else
     227          32 :         result = (int64) (tabentry->blocks_fetched);
     228             : 
     229          32 :     PG_RETURN_INT64(result);
     230             : }
     231             : 
     232             : 
     233             : Datum
     234          64 : pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
     235             : {
     236          64 :     Oid         relid = PG_GETARG_OID(0);
     237             :     int64       result;
     238             :     PgStat_StatTabEntry *tabentry;
     239             : 
     240          64 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     241           0 :         result = 0;
     242             :     else
     243          64 :         result = (int64) (tabentry->blocks_hit);
     244             : 
     245          64 :     PG_RETURN_INT64(result);
     246             : }
     247             : 
     248             : Datum
     249           0 : pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
     250             : {
     251           0 :     Oid         relid = PG_GETARG_OID(0);
     252             :     TimestampTz result;
     253             :     PgStat_StatTabEntry *tabentry;
     254             : 
     255           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     256           0 :         result = 0;
     257             :     else
     258           0 :         result = tabentry->vacuum_timestamp;
     259             : 
     260           0 :     if (result == 0)
     261           0 :         PG_RETURN_NULL();
     262             :     else
     263           0 :         PG_RETURN_TIMESTAMPTZ(result);
     264             : }
     265             : 
     266             : Datum
     267           0 : pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
     268             : {
     269           0 :     Oid         relid = PG_GETARG_OID(0);
     270             :     TimestampTz result;
     271             :     PgStat_StatTabEntry *tabentry;
     272             : 
     273           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     274           0 :         result = 0;
     275             :     else
     276           0 :         result = tabentry->autovac_vacuum_timestamp;
     277             : 
     278           0 :     if (result == 0)
     279           0 :         PG_RETURN_NULL();
     280             :     else
     281           0 :         PG_RETURN_TIMESTAMPTZ(result);
     282             : }
     283             : 
     284             : Datum
     285           0 : pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
     286             : {
     287           0 :     Oid         relid = PG_GETARG_OID(0);
     288             :     TimestampTz result;
     289             :     PgStat_StatTabEntry *tabentry;
     290             : 
     291           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     292           0 :         result = 0;
     293             :     else
     294           0 :         result = tabentry->analyze_timestamp;
     295             : 
     296           0 :     if (result == 0)
     297           0 :         PG_RETURN_NULL();
     298             :     else
     299           0 :         PG_RETURN_TIMESTAMPTZ(result);
     300             : }
     301             : 
     302             : Datum
     303           0 : pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
     304             : {
     305           0 :     Oid         relid = PG_GETARG_OID(0);
     306             :     TimestampTz result;
     307             :     PgStat_StatTabEntry *tabentry;
     308             : 
     309           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     310           0 :         result = 0;
     311             :     else
     312           0 :         result = tabentry->autovac_analyze_timestamp;
     313             : 
     314           0 :     if (result == 0)
     315           0 :         PG_RETURN_NULL();
     316             :     else
     317           0 :         PG_RETURN_TIMESTAMPTZ(result);
     318             : }
     319             : 
     320             : Datum
     321           0 : pg_stat_get_vacuum_count(PG_FUNCTION_ARGS)
     322             : {
     323           0 :     Oid         relid = PG_GETARG_OID(0);
     324             :     int64       result;
     325             :     PgStat_StatTabEntry *tabentry;
     326             : 
     327           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     328           0 :         result = 0;
     329             :     else
     330           0 :         result = (int64) (tabentry->vacuum_count);
     331             : 
     332           0 :     PG_RETURN_INT64(result);
     333             : }
     334             : 
     335             : Datum
     336           0 : pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS)
     337             : {
     338           0 :     Oid         relid = PG_GETARG_OID(0);
     339             :     int64       result;
     340             :     PgStat_StatTabEntry *tabentry;
     341             : 
     342           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     343           0 :         result = 0;
     344             :     else
     345           0 :         result = (int64) (tabentry->autovac_vacuum_count);
     346             : 
     347           0 :     PG_RETURN_INT64(result);
     348             : }
     349             : 
     350             : Datum
     351           0 : pg_stat_get_analyze_count(PG_FUNCTION_ARGS)
     352             : {
     353           0 :     Oid         relid = PG_GETARG_OID(0);
     354             :     int64       result;
     355             :     PgStat_StatTabEntry *tabentry;
     356             : 
     357           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     358           0 :         result = 0;
     359             :     else
     360           0 :         result = (int64) (tabentry->analyze_count);
     361             : 
     362           0 :     PG_RETURN_INT64(result);
     363             : }
     364             : 
     365             : Datum
     366           0 : pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS)
     367             : {
     368           0 :     Oid         relid = PG_GETARG_OID(0);
     369             :     int64       result;
     370             :     PgStat_StatTabEntry *tabentry;
     371             : 
     372           0 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
     373           0 :         result = 0;
     374             :     else
     375           0 :         result = (int64) (tabentry->autovac_analyze_count);
     376             : 
     377           0 :     PG_RETURN_INT64(result);
     378             : }
     379             : 
     380             : Datum
     381           0 : pg_stat_get_function_calls(PG_FUNCTION_ARGS)
     382             : {
     383           0 :     Oid         funcid = PG_GETARG_OID(0);
     384             :     PgStat_StatFuncEntry *funcentry;
     385             : 
     386           0 :     if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
     387           0 :         PG_RETURN_NULL();
     388           0 :     PG_RETURN_INT64(funcentry->f_numcalls);
     389             : }
     390             : 
     391             : Datum
     392           0 : pg_stat_get_function_total_time(PG_FUNCTION_ARGS)
     393             : {
     394           0 :     Oid         funcid = PG_GETARG_OID(0);
     395             :     PgStat_StatFuncEntry *funcentry;
     396             : 
     397           0 :     if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
     398           0 :         PG_RETURN_NULL();
     399             :     /* convert counter from microsec to millisec for display */
     400           0 :     PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
     401             : }
     402             : 
     403             : Datum
     404           0 : pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
     405             : {
     406           0 :     Oid         funcid = PG_GETARG_OID(0);
     407             :     PgStat_StatFuncEntry *funcentry;
     408             : 
     409           0 :     if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
     410           0 :         PG_RETURN_NULL();
     411             :     /* convert counter from microsec to millisec for display */
     412           0 :     PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
     413             : }
     414             : 
     415             : Datum
     416           0 : pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
     417             : {
     418             :     FuncCallContext *funcctx;
     419             :     int        *fctx;
     420             :     int32       result;
     421             : 
     422             :     /* stuff done only on the first call of the function */
     423           0 :     if (SRF_IS_FIRSTCALL())
     424             :     {
     425             :         /* create a function context for cross-call persistence */
     426           0 :         funcctx = SRF_FIRSTCALL_INIT();
     427             : 
     428           0 :         fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
     429             :                                   2 * sizeof(int));
     430           0 :         funcctx->user_fctx = fctx;
     431             : 
     432           0 :         fctx[0] = 0;
     433           0 :         fctx[1] = pgstat_fetch_stat_numbackends();
     434             :     }
     435             : 
     436             :     /* stuff done on every call of the function */
     437           0 :     funcctx = SRF_PERCALL_SETUP();
     438           0 :     fctx = funcctx->user_fctx;
     439             : 
     440           0 :     fctx[0] += 1;
     441           0 :     result = fctx[0];
     442             : 
     443           0 :     if (result <= fctx[1])
     444             :     {
     445             :         /* do when there is more left to send */
     446           0 :         SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
     447             :     }
     448             :     else
     449             :     {
     450             :         /* do when there is no more left */
     451           0 :         SRF_RETURN_DONE(funcctx);
     452             :     }
     453             : }
     454             : 
     455             : /*
     456             :  * Returns command progress information for the named command.
     457             :  */
     458             : Datum
     459           0 : pg_stat_get_progress_info(PG_FUNCTION_ARGS)
     460             : {
     461             : #define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3
     462           0 :     int         num_backends = pgstat_fetch_stat_numbackends();
     463             :     int         curr_backend;
     464           0 :     char       *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
     465             :     ProgressCommandType cmdtype;
     466             :     TupleDesc   tupdesc;
     467             :     Tuplestorestate *tupstore;
     468           0 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     469             :     MemoryContext per_query_ctx;
     470             :     MemoryContext oldcontext;
     471             : 
     472             :     /* check to see if caller supports us returning a tuplestore */
     473           0 :     if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
     474           0 :         ereport(ERROR,
     475             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     476             :                  errmsg("set-valued function called in context that cannot accept a set")));
     477           0 :     if (!(rsinfo->allowedModes & SFRM_Materialize))
     478           0 :         ereport(ERROR,
     479             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     480             :                  errmsg("materialize mode required, but it is not allowed in this context")));
     481             : 
     482             :     /* Build a tuple descriptor for our result type */
     483           0 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     484           0 :         elog(ERROR, "return type must be a row type");
     485             : 
     486             :     /* Translate command name into command type code. */
     487           0 :     if (pg_strcasecmp(cmd, "VACUUM") == 0)
     488           0 :         cmdtype = PROGRESS_COMMAND_VACUUM;
     489           0 :     else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
     490           0 :         cmdtype = PROGRESS_COMMAND_ANALYZE;
     491           0 :     else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
     492           0 :         cmdtype = PROGRESS_COMMAND_CLUSTER;
     493           0 :     else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
     494           0 :         cmdtype = PROGRESS_COMMAND_CREATE_INDEX;
     495           0 :     else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
     496           0 :         cmdtype = PROGRESS_COMMAND_BASEBACKUP;
     497             :     else
     498           0 :         ereport(ERROR,
     499             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     500             :                  errmsg("invalid command name: \"%s\"", cmd)));
     501             : 
     502           0 :     per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
     503           0 :     oldcontext = MemoryContextSwitchTo(per_query_ctx);
     504             : 
     505           0 :     tupstore = tuplestore_begin_heap(true, false, work_mem);
     506           0 :     rsinfo->returnMode = SFRM_Materialize;
     507           0 :     rsinfo->setResult = tupstore;
     508           0 :     rsinfo->setDesc = tupdesc;
     509           0 :     MemoryContextSwitchTo(oldcontext);
     510             : 
     511             :     /* 1-based index */
     512           0 :     for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
     513             :     {
     514             :         LocalPgBackendStatus *local_beentry;
     515             :         PgBackendStatus *beentry;
     516             :         Datum       values[PG_STAT_GET_PROGRESS_COLS];
     517             :         bool        nulls[PG_STAT_GET_PROGRESS_COLS];
     518             :         int         i;
     519             : 
     520           0 :         MemSet(values, 0, sizeof(values));
     521           0 :         MemSet(nulls, 0, sizeof(nulls));
     522             : 
     523           0 :         local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
     524             : 
     525           0 :         if (!local_beentry)
     526           0 :             continue;
     527             : 
     528           0 :         beentry = &local_beentry->backendStatus;
     529             : 
     530             :         /*
     531             :          * Report values for only those backends which are running the given
     532             :          * command.
     533             :          */
     534           0 :         if (!beentry || beentry->st_progress_command != cmdtype)
     535           0 :             continue;
     536             : 
     537             :         /* Value available to all callers */
     538           0 :         values[0] = Int32GetDatum(beentry->st_procpid);
     539           0 :         values[1] = ObjectIdGetDatum(beentry->st_databaseid);
     540             : 
     541             :         /* show rest of the values including relid only to role members */
     542           0 :         if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
     543             :         {
     544           0 :             values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
     545           0 :             for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
     546           0 :                 values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
     547             :         }
     548             :         else
     549             :         {
     550           0 :             nulls[2] = true;
     551           0 :             for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
     552           0 :                 nulls[i + 3] = true;
     553             :         }
     554             : 
     555           0 :         tuplestore_putvalues(tupstore, tupdesc, values, nulls);
     556             :     }
     557             : 
     558             :     /* clean up and return the tuplestore */
     559             :     tuplestore_donestoring(tupstore);
     560             : 
     561           0 :     return (Datum) 0;
     562             : }
     563             : 
     564             : /*
     565             :  * Returns activity of PG backends.
     566             :  */
     567             : Datum
     568         410 : pg_stat_get_activity(PG_FUNCTION_ARGS)
     569             : {
     570             : #define PG_STAT_GET_ACTIVITY_COLS   30
     571         410 :     int         num_backends = pgstat_fetch_stat_numbackends();
     572             :     int         curr_backend;
     573         410 :     int         pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
     574         410 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     575             :     TupleDesc   tupdesc;
     576             :     Tuplestorestate *tupstore;
     577             :     MemoryContext per_query_ctx;
     578             :     MemoryContext oldcontext;
     579             : 
     580             :     /* check to see if caller supports us returning a tuplestore */
     581         410 :     if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
     582           0 :         ereport(ERROR,
     583             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     584             :                  errmsg("set-valued function called in context that cannot accept a set")));
     585         410 :     if (!(rsinfo->allowedModes & SFRM_Materialize))
     586           0 :         ereport(ERROR,
     587             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     588             :                  errmsg("materialize mode required, but it is not allowed in this context")));
     589             : 
     590             :     /* Build a tuple descriptor for our result type */
     591         410 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     592           0 :         elog(ERROR, "return type must be a row type");
     593             : 
     594         410 :     per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
     595         410 :     oldcontext = MemoryContextSwitchTo(per_query_ctx);
     596             : 
     597         410 :     tupstore = tuplestore_begin_heap(true, false, work_mem);
     598         410 :     rsinfo->returnMode = SFRM_Materialize;
     599         410 :     rsinfo->setResult = tupstore;
     600         410 :     rsinfo->setDesc = tupdesc;
     601             : 
     602         410 :     MemoryContextSwitchTo(oldcontext);
     603             : 
     604             :     /* 1-based index */
     605        3506 :     for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
     606             :     {
     607             :         /* for each row */
     608             :         Datum       values[PG_STAT_GET_ACTIVITY_COLS];
     609             :         bool        nulls[PG_STAT_GET_ACTIVITY_COLS];
     610             :         LocalPgBackendStatus *local_beentry;
     611             :         PgBackendStatus *beentry;
     612             :         PGPROC     *proc;
     613        3096 :         const char *wait_event_type = NULL;
     614        3096 :         const char *wait_event = NULL;
     615             : 
     616       95976 :         MemSet(values, 0, sizeof(values));
     617        3096 :         MemSet(nulls, 0, sizeof(nulls));
     618             : 
     619             :         /* Get the next one in the list */
     620        3096 :         local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
     621        3096 :         if (!local_beentry)
     622             :         {
     623             :             int         i;
     624             : 
     625             :             /* Ignore missing entries if looking for specific PID */
     626           0 :             if (pid != -1)
     627           0 :                 continue;
     628             : 
     629           0 :             for (i = 0; i < lengthof(nulls); i++)
     630           0 :                 nulls[i] = true;
     631             : 
     632           0 :             nulls[5] = false;
     633           0 :             values[5] = CStringGetTextDatum("<backend information not available>");
     634             : 
     635           0 :             tuplestore_putvalues(tupstore, tupdesc, values, nulls);
     636           0 :             continue;
     637             :         }
     638             : 
     639        3096 :         beentry = &local_beentry->backendStatus;
     640             : 
     641             :         /* If looking for specific PID, ignore all the others */
     642        3096 :         if (pid != -1 && beentry->st_procpid != pid)
     643           0 :             continue;
     644             : 
     645             :         /* Values available to all callers */
     646        3096 :         if (beentry->st_databaseid != InvalidOid)
     647         858 :             values[0] = ObjectIdGetDatum(beentry->st_databaseid);
     648             :         else
     649        2238 :             nulls[0] = true;
     650             : 
     651        3096 :         values[1] = Int32GetDatum(beentry->st_procpid);
     652             : 
     653        3096 :         if (beentry->st_userid != InvalidOid)
     654        1460 :             values[2] = ObjectIdGetDatum(beentry->st_userid);
     655             :         else
     656        1636 :             nulls[2] = true;
     657             : 
     658        3096 :         if (beentry->st_appname)
     659        3096 :             values[3] = CStringGetTextDatum(beentry->st_appname);
     660             :         else
     661           0 :             nulls[3] = true;
     662             : 
     663        3096 :         if (TransactionIdIsValid(local_beentry->backend_xid))
     664          12 :             values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
     665             :         else
     666        3084 :             nulls[15] = true;
     667             : 
     668        3096 :         if (TransactionIdIsValid(local_beentry->backend_xmin))
     669         426 :             values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
     670             :         else
     671        2670 :             nulls[16] = true;
     672             : 
     673             :         /* Values only available to role member or pg_read_all_stats */
     674        3096 :         if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
     675        3064 :         {
     676             :             SockAddr    zero_clientaddr;
     677             :             char       *clipped_activity;
     678             : 
     679        3064 :             switch (beentry->st_state)
     680             :             {
     681          98 :                 case STATE_IDLE:
     682          98 :                     values[4] = CStringGetTextDatum("idle");
     683          98 :                     break;
     684         950 :                 case STATE_RUNNING:
     685         950 :                     values[4] = CStringGetTextDatum("active");
     686         950 :                     break;
     687          10 :                 case STATE_IDLEINTRANSACTION:
     688          10 :                     values[4] = CStringGetTextDatum("idle in transaction");
     689          10 :                     break;
     690           0 :                 case STATE_FASTPATH:
     691           0 :                     values[4] = CStringGetTextDatum("fastpath function call");
     692           0 :                     break;
     693           0 :                 case STATE_IDLEINTRANSACTION_ABORTED:
     694           0 :                     values[4] = CStringGetTextDatum("idle in transaction (aborted)");
     695           0 :                     break;
     696           0 :                 case STATE_DISABLED:
     697           0 :                     values[4] = CStringGetTextDatum("disabled");
     698           0 :                     break;
     699        2006 :                 case STATE_UNDEFINED:
     700        2006 :                     nulls[4] = true;
     701        2006 :                     break;
     702             :             }
     703             : 
     704        3064 :             clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
     705        3064 :             values[5] = CStringGetTextDatum(clipped_activity);
     706        3064 :             pfree(clipped_activity);
     707             : 
     708             :             /* leader_pid */
     709        3064 :             nulls[29] = true;
     710             : 
     711        3064 :             proc = BackendPidGetProc(beentry->st_procpid);
     712             : 
     713        3064 :             if (proc == NULL && (beentry->st_backendType != B_BACKEND))
     714             :             {
     715             :                 /*
     716             :                  * For an auxiliary process, retrieve process info from
     717             :                  * AuxiliaryProcs stored in shared-memory.
     718             :                  */
     719        1224 :                 proc = AuxiliaryPidGetProc(beentry->st_procpid);
     720             :             }
     721             : 
     722             :             /*
     723             :              * If a PGPROC entry was retrieved, display wait events and lock
     724             :              * group leader information if any.  To avoid extra overhead, no
     725             :              * extra lock is being held, so there is no guarantee of
     726             :              * consistency across multiple rows.
     727             :              */
     728        3064 :             if (proc != NULL)
     729             :             {
     730             :                 uint32      raw_wait_event;
     731             :                 PGPROC     *leader;
     732             : 
     733        3060 :                 raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
     734        3060 :                 wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
     735        3060 :                 wait_event = pgstat_get_wait_event(raw_wait_event);
     736             : 
     737        3060 :                 leader = proc->lockGroupLeader;
     738             : 
     739             :                 /*
     740             :                  * Show the leader only for active parallel workers.  This
     741             :                  * leaves the field as NULL for the leader of a parallel
     742             :                  * group.
     743             :                  */
     744        3060 :                 if (leader && leader->pid != beentry->st_procpid)
     745             :                 {
     746           0 :                     values[29] = Int32GetDatum(leader->pid);
     747           0 :                     nulls[29] = false;
     748             :                 }
     749             :             }
     750             : 
     751        3064 :             if (wait_event_type)
     752        2640 :                 values[6] = CStringGetTextDatum(wait_event_type);
     753             :             else
     754         424 :                 nulls[6] = true;
     755             : 
     756        3064 :             if (wait_event)
     757        2640 :                 values[7] = CStringGetTextDatum(wait_event);
     758             :             else
     759         424 :                 nulls[7] = true;
     760             : 
     761             :             /*
     762             :              * Don't expose transaction time for walsenders; it confuses
     763             :              * monitoring, particularly because we don't keep the time up-to-
     764             :              * date.
     765             :              */
     766        3064 :             if (beentry->st_xact_start_timestamp != 0 &&
     767         430 :                 beentry->st_backendType != B_WAL_SENDER)
     768         428 :                 values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
     769             :             else
     770        2636 :                 nulls[8] = true;
     771             : 
     772        3064 :             if (beentry->st_activity_start_timestamp != 0)
     773        1054 :                 values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
     774             :             else
     775        2010 :                 nulls[9] = true;
     776             : 
     777        3064 :             if (beentry->st_proc_start_timestamp != 0)
     778        3064 :                 values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
     779             :             else
     780           0 :                 nulls[10] = true;
     781             : 
     782        3064 :             if (beentry->st_state_start_timestamp != 0)
     783        1058 :                 values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
     784             :             else
     785        2006 :                 nulls[11] = true;
     786             : 
     787             :             /* A zeroed client addr means we don't know */
     788        3064 :             memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
     789        3064 :             if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
     790             :                        sizeof(zero_clientaddr)) == 0)
     791             :             {
     792        2008 :                 nulls[12] = true;
     793        2008 :                 nulls[13] = true;
     794        2008 :                 nulls[14] = true;
     795             :             }
     796             :             else
     797             :             {
     798        1056 :                 if (beentry->st_clientaddr.addr.ss_family == AF_INET
     799             : #ifdef HAVE_IPV6
     800        1052 :                     || beentry->st_clientaddr.addr.ss_family == AF_INET6
     801             : #endif
     802             :                     )
     803           4 :                 {
     804             :                     char        remote_host[NI_MAXHOST];
     805             :                     char        remote_port[NI_MAXSERV];
     806             :                     int         ret;
     807             : 
     808           4 :                     remote_host[0] = '\0';
     809           4 :                     remote_port[0] = '\0';
     810           4 :                     ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
     811           4 :                                              beentry->st_clientaddr.salen,
     812             :                                              remote_host, sizeof(remote_host),
     813             :                                              remote_port, sizeof(remote_port),
     814             :                                              NI_NUMERICHOST | NI_NUMERICSERV);
     815           4 :                     if (ret == 0)
     816             :                     {
     817           4 :                         clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
     818           4 :                         values[12] = DirectFunctionCall1(inet_in,
     819             :                                                          CStringGetDatum(remote_host));
     820           4 :                         if (beentry->st_clienthostname &&
     821           4 :                             beentry->st_clienthostname[0])
     822           4 :                             values[13] = CStringGetTextDatum(beentry->st_clienthostname);
     823             :                         else
     824           0 :                             nulls[13] = true;
     825           4 :                         values[14] = Int32GetDatum(atoi(remote_port));
     826             :                     }
     827             :                     else
     828             :                     {
     829           0 :                         nulls[12] = true;
     830           0 :                         nulls[13] = true;
     831           0 :                         nulls[14] = true;
     832             :                     }
     833             :                 }
     834        1052 :                 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
     835             :                 {
     836             :                     /*
     837             :                      * Unix sockets always reports NULL for host and -1 for
     838             :                      * port, so it's possible to tell the difference to
     839             :                      * connections we have no permissions to view, or with
     840             :                      * errors.
     841             :                      */
     842        1052 :                     nulls[12] = true;
     843        1052 :                     nulls[13] = true;
     844        1052 :                     values[14] = Int32GetDatum(-1);
     845             :                 }
     846             :                 else
     847             :                 {
     848             :                     /* Unknown address type, should never happen */
     849           0 :                     nulls[12] = true;
     850           0 :                     nulls[13] = true;
     851           0 :                     nulls[14] = true;
     852             :                 }
     853             :             }
     854             :             /* Add backend type */
     855        3064 :             if (beentry->st_backendType == B_BG_WORKER)
     856             :             {
     857             :                 const char *bgw_type;
     858             : 
     859         396 :                 bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
     860         396 :                 if (bgw_type)
     861         396 :                     values[17] = CStringGetTextDatum(bgw_type);
     862             :                 else
     863           0 :                     nulls[17] = true;
     864             :             }
     865             :             else
     866        2668 :                 values[17] =
     867        2668 :                     CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType));
     868             : 
     869             :             /* SSL information */
     870        3064 :             if (beentry->st_ssl)
     871             :             {
     872           4 :                 values[18] = BoolGetDatum(true);    /* ssl */
     873           4 :                 values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
     874           4 :                 values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
     875           4 :                 values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
     876           4 :                 values[22] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
     877             : 
     878           4 :                 if (beentry->st_sslstatus->ssl_client_dn[0])
     879           2 :                     values[23] = CStringGetTextDatum(beentry->st_sslstatus->ssl_client_dn);
     880             :                 else
     881           2 :                     nulls[23] = true;
     882             : 
     883           4 :                 if (beentry->st_sslstatus->ssl_client_serial[0])
     884           2 :                     values[24] = DirectFunctionCall3(numeric_in,
     885             :                                                      CStringGetDatum(beentry->st_sslstatus->ssl_client_serial),
     886             :                                                      ObjectIdGetDatum(InvalidOid),
     887             :                                                      Int32GetDatum(-1));
     888             :                 else
     889           2 :                     nulls[24] = true;
     890             : 
     891           4 :                 if (beentry->st_sslstatus->ssl_issuer_dn[0])
     892           2 :                     values[25] = CStringGetTextDatum(beentry->st_sslstatus->ssl_issuer_dn);
     893             :                 else
     894           2 :                     nulls[25] = true;
     895             :             }
     896             :             else
     897             :             {
     898        3060 :                 values[18] = BoolGetDatum(false);   /* ssl */
     899        3060 :                 nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = nulls[25] = true;
     900             :             }
     901             : 
     902             :             /* GSSAPI information */
     903        3064 :             if (beentry->st_gss)
     904             :             {
     905           0 :                 values[26] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
     906           0 :                 values[27] = CStringGetTextDatum(beentry->st_gssstatus->gss_princ);
     907           0 :                 values[28] = BoolGetDatum(beentry->st_gssstatus->gss_enc);    /* GSS Encryption in use */
     908             :             }
     909             :             else
     910             :             {
     911        3064 :                 values[26] = BoolGetDatum(false);   /* gss_auth */
     912        3064 :                 nulls[27] = true;   /* No GSS principal */
     913        3064 :                 values[28] = BoolGetDatum(false);   /* GSS Encryption not in
     914             :                                                      * use */
     915             :             }
     916             :         }
     917             :         else
     918             :         {
     919             :             /* No permissions to view data about this session */
     920          32 :             values[5] = CStringGetTextDatum("<insufficient privilege>");
     921          32 :             nulls[4] = true;
     922          32 :             nulls[6] = true;
     923          32 :             nulls[7] = true;
     924          32 :             nulls[8] = true;
     925          32 :             nulls[9] = true;
     926          32 :             nulls[10] = true;
     927          32 :             nulls[11] = true;
     928          32 :             nulls[12] = true;
     929          32 :             nulls[13] = true;
     930          32 :             nulls[14] = true;
     931          32 :             nulls[17] = true;
     932          32 :             nulls[18] = true;
     933          32 :             nulls[19] = true;
     934          32 :             nulls[20] = true;
     935          32 :             nulls[21] = true;
     936          32 :             nulls[22] = true;
     937          32 :             nulls[23] = true;
     938          32 :             nulls[24] = true;
     939          32 :             nulls[25] = true;
     940          32 :             nulls[26] = true;
     941          32 :             nulls[27] = true;
     942          32 :             nulls[28] = true;
     943          32 :             nulls[29] = true;
     944             :         }
     945             : 
     946        3096 :         tuplestore_putvalues(tupstore, tupdesc, values, nulls);
     947             : 
     948             :         /* If only a single backend was requested, and we found it, break. */
     949        3096 :         if (pid != -1)
     950           0 :             break;
     951             :     }
     952             : 
     953             :     /* clean up and return the tuplestore */
     954             :     tuplestore_donestoring(tupstore);
     955             : 
     956         410 :     return (Datum) 0;
     957             : }
     958             : 
     959             : 
     960             : Datum
     961          26 : pg_backend_pid(PG_FUNCTION_ARGS)
     962             : {
     963          26 :     PG_RETURN_INT32(MyProcPid);
     964             : }
     965             : 
     966             : 
     967             : Datum
     968           0 : pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
     969             : {
     970           0 :     int32       beid = PG_GETARG_INT32(0);
     971             :     PgBackendStatus *beentry;
     972             : 
     973           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
     974           0 :         PG_RETURN_NULL();
     975             : 
     976           0 :     PG_RETURN_INT32(beentry->st_procpid);
     977             : }
     978             : 
     979             : 
     980             : Datum
     981           0 : pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
     982             : {
     983           0 :     int32       beid = PG_GETARG_INT32(0);
     984             :     PgBackendStatus *beentry;
     985             : 
     986           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
     987           0 :         PG_RETURN_NULL();
     988             : 
     989           0 :     PG_RETURN_OID(beentry->st_databaseid);
     990             : }
     991             : 
     992             : 
     993             : Datum
     994           0 : pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
     995             : {
     996           0 :     int32       beid = PG_GETARG_INT32(0);
     997             :     PgBackendStatus *beentry;
     998             : 
     999           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1000           0 :         PG_RETURN_NULL();
    1001             : 
    1002           0 :     PG_RETURN_OID(beentry->st_userid);
    1003             : }
    1004             : 
    1005             : 
    1006             : Datum
    1007           0 : pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
    1008             : {
    1009           0 :     int32       beid = PG_GETARG_INT32(0);
    1010             :     PgBackendStatus *beentry;
    1011             :     const char *activity;
    1012             :     char       *clipped_activity;
    1013             :     text       *ret;
    1014             : 
    1015           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1016           0 :         activity = "<backend information not available>";
    1017           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1018           0 :         activity = "<insufficient privilege>";
    1019           0 :     else if (*(beentry->st_activity_raw) == '\0')
    1020           0 :         activity = "<command string not enabled>";
    1021             :     else
    1022           0 :         activity = beentry->st_activity_raw;
    1023             : 
    1024           0 :     clipped_activity = pgstat_clip_activity(activity);
    1025           0 :     ret = cstring_to_text(activity);
    1026           0 :     pfree(clipped_activity);
    1027             : 
    1028           0 :     PG_RETURN_TEXT_P(ret);
    1029             : }
    1030             : 
    1031             : Datum
    1032           0 : pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
    1033             : {
    1034           0 :     int32       beid = PG_GETARG_INT32(0);
    1035             :     PgBackendStatus *beentry;
    1036             :     PGPROC     *proc;
    1037           0 :     const char *wait_event_type = NULL;
    1038             : 
    1039           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1040           0 :         wait_event_type = "<backend information not available>";
    1041           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1042           0 :         wait_event_type = "<insufficient privilege>";
    1043           0 :     else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
    1044           0 :         wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
    1045             : 
    1046           0 :     if (!wait_event_type)
    1047           0 :         PG_RETURN_NULL();
    1048             : 
    1049           0 :     PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
    1050             : }
    1051             : 
    1052             : Datum
    1053           0 : pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
    1054             : {
    1055           0 :     int32       beid = PG_GETARG_INT32(0);
    1056             :     PgBackendStatus *beentry;
    1057             :     PGPROC     *proc;
    1058           0 :     const char *wait_event = NULL;
    1059             : 
    1060           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1061           0 :         wait_event = "<backend information not available>";
    1062           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1063           0 :         wait_event = "<insufficient privilege>";
    1064           0 :     else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
    1065           0 :         wait_event = pgstat_get_wait_event(proc->wait_event_info);
    1066             : 
    1067           0 :     if (!wait_event)
    1068           0 :         PG_RETURN_NULL();
    1069             : 
    1070           0 :     PG_RETURN_TEXT_P(cstring_to_text(wait_event));
    1071             : }
    1072             : 
    1073             : 
    1074             : Datum
    1075           0 : pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
    1076             : {
    1077           0 :     int32       beid = PG_GETARG_INT32(0);
    1078             :     TimestampTz result;
    1079             :     PgBackendStatus *beentry;
    1080             : 
    1081           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1082           0 :         PG_RETURN_NULL();
    1083             : 
    1084           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1085           0 :         PG_RETURN_NULL();
    1086             : 
    1087           0 :     result = beentry->st_activity_start_timestamp;
    1088             : 
    1089             :     /*
    1090             :      * No time recorded for start of current query -- this is the case if the
    1091             :      * user hasn't enabled query-level stats collection.
    1092             :      */
    1093           0 :     if (result == 0)
    1094           0 :         PG_RETURN_NULL();
    1095             : 
    1096           0 :     PG_RETURN_TIMESTAMPTZ(result);
    1097             : }
    1098             : 
    1099             : 
    1100             : Datum
    1101           0 : pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
    1102             : {
    1103           0 :     int32       beid = PG_GETARG_INT32(0);
    1104             :     TimestampTz result;
    1105             :     PgBackendStatus *beentry;
    1106             : 
    1107           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1108           0 :         PG_RETURN_NULL();
    1109             : 
    1110           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1111           0 :         PG_RETURN_NULL();
    1112             : 
    1113           0 :     result = beentry->st_xact_start_timestamp;
    1114             : 
    1115           0 :     if (result == 0)            /* not in a transaction */
    1116           0 :         PG_RETURN_NULL();
    1117             : 
    1118           0 :     PG_RETURN_TIMESTAMPTZ(result);
    1119             : }
    1120             : 
    1121             : 
    1122             : Datum
    1123           0 : pg_stat_get_backend_start(PG_FUNCTION_ARGS)
    1124             : {
    1125           0 :     int32       beid = PG_GETARG_INT32(0);
    1126             :     TimestampTz result;
    1127             :     PgBackendStatus *beentry;
    1128             : 
    1129           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1130           0 :         PG_RETURN_NULL();
    1131             : 
    1132           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1133           0 :         PG_RETURN_NULL();
    1134             : 
    1135           0 :     result = beentry->st_proc_start_timestamp;
    1136             : 
    1137           0 :     if (result == 0)            /* probably can't happen? */
    1138           0 :         PG_RETURN_NULL();
    1139             : 
    1140           0 :     PG_RETURN_TIMESTAMPTZ(result);
    1141             : }
    1142             : 
    1143             : 
    1144             : Datum
    1145           0 : pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
    1146             : {
    1147           0 :     int32       beid = PG_GETARG_INT32(0);
    1148             :     PgBackendStatus *beentry;
    1149             :     SockAddr    zero_clientaddr;
    1150             :     char        remote_host[NI_MAXHOST];
    1151             :     int         ret;
    1152             : 
    1153           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1154           0 :         PG_RETURN_NULL();
    1155             : 
    1156           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1157           0 :         PG_RETURN_NULL();
    1158             : 
    1159             :     /* A zeroed client addr means we don't know */
    1160           0 :     memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
    1161           0 :     if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
    1162             :                sizeof(zero_clientaddr)) == 0)
    1163           0 :         PG_RETURN_NULL();
    1164             : 
    1165           0 :     switch (beentry->st_clientaddr.addr.ss_family)
    1166             :     {
    1167           0 :         case AF_INET:
    1168             : #ifdef HAVE_IPV6
    1169             :         case AF_INET6:
    1170             : #endif
    1171           0 :             break;
    1172           0 :         default:
    1173           0 :             PG_RETURN_NULL();
    1174             :     }
    1175             : 
    1176           0 :     remote_host[0] = '\0';
    1177           0 :     ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
    1178           0 :                              beentry->st_clientaddr.salen,
    1179             :                              remote_host, sizeof(remote_host),
    1180             :                              NULL, 0,
    1181             :                              NI_NUMERICHOST | NI_NUMERICSERV);
    1182           0 :     if (ret != 0)
    1183           0 :         PG_RETURN_NULL();
    1184             : 
    1185           0 :     clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
    1186             : 
    1187           0 :     PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
    1188             :                                          CStringGetDatum(remote_host)));
    1189             : }
    1190             : 
    1191             : Datum
    1192           0 : pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
    1193             : {
    1194           0 :     int32       beid = PG_GETARG_INT32(0);
    1195             :     PgBackendStatus *beentry;
    1196             :     SockAddr    zero_clientaddr;
    1197             :     char        remote_port[NI_MAXSERV];
    1198             :     int         ret;
    1199             : 
    1200           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1201           0 :         PG_RETURN_NULL();
    1202             : 
    1203           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1204           0 :         PG_RETURN_NULL();
    1205             : 
    1206             :     /* A zeroed client addr means we don't know */
    1207           0 :     memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
    1208           0 :     if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
    1209             :                sizeof(zero_clientaddr)) == 0)
    1210           0 :         PG_RETURN_NULL();
    1211             : 
    1212           0 :     switch (beentry->st_clientaddr.addr.ss_family)
    1213             :     {
    1214           0 :         case AF_INET:
    1215             : #ifdef HAVE_IPV6
    1216             :         case AF_INET6:
    1217             : #endif
    1218           0 :             break;
    1219           0 :         case AF_UNIX:
    1220           0 :             PG_RETURN_INT32(-1);
    1221           0 :         default:
    1222           0 :             PG_RETURN_NULL();
    1223             :     }
    1224             : 
    1225           0 :     remote_port[0] = '\0';
    1226           0 :     ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
    1227           0 :                              beentry->st_clientaddr.salen,
    1228             :                              NULL, 0,
    1229             :                              remote_port, sizeof(remote_port),
    1230             :                              NI_NUMERICHOST | NI_NUMERICSERV);
    1231           0 :     if (ret != 0)
    1232           0 :         PG_RETURN_NULL();
    1233             : 
    1234           0 :     PG_RETURN_DATUM(DirectFunctionCall1(int4in,
    1235             :                                         CStringGetDatum(remote_port)));
    1236             : }
    1237             : 
    1238             : 
    1239             : Datum
    1240           0 : pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
    1241             : {
    1242           0 :     Oid         dbid = PG_GETARG_OID(0);
    1243             :     int32       result;
    1244           0 :     int         tot_backends = pgstat_fetch_stat_numbackends();
    1245             :     int         beid;
    1246             : 
    1247           0 :     result = 0;
    1248           0 :     for (beid = 1; beid <= tot_backends; beid++)
    1249             :     {
    1250           0 :         PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
    1251             : 
    1252           0 :         if (beentry && beentry->st_databaseid == dbid)
    1253           0 :             result++;
    1254             :     }
    1255             : 
    1256           0 :     PG_RETURN_INT32(result);
    1257             : }
    1258             : 
    1259             : 
    1260             : Datum
    1261           0 : pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
    1262             : {
    1263           0 :     Oid         dbid = PG_GETARG_OID(0);
    1264             :     int64       result;
    1265             :     PgStat_StatDBEntry *dbentry;
    1266             : 
    1267           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1268           0 :         result = 0;
    1269             :     else
    1270           0 :         result = (int64) (dbentry->n_xact_commit);
    1271             : 
    1272           0 :     PG_RETURN_INT64(result);
    1273             : }
    1274             : 
    1275             : 
    1276             : Datum
    1277           0 : pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
    1278             : {
    1279           0 :     Oid         dbid = PG_GETARG_OID(0);
    1280             :     int64       result;
    1281             :     PgStat_StatDBEntry *dbentry;
    1282             : 
    1283           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1284           0 :         result = 0;
    1285             :     else
    1286           0 :         result = (int64) (dbentry->n_xact_rollback);
    1287             : 
    1288           0 :     PG_RETURN_INT64(result);
    1289             : }
    1290             : 
    1291             : 
    1292             : Datum
    1293           0 : pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
    1294             : {
    1295           0 :     Oid         dbid = PG_GETARG_OID(0);
    1296             :     int64       result;
    1297             :     PgStat_StatDBEntry *dbentry;
    1298             : 
    1299           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1300           0 :         result = 0;
    1301             :     else
    1302           0 :         result = (int64) (dbentry->n_blocks_fetched);
    1303             : 
    1304           0 :     PG_RETURN_INT64(result);
    1305             : }
    1306             : 
    1307             : 
    1308             : Datum
    1309           0 : pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
    1310             : {
    1311           0 :     Oid         dbid = PG_GETARG_OID(0);
    1312             :     int64       result;
    1313             :     PgStat_StatDBEntry *dbentry;
    1314             : 
    1315           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1316           0 :         result = 0;
    1317             :     else
    1318           0 :         result = (int64) (dbentry->n_blocks_hit);
    1319             : 
    1320           0 :     PG_RETURN_INT64(result);
    1321             : }
    1322             : 
    1323             : 
    1324             : Datum
    1325           0 : pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
    1326             : {
    1327           0 :     Oid         dbid = PG_GETARG_OID(0);
    1328             :     int64       result;
    1329             :     PgStat_StatDBEntry *dbentry;
    1330             : 
    1331           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1332           0 :         result = 0;
    1333             :     else
    1334           0 :         result = (int64) (dbentry->n_tuples_returned);
    1335             : 
    1336           0 :     PG_RETURN_INT64(result);
    1337             : }
    1338             : 
    1339             : 
    1340             : Datum
    1341           0 : pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
    1342             : {
    1343           0 :     Oid         dbid = PG_GETARG_OID(0);
    1344             :     int64       result;
    1345             :     PgStat_StatDBEntry *dbentry;
    1346             : 
    1347           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1348           0 :         result = 0;
    1349             :     else
    1350           0 :         result = (int64) (dbentry->n_tuples_fetched);
    1351             : 
    1352           0 :     PG_RETURN_INT64(result);
    1353             : }
    1354             : 
    1355             : 
    1356             : Datum
    1357           0 : pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
    1358             : {
    1359           0 :     Oid         dbid = PG_GETARG_OID(0);
    1360             :     int64       result;
    1361             :     PgStat_StatDBEntry *dbentry;
    1362             : 
    1363           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1364           0 :         result = 0;
    1365             :     else
    1366           0 :         result = (int64) (dbentry->n_tuples_inserted);
    1367             : 
    1368           0 :     PG_RETURN_INT64(result);
    1369             : }
    1370             : 
    1371             : 
    1372             : Datum
    1373           0 : pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
    1374             : {
    1375           0 :     Oid         dbid = PG_GETARG_OID(0);
    1376             :     int64       result;
    1377             :     PgStat_StatDBEntry *dbentry;
    1378             : 
    1379           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1380           0 :         result = 0;
    1381             :     else
    1382           0 :         result = (int64) (dbentry->n_tuples_updated);
    1383             : 
    1384           0 :     PG_RETURN_INT64(result);
    1385             : }
    1386             : 
    1387             : 
    1388             : Datum
    1389           0 : pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
    1390             : {
    1391           0 :     Oid         dbid = PG_GETARG_OID(0);
    1392             :     int64       result;
    1393             :     PgStat_StatDBEntry *dbentry;
    1394             : 
    1395           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1396           0 :         result = 0;
    1397             :     else
    1398           0 :         result = (int64) (dbentry->n_tuples_deleted);
    1399             : 
    1400           0 :     PG_RETURN_INT64(result);
    1401             : }
    1402             : 
    1403             : Datum
    1404           0 : pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
    1405             : {
    1406           0 :     Oid         dbid = PG_GETARG_OID(0);
    1407             :     TimestampTz result;
    1408             :     PgStat_StatDBEntry *dbentry;
    1409             : 
    1410           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1411           0 :         result = 0;
    1412             :     else
    1413           0 :         result = dbentry->stat_reset_timestamp;
    1414             : 
    1415           0 :     if (result == 0)
    1416           0 :         PG_RETURN_NULL();
    1417             :     else
    1418           0 :         PG_RETURN_TIMESTAMPTZ(result);
    1419             : }
    1420             : 
    1421             : Datum
    1422           0 : pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
    1423             : {
    1424           0 :     Oid         dbid = PG_GETARG_OID(0);
    1425             :     int64       result;
    1426             :     PgStat_StatDBEntry *dbentry;
    1427             : 
    1428           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1429           0 :         result = 0;
    1430             :     else
    1431           0 :         result = dbentry->n_temp_files;
    1432             : 
    1433           0 :     PG_RETURN_INT64(result);
    1434             : }
    1435             : 
    1436             : 
    1437             : Datum
    1438           0 : pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
    1439             : {
    1440           0 :     Oid         dbid = PG_GETARG_OID(0);
    1441             :     int64       result;
    1442             :     PgStat_StatDBEntry *dbentry;
    1443             : 
    1444           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1445           0 :         result = 0;
    1446             :     else
    1447           0 :         result = dbentry->n_temp_bytes;
    1448             : 
    1449           0 :     PG_RETURN_INT64(result);
    1450             : }
    1451             : 
    1452             : Datum
    1453           0 : pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
    1454             : {
    1455           0 :     Oid         dbid = PG_GETARG_OID(0);
    1456             :     int64       result;
    1457             :     PgStat_StatDBEntry *dbentry;
    1458             : 
    1459           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1460           0 :         result = 0;
    1461             :     else
    1462           0 :         result = (int64) (dbentry->n_conflict_tablespace);
    1463             : 
    1464           0 :     PG_RETURN_INT64(result);
    1465             : }
    1466             : 
    1467             : Datum
    1468           0 : pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
    1469             : {
    1470           0 :     Oid         dbid = PG_GETARG_OID(0);
    1471             :     int64       result;
    1472             :     PgStat_StatDBEntry *dbentry;
    1473             : 
    1474           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1475           0 :         result = 0;
    1476             :     else
    1477           0 :         result = (int64) (dbentry->n_conflict_lock);
    1478             : 
    1479           0 :     PG_RETURN_INT64(result);
    1480             : }
    1481             : 
    1482             : Datum
    1483           0 : pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS)
    1484             : {
    1485           0 :     Oid         dbid = PG_GETARG_OID(0);
    1486             :     int64       result;
    1487             :     PgStat_StatDBEntry *dbentry;
    1488             : 
    1489           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1490           0 :         result = 0;
    1491             :     else
    1492           0 :         result = (int64) (dbentry->n_conflict_snapshot);
    1493             : 
    1494           0 :     PG_RETURN_INT64(result);
    1495             : }
    1496             : 
    1497             : Datum
    1498           0 : pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
    1499             : {
    1500           0 :     Oid         dbid = PG_GETARG_OID(0);
    1501             :     int64       result;
    1502             :     PgStat_StatDBEntry *dbentry;
    1503             : 
    1504           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1505           0 :         result = 0;
    1506             :     else
    1507           0 :         result = (int64) (dbentry->n_conflict_bufferpin);
    1508             : 
    1509           0 :     PG_RETURN_INT64(result);
    1510             : }
    1511             : 
    1512             : Datum
    1513           0 : pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
    1514             : {
    1515           0 :     Oid         dbid = PG_GETARG_OID(0);
    1516             :     int64       result;
    1517             :     PgStat_StatDBEntry *dbentry;
    1518             : 
    1519           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1520           0 :         result = 0;
    1521             :     else
    1522           0 :         result = (int64) (dbentry->n_conflict_startup_deadlock);
    1523             : 
    1524           0 :     PG_RETURN_INT64(result);
    1525             : }
    1526             : 
    1527             : Datum
    1528           0 : pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
    1529             : {
    1530           0 :     Oid         dbid = PG_GETARG_OID(0);
    1531             :     int64       result;
    1532             :     PgStat_StatDBEntry *dbentry;
    1533             : 
    1534           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1535           0 :         result = 0;
    1536             :     else
    1537           0 :         result = (int64) (dbentry->n_conflict_tablespace +
    1538           0 :                           dbentry->n_conflict_lock +
    1539           0 :                           dbentry->n_conflict_snapshot +
    1540           0 :                           dbentry->n_conflict_bufferpin +
    1541           0 :                           dbentry->n_conflict_startup_deadlock);
    1542             : 
    1543           0 :     PG_RETURN_INT64(result);
    1544             : }
    1545             : 
    1546             : Datum
    1547           0 : pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
    1548             : {
    1549           0 :     Oid         dbid = PG_GETARG_OID(0);
    1550             :     int64       result;
    1551             :     PgStat_StatDBEntry *dbentry;
    1552             : 
    1553           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1554           0 :         result = 0;
    1555             :     else
    1556           0 :         result = (int64) (dbentry->n_deadlocks);
    1557             : 
    1558           0 :     PG_RETURN_INT64(result);
    1559             : }
    1560             : 
    1561             : Datum
    1562           0 : pg_stat_get_db_checksum_failures(PG_FUNCTION_ARGS)
    1563             : {
    1564           0 :     Oid         dbid = PG_GETARG_OID(0);
    1565             :     int64       result;
    1566             :     PgStat_StatDBEntry *dbentry;
    1567             : 
    1568           0 :     if (!DataChecksumsEnabled())
    1569           0 :         PG_RETURN_NULL();
    1570             : 
    1571           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1572           0 :         result = 0;
    1573             :     else
    1574           0 :         result = (int64) (dbentry->n_checksum_failures);
    1575             : 
    1576           0 :     PG_RETURN_INT64(result);
    1577             : }
    1578             : 
    1579             : Datum
    1580           0 : pg_stat_get_db_checksum_last_failure(PG_FUNCTION_ARGS)
    1581             : {
    1582           0 :     Oid         dbid = PG_GETARG_OID(0);
    1583             :     TimestampTz result;
    1584             :     PgStat_StatDBEntry *dbentry;
    1585             : 
    1586           0 :     if (!DataChecksumsEnabled())
    1587           0 :         PG_RETURN_NULL();
    1588             : 
    1589           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1590           0 :         result = 0;
    1591             :     else
    1592           0 :         result = dbentry->last_checksum_failure;
    1593             : 
    1594           0 :     if (result == 0)
    1595           0 :         PG_RETURN_NULL();
    1596             :     else
    1597           0 :         PG_RETURN_TIMESTAMPTZ(result);
    1598             : }
    1599             : 
    1600             : Datum
    1601           0 : pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
    1602             : {
    1603           0 :     Oid         dbid = PG_GETARG_OID(0);
    1604             :     double      result;
    1605             :     PgStat_StatDBEntry *dbentry;
    1606             : 
    1607             :     /* convert counter from microsec to millisec for display */
    1608           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1609           0 :         result = 0;
    1610             :     else
    1611           0 :         result = ((double) dbentry->n_block_read_time) / 1000.0;
    1612             : 
    1613           0 :     PG_RETURN_FLOAT8(result);
    1614             : }
    1615             : 
    1616             : Datum
    1617           0 : pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
    1618             : {
    1619           0 :     Oid         dbid = PG_GETARG_OID(0);
    1620             :     double      result;
    1621             :     PgStat_StatDBEntry *dbentry;
    1622             : 
    1623             :     /* convert counter from microsec to millisec for display */
    1624           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1625           0 :         result = 0;
    1626             :     else
    1627           0 :         result = ((double) dbentry->n_block_write_time) / 1000.0;
    1628             : 
    1629           0 :     PG_RETURN_FLOAT8(result);
    1630             : }
    1631             : 
    1632             : Datum
    1633           0 : pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
    1634             : {
    1635           0 :     PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
    1636             : }
    1637             : 
    1638             : Datum
    1639           0 : pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
    1640             : {
    1641           0 :     PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
    1642             : }
    1643             : 
    1644             : Datum
    1645           0 : pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
    1646             : {
    1647           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
    1648             : }
    1649             : 
    1650             : Datum
    1651           0 : pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
    1652             : {
    1653           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
    1654             : }
    1655             : 
    1656             : Datum
    1657           0 : pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
    1658             : {
    1659           0 :     PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
    1660             : }
    1661             : 
    1662             : Datum
    1663           0 : pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
    1664             : {
    1665             :     /* time is already in msec, just convert to double for presentation */
    1666           0 :     PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
    1667             : }
    1668             : 
    1669             : Datum
    1670           0 : pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
    1671             : {
    1672             :     /* time is already in msec, just convert to double for presentation */
    1673           0 :     PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
    1674             : }
    1675             : 
    1676             : Datum
    1677           0 : pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
    1678             : {
    1679           0 :     PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
    1680             : }
    1681             : 
    1682             : Datum
    1683           0 : pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
    1684             : {
    1685           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
    1686             : }
    1687             : 
    1688             : Datum
    1689           0 : pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
    1690             : {
    1691           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
    1692             : }
    1693             : 
    1694             : Datum
    1695           0 : pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
    1696             : {
    1697           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
    1698             : }
    1699             : 
    1700             : /*
    1701             :  * Returns statistics of WAL activity
    1702             :  */
    1703             : Datum
    1704           4 : pg_stat_get_wal(PG_FUNCTION_ARGS)
    1705             : {
    1706             : #define PG_STAT_GET_WAL_COLS    2
    1707             :     TupleDesc   tupdesc;
    1708             :     Datum       values[PG_STAT_GET_WAL_COLS];
    1709             :     bool        nulls[PG_STAT_GET_WAL_COLS];
    1710             :     PgStat_WalStats *wal_stats;
    1711             : 
    1712             :     /* Initialise values and NULL flags arrays */
    1713          12 :     MemSet(values, 0, sizeof(values));
    1714           4 :     MemSet(nulls, 0, sizeof(nulls));
    1715             : 
    1716             :     /* Initialise attributes information in the tuple descriptor */
    1717           4 :     tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_WAL_COLS);
    1718           4 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_buffers_full",
    1719             :                        INT8OID, -1, 0);
    1720           4 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2, "stats_reset",
    1721             :                        TIMESTAMPTZOID, -1, 0);
    1722             : 
    1723           4 :     BlessTupleDesc(tupdesc);
    1724             : 
    1725             :     /* Get statistics about WAL activity */
    1726           4 :     wal_stats = pgstat_fetch_stat_wal();
    1727             : 
    1728             :     /* Fill values and NULLs */
    1729           4 :     values[0] = Int64GetDatum(wal_stats->wal_buffers_full);
    1730           4 :     values[1] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
    1731             : 
    1732             :     /* Returns the record as Datum */
    1733           4 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
    1734             : }
    1735             : 
    1736             : /*
    1737             :  * Returns statistics of SLRU caches.
    1738             :  */
    1739             : Datum
    1740           0 : pg_stat_get_slru(PG_FUNCTION_ARGS)
    1741             : {
    1742             : #define PG_STAT_GET_SLRU_COLS   9
    1743           0 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    1744             :     TupleDesc   tupdesc;
    1745             :     Tuplestorestate *tupstore;
    1746             :     MemoryContext per_query_ctx;
    1747             :     MemoryContext oldcontext;
    1748             :     int         i;
    1749             :     PgStat_SLRUStats *stats;
    1750             : 
    1751             :     /* check to see if caller supports us returning a tuplestore */
    1752           0 :     if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
    1753           0 :         ereport(ERROR,
    1754             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1755             :                  errmsg("set-valued function called in context that cannot accept a set")));
    1756           0 :     if (!(rsinfo->allowedModes & SFRM_Materialize))
    1757           0 :         ereport(ERROR,
    1758             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1759             :                  errmsg("materialize mode required, but it is not allowed in this context")));
    1760             : 
    1761             :     /* Build a tuple descriptor for our result type */
    1762           0 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
    1763           0 :         elog(ERROR, "return type must be a row type");
    1764             : 
    1765           0 :     per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
    1766           0 :     oldcontext = MemoryContextSwitchTo(per_query_ctx);
    1767             : 
    1768           0 :     tupstore = tuplestore_begin_heap(true, false, work_mem);
    1769           0 :     rsinfo->returnMode = SFRM_Materialize;
    1770           0 :     rsinfo->setResult = tupstore;
    1771           0 :     rsinfo->setDesc = tupdesc;
    1772             : 
    1773           0 :     MemoryContextSwitchTo(oldcontext);
    1774             : 
    1775             :     /* request SLRU stats from the stat collector */
    1776           0 :     stats = pgstat_fetch_slru();
    1777             : 
    1778           0 :     for (i = 0;; i++)
    1779           0 :     {
    1780             :         /* for each row */
    1781             :         Datum       values[PG_STAT_GET_SLRU_COLS];
    1782             :         bool        nulls[PG_STAT_GET_SLRU_COLS];
    1783           0 :         PgStat_SLRUStats stat = stats[i];
    1784             :         const char *name;
    1785             : 
    1786           0 :         name = pgstat_slru_name(i);
    1787             : 
    1788           0 :         if (!name)
    1789           0 :             break;
    1790             : 
    1791           0 :         MemSet(values, 0, sizeof(values));
    1792           0 :         MemSet(nulls, 0, sizeof(nulls));
    1793             : 
    1794           0 :         values[0] = PointerGetDatum(cstring_to_text(name));
    1795           0 :         values[1] = Int64GetDatum(stat.blocks_zeroed);
    1796           0 :         values[2] = Int64GetDatum(stat.blocks_hit);
    1797           0 :         values[3] = Int64GetDatum(stat.blocks_read);
    1798           0 :         values[4] = Int64GetDatum(stat.blocks_written);
    1799           0 :         values[5] = Int64GetDatum(stat.blocks_exists);
    1800           0 :         values[6] = Int64GetDatum(stat.flush);
    1801           0 :         values[7] = Int64GetDatum(stat.truncate);
    1802           0 :         values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
    1803             : 
    1804           0 :         tuplestore_putvalues(tupstore, tupdesc, values, nulls);
    1805             :     }
    1806             : 
    1807             :     /* clean up and return the tuplestore */
    1808             :     tuplestore_donestoring(tupstore);
    1809             : 
    1810           0 :     return (Datum) 0;
    1811             : }
    1812             : 
    1813             : Datum
    1814           0 : pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
    1815             : {
    1816           0 :     Oid         relid = PG_GETARG_OID(0);
    1817             :     int64       result;
    1818             :     PgStat_TableStatus *tabentry;
    1819             : 
    1820           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1821           0 :         result = 0;
    1822             :     else
    1823           0 :         result = (int64) (tabentry->t_counts.t_numscans);
    1824             : 
    1825           0 :     PG_RETURN_INT64(result);
    1826             : }
    1827             : 
    1828             : Datum
    1829           0 : pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
    1830             : {
    1831           0 :     Oid         relid = PG_GETARG_OID(0);
    1832             :     int64       result;
    1833             :     PgStat_TableStatus *tabentry;
    1834             : 
    1835           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1836           0 :         result = 0;
    1837             :     else
    1838           0 :         result = (int64) (tabentry->t_counts.t_tuples_returned);
    1839             : 
    1840           0 :     PG_RETURN_INT64(result);
    1841             : }
    1842             : 
    1843             : Datum
    1844           0 : pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
    1845             : {
    1846           0 :     Oid         relid = PG_GETARG_OID(0);
    1847             :     int64       result;
    1848             :     PgStat_TableStatus *tabentry;
    1849             : 
    1850           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1851           0 :         result = 0;
    1852             :     else
    1853           0 :         result = (int64) (tabentry->t_counts.t_tuples_fetched);
    1854             : 
    1855           0 :     PG_RETURN_INT64(result);
    1856             : }
    1857             : 
    1858             : Datum
    1859           0 : pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
    1860             : {
    1861           0 :     Oid         relid = PG_GETARG_OID(0);
    1862             :     int64       result;
    1863             :     PgStat_TableStatus *tabentry;
    1864             :     PgStat_TableXactStatus *trans;
    1865             : 
    1866           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1867           0 :         result = 0;
    1868             :     else
    1869             :     {
    1870           0 :         result = tabentry->t_counts.t_tuples_inserted;
    1871             :         /* live subtransactions' counts aren't in t_tuples_inserted yet */
    1872           0 :         for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
    1873           0 :             result += trans->tuples_inserted;
    1874             :     }
    1875             : 
    1876           0 :     PG_RETURN_INT64(result);
    1877             : }
    1878             : 
    1879             : Datum
    1880           0 : pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
    1881             : {
    1882           0 :     Oid         relid = PG_GETARG_OID(0);
    1883             :     int64       result;
    1884             :     PgStat_TableStatus *tabentry;
    1885             :     PgStat_TableXactStatus *trans;
    1886             : 
    1887           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1888           0 :         result = 0;
    1889             :     else
    1890             :     {
    1891           0 :         result = tabentry->t_counts.t_tuples_updated;
    1892             :         /* live subtransactions' counts aren't in t_tuples_updated yet */
    1893           0 :         for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
    1894           0 :             result += trans->tuples_updated;
    1895             :     }
    1896             : 
    1897           0 :     PG_RETURN_INT64(result);
    1898             : }
    1899             : 
    1900             : Datum
    1901           0 : pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS)
    1902             : {
    1903           0 :     Oid         relid = PG_GETARG_OID(0);
    1904             :     int64       result;
    1905             :     PgStat_TableStatus *tabentry;
    1906             :     PgStat_TableXactStatus *trans;
    1907             : 
    1908           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1909           0 :         result = 0;
    1910             :     else
    1911             :     {
    1912           0 :         result = tabentry->t_counts.t_tuples_deleted;
    1913             :         /* live subtransactions' counts aren't in t_tuples_deleted yet */
    1914           0 :         for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
    1915           0 :             result += trans->tuples_deleted;
    1916             :     }
    1917             : 
    1918           0 :     PG_RETURN_INT64(result);
    1919             : }
    1920             : 
    1921             : Datum
    1922           0 : pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
    1923             : {
    1924           0 :     Oid         relid = PG_GETARG_OID(0);
    1925             :     int64       result;
    1926             :     PgStat_TableStatus *tabentry;
    1927             : 
    1928           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1929           0 :         result = 0;
    1930             :     else
    1931           0 :         result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
    1932             : 
    1933           0 :     PG_RETURN_INT64(result);
    1934             : }
    1935             : 
    1936             : Datum
    1937           0 : pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
    1938             : {
    1939           0 :     Oid         relid = PG_GETARG_OID(0);
    1940             :     int64       result;
    1941             :     PgStat_TableStatus *tabentry;
    1942             : 
    1943           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1944           0 :         result = 0;
    1945             :     else
    1946           0 :         result = (int64) (tabentry->t_counts.t_blocks_fetched);
    1947             : 
    1948           0 :     PG_RETURN_INT64(result);
    1949             : }
    1950             : 
    1951             : Datum
    1952           0 : pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
    1953             : {
    1954           0 :     Oid         relid = PG_GETARG_OID(0);
    1955             :     int64       result;
    1956             :     PgStat_TableStatus *tabentry;
    1957             : 
    1958           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1959           0 :         result = 0;
    1960             :     else
    1961           0 :         result = (int64) (tabentry->t_counts.t_blocks_hit);
    1962             : 
    1963           0 :     PG_RETURN_INT64(result);
    1964             : }
    1965             : 
    1966             : Datum
    1967           0 : pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
    1968             : {
    1969           0 :     Oid         funcid = PG_GETARG_OID(0);
    1970             :     PgStat_BackendFunctionEntry *funcentry;
    1971             : 
    1972           0 :     if ((funcentry = find_funcstat_entry(funcid)) == NULL)
    1973           0 :         PG_RETURN_NULL();
    1974           0 :     PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
    1975             : }
    1976             : 
    1977             : Datum
    1978           0 : pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
    1979             : {
    1980           0 :     Oid         funcid = PG_GETARG_OID(0);
    1981             :     PgStat_BackendFunctionEntry *funcentry;
    1982             : 
    1983           0 :     if ((funcentry = find_funcstat_entry(funcid)) == NULL)
    1984           0 :         PG_RETURN_NULL();
    1985           0 :     PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_total_time));
    1986             : }
    1987             : 
    1988             : Datum
    1989           0 : pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
    1990             : {
    1991           0 :     Oid         funcid = PG_GETARG_OID(0);
    1992             :     PgStat_BackendFunctionEntry *funcentry;
    1993             : 
    1994           0 :     if ((funcentry = find_funcstat_entry(funcid)) == NULL)
    1995           0 :         PG_RETURN_NULL();
    1996           0 :     PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_self_time));
    1997             : }
    1998             : 
    1999             : 
    2000             : /* Get the timestamp of the current statistics snapshot */
    2001             : Datum
    2002          28 : pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
    2003             : {
    2004          28 :     PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
    2005             : }
    2006             : 
    2007             : /* Discard the active statistics snapshot */
    2008             : Datum
    2009          40 : pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
    2010             : {
    2011          40 :     pgstat_clear_snapshot();
    2012             : 
    2013          40 :     PG_RETURN_VOID();
    2014             : }
    2015             : 
    2016             : 
    2017             : /* Reset all counters for the current database */
    2018             : Datum
    2019           0 : pg_stat_reset(PG_FUNCTION_ARGS)
    2020             : {
    2021           0 :     pgstat_reset_counters();
    2022             : 
    2023           0 :     PG_RETURN_VOID();
    2024             : }
    2025             : 
    2026             : /* Reset some shared cluster-wide counters */
    2027             : Datum
    2028           0 : pg_stat_reset_shared(PG_FUNCTION_ARGS)
    2029             : {
    2030           0 :     char       *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
    2031             : 
    2032           0 :     pgstat_reset_shared_counters(target);
    2033             : 
    2034           0 :     PG_RETURN_VOID();
    2035             : }
    2036             : 
    2037             : /* Reset a single counter in the current database */
    2038             : Datum
    2039           0 : pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
    2040             : {
    2041           0 :     Oid         taboid = PG_GETARG_OID(0);
    2042             : 
    2043           0 :     pgstat_reset_single_counter(taboid, RESET_TABLE);
    2044             : 
    2045           0 :     PG_RETURN_VOID();
    2046             : }
    2047             : 
    2048             : Datum
    2049           0 : pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
    2050             : {
    2051           0 :     Oid         funcoid = PG_GETARG_OID(0);
    2052             : 
    2053           0 :     pgstat_reset_single_counter(funcoid, RESET_FUNCTION);
    2054             : 
    2055           0 :     PG_RETURN_VOID();
    2056             : }
    2057             : 
    2058             : /* Reset SLRU counters (a specific one or all of them). */
    2059             : Datum
    2060           0 : pg_stat_reset_slru(PG_FUNCTION_ARGS)
    2061             : {
    2062           0 :     char       *target = NULL;
    2063             : 
    2064           0 :     if (!PG_ARGISNULL(0))
    2065           0 :         target = text_to_cstring(PG_GETARG_TEXT_PP(0));
    2066             : 
    2067           0 :     pgstat_reset_slru_counter(target);
    2068             : 
    2069           0 :     PG_RETURN_VOID();
    2070             : }
    2071             : 
    2072             : /* Reset replication slots stats (a specific one or all of them). */
    2073             : Datum
    2074           2 : pg_stat_reset_replication_slot(PG_FUNCTION_ARGS)
    2075             : {
    2076           2 :     char       *target = NULL;
    2077             : 
    2078           2 :     if (!PG_ARGISNULL(0))
    2079           2 :         target = text_to_cstring(PG_GETARG_TEXT_PP(0));
    2080             : 
    2081           2 :     pgstat_reset_replslot_counter(target);
    2082             : 
    2083           2 :     PG_RETURN_VOID();
    2084             : }
    2085             : 
    2086             : Datum
    2087          26 : pg_stat_get_archiver(PG_FUNCTION_ARGS)
    2088             : {
    2089             :     TupleDesc   tupdesc;
    2090             :     Datum       values[7];
    2091             :     bool        nulls[7];
    2092             :     PgStat_ArchiverStats *archiver_stats;
    2093             : 
    2094             :     /* Initialise values and NULL flags arrays */
    2095         208 :     MemSet(values, 0, sizeof(values));
    2096          26 :     MemSet(nulls, 0, sizeof(nulls));
    2097             : 
    2098             :     /* Initialise attributes information in the tuple descriptor */
    2099          26 :     tupdesc = CreateTemplateTupleDesc(7);
    2100          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
    2101             :                        INT8OID, -1, 0);
    2102          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
    2103             :                        TEXTOID, -1, 0);
    2104          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
    2105             :                        TIMESTAMPTZOID, -1, 0);
    2106          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
    2107             :                        INT8OID, -1, 0);
    2108          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
    2109             :                        TEXTOID, -1, 0);
    2110          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
    2111             :                        TIMESTAMPTZOID, -1, 0);
    2112          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
    2113             :                        TIMESTAMPTZOID, -1, 0);
    2114             : 
    2115          26 :     BlessTupleDesc(tupdesc);
    2116             : 
    2117             :     /* Get statistics about the archiver process */
    2118          26 :     archiver_stats = pgstat_fetch_stat_archiver();
    2119             : 
    2120             :     /* Fill values and NULLs */
    2121          26 :     values[0] = Int64GetDatum(archiver_stats->archived_count);
    2122          26 :     if (*(archiver_stats->last_archived_wal) == '\0')
    2123          10 :         nulls[1] = true;
    2124             :     else
    2125          16 :         values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
    2126             : 
    2127          26 :     if (archiver_stats->last_archived_timestamp == 0)
    2128          10 :         nulls[2] = true;
    2129             :     else
    2130          16 :         values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
    2131             : 
    2132          26 :     values[3] = Int64GetDatum(archiver_stats->failed_count);
    2133          26 :     if (*(archiver_stats->last_failed_wal) == '\0')
    2134           0 :         nulls[4] = true;
    2135             :     else
    2136          26 :         values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
    2137             : 
    2138          26 :     if (archiver_stats->last_failed_timestamp == 0)
    2139           0 :         nulls[5] = true;
    2140             :     else
    2141          26 :         values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
    2142             : 
    2143          26 :     if (archiver_stats->stat_reset_timestamp == 0)
    2144           0 :         nulls[6] = true;
    2145             :     else
    2146          26 :         values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
    2147             : 
    2148             :     /* Returns the record as Datum */
    2149          26 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
    2150             : }
    2151             : 
    2152             : /* Get the statistics for the replication slots */
    2153             : Datum
    2154          32 : pg_stat_get_replication_slots(PG_FUNCTION_ARGS)
    2155             : {
    2156             : #define PG_STAT_GET_REPLICATION_SLOT_COLS 8
    2157          32 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    2158             :     TupleDesc   tupdesc;
    2159             :     Tuplestorestate *tupstore;
    2160             :     MemoryContext per_query_ctx;
    2161             :     MemoryContext oldcontext;
    2162             :     PgStat_ReplSlotStats *slotstats;
    2163             :     int         nstats;
    2164             :     int         i;
    2165             : 
    2166             :     /* check to see if caller supports us returning a tuplestore */
    2167          32 :     if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
    2168           0 :         ereport(ERROR,
    2169             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2170             :                  errmsg("set-valued function called in context that cannot accept a set")));
    2171          32 :     if (!(rsinfo->allowedModes & SFRM_Materialize))
    2172           0 :         ereport(ERROR,
    2173             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2174             :                  errmsg("materialize mode required, but it is not allowed in this context")));
    2175             : 
    2176             :     /* Build a tuple descriptor for our result type */
    2177          32 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
    2178           0 :         elog(ERROR, "return type must be a row type");
    2179             : 
    2180          32 :     per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
    2181          32 :     oldcontext = MemoryContextSwitchTo(per_query_ctx);
    2182             : 
    2183          32 :     tupstore = tuplestore_begin_heap(true, false, work_mem);
    2184          32 :     rsinfo->returnMode = SFRM_Materialize;
    2185          32 :     rsinfo->setResult = tupstore;
    2186          32 :     rsinfo->setDesc = tupdesc;
    2187             : 
    2188          32 :     MemoryContextSwitchTo(oldcontext);
    2189             : 
    2190          32 :     slotstats = pgstat_fetch_replslot(&nstats);
    2191          64 :     for (i = 0; i < nstats; i++)
    2192             :     {
    2193             :         Datum       values[PG_STAT_GET_REPLICATION_SLOT_COLS];
    2194             :         bool        nulls[PG_STAT_GET_REPLICATION_SLOT_COLS];
    2195          32 :         PgStat_ReplSlotStats *s = &(slotstats[i]);
    2196             : 
    2197         288 :         MemSet(values, 0, sizeof(values));
    2198          32 :         MemSet(nulls, 0, sizeof(nulls));
    2199             : 
    2200          32 :         values[0] = PointerGetDatum(cstring_to_text(s->slotname));
    2201          32 :         values[1] = Int64GetDatum(s->spill_txns);
    2202          32 :         values[2] = Int64GetDatum(s->spill_count);
    2203          32 :         values[3] = Int64GetDatum(s->spill_bytes);
    2204          32 :         values[4] = Int64GetDatum(s->stream_txns);
    2205          32 :         values[5] = Int64GetDatum(s->stream_count);
    2206          32 :         values[6] = Int64GetDatum(s->stream_bytes);
    2207             : 
    2208          32 :         if (s->stat_reset_timestamp == 0)
    2209          14 :             nulls[7] = true;
    2210             :         else
    2211          18 :             values[7] = TimestampTzGetDatum(s->stat_reset_timestamp);
    2212             : 
    2213          32 :         tuplestore_putvalues(tupstore, tupdesc, values, nulls);
    2214             :     }
    2215             : 
    2216             :     tuplestore_donestoring(tupstore);
    2217             : 
    2218          32 :     return (Datum) 0;
    2219             : }

Generated by: LCOV version 1.13