LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pgstatfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 247 884 27.9 %
Date: 2020-05-29 00:07:09 Functions: 15 95 15.8 %
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         120 : pg_stat_get_numscans(PG_FUNCTION_ARGS)
      43             : {
      44         120 :     Oid         relid = PG_GETARG_OID(0);
      45             :     int64       result;
      46             :     PgStat_StatTabEntry *tabentry;
      47             : 
      48         120 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
      49           0 :         result = 0;
      50             :     else
      51         120 :         result = (int64) (tabentry->numscans);
      52             : 
      53         120 :     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          42 : pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
      91             : {
      92          42 :     Oid         relid = PG_GETARG_OID(0);
      93             :     int64       result;
      94             :     PgStat_StatTabEntry *tabentry;
      95             : 
      96          42 :     if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
      97          18 :         result = 0;
      98             :     else
      99          24 :         result = (int64) (tabentry->tuples_inserted);
     100             : 
     101          42 :     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         316 : pg_stat_get_activity(PG_FUNCTION_ARGS)
     569             : {
     570             : #define PG_STAT_GET_ACTIVITY_COLS   30
     571         316 :     int         num_backends = pgstat_fetch_stat_numbackends();
     572             :     int         curr_backend;
     573         316 :     int         pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
     574         316 :     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         316 :     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         316 :     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         316 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     592           0 :         elog(ERROR, "return type must be a row type");
     593             : 
     594         316 :     per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
     595         316 :     oldcontext = MemoryContextSwitchTo(per_query_ctx);
     596             : 
     597         316 :     tupstore = tuplestore_begin_heap(true, false, work_mem);
     598         316 :     rsinfo->returnMode = SFRM_Materialize;
     599         316 :     rsinfo->setResult = tupstore;
     600         316 :     rsinfo->setDesc = tupdesc;
     601             : 
     602         316 :     MemoryContextSwitchTo(oldcontext);
     603             : 
     604             :     /* 1-based index */
     605        2668 :     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        2352 :         const char *wait_event_type = NULL;
     614        2352 :         const char *wait_event = NULL;
     615             : 
     616       72912 :         MemSet(values, 0, sizeof(values));
     617        2352 :         MemSet(nulls, 0, sizeof(nulls));
     618             : 
     619             :         /* Get the next one in the list */
     620        2352 :         local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
     621        2352 :         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        2352 :         beentry = &local_beentry->backendStatus;
     640             : 
     641             :         /* If looking for specific PID, ignore all the others */
     642        2352 :         if (pid != -1 && beentry->st_procpid != pid)
     643           0 :             continue;
     644             : 
     645             :         /* Values available to all callers */
     646        2352 :         if (beentry->st_databaseid != InvalidOid)
     647         600 :             values[0] = ObjectIdGetDatum(beentry->st_databaseid);
     648             :         else
     649        1752 :             nulls[0] = true;
     650             : 
     651        2352 :         values[1] = Int32GetDatum(beentry->st_procpid);
     652             : 
     653        2352 :         if (beentry->st_userid != InvalidOid)
     654        1092 :             values[2] = ObjectIdGetDatum(beentry->st_userid);
     655             :         else
     656        1260 :             nulls[2] = true;
     657             : 
     658        2352 :         if (beentry->st_appname)
     659        2352 :             values[3] = CStringGetTextDatum(beentry->st_appname);
     660             :         else
     661           0 :             nulls[3] = true;
     662             : 
     663        2352 :         if (TransactionIdIsValid(local_beentry->backend_xid))
     664           8 :             values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
     665             :         else
     666        2344 :             nulls[15] = true;
     667             : 
     668        2352 :         if (TransactionIdIsValid(local_beentry->backend_xmin))
     669         326 :             values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
     670             :         else
     671        2026 :             nulls[16] = true;
     672             : 
     673             :         /* Values only available to role member or pg_read_all_stats */
     674        2352 :         if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
     675        2320 :         {
     676             :             SockAddr    zero_clientaddr;
     677             :             char       *clipped_activity;
     678             : 
     679        2320 :             switch (beentry->st_state)
     680             :             {
     681          10 :                 case STATE_IDLE:
     682          10 :                     values[4] = CStringGetTextDatum("idle");
     683          10 :                     break;
     684         770 :                 case STATE_RUNNING:
     685         770 :                     values[4] = CStringGetTextDatum("active");
     686         770 :                     break;
     687           2 :                 case STATE_IDLEINTRANSACTION:
     688           2 :                     values[4] = CStringGetTextDatum("idle in transaction");
     689           2 :                     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        1538 :                 case STATE_UNDEFINED:
     700        1538 :                     nulls[4] = true;
     701        1538 :                     break;
     702             :             }
     703             : 
     704        2320 :             clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
     705        2320 :             values[5] = CStringGetTextDatum(clipped_activity);
     706        2320 :             pfree(clipped_activity);
     707             : 
     708             :             /* leader_pid */
     709        2320 :             nulls[29] = true;
     710             : 
     711        2320 :             proc = BackendPidGetProc(beentry->st_procpid);
     712             : 
     713        2320 :             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         938 :                 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        2320 :             if (proc != NULL)
     729             :             {
     730             :                 uint32      raw_wait_event;
     731             :                 PGPROC     *leader;
     732             : 
     733        2320 :                 raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
     734        2320 :                 wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
     735        2320 :                 wait_event = pgstat_get_wait_event(raw_wait_event);
     736             : 
     737        2320 :                 leader = proc->lockGroupLeader;
     738        2320 :                 if (leader)
     739             :                 {
     740           0 :                     values[29] = Int32GetDatum(leader->pid);
     741           0 :                     nulls[29] = false;
     742             :                 }
     743             :             }
     744             : 
     745        2320 :             if (wait_event_type)
     746        1996 :                 values[6] = CStringGetTextDatum(wait_event_type);
     747             :             else
     748         324 :                 nulls[6] = true;
     749             : 
     750        2320 :             if (wait_event)
     751        1996 :                 values[7] = CStringGetTextDatum(wait_event);
     752             :             else
     753         324 :                 nulls[7] = true;
     754             : 
     755             :             /*
     756             :              * Don't expose transaction time for walsenders; it confuses
     757             :              * monitoring, particularly because we don't keep the time up-to-
     758             :              * date.
     759             :              */
     760        2320 :             if (beentry->st_xact_start_timestamp != 0 &&
     761         326 :                 beentry->st_backendType != B_WAL_SENDER)
     762         326 :                 values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
     763             :             else
     764        1994 :                 nulls[8] = true;
     765             : 
     766        2320 :             if (beentry->st_activity_start_timestamp != 0)
     767         336 :                 values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
     768             :             else
     769        1984 :                 nulls[9] = true;
     770             : 
     771        2320 :             if (beentry->st_proc_start_timestamp != 0)
     772        2320 :                 values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
     773             :             else
     774           0 :                 nulls[10] = true;
     775             : 
     776        2320 :             if (beentry->st_state_start_timestamp != 0)
     777         782 :                 values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
     778             :             else
     779        1538 :                 nulls[11] = true;
     780             : 
     781             :             /* A zeroed client addr means we don't know */
     782        2320 :             memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
     783        2320 :             if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
     784             :                        sizeof(zero_clientaddr)) == 0)
     785             :             {
     786        1538 :                 nulls[12] = true;
     787        1538 :                 nulls[13] = true;
     788        1538 :                 nulls[14] = true;
     789             :             }
     790             :             else
     791             :             {
     792         782 :                 if (beentry->st_clientaddr.addr.ss_family == AF_INET
     793             : #ifdef HAVE_IPV6
     794         778 :                     || beentry->st_clientaddr.addr.ss_family == AF_INET6
     795             : #endif
     796             :                     )
     797           4 :                 {
     798             :                     char        remote_host[NI_MAXHOST];
     799             :                     char        remote_port[NI_MAXSERV];
     800             :                     int         ret;
     801             : 
     802           4 :                     remote_host[0] = '\0';
     803           4 :                     remote_port[0] = '\0';
     804           4 :                     ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
     805           4 :                                              beentry->st_clientaddr.salen,
     806             :                                              remote_host, sizeof(remote_host),
     807             :                                              remote_port, sizeof(remote_port),
     808             :                                              NI_NUMERICHOST | NI_NUMERICSERV);
     809           4 :                     if (ret == 0)
     810             :                     {
     811           4 :                         clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
     812           4 :                         values[12] = DirectFunctionCall1(inet_in,
     813             :                                                          CStringGetDatum(remote_host));
     814           4 :                         if (beentry->st_clienthostname &&
     815           4 :                             beentry->st_clienthostname[0])
     816           4 :                             values[13] = CStringGetTextDatum(beentry->st_clienthostname);
     817             :                         else
     818           0 :                             nulls[13] = true;
     819           4 :                         values[14] = Int32GetDatum(atoi(remote_port));
     820             :                     }
     821             :                     else
     822             :                     {
     823           0 :                         nulls[12] = true;
     824           0 :                         nulls[13] = true;
     825           0 :                         nulls[14] = true;
     826             :                     }
     827             :                 }
     828         778 :                 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
     829             :                 {
     830             :                     /*
     831             :                      * Unix sockets always reports NULL for host and -1 for
     832             :                      * port, so it's possible to tell the difference to
     833             :                      * connections we have no permissions to view, or with
     834             :                      * errors.
     835             :                      */
     836         778 :                     nulls[12] = true;
     837         778 :                     nulls[13] = true;
     838         778 :                     values[14] = Int32GetDatum(-1);
     839             :                 }
     840             :                 else
     841             :                 {
     842             :                     /* Unknown address type, should never happen */
     843           0 :                     nulls[12] = true;
     844           0 :                     nulls[13] = true;
     845           0 :                     nulls[14] = true;
     846             :                 }
     847             :             }
     848             :             /* Add backend type */
     849        2320 :             if (beentry->st_backendType == B_BG_WORKER)
     850             :             {
     851             :                 const char *bgw_type;
     852             : 
     853         302 :                 bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
     854         302 :                 if (bgw_type)
     855         302 :                     values[17] = CStringGetTextDatum(bgw_type);
     856             :                 else
     857           0 :                     nulls[17] = true;
     858             :             }
     859             :             else
     860        2018 :                 values[17] =
     861        2018 :                     CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType));
     862             : 
     863             :             /* SSL information */
     864        2320 :             if (beentry->st_ssl)
     865             :             {
     866           4 :                 values[18] = BoolGetDatum(true);    /* ssl */
     867           4 :                 values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
     868           4 :                 values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
     869           4 :                 values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
     870           4 :                 values[22] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
     871             : 
     872           4 :                 if (beentry->st_sslstatus->ssl_client_dn[0])
     873           2 :                     values[23] = CStringGetTextDatum(beentry->st_sslstatus->ssl_client_dn);
     874             :                 else
     875           2 :                     nulls[23] = true;
     876             : 
     877           4 :                 if (beentry->st_sslstatus->ssl_client_serial[0])
     878           2 :                     values[24] = DirectFunctionCall3(numeric_in,
     879             :                                                      CStringGetDatum(beentry->st_sslstatus->ssl_client_serial),
     880             :                                                      ObjectIdGetDatum(InvalidOid),
     881             :                                                      Int32GetDatum(-1));
     882             :                 else
     883           2 :                     nulls[24] = true;
     884             : 
     885           4 :                 if (beentry->st_sslstatus->ssl_issuer_dn[0])
     886           2 :                     values[25] = CStringGetTextDatum(beentry->st_sslstatus->ssl_issuer_dn);
     887             :                 else
     888           2 :                     nulls[25] = true;
     889             :             }
     890             :             else
     891             :             {
     892        2316 :                 values[18] = BoolGetDatum(false);   /* ssl */
     893        2316 :                 nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = nulls[25] = true;
     894             :             }
     895             : 
     896             :             /* GSSAPI information */
     897        2320 :             if (beentry->st_gss)
     898             :             {
     899           0 :                 values[26] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
     900           0 :                 values[27] = CStringGetTextDatum(beentry->st_gssstatus->gss_princ);
     901           0 :                 values[28] = BoolGetDatum(beentry->st_gssstatus->gss_enc);    /* GSS Encryption in use */
     902             :             }
     903             :             else
     904             :             {
     905        2320 :                 values[26] = BoolGetDatum(false);   /* gss_auth */
     906        2320 :                 nulls[27] = true;   /* No GSS principal */
     907        2320 :                 values[28] = BoolGetDatum(false);   /* GSS Encryption not in
     908             :                                                      * use */
     909             :             }
     910             :         }
     911             :         else
     912             :         {
     913             :             /* No permissions to view data about this session */
     914          32 :             values[5] = CStringGetTextDatum("<insufficient privilege>");
     915          32 :             nulls[4] = true;
     916          32 :             nulls[6] = true;
     917          32 :             nulls[7] = true;
     918          32 :             nulls[8] = true;
     919          32 :             nulls[9] = true;
     920          32 :             nulls[10] = true;
     921          32 :             nulls[11] = true;
     922          32 :             nulls[12] = true;
     923          32 :             nulls[13] = true;
     924          32 :             nulls[14] = true;
     925          32 :             nulls[17] = true;
     926          32 :             nulls[18] = true;
     927          32 :             nulls[19] = true;
     928          32 :             nulls[20] = true;
     929          32 :             nulls[21] = true;
     930          32 :             nulls[22] = true;
     931          32 :             nulls[23] = true;
     932          32 :             nulls[24] = true;
     933          32 :             nulls[25] = true;
     934          32 :             nulls[26] = true;
     935          32 :             nulls[27] = true;
     936          32 :             nulls[28] = true;
     937          32 :             nulls[29] = true;
     938             :         }
     939             : 
     940        2352 :         tuplestore_putvalues(tupstore, tupdesc, values, nulls);
     941             : 
     942             :         /* If only a single backend was requested, and we found it, break. */
     943        2352 :         if (pid != -1)
     944           0 :             break;
     945             :     }
     946             : 
     947             :     /* clean up and return the tuplestore */
     948             :     tuplestore_donestoring(tupstore);
     949             : 
     950         316 :     return (Datum) 0;
     951             : }
     952             : 
     953             : 
     954             : Datum
     955          26 : pg_backend_pid(PG_FUNCTION_ARGS)
     956             : {
     957          26 :     PG_RETURN_INT32(MyProcPid);
     958             : }
     959             : 
     960             : 
     961             : Datum
     962           0 : pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
     963             : {
     964           0 :     int32       beid = PG_GETARG_INT32(0);
     965             :     PgBackendStatus *beentry;
     966             : 
     967           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
     968           0 :         PG_RETURN_NULL();
     969             : 
     970           0 :     PG_RETURN_INT32(beentry->st_procpid);
     971             : }
     972             : 
     973             : 
     974             : Datum
     975           0 : pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
     976             : {
     977           0 :     int32       beid = PG_GETARG_INT32(0);
     978             :     PgBackendStatus *beentry;
     979             : 
     980           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
     981           0 :         PG_RETURN_NULL();
     982             : 
     983           0 :     PG_RETURN_OID(beentry->st_databaseid);
     984             : }
     985             : 
     986             : 
     987             : Datum
     988           0 : pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
     989             : {
     990           0 :     int32       beid = PG_GETARG_INT32(0);
     991             :     PgBackendStatus *beentry;
     992             : 
     993           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
     994           0 :         PG_RETURN_NULL();
     995             : 
     996           0 :     PG_RETURN_OID(beentry->st_userid);
     997             : }
     998             : 
     999             : 
    1000             : Datum
    1001           0 : pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
    1002             : {
    1003           0 :     int32       beid = PG_GETARG_INT32(0);
    1004             :     PgBackendStatus *beentry;
    1005             :     const char *activity;
    1006             :     char       *clipped_activity;
    1007             :     text       *ret;
    1008             : 
    1009           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1010           0 :         activity = "<backend information not available>";
    1011           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1012           0 :         activity = "<insufficient privilege>";
    1013           0 :     else if (*(beentry->st_activity_raw) == '\0')
    1014           0 :         activity = "<command string not enabled>";
    1015             :     else
    1016           0 :         activity = beentry->st_activity_raw;
    1017             : 
    1018           0 :     clipped_activity = pgstat_clip_activity(activity);
    1019           0 :     ret = cstring_to_text(activity);
    1020           0 :     pfree(clipped_activity);
    1021             : 
    1022           0 :     PG_RETURN_TEXT_P(ret);
    1023             : }
    1024             : 
    1025             : Datum
    1026           0 : pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
    1027             : {
    1028           0 :     int32       beid = PG_GETARG_INT32(0);
    1029             :     PgBackendStatus *beentry;
    1030             :     PGPROC     *proc;
    1031           0 :     const char *wait_event_type = NULL;
    1032             : 
    1033           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1034           0 :         wait_event_type = "<backend information not available>";
    1035           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1036           0 :         wait_event_type = "<insufficient privilege>";
    1037           0 :     else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
    1038           0 :         wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
    1039             : 
    1040           0 :     if (!wait_event_type)
    1041           0 :         PG_RETURN_NULL();
    1042             : 
    1043           0 :     PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
    1044             : }
    1045             : 
    1046             : Datum
    1047           0 : pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
    1048             : {
    1049           0 :     int32       beid = PG_GETARG_INT32(0);
    1050             :     PgBackendStatus *beentry;
    1051             :     PGPROC     *proc;
    1052           0 :     const char *wait_event = NULL;
    1053             : 
    1054           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1055           0 :         wait_event = "<backend information not available>";
    1056           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1057           0 :         wait_event = "<insufficient privilege>";
    1058           0 :     else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
    1059           0 :         wait_event = pgstat_get_wait_event(proc->wait_event_info);
    1060             : 
    1061           0 :     if (!wait_event)
    1062           0 :         PG_RETURN_NULL();
    1063             : 
    1064           0 :     PG_RETURN_TEXT_P(cstring_to_text(wait_event));
    1065             : }
    1066             : 
    1067             : 
    1068             : Datum
    1069           0 : pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
    1070             : {
    1071           0 :     int32       beid = PG_GETARG_INT32(0);
    1072             :     TimestampTz result;
    1073             :     PgBackendStatus *beentry;
    1074             : 
    1075           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1076           0 :         PG_RETURN_NULL();
    1077             : 
    1078           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1079           0 :         PG_RETURN_NULL();
    1080             : 
    1081           0 :     result = beentry->st_activity_start_timestamp;
    1082             : 
    1083             :     /*
    1084             :      * No time recorded for start of current query -- this is the case if the
    1085             :      * user hasn't enabled query-level stats collection.
    1086             :      */
    1087           0 :     if (result == 0)
    1088           0 :         PG_RETURN_NULL();
    1089             : 
    1090           0 :     PG_RETURN_TIMESTAMPTZ(result);
    1091             : }
    1092             : 
    1093             : 
    1094             : Datum
    1095           0 : pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
    1096             : {
    1097           0 :     int32       beid = PG_GETARG_INT32(0);
    1098             :     TimestampTz result;
    1099             :     PgBackendStatus *beentry;
    1100             : 
    1101           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1102           0 :         PG_RETURN_NULL();
    1103             : 
    1104           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1105           0 :         PG_RETURN_NULL();
    1106             : 
    1107           0 :     result = beentry->st_xact_start_timestamp;
    1108             : 
    1109           0 :     if (result == 0)            /* not in a transaction */
    1110           0 :         PG_RETURN_NULL();
    1111             : 
    1112           0 :     PG_RETURN_TIMESTAMPTZ(result);
    1113             : }
    1114             : 
    1115             : 
    1116             : Datum
    1117           0 : pg_stat_get_backend_start(PG_FUNCTION_ARGS)
    1118             : {
    1119           0 :     int32       beid = PG_GETARG_INT32(0);
    1120             :     TimestampTz result;
    1121             :     PgBackendStatus *beentry;
    1122             : 
    1123           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1124           0 :         PG_RETURN_NULL();
    1125             : 
    1126           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1127           0 :         PG_RETURN_NULL();
    1128             : 
    1129           0 :     result = beentry->st_proc_start_timestamp;
    1130             : 
    1131           0 :     if (result == 0)            /* probably can't happen? */
    1132           0 :         PG_RETURN_NULL();
    1133             : 
    1134           0 :     PG_RETURN_TIMESTAMPTZ(result);
    1135             : }
    1136             : 
    1137             : 
    1138             : Datum
    1139           0 : pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
    1140             : {
    1141           0 :     int32       beid = PG_GETARG_INT32(0);
    1142             :     PgBackendStatus *beentry;
    1143             :     SockAddr    zero_clientaddr;
    1144             :     char        remote_host[NI_MAXHOST];
    1145             :     int         ret;
    1146             : 
    1147           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1148           0 :         PG_RETURN_NULL();
    1149             : 
    1150           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1151           0 :         PG_RETURN_NULL();
    1152             : 
    1153             :     /* A zeroed client addr means we don't know */
    1154           0 :     memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
    1155           0 :     if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
    1156             :                sizeof(zero_clientaddr)) == 0)
    1157           0 :         PG_RETURN_NULL();
    1158             : 
    1159           0 :     switch (beentry->st_clientaddr.addr.ss_family)
    1160             :     {
    1161           0 :         case AF_INET:
    1162             : #ifdef HAVE_IPV6
    1163             :         case AF_INET6:
    1164             : #endif
    1165           0 :             break;
    1166           0 :         default:
    1167           0 :             PG_RETURN_NULL();
    1168             :     }
    1169             : 
    1170           0 :     remote_host[0] = '\0';
    1171           0 :     ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
    1172           0 :                              beentry->st_clientaddr.salen,
    1173             :                              remote_host, sizeof(remote_host),
    1174             :                              NULL, 0,
    1175             :                              NI_NUMERICHOST | NI_NUMERICSERV);
    1176           0 :     if (ret != 0)
    1177           0 :         PG_RETURN_NULL();
    1178             : 
    1179           0 :     clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
    1180             : 
    1181           0 :     PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
    1182             :                                          CStringGetDatum(remote_host)));
    1183             : }
    1184             : 
    1185             : Datum
    1186           0 : pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
    1187             : {
    1188           0 :     int32       beid = PG_GETARG_INT32(0);
    1189             :     PgBackendStatus *beentry;
    1190             :     SockAddr    zero_clientaddr;
    1191             :     char        remote_port[NI_MAXSERV];
    1192             :     int         ret;
    1193             : 
    1194           0 :     if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
    1195           0 :         PG_RETURN_NULL();
    1196             : 
    1197           0 :     else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
    1198           0 :         PG_RETURN_NULL();
    1199             : 
    1200             :     /* A zeroed client addr means we don't know */
    1201           0 :     memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
    1202           0 :     if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
    1203             :                sizeof(zero_clientaddr)) == 0)
    1204           0 :         PG_RETURN_NULL();
    1205             : 
    1206           0 :     switch (beentry->st_clientaddr.addr.ss_family)
    1207             :     {
    1208           0 :         case AF_INET:
    1209             : #ifdef HAVE_IPV6
    1210             :         case AF_INET6:
    1211             : #endif
    1212           0 :             break;
    1213           0 :         case AF_UNIX:
    1214           0 :             PG_RETURN_INT32(-1);
    1215           0 :         default:
    1216           0 :             PG_RETURN_NULL();
    1217             :     }
    1218             : 
    1219           0 :     remote_port[0] = '\0';
    1220           0 :     ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
    1221           0 :                              beentry->st_clientaddr.salen,
    1222             :                              NULL, 0,
    1223             :                              remote_port, sizeof(remote_port),
    1224             :                              NI_NUMERICHOST | NI_NUMERICSERV);
    1225           0 :     if (ret != 0)
    1226           0 :         PG_RETURN_NULL();
    1227             : 
    1228           0 :     PG_RETURN_DATUM(DirectFunctionCall1(int4in,
    1229             :                                         CStringGetDatum(remote_port)));
    1230             : }
    1231             : 
    1232             : 
    1233             : Datum
    1234           0 : pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
    1235             : {
    1236           0 :     Oid         dbid = PG_GETARG_OID(0);
    1237             :     int32       result;
    1238           0 :     int         tot_backends = pgstat_fetch_stat_numbackends();
    1239             :     int         beid;
    1240             : 
    1241           0 :     result = 0;
    1242           0 :     for (beid = 1; beid <= tot_backends; beid++)
    1243             :     {
    1244           0 :         PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
    1245             : 
    1246           0 :         if (beentry && beentry->st_databaseid == dbid)
    1247           0 :             result++;
    1248             :     }
    1249             : 
    1250           0 :     PG_RETURN_INT32(result);
    1251             : }
    1252             : 
    1253             : 
    1254             : Datum
    1255           0 : pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
    1256             : {
    1257           0 :     Oid         dbid = PG_GETARG_OID(0);
    1258             :     int64       result;
    1259             :     PgStat_StatDBEntry *dbentry;
    1260             : 
    1261           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1262           0 :         result = 0;
    1263             :     else
    1264           0 :         result = (int64) (dbentry->n_xact_commit);
    1265             : 
    1266           0 :     PG_RETURN_INT64(result);
    1267             : }
    1268             : 
    1269             : 
    1270             : Datum
    1271           0 : pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
    1272             : {
    1273           0 :     Oid         dbid = PG_GETARG_OID(0);
    1274             :     int64       result;
    1275             :     PgStat_StatDBEntry *dbentry;
    1276             : 
    1277           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1278           0 :         result = 0;
    1279             :     else
    1280           0 :         result = (int64) (dbentry->n_xact_rollback);
    1281             : 
    1282           0 :     PG_RETURN_INT64(result);
    1283             : }
    1284             : 
    1285             : 
    1286             : Datum
    1287           0 : pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
    1288             : {
    1289           0 :     Oid         dbid = PG_GETARG_OID(0);
    1290             :     int64       result;
    1291             :     PgStat_StatDBEntry *dbentry;
    1292             : 
    1293           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1294           0 :         result = 0;
    1295             :     else
    1296           0 :         result = (int64) (dbentry->n_blocks_fetched);
    1297             : 
    1298           0 :     PG_RETURN_INT64(result);
    1299             : }
    1300             : 
    1301             : 
    1302             : Datum
    1303           0 : pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
    1304             : {
    1305           0 :     Oid         dbid = PG_GETARG_OID(0);
    1306             :     int64       result;
    1307             :     PgStat_StatDBEntry *dbentry;
    1308             : 
    1309           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1310           0 :         result = 0;
    1311             :     else
    1312           0 :         result = (int64) (dbentry->n_blocks_hit);
    1313             : 
    1314           0 :     PG_RETURN_INT64(result);
    1315             : }
    1316             : 
    1317             : 
    1318             : Datum
    1319           0 : pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
    1320             : {
    1321           0 :     Oid         dbid = PG_GETARG_OID(0);
    1322             :     int64       result;
    1323             :     PgStat_StatDBEntry *dbentry;
    1324             : 
    1325           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1326           0 :         result = 0;
    1327             :     else
    1328           0 :         result = (int64) (dbentry->n_tuples_returned);
    1329             : 
    1330           0 :     PG_RETURN_INT64(result);
    1331             : }
    1332             : 
    1333             : 
    1334             : Datum
    1335           0 : pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
    1336             : {
    1337           0 :     Oid         dbid = PG_GETARG_OID(0);
    1338             :     int64       result;
    1339             :     PgStat_StatDBEntry *dbentry;
    1340             : 
    1341           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1342           0 :         result = 0;
    1343             :     else
    1344           0 :         result = (int64) (dbentry->n_tuples_fetched);
    1345             : 
    1346           0 :     PG_RETURN_INT64(result);
    1347             : }
    1348             : 
    1349             : 
    1350             : Datum
    1351           0 : pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
    1352             : {
    1353           0 :     Oid         dbid = PG_GETARG_OID(0);
    1354             :     int64       result;
    1355             :     PgStat_StatDBEntry *dbentry;
    1356             : 
    1357           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1358           0 :         result = 0;
    1359             :     else
    1360           0 :         result = (int64) (dbentry->n_tuples_inserted);
    1361             : 
    1362           0 :     PG_RETURN_INT64(result);
    1363             : }
    1364             : 
    1365             : 
    1366             : Datum
    1367           0 : pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
    1368             : {
    1369           0 :     Oid         dbid = PG_GETARG_OID(0);
    1370             :     int64       result;
    1371             :     PgStat_StatDBEntry *dbentry;
    1372             : 
    1373           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1374           0 :         result = 0;
    1375             :     else
    1376           0 :         result = (int64) (dbentry->n_tuples_updated);
    1377             : 
    1378           0 :     PG_RETURN_INT64(result);
    1379             : }
    1380             : 
    1381             : 
    1382             : Datum
    1383           0 : pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
    1384             : {
    1385           0 :     Oid         dbid = PG_GETARG_OID(0);
    1386             :     int64       result;
    1387             :     PgStat_StatDBEntry *dbentry;
    1388             : 
    1389           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1390           0 :         result = 0;
    1391             :     else
    1392           0 :         result = (int64) (dbentry->n_tuples_deleted);
    1393             : 
    1394           0 :     PG_RETURN_INT64(result);
    1395             : }
    1396             : 
    1397             : Datum
    1398           0 : pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
    1399             : {
    1400           0 :     Oid         dbid = PG_GETARG_OID(0);
    1401             :     TimestampTz result;
    1402             :     PgStat_StatDBEntry *dbentry;
    1403             : 
    1404           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1405           0 :         result = 0;
    1406             :     else
    1407           0 :         result = dbentry->stat_reset_timestamp;
    1408             : 
    1409           0 :     if (result == 0)
    1410           0 :         PG_RETURN_NULL();
    1411             :     else
    1412           0 :         PG_RETURN_TIMESTAMPTZ(result);
    1413             : }
    1414             : 
    1415             : Datum
    1416           0 : pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
    1417             : {
    1418           0 :     Oid         dbid = PG_GETARG_OID(0);
    1419             :     int64       result;
    1420             :     PgStat_StatDBEntry *dbentry;
    1421             : 
    1422           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1423           0 :         result = 0;
    1424             :     else
    1425           0 :         result = dbentry->n_temp_files;
    1426             : 
    1427           0 :     PG_RETURN_INT64(result);
    1428             : }
    1429             : 
    1430             : 
    1431             : Datum
    1432           0 : pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
    1433             : {
    1434           0 :     Oid         dbid = PG_GETARG_OID(0);
    1435             :     int64       result;
    1436             :     PgStat_StatDBEntry *dbentry;
    1437             : 
    1438           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1439           0 :         result = 0;
    1440             :     else
    1441           0 :         result = dbentry->n_temp_bytes;
    1442             : 
    1443           0 :     PG_RETURN_INT64(result);
    1444             : }
    1445             : 
    1446             : Datum
    1447           0 : pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
    1448             : {
    1449           0 :     Oid         dbid = PG_GETARG_OID(0);
    1450             :     int64       result;
    1451             :     PgStat_StatDBEntry *dbentry;
    1452             : 
    1453           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1454           0 :         result = 0;
    1455             :     else
    1456           0 :         result = (int64) (dbentry->n_conflict_tablespace);
    1457             : 
    1458           0 :     PG_RETURN_INT64(result);
    1459             : }
    1460             : 
    1461             : Datum
    1462           0 : pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
    1463             : {
    1464           0 :     Oid         dbid = PG_GETARG_OID(0);
    1465             :     int64       result;
    1466             :     PgStat_StatDBEntry *dbentry;
    1467             : 
    1468           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1469           0 :         result = 0;
    1470             :     else
    1471           0 :         result = (int64) (dbentry->n_conflict_lock);
    1472             : 
    1473           0 :     PG_RETURN_INT64(result);
    1474             : }
    1475             : 
    1476             : Datum
    1477           0 : pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS)
    1478             : {
    1479           0 :     Oid         dbid = PG_GETARG_OID(0);
    1480             :     int64       result;
    1481             :     PgStat_StatDBEntry *dbentry;
    1482             : 
    1483           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1484           0 :         result = 0;
    1485             :     else
    1486           0 :         result = (int64) (dbentry->n_conflict_snapshot);
    1487             : 
    1488           0 :     PG_RETURN_INT64(result);
    1489             : }
    1490             : 
    1491             : Datum
    1492           0 : pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
    1493             : {
    1494           0 :     Oid         dbid = PG_GETARG_OID(0);
    1495             :     int64       result;
    1496             :     PgStat_StatDBEntry *dbentry;
    1497             : 
    1498           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1499           0 :         result = 0;
    1500             :     else
    1501           0 :         result = (int64) (dbentry->n_conflict_bufferpin);
    1502             : 
    1503           0 :     PG_RETURN_INT64(result);
    1504             : }
    1505             : 
    1506             : Datum
    1507           0 : pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
    1508             : {
    1509           0 :     Oid         dbid = PG_GETARG_OID(0);
    1510             :     int64       result;
    1511             :     PgStat_StatDBEntry *dbentry;
    1512             : 
    1513           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1514           0 :         result = 0;
    1515             :     else
    1516           0 :         result = (int64) (dbentry->n_conflict_startup_deadlock);
    1517             : 
    1518           0 :     PG_RETURN_INT64(result);
    1519             : }
    1520             : 
    1521             : Datum
    1522           0 : pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
    1523             : {
    1524           0 :     Oid         dbid = PG_GETARG_OID(0);
    1525             :     int64       result;
    1526             :     PgStat_StatDBEntry *dbentry;
    1527             : 
    1528           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1529           0 :         result = 0;
    1530             :     else
    1531           0 :         result = (int64) (dbentry->n_conflict_tablespace +
    1532           0 :                           dbentry->n_conflict_lock +
    1533           0 :                           dbentry->n_conflict_snapshot +
    1534           0 :                           dbentry->n_conflict_bufferpin +
    1535           0 :                           dbentry->n_conflict_startup_deadlock);
    1536             : 
    1537           0 :     PG_RETURN_INT64(result);
    1538             : }
    1539             : 
    1540             : Datum
    1541           0 : pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
    1542             : {
    1543           0 :     Oid         dbid = PG_GETARG_OID(0);
    1544             :     int64       result;
    1545             :     PgStat_StatDBEntry *dbentry;
    1546             : 
    1547           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1548           0 :         result = 0;
    1549             :     else
    1550           0 :         result = (int64) (dbentry->n_deadlocks);
    1551             : 
    1552           0 :     PG_RETURN_INT64(result);
    1553             : }
    1554             : 
    1555             : Datum
    1556           0 : pg_stat_get_db_checksum_failures(PG_FUNCTION_ARGS)
    1557             : {
    1558           0 :     Oid         dbid = PG_GETARG_OID(0);
    1559             :     int64       result;
    1560             :     PgStat_StatDBEntry *dbentry;
    1561             : 
    1562           0 :     if (!DataChecksumsEnabled())
    1563           0 :         PG_RETURN_NULL();
    1564             : 
    1565           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1566           0 :         result = 0;
    1567             :     else
    1568           0 :         result = (int64) (dbentry->n_checksum_failures);
    1569             : 
    1570           0 :     PG_RETURN_INT64(result);
    1571             : }
    1572             : 
    1573             : Datum
    1574           0 : pg_stat_get_db_checksum_last_failure(PG_FUNCTION_ARGS)
    1575             : {
    1576           0 :     Oid         dbid = PG_GETARG_OID(0);
    1577             :     TimestampTz result;
    1578             :     PgStat_StatDBEntry *dbentry;
    1579             : 
    1580           0 :     if (!DataChecksumsEnabled())
    1581           0 :         PG_RETURN_NULL();
    1582             : 
    1583           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1584           0 :         result = 0;
    1585             :     else
    1586           0 :         result = dbentry->last_checksum_failure;
    1587             : 
    1588           0 :     if (result == 0)
    1589           0 :         PG_RETURN_NULL();
    1590             :     else
    1591           0 :         PG_RETURN_TIMESTAMPTZ(result);
    1592             : }
    1593             : 
    1594             : Datum
    1595           0 : pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
    1596             : {
    1597           0 :     Oid         dbid = PG_GETARG_OID(0);
    1598             :     double      result;
    1599             :     PgStat_StatDBEntry *dbentry;
    1600             : 
    1601             :     /* convert counter from microsec to millisec for display */
    1602           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1603           0 :         result = 0;
    1604             :     else
    1605           0 :         result = ((double) dbentry->n_block_read_time) / 1000.0;
    1606             : 
    1607           0 :     PG_RETURN_FLOAT8(result);
    1608             : }
    1609             : 
    1610             : Datum
    1611           0 : pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
    1612             : {
    1613           0 :     Oid         dbid = PG_GETARG_OID(0);
    1614             :     double      result;
    1615             :     PgStat_StatDBEntry *dbentry;
    1616             : 
    1617             :     /* convert counter from microsec to millisec for display */
    1618           0 :     if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
    1619           0 :         result = 0;
    1620             :     else
    1621           0 :         result = ((double) dbentry->n_block_write_time) / 1000.0;
    1622             : 
    1623           0 :     PG_RETURN_FLOAT8(result);
    1624             : }
    1625             : 
    1626             : Datum
    1627           0 : pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
    1628             : {
    1629           0 :     PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
    1630             : }
    1631             : 
    1632             : Datum
    1633           0 : pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
    1634             : {
    1635           0 :     PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
    1636             : }
    1637             : 
    1638             : Datum
    1639           0 : pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
    1640             : {
    1641           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
    1642             : }
    1643             : 
    1644             : Datum
    1645           0 : pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
    1646             : {
    1647           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
    1648             : }
    1649             : 
    1650             : Datum
    1651           0 : pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
    1652             : {
    1653           0 :     PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
    1654             : }
    1655             : 
    1656             : Datum
    1657           0 : pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
    1658             : {
    1659             :     /* time is already in msec, just convert to double for presentation */
    1660           0 :     PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
    1661             : }
    1662             : 
    1663             : Datum
    1664           0 : pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
    1665             : {
    1666             :     /* time is already in msec, just convert to double for presentation */
    1667           0 :     PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
    1668             : }
    1669             : 
    1670             : Datum
    1671           0 : pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
    1672             : {
    1673           0 :     PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
    1674             : }
    1675             : 
    1676             : Datum
    1677           0 : pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
    1678             : {
    1679           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
    1680             : }
    1681             : 
    1682             : Datum
    1683           0 : pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
    1684             : {
    1685           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
    1686             : }
    1687             : 
    1688             : Datum
    1689           0 : pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
    1690             : {
    1691           0 :     PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
    1692             : }
    1693             : 
    1694             : /*
    1695             :  * Returns statistics of SLRU caches.
    1696             :  */
    1697             : Datum
    1698           0 : pg_stat_get_slru(PG_FUNCTION_ARGS)
    1699             : {
    1700             : #define PG_STAT_GET_SLRU_COLS   9
    1701           0 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    1702             :     TupleDesc   tupdesc;
    1703             :     Tuplestorestate *tupstore;
    1704             :     MemoryContext per_query_ctx;
    1705             :     MemoryContext oldcontext;
    1706             :     int         i;
    1707             :     PgStat_SLRUStats *stats;
    1708             : 
    1709             :     /* check to see if caller supports us returning a tuplestore */
    1710           0 :     if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
    1711           0 :         ereport(ERROR,
    1712             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1713             :                  errmsg("set-valued function called in context that cannot accept a set")));
    1714           0 :     if (!(rsinfo->allowedModes & SFRM_Materialize))
    1715           0 :         ereport(ERROR,
    1716             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1717             :                  errmsg("materialize mode required, but it is not allowed in this context")));
    1718             : 
    1719             :     /* Build a tuple descriptor for our result type */
    1720           0 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
    1721           0 :         elog(ERROR, "return type must be a row type");
    1722             : 
    1723           0 :     per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
    1724           0 :     oldcontext = MemoryContextSwitchTo(per_query_ctx);
    1725             : 
    1726           0 :     tupstore = tuplestore_begin_heap(true, false, work_mem);
    1727           0 :     rsinfo->returnMode = SFRM_Materialize;
    1728           0 :     rsinfo->setResult = tupstore;
    1729           0 :     rsinfo->setDesc = tupdesc;
    1730             : 
    1731           0 :     MemoryContextSwitchTo(oldcontext);
    1732             : 
    1733             :     /* request SLRU stats from the stat collector */
    1734           0 :     stats = pgstat_fetch_slru();
    1735             : 
    1736           0 :     for (i = 0;; i++)
    1737           0 :     {
    1738             :         /* for each row */
    1739             :         Datum       values[PG_STAT_GET_SLRU_COLS];
    1740             :         bool        nulls[PG_STAT_GET_SLRU_COLS];
    1741           0 :         PgStat_SLRUStats stat = stats[i];
    1742             :         const char *name;
    1743             : 
    1744           0 :         name = pgstat_slru_name(i);
    1745             : 
    1746           0 :         if (!name)
    1747           0 :             break;
    1748             : 
    1749           0 :         MemSet(values, 0, sizeof(values));
    1750           0 :         MemSet(nulls, 0, sizeof(nulls));
    1751             : 
    1752           0 :         values[0] = PointerGetDatum(cstring_to_text(name));
    1753           0 :         values[1] = Int64GetDatum(stat.blocks_zeroed);
    1754           0 :         values[2] = Int64GetDatum(stat.blocks_hit);
    1755           0 :         values[3] = Int64GetDatum(stat.blocks_read);
    1756           0 :         values[4] = Int64GetDatum(stat.blocks_written);
    1757           0 :         values[5] = Int64GetDatum(stat.blocks_exists);
    1758           0 :         values[6] = Int64GetDatum(stat.flush);
    1759           0 :         values[7] = Int64GetDatum(stat.truncate);
    1760           0 :         values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
    1761             : 
    1762           0 :         tuplestore_putvalues(tupstore, tupdesc, values, nulls);
    1763             :     }
    1764             : 
    1765             :     /* clean up and return the tuplestore */
    1766             :     tuplestore_donestoring(tupstore);
    1767             : 
    1768           0 :     return (Datum) 0;
    1769             : }
    1770             : 
    1771             : Datum
    1772           0 : pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
    1773             : {
    1774           0 :     Oid         relid = PG_GETARG_OID(0);
    1775             :     int64       result;
    1776             :     PgStat_TableStatus *tabentry;
    1777             : 
    1778           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1779           0 :         result = 0;
    1780             :     else
    1781           0 :         result = (int64) (tabentry->t_counts.t_numscans);
    1782             : 
    1783           0 :     PG_RETURN_INT64(result);
    1784             : }
    1785             : 
    1786             : Datum
    1787           0 : pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
    1788             : {
    1789           0 :     Oid         relid = PG_GETARG_OID(0);
    1790             :     int64       result;
    1791             :     PgStat_TableStatus *tabentry;
    1792             : 
    1793           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1794           0 :         result = 0;
    1795             :     else
    1796           0 :         result = (int64) (tabentry->t_counts.t_tuples_returned);
    1797             : 
    1798           0 :     PG_RETURN_INT64(result);
    1799             : }
    1800             : 
    1801             : Datum
    1802           0 : pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
    1803             : {
    1804           0 :     Oid         relid = PG_GETARG_OID(0);
    1805             :     int64       result;
    1806             :     PgStat_TableStatus *tabentry;
    1807             : 
    1808           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1809           0 :         result = 0;
    1810             :     else
    1811           0 :         result = (int64) (tabentry->t_counts.t_tuples_fetched);
    1812             : 
    1813           0 :     PG_RETURN_INT64(result);
    1814             : }
    1815             : 
    1816             : Datum
    1817           0 : pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
    1818             : {
    1819           0 :     Oid         relid = PG_GETARG_OID(0);
    1820             :     int64       result;
    1821             :     PgStat_TableStatus *tabentry;
    1822             :     PgStat_TableXactStatus *trans;
    1823             : 
    1824           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1825           0 :         result = 0;
    1826             :     else
    1827             :     {
    1828           0 :         result = tabentry->t_counts.t_tuples_inserted;
    1829             :         /* live subtransactions' counts aren't in t_tuples_inserted yet */
    1830           0 :         for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
    1831           0 :             result += trans->tuples_inserted;
    1832             :     }
    1833             : 
    1834           0 :     PG_RETURN_INT64(result);
    1835             : }
    1836             : 
    1837             : Datum
    1838           0 : pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
    1839             : {
    1840           0 :     Oid         relid = PG_GETARG_OID(0);
    1841             :     int64       result;
    1842             :     PgStat_TableStatus *tabentry;
    1843             :     PgStat_TableXactStatus *trans;
    1844             : 
    1845           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1846           0 :         result = 0;
    1847             :     else
    1848             :     {
    1849           0 :         result = tabentry->t_counts.t_tuples_updated;
    1850             :         /* live subtransactions' counts aren't in t_tuples_updated yet */
    1851           0 :         for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
    1852           0 :             result += trans->tuples_updated;
    1853             :     }
    1854             : 
    1855           0 :     PG_RETURN_INT64(result);
    1856             : }
    1857             : 
    1858             : Datum
    1859           0 : pg_stat_get_xact_tuples_deleted(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_deleted;
    1871             :         /* live subtransactions' counts aren't in t_tuples_deleted yet */
    1872           0 :         for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
    1873           0 :             result += trans->tuples_deleted;
    1874             :     }
    1875             : 
    1876           0 :     PG_RETURN_INT64(result);
    1877             : }
    1878             : 
    1879             : Datum
    1880           0 : pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
    1881             : {
    1882           0 :     Oid         relid = PG_GETARG_OID(0);
    1883             :     int64       result;
    1884             :     PgStat_TableStatus *tabentry;
    1885             : 
    1886           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1887           0 :         result = 0;
    1888             :     else
    1889           0 :         result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
    1890             : 
    1891           0 :     PG_RETURN_INT64(result);
    1892             : }
    1893             : 
    1894             : Datum
    1895           0 : pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
    1896             : {
    1897           0 :     Oid         relid = PG_GETARG_OID(0);
    1898             :     int64       result;
    1899             :     PgStat_TableStatus *tabentry;
    1900             : 
    1901           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1902           0 :         result = 0;
    1903             :     else
    1904           0 :         result = (int64) (tabentry->t_counts.t_blocks_fetched);
    1905             : 
    1906           0 :     PG_RETURN_INT64(result);
    1907             : }
    1908             : 
    1909             : Datum
    1910           0 : pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
    1911             : {
    1912           0 :     Oid         relid = PG_GETARG_OID(0);
    1913             :     int64       result;
    1914             :     PgStat_TableStatus *tabentry;
    1915             : 
    1916           0 :     if ((tabentry = find_tabstat_entry(relid)) == NULL)
    1917           0 :         result = 0;
    1918             :     else
    1919           0 :         result = (int64) (tabentry->t_counts.t_blocks_hit);
    1920             : 
    1921           0 :     PG_RETURN_INT64(result);
    1922             : }
    1923             : 
    1924             : Datum
    1925           0 : pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
    1926             : {
    1927           0 :     Oid         funcid = PG_GETARG_OID(0);
    1928             :     PgStat_BackendFunctionEntry *funcentry;
    1929             : 
    1930           0 :     if ((funcentry = find_funcstat_entry(funcid)) == NULL)
    1931           0 :         PG_RETURN_NULL();
    1932           0 :     PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
    1933             : }
    1934             : 
    1935             : Datum
    1936           0 : pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
    1937             : {
    1938           0 :     Oid         funcid = PG_GETARG_OID(0);
    1939             :     PgStat_BackendFunctionEntry *funcentry;
    1940             : 
    1941           0 :     if ((funcentry = find_funcstat_entry(funcid)) == NULL)
    1942           0 :         PG_RETURN_NULL();
    1943           0 :     PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_total_time));
    1944             : }
    1945             : 
    1946             : Datum
    1947           0 : pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
    1948             : {
    1949           0 :     Oid         funcid = PG_GETARG_OID(0);
    1950             :     PgStat_BackendFunctionEntry *funcentry;
    1951             : 
    1952           0 :     if ((funcentry = find_funcstat_entry(funcid)) == NULL)
    1953           0 :         PG_RETURN_NULL();
    1954           0 :     PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_self_time));
    1955             : }
    1956             : 
    1957             : 
    1958             : /* Get the timestamp of the current statistics snapshot */
    1959             : Datum
    1960          30 : pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
    1961             : {
    1962          30 :     PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
    1963             : }
    1964             : 
    1965             : /* Discard the active statistics snapshot */
    1966             : Datum
    1967          18 : pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
    1968             : {
    1969          18 :     pgstat_clear_snapshot();
    1970             : 
    1971          18 :     PG_RETURN_VOID();
    1972             : }
    1973             : 
    1974             : 
    1975             : /* Reset all counters for the current database */
    1976             : Datum
    1977           0 : pg_stat_reset(PG_FUNCTION_ARGS)
    1978             : {
    1979           0 :     pgstat_reset_counters();
    1980             : 
    1981           0 :     PG_RETURN_VOID();
    1982             : }
    1983             : 
    1984             : /* Reset some shared cluster-wide counters */
    1985             : Datum
    1986           0 : pg_stat_reset_shared(PG_FUNCTION_ARGS)
    1987             : {
    1988           0 :     char       *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
    1989             : 
    1990           0 :     pgstat_reset_shared_counters(target);
    1991             : 
    1992           0 :     PG_RETURN_VOID();
    1993             : }
    1994             : 
    1995             : /* Reset a single counter in the current database */
    1996             : Datum
    1997           0 : pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
    1998             : {
    1999           0 :     Oid         taboid = PG_GETARG_OID(0);
    2000             : 
    2001           0 :     pgstat_reset_single_counter(taboid, RESET_TABLE);
    2002             : 
    2003           0 :     PG_RETURN_VOID();
    2004             : }
    2005             : 
    2006             : Datum
    2007           0 : pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
    2008             : {
    2009           0 :     Oid         funcoid = PG_GETARG_OID(0);
    2010             : 
    2011           0 :     pgstat_reset_single_counter(funcoid, RESET_FUNCTION);
    2012             : 
    2013           0 :     PG_RETURN_VOID();
    2014             : }
    2015             : 
    2016             : /* Reset SLRU counters (a specific one or all of them). */
    2017             : Datum
    2018           0 : pg_stat_reset_slru(PG_FUNCTION_ARGS)
    2019             : {
    2020           0 :     char       *target = NULL;
    2021             : 
    2022           0 :     if (!PG_ARGISNULL(0))
    2023           0 :         target = text_to_cstring(PG_GETARG_TEXT_PP(0));
    2024             : 
    2025           0 :     pgstat_reset_slru_counter(target);
    2026             : 
    2027           0 :     PG_RETURN_VOID();
    2028             : }
    2029             : 
    2030             : Datum
    2031          26 : pg_stat_get_archiver(PG_FUNCTION_ARGS)
    2032             : {
    2033             :     TupleDesc   tupdesc;
    2034             :     Datum       values[7];
    2035             :     bool        nulls[7];
    2036             :     PgStat_ArchiverStats *archiver_stats;
    2037             : 
    2038             :     /* Initialise values and NULL flags arrays */
    2039         208 :     MemSet(values, 0, sizeof(values));
    2040          26 :     MemSet(nulls, 0, sizeof(nulls));
    2041             : 
    2042             :     /* Initialise attributes information in the tuple descriptor */
    2043          26 :     tupdesc = CreateTemplateTupleDesc(7);
    2044          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
    2045             :                        INT8OID, -1, 0);
    2046          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
    2047             :                        TEXTOID, -1, 0);
    2048          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
    2049             :                        TIMESTAMPTZOID, -1, 0);
    2050          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
    2051             :                        INT8OID, -1, 0);
    2052          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
    2053             :                        TEXTOID, -1, 0);
    2054          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
    2055             :                        TIMESTAMPTZOID, -1, 0);
    2056          26 :     TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
    2057             :                        TIMESTAMPTZOID, -1, 0);
    2058             : 
    2059          26 :     BlessTupleDesc(tupdesc);
    2060             : 
    2061             :     /* Get statistics about the archiver process */
    2062          26 :     archiver_stats = pgstat_fetch_stat_archiver();
    2063             : 
    2064             :     /* Fill values and NULLs */
    2065          26 :     values[0] = Int64GetDatum(archiver_stats->archived_count);
    2066          26 :     if (*(archiver_stats->last_archived_wal) == '\0')
    2067          10 :         nulls[1] = true;
    2068             :     else
    2069          16 :         values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
    2070             : 
    2071          26 :     if (archiver_stats->last_archived_timestamp == 0)
    2072          10 :         nulls[2] = true;
    2073             :     else
    2074          16 :         values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
    2075             : 
    2076          26 :     values[3] = Int64GetDatum(archiver_stats->failed_count);
    2077          26 :     if (*(archiver_stats->last_failed_wal) == '\0')
    2078           0 :         nulls[4] = true;
    2079             :     else
    2080          26 :         values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
    2081             : 
    2082          26 :     if (archiver_stats->last_failed_timestamp == 0)
    2083           0 :         nulls[5] = true;
    2084             :     else
    2085          26 :         values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
    2086             : 
    2087          26 :     if (archiver_stats->stat_reset_timestamp == 0)
    2088           0 :         nulls[6] = true;
    2089             :     else
    2090          26 :         values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
    2091             : 
    2092             :     /* Returns the record as Datum */
    2093          26 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
    2094             : }

Generated by: LCOV version 1.13