Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Typedefs | Functions | Variables

pgstat.c File Reference

#include "postgres.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include "pgstat.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/transam.h"
#include "access/twophase_rmgr.h"
#include "access/xact.h"
#include "catalog/pg_database.h"
#include "catalog/pg_proc.h"
#include "lib/ilist.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
#include "libpq/pqsignal.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "postmaster/autovacuum.h"
#include "postmaster/fork_process.h"
#include "postmaster/postmaster.h"
#include "storage/backendid.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pg_shmem.h"
#include "storage/procsignal.h"
#include "utils/ascii.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/rel.h"
#include "utils/timestamp.h"
#include "utils/tqual.h"
Include dependency graph for pgstat.c:

Go to the source code of this file.

Data Structures

struct  TabStatusArray
struct  PgStat_SubXactStatus
struct  TwoPhasePgStatRecord
struct  DBWriteRequest

Defines

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"
#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"
#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"
#define PGSTAT_STAT_INTERVAL   500
#define PGSTAT_RETRY_DELAY   10
#define PGSTAT_MAX_WAIT_TIME   10000
#define PGSTAT_INQ_INTERVAL   640
#define PGSTAT_RESTART_INTERVAL   60
#define PGSTAT_POLL_LOOP_COUNT   (PGSTAT_MAX_WAIT_TIME / PGSTAT_RETRY_DELAY)
#define PGSTAT_INQ_LOOP_COUNT   (PGSTAT_INQ_INTERVAL / PGSTAT_RETRY_DELAY)
#define PGSTAT_DB_HASH_SIZE   16
#define PGSTAT_TAB_HASH_SIZE   512
#define PGSTAT_FUNCTION_HASH_SIZE   512
#define TABSTAT_QUANTUM   100
#define TESTBYTEVAL   ((char) 199)

Typedefs

typedef struct TabStatusArray TabStatusArray
typedef struct PgStat_SubXactStatus PgStat_SubXactStatus
typedef struct TwoPhasePgStatRecord TwoPhasePgStatRecord
typedef struct DBWriteRequest DBWriteRequest

Functions

NON_EXEC_STATIC void PgstatCollectorMain (int argc, char *argv[]) __attribute__((noreturn))
static void pgstat_exit (SIGNAL_ARGS)
static void pgstat_beshutdown_hook (int code, Datum arg)
static void pgstat_sighup_handler (SIGNAL_ARGS)
static PgStat_StatDBEntrypgstat_get_db_entry (Oid databaseid, bool create)
static PgStat_StatTabEntrypgstat_get_tab_entry (PgStat_StatDBEntry *dbentry, Oid tableoid, bool create)
static void pgstat_write_statsfiles (bool permanent, bool allDbs)
static void pgstat_write_db_statsfile (PgStat_StatDBEntry *dbentry, bool permanent)
static HTABpgstat_read_statsfiles (Oid onlydb, bool permanent, bool deep)
static void pgstat_read_db_statsfile (Oid databaseid, HTAB *tabhash, HTAB *funchash, bool permanent)
static void backend_read_statsfile (void)
static void pgstat_read_current_status (void)
static bool pgstat_write_statsfile_needed (void)
static bool pgstat_db_requested (Oid databaseid)
static void pgstat_send_tabstat (PgStat_MsgTabstat *tsmsg)
static void pgstat_send_funcstats (void)
static HTABpgstat_collect_oids (Oid catalogid)
static PgStat_TableStatusget_tabstat_entry (Oid rel_id, bool isshared)
static void pgstat_setup_memcxt (void)
static void pgstat_setheader (PgStat_MsgHdr *hdr, StatMsgType mtype)
static void pgstat_send (void *msg, int len)
static void pgstat_recv_inquiry (PgStat_MsgInquiry *msg, int len)
static void pgstat_recv_tabstat (PgStat_MsgTabstat *msg, int len)
static void pgstat_recv_tabpurge (PgStat_MsgTabpurge *msg, int len)
static void pgstat_recv_dropdb (PgStat_MsgDropdb *msg, int len)
static void pgstat_recv_resetcounter (PgStat_MsgResetcounter *msg, int len)
static void pgstat_recv_resetsharedcounter (PgStat_MsgResetsharedcounter *msg, int len)
static void pgstat_recv_resetsinglecounter (PgStat_MsgResetsinglecounter *msg, int len)
static void pgstat_recv_autovac (PgStat_MsgAutovacStart *msg, int len)
static void pgstat_recv_vacuum (PgStat_MsgVacuum *msg, int len)
static void pgstat_recv_analyze (PgStat_MsgAnalyze *msg, int len)
static void pgstat_recv_bgwriter (PgStat_MsgBgWriter *msg, int len)
static void pgstat_recv_funcstat (PgStat_MsgFuncstat *msg, int len)
static void pgstat_recv_funcpurge (PgStat_MsgFuncpurge *msg, int len)
static void pgstat_recv_recoveryconflict (PgStat_MsgRecoveryConflict *msg, int len)
static void pgstat_recv_deadlock (PgStat_MsgDeadlock *msg, int len)
static void pgstat_recv_tempfile (PgStat_MsgTempFile *msg, int len)
void pgstat_init (void)
static void pgstat_reset_remove_files (const char *directory)
void pgstat_reset_all (void)
int pgstat_start (void)
void allow_immediate_pgstat_restart (void)
void pgstat_report_stat (bool force)
void pgstat_vacuum_stat (void)
void pgstat_drop_database (Oid databaseid)
void pgstat_reset_counters (void)
void pgstat_reset_shared_counters (const char *target)
void pgstat_reset_single_counter (Oid objoid, PgStat_Single_Reset_Type type)
void pgstat_report_autovac (Oid dboid)
void pgstat_report_vacuum (Oid tableoid, bool shared, PgStat_Counter tuples)
void pgstat_report_analyze (Relation rel, PgStat_Counter livetuples, PgStat_Counter deadtuples)
void pgstat_report_recovery_conflict (int reason)
void pgstat_report_deadlock (void)
void pgstat_report_tempfile (size_t filesize)
void pgstat_ping (void)
static void pgstat_send_inquiry (TimestampTz clock_time, TimestampTz cutoff_time, Oid databaseid)
void pgstat_init_function_usage (FunctionCallInfoData *fcinfo, PgStat_FunctionCallUsage *fcu)
PgStat_BackendFunctionEntryfind_funcstat_entry (Oid func_id)
void pgstat_end_function_usage (PgStat_FunctionCallUsage *fcu, bool finalize)
void pgstat_initstats (Relation rel)
PgStat_TableStatusfind_tabstat_entry (Oid rel_id)
static PgStat_SubXactStatusget_tabstat_stack_level (int nest_level)
static void add_tabstat_xact_level (PgStat_TableStatus *pgstat_info, int nest_level)
void pgstat_count_heap_insert (Relation rel, int n)
void pgstat_count_heap_update (Relation rel, bool hot)
void pgstat_count_heap_delete (Relation rel)
void pgstat_update_heap_dead_tuples (Relation rel, int delta)
void AtEOXact_PgStat (bool isCommit)
void AtEOSubXact_PgStat (bool isCommit, int nestDepth)
void AtPrepare_PgStat (void)
void PostPrepare_PgStat (void)
void pgstat_twophase_postcommit (TransactionId xid, uint16 info, void *recdata, uint32 len)
void pgstat_twophase_postabort (TransactionId xid, uint16 info, void *recdata, uint32 len)
PgStat_StatDBEntrypgstat_fetch_stat_dbentry (Oid dbid)
PgStat_StatTabEntrypgstat_fetch_stat_tabentry (Oid relid)
PgStat_StatFuncEntrypgstat_fetch_stat_funcentry (Oid func_id)
PgBackendStatuspgstat_fetch_stat_beentry (int beid)
int pgstat_fetch_stat_numbackends (void)
PgStat_GlobalStatspgstat_fetch_global (void)
Size BackendStatusShmemSize (void)
void CreateSharedBackendStatus (void)
void pgstat_initialize (void)
void pgstat_bestart (void)
void pgstat_report_activity (BackendState state, const char *cmd_str)
void pgstat_report_appname (const char *appname)
void pgstat_report_xact_timestamp (TimestampTz tstamp)
void pgstat_report_waiting (bool waiting)
const char * pgstat_get_backend_current_activity (int pid, bool checkUser)
const char * pgstat_get_crashed_backend_activity (int pid, char *buffer, int buflen)
void pgstat_send_bgwriter (void)
static void reset_dbentry_counters (PgStat_StatDBEntry *dbentry)
static void get_dbstat_filename (bool permanent, bool tempname, Oid databaseid, char *filename, int len)
static bool pgstat_read_db_statsfile_timestamp (Oid databaseid, bool permanent, TimestampTz *ts)
void pgstat_clear_snapshot (void)

Variables

bool pgstat_track_activities = false
bool pgstat_track_counts = false
int pgstat_track_functions = TRACK_FUNC_OFF
int pgstat_track_activity_query_size = 1024
char * pgstat_stat_directory = NULL
char * pgstat_stat_filename = NULL
char * pgstat_stat_tmpname = NULL
PgStat_MsgBgWriter BgWriterStats
NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET
static Latch pgStatLatch
static struct sockaddr_storage pgStatAddr
static time_t last_pgstat_start_time
static bool pgStatRunningInCollector = false
static TabStatusArraypgStatTabList = NULL
static HTABpgStatFunctions = NULL
static bool have_function_stats = false
static PgStat_SubXactStatuspgStatXactStack = NULL
static int pgStatXactCommit = 0
static int pgStatXactRollback = 0
PgStat_Counter pgStatBlockReadTime = 0
PgStat_Counter pgStatBlockWriteTime = 0
static MemoryContext pgStatLocalContext = NULL
static HTABpgStatDBHash = NULL
static PgBackendStatuslocalBackendStatusTable = NULL
static int localNumBackends = 0
static PgStat_GlobalStats globalStats
static slist_head last_statrequests = SLIST_STATIC_INIT(last_statrequests)
static volatile bool need_exit = false
static volatile bool got_SIGHUP = false
static instr_time total_func_time
static PgBackendStatusBackendStatusArray = NULL
static PgBackendStatusMyBEEntry = NULL
static char * BackendClientHostnameBuffer = NULL
static char * BackendAppnameBuffer = NULL
static char * BackendActivityBuffer = NULL
static Size BackendActivityBufferSize = 0

Define Documentation

#define PGSTAT_DB_HASH_SIZE   16

Definition at line 102 of file pgstat.c.

Referenced by pgstat_read_statsfiles().

#define PGSTAT_FUNCTION_HASH_SIZE   512
#define PGSTAT_INQ_INTERVAL   640

Definition at line 87 of file pgstat.c.

#define PGSTAT_INQ_LOOP_COUNT   (PGSTAT_INQ_INTERVAL / PGSTAT_RETRY_DELAY)

Definition at line 95 of file pgstat.c.

Referenced by backend_read_statsfile().

#define PGSTAT_MAX_WAIT_TIME   10000

Definition at line 84 of file pgstat.c.

#define PGSTAT_POLL_LOOP_COUNT   (PGSTAT_MAX_WAIT_TIME / PGSTAT_RETRY_DELAY)

Definition at line 94 of file pgstat.c.

#define PGSTAT_RESTART_INTERVAL   60

Definition at line 90 of file pgstat.c.

Referenced by pgstat_start().

#define PGSTAT_RETRY_DELAY   10

Definition at line 81 of file pgstat.c.

Referenced by backend_read_statsfile().

#define PGSTAT_STAT_INTERVAL   500

Definition at line 78 of file pgstat.c.

Referenced by backend_read_statsfile(), and pgstat_report_stat().

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"

Definition at line 70 of file pgstat.c.

Referenced by get_dbstat_filename(), and pgstat_reset_all().

#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"
#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"

Definition at line 72 of file pgstat.c.

Referenced by pgstat_write_statsfiles().

#define PGSTAT_TAB_HASH_SIZE   512

Definition at line 103 of file pgstat.c.

Referenced by pgstat_collect_oids(), pgstat_read_statsfiles(), and reset_dbentry_counters().

#define TABSTAT_QUANTUM   100

Definition at line 156 of file pgstat.c.

Referenced by get_tabstat_entry().

#define TESTBYTEVAL   ((char) 199)

Referenced by pgstat_init().


Typedef Documentation


Function Documentation

static void add_tabstat_xact_level ( PgStat_TableStatus pgstat_info,
int  nest_level 
) [static]

Definition at line 1739 of file pgstat.c.

References PgStat_SubXactStatus::first, get_tabstat_stack_level(), MemoryContextAllocZero(), PgStat_TableXactStatus::nest_level, PgStat_TableXactStatus::next, PgStat_TableXactStatus::parent, TopTransactionContext, PgStat_TableStatus::trans, trans, and PgStat_TableXactStatus::upper.

Referenced by pgstat_count_heap_delete(), pgstat_count_heap_insert(), and pgstat_count_heap_update().

{
    PgStat_SubXactStatus *xact_state;
    PgStat_TableXactStatus *trans;

    /*
     * If this is the first rel to be modified at the current nest level, we
     * first have to push a transaction stack entry.
     */
    xact_state = get_tabstat_stack_level(nest_level);

    /* Now make a per-table stack entry */
    trans = (PgStat_TableXactStatus *)
        MemoryContextAllocZero(TopTransactionContext,
                               sizeof(PgStat_TableXactStatus));
    trans->nest_level = nest_level;
    trans->upper = pgstat_info->trans;
    trans->parent = pgstat_info;
    trans->next = xact_state->first;
    xact_state->first = trans;
    pgstat_info->trans = trans;
}

void allow_immediate_pgstat_restart ( void   ) 

Definition at line 692 of file pgstat.c.

References last_pgstat_start_time.

Referenced by HandleChildCrash().

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 1927 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, get_tabstat_stack_level(), PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, NULL, PgStat_TableXactStatus::parent, pfree(), PgStat_SubXactStatus::prev, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, PgStat_TableXactStatus::tuples_updated, and PgStat_TableXactStatus::upper.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

{
    PgStat_SubXactStatus *xact_state;

    /*
     * Transfer transactional insert/update counts into the next higher
     * subtransaction state.
     */
    xact_state = pgStatXactStack;
    if (xact_state != NULL &&
        xact_state->nest_level >= nestDepth)
    {
        PgStat_TableXactStatus *trans;
        PgStat_TableXactStatus *next_trans;

        /* delink xact_state from stack immediately to simplify reuse case */
        pgStatXactStack = xact_state->prev;

        for (trans = xact_state->first; trans != NULL; trans = next_trans)
        {
            PgStat_TableStatus *tabstat;

            next_trans = trans->next;
            Assert(trans->nest_level == nestDepth);
            tabstat = trans->parent;
            Assert(tabstat->trans == trans);
            if (isCommit)
            {
                if (trans->upper && trans->upper->nest_level == nestDepth - 1)
                {
                    trans->upper->tuples_inserted += trans->tuples_inserted;
                    trans->upper->tuples_updated += trans->tuples_updated;
                    trans->upper->tuples_deleted += trans->tuples_deleted;
                    tabstat->trans = trans->upper;
                    pfree(trans);
                }
                else
                {
                    /*
                     * When there isn't an immediate parent state, we can just
                     * reuse the record instead of going through a
                     * palloc/pfree pushup (this works since it's all in
                     * TopTransactionContext anyway).  We have to re-link it
                     * into the parent level, though, and that might mean
                     * pushing a new entry into the pgStatXactStack.
                     */
                    PgStat_SubXactStatus *upper_xact_state;

                    upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
                    trans->next = upper_xact_state->first;
                    upper_xact_state->first = trans;
                    trans->nest_level = nestDepth - 1;
                }
            }
            else
            {
                /*
                 * On abort, update top-level tabstat counts, then forget the
                 * subtransaction
                 */

                /* count attempted actions regardless of commit/abort */
                tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
                tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
                tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
                /* inserted tuples are dead, deleted tuples are unaffected */
                tabstat->t_counts.t_delta_dead_tuples +=
                    trans->tuples_inserted + trans->tuples_updated;
                tabstat->trans = trans->upper;
                pfree(trans);
            }
        }
        pfree(xact_state);
    }
}

void AtEOXact_PgStat ( bool  isCommit  ) 

Definition at line 1854 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, NULL, PgStat_TableXactStatus::parent, pgstat_clear_snapshot(), pgStatXactCommit, pgStatXactRollback, PgStat_SubXactStatus::prev, PgStat_TableCounts::t_changed_tuples, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, PgStat_TableXactStatus::tuples_updated, and PgStat_TableXactStatus::upper.

Referenced by AbortTransaction(), CommitTransaction(), and FinishPreparedTransaction().

{
    PgStat_SubXactStatus *xact_state;

    /*
     * Count transaction commit or abort.  (We use counters, not just bools,
     * in case the reporting message isn't sent right away.)
     */
    if (isCommit)
        pgStatXactCommit++;
    else
        pgStatXactRollback++;

    /*
     * Transfer transactional insert/update counts into the base tabstat
     * entries.  We don't bother to free any of the transactional state, since
     * it's all in TopTransactionContext and will go away anyway.
     */
    xact_state = pgStatXactStack;
    if (xact_state != NULL)
    {
        PgStat_TableXactStatus *trans;

        Assert(xact_state->nest_level == 1);
        Assert(xact_state->prev == NULL);
        for (trans = xact_state->first; trans != NULL; trans = trans->next)
        {
            PgStat_TableStatus *tabstat;

            Assert(trans->nest_level == 1);
            Assert(trans->upper == NULL);
            tabstat = trans->parent;
            Assert(tabstat->trans == trans);
            /* count attempted actions regardless of commit/abort */
            tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
            tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
            tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
            if (isCommit)
            {
                /* insert adds a live tuple, delete removes one */
                tabstat->t_counts.t_delta_live_tuples +=
                    trans->tuples_inserted - trans->tuples_deleted;
                /* update and delete each create a dead tuple */
                tabstat->t_counts.t_delta_dead_tuples +=
                    trans->tuples_updated + trans->tuples_deleted;
                /* insert, update, delete each count as one change event */
                tabstat->t_counts.t_changed_tuples +=
                    trans->tuples_inserted + trans->tuples_updated +
                    trans->tuples_deleted;
            }
            else
            {
                /* inserted tuples are dead, deleted tuples are unaffected */
                tabstat->t_counts.t_delta_dead_tuples +=
                    trans->tuples_inserted + trans->tuples_updated;
                /* an aborted xact generates no changed_tuple events */
            }
            tabstat->trans = NULL;
        }
    }
    pgStatXactStack = NULL;

    /* Make sure any stats snapshot is thrown away */
    pgstat_clear_snapshot();
}

void AtPrepare_PgStat ( void   ) 

Definition at line 2012 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, NULL, PgStat_TableXactStatus::parent, PgStat_SubXactStatus::prev, RegisterTwoPhaseRecord(), PgStat_TableStatus::t_id, TwoPhasePgStatRecord::t_id, PgStat_TableStatus::t_shared, TwoPhasePgStatRecord::t_shared, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::tuples_deleted, TwoPhasePgStatRecord::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, TwoPhasePgStatRecord::tuples_inserted, PgStat_TableXactStatus::tuples_updated, TwoPhasePgStatRecord::tuples_updated, TWOPHASE_RM_PGSTAT_ID, and PgStat_TableXactStatus::upper.

Referenced by PrepareTransaction().

{
    PgStat_SubXactStatus *xact_state;

    xact_state = pgStatXactStack;
    if (xact_state != NULL)
    {
        PgStat_TableXactStatus *trans;

        Assert(xact_state->nest_level == 1);
        Assert(xact_state->prev == NULL);
        for (trans = xact_state->first; trans != NULL; trans = trans->next)
        {
            PgStat_TableStatus *tabstat;
            TwoPhasePgStatRecord record;

            Assert(trans->nest_level == 1);
            Assert(trans->upper == NULL);
            tabstat = trans->parent;
            Assert(tabstat->trans == trans);

            record.tuples_inserted = trans->tuples_inserted;
            record.tuples_updated = trans->tuples_updated;
            record.tuples_deleted = trans->tuples_deleted;
            record.t_id = tabstat->t_id;
            record.t_shared = tabstat->t_shared;

            RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
                                   &record, sizeof(TwoPhasePgStatRecord));
        }
    }
}

static void backend_read_statsfile ( void   )  [static]

Definition at line 4229 of file pgstat.c.

References Assert, CHECK_FOR_INTERRUPTS, elog, GetCurrentTimestamp(), InvalidOid, IsAutoVacuumLauncherProcess(), IsAutoVacuumWorkerProcess(), LOG, MyDatabaseId, pfree(), pg_usleep(), PGSTAT_INQ_LOOP_COUNT, pgstat_read_db_statsfile_timestamp(), pgstat_read_statsfiles(), PGSTAT_RETRY_DELAY, pgstat_send_inquiry(), PGSTAT_STAT_INTERVAL, pgStatRunningInCollector, pstrdup(), timestamptz_to_str(), TimestampTzPlusMilliseconds, and WARNING.

Referenced by pgstat_fetch_global(), pgstat_fetch_stat_dbentry(), pgstat_fetch_stat_funcentry(), pgstat_fetch_stat_tabentry(), and pgstat_vacuum_stat().

{
    TimestampTz min_ts = 0;
    TimestampTz ref_ts = 0;
    int         count;

    /* already read it? */
    if (pgStatDBHash)
        return;
    Assert(!pgStatRunningInCollector);

    /*
     * Loop until fresh enough stats file is available or we ran out of time.
     * The stats inquiry message is sent repeatedly in case collector drops
     * it; but not every single time, as that just swamps the collector.
     */
    for (count = 0; count < PGSTAT_POLL_LOOP_COUNT; count++)
    {
        bool        ok;
        TimestampTz file_ts = 0;
        TimestampTz cur_ts;

        CHECK_FOR_INTERRUPTS();

        ok = pgstat_read_db_statsfile_timestamp(MyDatabaseId, false, &file_ts);

        cur_ts = GetCurrentTimestamp();
        /* Calculate min acceptable timestamp, if we didn't already */
        if (count == 0 || cur_ts < ref_ts)
        {
            /*
             * We set the minimum acceptable timestamp to PGSTAT_STAT_INTERVAL
             * msec before now.  This indirectly ensures that the collector
             * needn't write the file more often than PGSTAT_STAT_INTERVAL. In
             * an autovacuum worker, however, we want a lower delay to avoid
             * using stale data, so we use PGSTAT_RETRY_DELAY (since the
             * number of workers is low, this shouldn't be a problem).
             *
             * We don't recompute min_ts after sleeping, except in the
             * unlikely case that cur_ts went backwards.  So we might end up
             * accepting a file a bit older than PGSTAT_STAT_INTERVAL.  In
             * practice that shouldn't happen, though, as long as the sleep
             * time is less than PGSTAT_STAT_INTERVAL; and we don't want to
             * tell the collector that our cutoff time is less than what we'd
             * actually accept.
             */
            ref_ts = cur_ts;
            if (IsAutoVacuumWorkerProcess())
                min_ts = TimestampTzPlusMilliseconds(ref_ts,
                                                     -PGSTAT_RETRY_DELAY);
            else
                min_ts = TimestampTzPlusMilliseconds(ref_ts,
                                                     -PGSTAT_STAT_INTERVAL);
        }

        /*
         * If the file timestamp is actually newer than cur_ts, we must have
         * had a clock glitch (system time went backwards) or there is clock
         * skew between our processor and the stats collector's processor.
         * Accept the file, but send an inquiry message anyway to make
         * pgstat_recv_inquiry do a sanity check on the collector's time.
         */
        if (ok && file_ts > cur_ts)
        {
            /*
             * A small amount of clock skew between processors isn't terribly
             * surprising, but a large difference is worth logging.  We
             * arbitrarily define "large" as 1000 msec.
             */
            if (file_ts >= TimestampTzPlusMilliseconds(cur_ts, 1000))
            {
                char       *filetime;
                char       *mytime;

                /* Copy because timestamptz_to_str returns a static buffer */
                filetime = pstrdup(timestamptz_to_str(file_ts));
                mytime = pstrdup(timestamptz_to_str(cur_ts));
                elog(LOG, "stats collector's time %s is later than backend local time %s",
                     filetime, mytime);
                pfree(filetime);
                pfree(mytime);
            }

            pgstat_send_inquiry(cur_ts, min_ts, MyDatabaseId);
            break;
        }

        /* Normal acceptance case: file is not older than cutoff time */
        if (ok && file_ts >= min_ts)
            break;

        /* Not there or too old, so kick the collector and wait a bit */
        if ((count % PGSTAT_INQ_LOOP_COUNT) == 0)
            pgstat_send_inquiry(cur_ts, min_ts, MyDatabaseId);

        pg_usleep(PGSTAT_RETRY_DELAY * 1000L);
    }

    if (count >= PGSTAT_POLL_LOOP_COUNT)
        elog(WARNING, "pgstat wait timeout");

    /*
     * Autovacuum launcher wants stats about all databases, but a shallow read
     * is sufficient.
     */
    if (IsAutoVacuumLauncherProcess())
        pgStatDBHash = pgstat_read_statsfiles(InvalidOid, false, false);
    else
        pgStatDBHash = pgstat_read_statsfiles(MyDatabaseId, false, true);
}

Size BackendStatusShmemSize ( void   ) 
void CreateSharedBackendStatus ( void   ) 

Definition at line 2341 of file pgstat.c.

References i, MaxBackends, MemSet, mul_size(), NAMEDATALEN, pgstat_track_activity_query_size, ShmemInitStruct(), PgBackendStatus::st_activity, PgBackendStatus::st_appname, and PgBackendStatus::st_clienthostname.

Referenced by CreateSharedMemoryAndSemaphores().

{
    Size        size;
    bool        found;
    int         i;
    char       *buffer;

    /* Create or attach to the shared array */
    size = mul_size(sizeof(PgBackendStatus), MaxBackends);
    BackendStatusArray = (PgBackendStatus *)
        ShmemInitStruct("Backend Status Array", size, &found);

    if (!found)
    {
        /*
         * We're the first - initialize.
         */
        MemSet(BackendStatusArray, 0, size);
    }

    /* Create or attach to the shared appname buffer */
    size = mul_size(NAMEDATALEN, MaxBackends);
    BackendAppnameBuffer = (char *)
        ShmemInitStruct("Backend Application Name Buffer", size, &found);

    if (!found)
    {
        MemSet(BackendAppnameBuffer, 0, size);

        /* Initialize st_appname pointers. */
        buffer = BackendAppnameBuffer;
        for (i = 0; i < MaxBackends; i++)
        {
            BackendStatusArray[i].st_appname = buffer;
            buffer += NAMEDATALEN;
        }
    }

    /* Create or attach to the shared client hostname buffer */
    size = mul_size(NAMEDATALEN, MaxBackends);
    BackendClientHostnameBuffer = (char *)
        ShmemInitStruct("Backend Client Host Name Buffer", size, &found);

    if (!found)
    {
        MemSet(BackendClientHostnameBuffer, 0, size);

        /* Initialize st_clienthostname pointers. */
        buffer = BackendClientHostnameBuffer;
        for (i = 0; i < MaxBackends; i++)
        {
            BackendStatusArray[i].st_clienthostname = buffer;
            buffer += NAMEDATALEN;
        }
    }

    /* Create or attach to the shared activity buffer */
    BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
                                         MaxBackends);
    BackendActivityBuffer = (char *)
        ShmemInitStruct("Backend Activity Buffer",
                        BackendActivityBufferSize,
                        &found);

    if (!found)
    {
        MemSet(BackendActivityBuffer, 0, size);

        /* Initialize st_activity pointers. */
        buffer = BackendActivityBuffer;
        for (i = 0; i < MaxBackends; i++)
        {
            BackendStatusArray[i].st_activity = buffer;
            buffer += pgstat_track_activity_query_size;
        }
    }
}

PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id  ) 
PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id  ) 
static void get_dbstat_filename ( bool  permanent,
bool  tempname,
Oid  databaseid,
char *  filename,
int  len 
) [static]

Definition at line 3618 of file pgstat.c.

References elog, ERROR, pgstat_stat_directory, PGSTAT_STAT_PERMANENT_DIRECTORY, and snprintf().

Referenced by pgstat_read_db_statsfile(), pgstat_recv_dropdb(), and pgstat_write_db_statsfile().

{
    int         printed;

    printed = snprintf(filename, len, "%s/db_%u.%s",
                       permanent ? PGSTAT_STAT_PERMANENT_DIRECTORY :
                       pgstat_stat_directory,
                       databaseid,
                       tempname ? "tmp" : "stat");
    if (printed > len)
        elog(ERROR, "overlength pgstat path");
}

static PgStat_TableStatus * get_tabstat_entry ( Oid  rel_id,
bool  isshared 
) [static]

Definition at line 1634 of file pgstat.c.

References i, MemoryContextAllocZero(), PgStat_TableStatus::t_id, PgStat_TableStatus::t_shared, TABSTAT_QUANTUM, TopMemoryContext, and TabStatusArray::tsa_next.

Referenced by pgstat_initstats(), pgstat_twophase_postabort(), and pgstat_twophase_postcommit().

{
    PgStat_TableStatus *entry;
    TabStatusArray *tsa;
    TabStatusArray *prev_tsa;
    int         i;

    /*
     * Search the already-used tabstat slots for this relation.
     */
    prev_tsa = NULL;
    for (tsa = pgStatTabList; tsa != NULL; prev_tsa = tsa, tsa = tsa->tsa_next)
    {
        for (i = 0; i < tsa->tsa_used; i++)
        {
            entry = &tsa->tsa_entries[i];
            if (entry->t_id == rel_id)
                return entry;
        }

        if (tsa->tsa_used < TABSTAT_QUANTUM)
        {
            /*
             * It must not be present, but we found a free slot instead. Fine,
             * let's use this one.  We assume the entry was already zeroed,
             * either at creation or after last use.
             */
            entry = &tsa->tsa_entries[tsa->tsa_used++];
            entry->t_id = rel_id;
            entry->t_shared = isshared;
            return entry;
        }
    }

    /*
     * We ran out of tabstat slots, so allocate more.  Be sure they're zeroed.
     */
    tsa = (TabStatusArray *) MemoryContextAllocZero(TopMemoryContext,
                                                    sizeof(TabStatusArray));
    if (prev_tsa)
        prev_tsa->tsa_next = tsa;
    else
        pgStatTabList = tsa;

    /*
     * Use the first entry of the new TabStatusArray.
     */
    entry = &tsa->tsa_entries[tsa->tsa_used++];
    entry->t_id = rel_id;
    entry->t_shared = isshared;
    return entry;
}

static PgStat_SubXactStatus* get_tabstat_stack_level ( int  nest_level  )  [static]

Definition at line 1717 of file pgstat.c.

References PgStat_SubXactStatus::first, MemoryContextAlloc(), PgStat_SubXactStatus::nest_level, NULL, PgStat_SubXactStatus::prev, and TopTransactionContext.

Referenced by add_tabstat_xact_level(), and AtEOSubXact_PgStat().

{
    PgStat_SubXactStatus *xact_state;

    xact_state = pgStatXactStack;
    if (xact_state == NULL || xact_state->nest_level != nest_level)
    {
        xact_state = (PgStat_SubXactStatus *)
            MemoryContextAlloc(TopTransactionContext,
                               sizeof(PgStat_SubXactStatus));
        xact_state->nest_level = nest_level;
        xact_state->prev = pgStatXactStack;
        xact_state->first = NULL;
        pgStatXactStack = xact_state;
    }
    return xact_state;
}

static void pgstat_beshutdown_hook ( int  code,
Datum  arg 
) [static]

Definition at line 2532 of file pgstat.c.

References Assert, MyDatabaseId, OidIsValid, pgstat_report_stat(), PgBackendStatus::st_changecount, and PgBackendStatus::st_procpid.

Referenced by pgstat_initialize().

{
    volatile PgBackendStatus *beentry = MyBEEntry;

    /*
     * If we got as far as discovering our own database ID, we can report what
     * we did to the collector.  Otherwise, we'd be sending an invalid
     * database ID, so forget it.  (This means that accesses to pg_database
     * during failed backend starts might never get counted.)
     */
    if (OidIsValid(MyDatabaseId))
        pgstat_report_stat(true);

    /*
     * Clear my status entry, following the protocol of bumping st_changecount
     * before and after.  We use a volatile pointer here to ensure the
     * compiler doesn't try to get cute.
     */
    beentry->st_changecount++;

    beentry->st_procpid = 0;    /* mark invalid */

    beentry->st_changecount++;
    Assert((beentry->st_changecount & 1) == 0);
}

void pgstat_bestart ( void   ) 

Definition at line 2451 of file pgstat.c.

References application_name, Assert, GetCurrentTimestamp(), GetSessionUserId(), MemSet, MyDatabaseId, MyProcPid, MyProcPort, NAMEDATALEN, pgstat_report_appname(), pgstat_track_activity_query_size, Port::raddr, Port::remote_hostname, Port::SessionStartTime, PgBackendStatus::st_activity, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_appname, PgBackendStatus::st_changecount, PgBackendStatus::st_clientaddr, PgBackendStatus::st_clienthostname, PgBackendStatus::st_databaseid, PgBackendStatus::st_proc_start_timestamp, PgBackendStatus::st_procpid, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_userid, PgBackendStatus::st_waiting, PgBackendStatus::st_xact_start_timestamp, and strlcpy().

Referenced by InitPostgres().

{
    TimestampTz proc_start_timestamp;
    Oid         userid;
    SockAddr    clientaddr;
    volatile PgBackendStatus *beentry;

    /*
     * To minimize the time spent modifying the PgBackendStatus entry, fetch
     * all the needed data first.
     *
     * If we have a MyProcPort, use its session start time (for consistency,
     * and to save a kernel call).
     */
    if (MyProcPort)
        proc_start_timestamp = MyProcPort->SessionStartTime;
    else
        proc_start_timestamp = GetCurrentTimestamp();
    userid = GetSessionUserId();

    /*
     * We may not have a MyProcPort (eg, if this is the autovacuum process).
     * If so, use all-zeroes client address, which is dealt with specially in
     * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
     */
    if (MyProcPort)
        memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
    else
        MemSet(&clientaddr, 0, sizeof(clientaddr));

    /*
     * Initialize my status entry, following the protocol of bumping
     * st_changecount before and after; and make sure it's even afterwards. We
     * use a volatile pointer here to ensure the compiler doesn't try to get
     * cute.
     */
    beentry = MyBEEntry;
    do
    {
        beentry->st_changecount++;
    } while ((beentry->st_changecount & 1) == 0);

    beentry->st_procpid = MyProcPid;
    beentry->st_proc_start_timestamp = proc_start_timestamp;
    beentry->st_activity_start_timestamp = 0;
    beentry->st_state_start_timestamp = 0;
    beentry->st_xact_start_timestamp = 0;
    beentry->st_databaseid = MyDatabaseId;
    beentry->st_userid = userid;
    beentry->st_clientaddr = clientaddr;
    beentry->st_clienthostname[0] = '\0';
    beentry->st_waiting = false;
    beentry->st_state = STATE_UNDEFINED;
    beentry->st_appname[0] = '\0';
    beentry->st_activity[0] = '\0';
    /* Also make sure the last byte in each string area is always 0 */
    beentry->st_clienthostname[NAMEDATALEN - 1] = '\0';
    beentry->st_appname[NAMEDATALEN - 1] = '\0';
    beentry->st_activity[pgstat_track_activity_query_size - 1] = '\0';

    beentry->st_changecount++;
    Assert((beentry->st_changecount & 1) == 0);

    if (MyProcPort && MyProcPort->remote_hostname)
        strlcpy(beentry->st_clienthostname, MyProcPort->remote_hostname, NAMEDATALEN);

    /* Update app name to current GUC setting */
    if (application_name)
        pgstat_report_appname(application_name);
}

void pgstat_clear_snapshot ( void   ) 

Definition at line 4370 of file pgstat.c.

References localNumBackends, and MemoryContextDelete().

Referenced by AtEOXact_PgStat(), autovac_refresh_stats(), AutoVacLauncherMain(), pg_stat_clear_snapshot(), and PostPrepare_PgStat().

{
    /* Release memory, if any was allocated */
    if (pgStatLocalContext)
        MemoryContextDelete(pgStatLocalContext);

    /* Reset variables */
    pgStatLocalContext = NULL;
    pgStatDBHash = NULL;
    localBackendStatusTable = NULL;
    localNumBackends = 0;
}

static HTAB * pgstat_collect_oids ( Oid  catalogid  )  [static]

Definition at line 1093 of file pgstat.c.

References AccessShareLock, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, HASHCTL::entrysize, ForwardScanDirection, HASHCTL::hash, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_FUNCTION, hash_search(), HASHCTL::hcxt, heap_beginscan(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleGetOid, HASHCTL::keysize, NULL, PGSTAT_TAB_HASH_SIZE, and SnapshotNow.

Referenced by pgstat_vacuum_stat().

{
    HTAB       *htab;
    HASHCTL     hash_ctl;
    Relation    rel;
    HeapScanDesc scan;
    HeapTuple   tup;

    memset(&hash_ctl, 0, sizeof(hash_ctl));
    hash_ctl.keysize = sizeof(Oid);
    hash_ctl.entrysize = sizeof(Oid);
    hash_ctl.hash = oid_hash;
    hash_ctl.hcxt = CurrentMemoryContext;
    htab = hash_create("Temporary table of OIDs",
                       PGSTAT_TAB_HASH_SIZE,
                       &hash_ctl,
                       HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);

    rel = heap_open(catalogid, AccessShareLock);
    scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
    while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
    {
        Oid         thisoid = HeapTupleGetOid(tup);

        CHECK_FOR_INTERRUPTS();

        (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
    }
    heap_endscan(scan);
    heap_close(rel, AccessShareLock);

    return htab;
}

void pgstat_count_heap_delete ( Relation  rel  ) 

Definition at line 1812 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, NULL, RelationData::pgstat_info, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_deleted.

Referenced by heap_delete().

{
    PgStat_TableStatus *pgstat_info = rel->pgstat_info;

    if (pgstat_info != NULL)
    {
        /* We have to log the effect at the proper transactional level */
        int         nest_level = GetCurrentTransactionNestLevel();

        if (pgstat_info->trans == NULL ||
            pgstat_info->trans->nest_level != nest_level)
            add_tabstat_xact_level(pgstat_info, nest_level);

        pgstat_info->trans->tuples_deleted++;
    }
}

void pgstat_count_heap_insert ( Relation  rel,
int  n 
)

Definition at line 1766 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, NULL, RelationData::pgstat_info, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_inserted.

Referenced by heap_insert(), and heap_multi_insert().

{
    PgStat_TableStatus *pgstat_info = rel->pgstat_info;

    if (pgstat_info != NULL)
    {
        /* We have to log the effect at the proper transactional level */
        int         nest_level = GetCurrentTransactionNestLevel();

        if (pgstat_info->trans == NULL ||
            pgstat_info->trans->nest_level != nest_level)
            add_tabstat_xact_level(pgstat_info, nest_level);

        pgstat_info->trans->tuples_inserted += n;
    }
}

void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

Definition at line 1787 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, NULL, RelationData::pgstat_info, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_tuples_hot_updated, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_updated.

Referenced by heap_update().

{
    PgStat_TableStatus *pgstat_info = rel->pgstat_info;

    if (pgstat_info != NULL)
    {
        /* We have to log the effect at the proper transactional level */
        int         nest_level = GetCurrentTransactionNestLevel();

        if (pgstat_info->trans == NULL ||
            pgstat_info->trans->nest_level != nest_level)
            add_tabstat_xact_level(pgstat_info, nest_level);

        pgstat_info->trans->tuples_updated++;

        /* t_tuples_hot_updated is nontransactional, so just advance it */
        if (hot)
            pgstat_info->t_counts.t_tuples_hot_updated++;
    }
}

static bool pgstat_db_requested ( Oid  databaseid  )  [static]

Definition at line 5024 of file pgstat.c.

References slist_iter::cur, DBWriteRequest::databaseid, next(), slist_container, and slist_foreach.

Referenced by pgstat_write_statsfiles().

{
    slist_iter  iter;

    /* Check the databases if they need to refresh the stats. */
    slist_foreach(iter, &last_statrequests)
    {
        DBWriteRequest *req = slist_container(DBWriteRequest, next, iter.cur);

        if (req->databaseid == databaseid)
            return true;
    }

    return false;
}

void pgstat_drop_database ( Oid  databaseid  ) 
void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

Definition at line 1538 of file pgstat.c.

References PgStat_FunctionCounts::f_numcalls, PgStat_FunctionCounts::f_self_time, PgStat_FunctionCallUsage::f_start, PgStat_FunctionCounts::f_total_time, PgStat_FunctionCallUsage::fs, have_function_stats, INSTR_TIME_ADD, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, NULL, PgStat_FunctionCallUsage::save_f_total_time, PgStat_FunctionCallUsage::save_total, and total_func_time.

Referenced by EventTriggerInvoke(), ExecCallTriggerFunc(), ExecMakeFunctionResult(), ExecMakeFunctionResultNoSets(), ExecMakeTableFunctionResult(), and fmgr_security_definer().

{
    PgStat_FunctionCounts *fs = fcu->fs;
    instr_time  f_total;
    instr_time  f_others;
    instr_time  f_self;

    /* stats not wanted? */
    if (fs == NULL)
        return;

    /* total elapsed time in this function call */
    INSTR_TIME_SET_CURRENT(f_total);
    INSTR_TIME_SUBTRACT(f_total, fcu->f_start);

    /* self usage: elapsed minus anything already charged to other calls */
    f_others = total_func_time;
    INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
    f_self = f_total;
    INSTR_TIME_SUBTRACT(f_self, f_others);

    /* update backend-wide total time */
    INSTR_TIME_ADD(total_func_time, f_self);

    /*
     * Compute the new f_total_time as the total elapsed time added to the
     * pre-call value of f_total_time.  This is necessary to avoid
     * double-counting any time taken by recursive calls of myself.  (We do
     * not need any similar kluge for self time, since that already excludes
     * any recursive calls.)
     */
    INSTR_TIME_ADD(f_total, fcu->save_f_total_time);

    /* update counters in function stats table */
    if (finalize)
        fs->f_numcalls++;
    fs->f_total_time = f_total;
    INSTR_TIME_ADD(fs->f_self_time, f_self);

    /* indicate that we have something to send */
    have_function_stats = true;
}

static void pgstat_exit ( SIGNAL_ARGS   )  [static]

Definition at line 3317 of file pgstat.c.

References need_exit, and SetLatch().

Referenced by PgstatCollectorMain().

{
    int         save_errno = errno;

    need_exit = true;
    SetLatch(&pgStatLatch);

    errno = save_errno;
}

PgStat_GlobalStats* pgstat_fetch_global ( void   ) 
PgBackendStatus* pgstat_fetch_stat_beentry ( int  beid  ) 
PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid  ) 
PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  func_id  ) 

Definition at line 2230 of file pgstat.c.

References backend_read_statsfile(), PgStat_StatDBEntry::functions, hash_search(), MyDatabaseId, NULL, and pgstat_fetch_stat_dbentry().

Referenced by pg_stat_get_function_calls(), pg_stat_get_function_self_time(), and pg_stat_get_function_total_time().

{
    PgStat_StatDBEntry *dbentry;
    PgStat_StatFuncEntry *funcentry = NULL;

    /* load the stats file if needed */
    backend_read_statsfile();

    /* Lookup our database, then find the requested function.  */
    dbentry = pgstat_fetch_stat_dbentry(MyDatabaseId);
    if (dbentry != NULL && dbentry->functions != NULL)
    {
        funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
                                                         (void *) &func_id,
                                                         HASH_FIND, NULL);
    }

    return funcentry;
}

int pgstat_fetch_stat_numbackends ( void   ) 
PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid  ) 

Definition at line 2174 of file pgstat.c.

References backend_read_statsfile(), hash_search(), MyDatabaseId, NULL, and PgStat_StatDBEntry::tables.

Referenced by pg_stat_get_analyze_count(), pg_stat_get_autoanalyze_count(), pg_stat_get_autovacuum_count(), pg_stat_get_blocks_fetched(), pg_stat_get_blocks_hit(), pg_stat_get_dead_tuples(), pg_stat_get_last_analyze_time(), pg_stat_get_last_autoanalyze_time(), pg_stat_get_last_autovacuum_time(), pg_stat_get_last_vacuum_time(), pg_stat_get_live_tuples(), pg_stat_get_numscans(), pg_stat_get_tuples_deleted(), pg_stat_get_tuples_fetched(), pg_stat_get_tuples_hot_updated(), pg_stat_get_tuples_inserted(), pg_stat_get_tuples_returned(), pg_stat_get_tuples_updated(), and pg_stat_get_vacuum_count().

{
    Oid         dbid;
    PgStat_StatDBEntry *dbentry;
    PgStat_StatTabEntry *tabentry;

    /*
     * If not done for this transaction, read the statistics collector stats
     * file into some hash tables.
     */
    backend_read_statsfile();

    /*
     * Lookup our database, then look in its table hash table.
     */
    dbid = MyDatabaseId;
    dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
                                                 (void *) &dbid,
                                                 HASH_FIND, NULL);
    if (dbentry != NULL && dbentry->tables != NULL)
    {
        tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
                                                       (void *) &relid,
                                                       HASH_FIND, NULL);
        if (tabentry)
            return tabentry;
    }

    /*
     * If we didn't find it, maybe it's a shared table.
     */
    dbid = InvalidOid;
    dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
                                                 (void *) &dbid,
                                                 HASH_FIND, NULL);
    if (dbentry != NULL && dbentry->tables != NULL)
    {
        tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
                                                       (void *) &relid,
                                                       HASH_FIND, NULL);
        if (tabentry)
            return tabentry;
    }

    return NULL;
}

const char* pgstat_get_backend_current_activity ( int  pid,
bool  checkUser 
)

Definition at line 2826 of file pgstat.c.

References CHECK_FOR_INTERRUPTS, GetUserId(), i, MaxBackends, PgBackendStatus::st_activity, PgBackendStatus::st_changecount, PgBackendStatus::st_procpid, PgBackendStatus::st_userid, and superuser().

Referenced by DeadLockReport().

{
    PgBackendStatus *beentry;
    int         i;

    beentry = BackendStatusArray;
    for (i = 1; i <= MaxBackends; i++)
    {
        /*
         * Although we expect the target backend's entry to be stable, that
         * doesn't imply that anyone else's is.  To avoid identifying the
         * wrong backend, while we check for a match to the desired PID we
         * must follow the protocol of retrying if st_changecount changes
         * while we examine the entry, or if it's odd.  (This might be
         * unnecessary, since fetching or storing an int is almost certainly
         * atomic, but let's play it safe.)  We use a volatile pointer here to
         * ensure the compiler doesn't try to get cute.
         */
        volatile PgBackendStatus *vbeentry = beentry;
        bool        found;

        for (;;)
        {
            int         save_changecount = vbeentry->st_changecount;

            found = (vbeentry->st_procpid == pid);

            if (save_changecount == vbeentry->st_changecount &&
                (save_changecount & 1) == 0)
                break;

            /* Make sure we can break out of loop if stuck... */
            CHECK_FOR_INTERRUPTS();
        }

        if (found)
        {
            /* Now it is safe to use the non-volatile pointer */
            if (checkUser && !superuser() && beentry->st_userid != GetUserId())
                return "<insufficient privilege>";
            else if (*(beentry->st_activity) == '\0')
                return "<command string not enabled>";
            else
                return beentry->st_activity;
        }

        beentry++;
    }

    /* If we get here, caller is in error ... */
    return "<backend information not available>";
}

const char* pgstat_get_crashed_backend_activity ( int  pid,
char *  buffer,
int  buflen 
)

Definition at line 2896 of file pgstat.c.

References ascii_safe_strlcpy(), i, MaxBackends, Min, NULL, pgstat_track_activity_query_size, PgBackendStatus::st_activity, and PgBackendStatus::st_procpid.

Referenced by LogChildExit().

{
    volatile PgBackendStatus *beentry;
    int         i;

    beentry = BackendStatusArray;

    /*
     * We probably shouldn't get here before shared memory has been set up,
     * but be safe.
     */
    if (beentry == NULL || BackendActivityBuffer == NULL)
        return NULL;

    for (i = 1; i <= MaxBackends; i++)
    {
        if (beentry->st_procpid == pid)
        {
            /* Read pointer just once, so it can't change after validation */
            const char *activity = beentry->st_activity;
            const char *activity_last;

            /*
             * We mustn't access activity string before we verify that it
             * falls within the BackendActivityBuffer. To make sure that the
             * entire string including its ending is contained within the
             * buffer, subtract one activity length from the buffer size.
             */
            activity_last = BackendActivityBuffer + BackendActivityBufferSize
                - pgstat_track_activity_query_size;

            if (activity < BackendActivityBuffer ||
                activity > activity_last)
                return NULL;

            /* If no string available, no point in a report */
            if (activity[0] == '\0')
                return NULL;

            /*
             * Copy only ASCII-safe characters so we don't run into encoding
             * problems when reporting the message; and be sure not to run off
             * the end of memory.
             */
            ascii_safe_strlcpy(buffer, activity,
                               Min(buflen, pgstat_track_activity_query_size));

            return buffer;
        }

        beentry++;
    }

    /* PID not found */
    return NULL;
}

static PgStat_StatDBEntry * pgstat_get_db_entry ( Oid  databaseid,
bool  create 
) [static]

Definition at line 3397 of file pgstat.c.

References HASH_ENTER, hash_search(), and reset_dbentry_counters().

Referenced by pgstat_recv_analyze(), pgstat_recv_autovac(), pgstat_recv_deadlock(), pgstat_recv_dropdb(), pgstat_recv_funcpurge(), pgstat_recv_funcstat(), pgstat_recv_inquiry(), pgstat_recv_recoveryconflict(), pgstat_recv_resetcounter(), pgstat_recv_resetsinglecounter(), pgstat_recv_tabpurge(), pgstat_recv_tabstat(), pgstat_recv_tempfile(), and pgstat_recv_vacuum().

{
    PgStat_StatDBEntry *result;
    bool        found;
    HASHACTION  action = (create ? HASH_ENTER : HASH_FIND);

    /* Lookup or create the hash table entry for this database */
    result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
                                                &databaseid,
                                                action, &found);

    if (!create && !found)
        return NULL;

    /*
     * If not found, initialize the new one.  This creates empty hash tables
     * for tables and functions, too.
     */
    if (!found)
        reset_dbentry_counters(result);

    return result;
}

static PgStat_StatTabEntry * pgstat_get_tab_entry ( PgStat_StatDBEntry dbentry,
Oid  tableoid,
bool  create 
) [static]

Definition at line 3428 of file pgstat.c.

References PgStat_StatTabEntry::analyze_count, PgStat_StatTabEntry::analyze_timestamp, PgStat_StatTabEntry::autovac_analyze_count, PgStat_StatTabEntry::autovac_analyze_timestamp, PgStat_StatTabEntry::autovac_vacuum_count, PgStat_StatTabEntry::autovac_vacuum_timestamp, PgStat_StatTabEntry::blocks_fetched, PgStat_StatTabEntry::blocks_hit, PgStat_StatTabEntry::changes_since_analyze, HASH_ENTER, hash_search(), PgStat_StatTabEntry::n_dead_tuples, PgStat_StatTabEntry::n_live_tuples, PgStat_StatTabEntry::numscans, PgStat_StatDBEntry::tables, PgStat_StatTabEntry::tuples_deleted, PgStat_StatTabEntry::tuples_fetched, PgStat_StatTabEntry::tuples_hot_updated, PgStat_StatTabEntry::tuples_inserted, PgStat_StatTabEntry::tuples_returned, PgStat_StatTabEntry::tuples_updated, PgStat_StatTabEntry::vacuum_count, and PgStat_StatTabEntry::vacuum_timestamp.

Referenced by pgstat_recv_analyze(), and pgstat_recv_vacuum().

{
    PgStat_StatTabEntry *result;
    bool        found;
    HASHACTION  action = (create ? HASH_ENTER : HASH_FIND);

    /* Lookup or create the hash table entry for this table */
    result = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
                                                 &tableoid,
                                                 action, &found);

    if (!create && !found)
        return NULL;

    /* If not found, initialize the new one. */
    if (!found)
    {
        result->numscans = 0;
        result->tuples_returned = 0;
        result->tuples_fetched = 0;
        result->tuples_inserted = 0;
        result->tuples_updated = 0;
        result->tuples_deleted = 0;
        result->tuples_hot_updated = 0;
        result->n_live_tuples = 0;
        result->n_dead_tuples = 0;
        result->changes_since_analyze = 0;
        result->blocks_fetched = 0;
        result->blocks_hit = 0;
        result->vacuum_timestamp = 0;
        result->vacuum_count = 0;
        result->autovac_vacuum_timestamp = 0;
        result->autovac_vacuum_count = 0;
        result->analyze_timestamp = 0;
        result->analyze_count = 0;
        result->autovac_analyze_timestamp = 0;
        result->autovac_analyze_count = 0;
    }

    return result;
}

void pgstat_init ( void   ) 

Definition at line 316 of file pgstat.c.

References addrinfo::ai_next, closesocket, connect, EINTR, ereport, errcode(), errcode_for_socket_access(), errmsg(), gai_strerror, LOG, NULL, pg_freeaddrinfo_all(), pg_getaddrinfo_all(), pg_set_noblock(), PGC_INTERNAL, PGC_S_OVERRIDE, PGINVALID_SOCKET, pgStatAddr, pgStatSock, recv, select, send, SetConfigOption(), socket, and TESTBYTEVAL.

Referenced by PostmasterMain().

{
    ACCEPT_TYPE_ARG3 alen;
    struct addrinfo *addrs = NULL,
               *addr,
                hints;
    int         ret;
    fd_set      rset;
    struct timeval tv;
    char        test_byte;
    int         sel_res;
    int         tries = 0;

#define TESTBYTEVAL ((char) 199)

    /*
     * Create the UDP socket for sending and receiving statistic messages
     */
    hints.ai_flags = AI_PASSIVE;
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = 0;
    hints.ai_addrlen = 0;
    hints.ai_addr = NULL;
    hints.ai_canonname = NULL;
    hints.ai_next = NULL;
    ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
    if (ret || !addrs)
    {
        ereport(LOG,
                (errmsg("could not resolve \"localhost\": %s",
                        gai_strerror(ret))));
        goto startup_failed;
    }

    /*
     * On some platforms, pg_getaddrinfo_all() may return multiple addresses
     * only one of which will actually work (eg, both IPv6 and IPv4 addresses
     * when kernel will reject IPv6).  Worse, the failure may occur at the
     * bind() or perhaps even connect() stage.  So we must loop through the
     * results till we find a working combination. We will generate LOG
     * messages, but no error, for bogus combinations.
     */
    for (addr = addrs; addr; addr = addr->ai_next)
    {
#ifdef HAVE_UNIX_SOCKETS
        /* Ignore AF_UNIX sockets, if any are returned. */
        if (addr->ai_family == AF_UNIX)
            continue;
#endif

        if (++tries > 1)
            ereport(LOG,
            (errmsg("trying another address for the statistics collector")));

        /*
         * Create the socket.
         */
        if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) == PGINVALID_SOCKET)
        {
            ereport(LOG,
                    (errcode_for_socket_access(),
            errmsg("could not create socket for statistics collector: %m")));
            continue;
        }

        /*
         * Bind it to a kernel assigned port on localhost and get the assigned
         * port via getsockname().
         */
        if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
        {
            ereport(LOG,
                    (errcode_for_socket_access(),
              errmsg("could not bind socket for statistics collector: %m")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        alen = sizeof(pgStatAddr);
        if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
        {
            ereport(LOG,
                    (errcode_for_socket_access(),
                     errmsg("could not get address of socket for statistics collector: %m")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        /*
         * Connect the socket to its own address.  This saves a few cycles by
         * not having to respecify the target address on every send. This also
         * provides a kernel-level check that only packets from this same
         * address will be received.
         */
        if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
        {
            ereport(LOG,
                    (errcode_for_socket_access(),
            errmsg("could not connect socket for statistics collector: %m")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        /*
         * Try to send and receive a one-byte test message on the socket. This
         * is to catch situations where the socket can be created but will not
         * actually pass data (for instance, because kernel packet filtering
         * rules prevent it).
         */
        test_byte = TESTBYTEVAL;

retry1:
        if (send(pgStatSock, &test_byte, 1, 0) != 1)
        {
            if (errno == EINTR)
                goto retry1;    /* if interrupted, just retry */
            ereport(LOG,
                    (errcode_for_socket_access(),
                     errmsg("could not send test message on socket for statistics collector: %m")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        /*
         * There could possibly be a little delay before the message can be
         * received.  We arbitrarily allow up to half a second before deciding
         * it's broken.
         */
        for (;;)                /* need a loop to handle EINTR */
        {
            FD_ZERO(&rset);
            FD_SET(pgStatSock, &rset);

            tv.tv_sec = 0;
            tv.tv_usec = 500000;
            sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
            if (sel_res >= 0 || errno != EINTR)
                break;
        }
        if (sel_res < 0)
        {
            ereport(LOG,
                    (errcode_for_socket_access(),
                     errmsg("select() failed in statistics collector: %m")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }
        if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
        {
            /*
             * This is the case we actually think is likely, so take pains to
             * give a specific message for it.
             *
             * errno will not be set meaningfully here, so don't use it.
             */
            ereport(LOG,
                    (errcode(ERRCODE_CONNECTION_FAILURE),
                     errmsg("test message did not get through on socket for statistics collector")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        test_byte++;            /* just make sure variable is changed */

retry2:
        if (recv(pgStatSock, &test_byte, 1, 0) != 1)
        {
            if (errno == EINTR)
                goto retry2;    /* if interrupted, just retry */
            ereport(LOG,
                    (errcode_for_socket_access(),
                     errmsg("could not receive test message on socket for statistics collector: %m")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        if (test_byte != TESTBYTEVAL)   /* strictly paranoia ... */
        {
            ereport(LOG,
                    (errcode(ERRCODE_INTERNAL_ERROR),
                     errmsg("incorrect test message transmission on socket for statistics collector")));
            closesocket(pgStatSock);
            pgStatSock = PGINVALID_SOCKET;
            continue;
        }

        /* If we get here, we have a working socket */
        break;
    }

    /* Did we find a working address? */
    if (!addr || pgStatSock == PGINVALID_SOCKET)
        goto startup_failed;

    /*
     * Set the socket to non-blocking IO.  This ensures that if the collector
     * falls behind, statistics messages will be discarded; backends won't
     * block waiting to send messages to the collector.
     */
    if (!pg_set_noblock(pgStatSock))
    {
        ereport(LOG,
                (errcode_for_socket_access(),
                 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
        goto startup_failed;
    }

    pg_freeaddrinfo_all(hints.ai_family, addrs);

    return;

startup_failed:
    ereport(LOG,
      (errmsg("disabling statistics collector for lack of working socket")));

    if (addrs)
        pg_freeaddrinfo_all(hints.ai_family, addrs);

    if (pgStatSock != PGINVALID_SOCKET)
        closesocket(pgStatSock);
    pgStatSock = PGINVALID_SOCKET;

    /*
     * Adjust GUC variables to suppress useless activity, and for debugging
     * purposes (seeing track_counts off is a clue that we failed here). We
     * use PGC_S_OVERRIDE because there is no point in trying to turn it back
     * on from postgresql.conf without a restart.
     */
    SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
}

void pgstat_init_function_usage ( FunctionCallInfoData fcinfo,
PgStat_FunctionCallUsage fcu 
)

Definition at line 1465 of file pgstat.c.

References HASHCTL::entrysize, PgStat_BackendFunctionEntry::f_counts, PgStat_FunctionCallUsage::f_start, PgStat_FunctionCounts::f_total_time, FunctionCallInfoData::flinfo, FmgrInfo::fn_oid, PgStat_FunctionCallUsage::fs, HASHCTL::hash, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FUNCTION, hash_search(), INSTR_TIME_SET_CURRENT, HASHCTL::keysize, MemSet, PGSTAT_FUNCTION_HASH_SIZE, PgStat_FunctionCallUsage::save_f_total_time, PgStat_FunctionCallUsage::save_total, and total_func_time.

Referenced by EventTriggerInvoke(), ExecCallTriggerFunc(), ExecMakeFunctionResult(), ExecMakeFunctionResultNoSets(), ExecMakeTableFunctionResult(), and fmgr_security_definer().

{
    PgStat_BackendFunctionEntry *htabent;
    bool        found;

    if (pgstat_track_functions <= fcinfo->flinfo->fn_stats)
    {
        /* stats not wanted */
        fcu->fs = NULL;
        return;
    }

    if (!pgStatFunctions)
    {
        /* First time through - initialize function stat table */
        HASHCTL     hash_ctl;

        memset(&hash_ctl, 0, sizeof(hash_ctl));
        hash_ctl.keysize = sizeof(Oid);
        hash_ctl.entrysize = sizeof(PgStat_BackendFunctionEntry);
        hash_ctl.hash = oid_hash;
        pgStatFunctions = hash_create("Function stat entries",
                                      PGSTAT_FUNCTION_HASH_SIZE,
                                      &hash_ctl,
                                      HASH_ELEM | HASH_FUNCTION);
    }

    /* Get the stats entry for this function, create if necessary */
    htabent = hash_search(pgStatFunctions, &fcinfo->flinfo->fn_oid,
                          HASH_ENTER, &found);
    if (!found)
        MemSet(&htabent->f_counts, 0, sizeof(PgStat_FunctionCounts));

    fcu->fs = &htabent->f_counts;

    /* save stats for this function, later used to compensate for recursion */
    fcu->save_f_total_time = htabent->f_counts.f_total_time;

    /* save current backend-wide total time */
    fcu->save_total = total_func_time;

    /* get clock time as of function start */
    INSTR_TIME_SET_CURRENT(fcu->f_start);
}

void pgstat_initialize ( void   ) 

Definition at line 2431 of file pgstat.c.

References Assert, MaxBackends, MyBackendId, on_shmem_exit(), and pgstat_beshutdown_hook().

Referenced by InitPostgres().

{
    /* Initialize MyBEEntry */
    Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
    MyBEEntry = &BackendStatusArray[MyBackendId - 1];

    /* Set up a process-exit hook to clean up */
    on_shmem_exit(pgstat_beshutdown_hook, 0);
}

void pgstat_initstats ( Relation  rel  ) 

Definition at line 1595 of file pgstat.c.

References get_tabstat_entry(), NULL, PGINVALID_SOCKET, RelationData::pgstat_info, pgstat_track_counts, pgStatSock, RelationData::rd_id, RelationData::rd_rel, RELKIND_INDEX, RELKIND_MATVIEW, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_TOASTVALUE, and PgStat_TableStatus::t_id.

Referenced by relation_open(), and try_relation_open().

{
    Oid         rel_id = rel->rd_id;
    char        relkind = rel->rd_rel->relkind;

    /* We only count stats for things that have storage */
    if (!(relkind == RELKIND_RELATION ||
          relkind == RELKIND_MATVIEW ||
          relkind == RELKIND_INDEX ||
          relkind == RELKIND_TOASTVALUE ||
          relkind == RELKIND_SEQUENCE))
    {
        rel->pgstat_info = NULL;
        return;
    }

    if (pgStatSock == PGINVALID_SOCKET || !pgstat_track_counts)
    {
        /* We're not counting at all */
        rel->pgstat_info = NULL;
        return;
    }

    /*
     * If we already set up this relation in the current transaction, nothing
     * to do.
     */
    if (rel->pgstat_info != NULL &&
        rel->pgstat_info->t_id == rel_id)
        return;

    /* Else find or make the PgStat_TableStatus entry, and update link */
    rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
}

void pgstat_ping ( void   ) 
static void pgstat_read_current_status ( void   )  [static]

Definition at line 2726 of file pgstat.c.

References Assert, CHECK_FOR_INTERRUPTS, i, localNumBackends, MaxBackends, MemoryContextAlloc(), NAMEDATALEN, pgstat_setup_memcxt(), pgstat_track_activity_query_size, pgStatRunningInCollector, PgBackendStatus::st_activity, PgBackendStatus::st_appname, PgBackendStatus::st_changecount, and PgBackendStatus::st_procpid.

Referenced by pgstat_fetch_stat_beentry(), and pgstat_fetch_stat_numbackends().

{
    volatile PgBackendStatus *beentry;
    PgBackendStatus *localtable;
    PgBackendStatus *localentry;
    char       *localappname,
               *localactivity;
    int         i;

    Assert(!pgStatRunningInCollector);
    if (localBackendStatusTable)
        return;                 /* already done */

    pgstat_setup_memcxt();

    localtable = (PgBackendStatus *)
        MemoryContextAlloc(pgStatLocalContext,
                           sizeof(PgBackendStatus) * MaxBackends);
    localappname = (char *)
        MemoryContextAlloc(pgStatLocalContext,
                           NAMEDATALEN * MaxBackends);
    localactivity = (char *)
        MemoryContextAlloc(pgStatLocalContext,
                           pgstat_track_activity_query_size * MaxBackends);
    localNumBackends = 0;

    beentry = BackendStatusArray;
    localentry = localtable;
    for (i = 1; i <= MaxBackends; i++)
    {
        /*
         * Follow the protocol of retrying if st_changecount changes while we
         * copy the entry, or if it's odd.  (The check for odd is needed to
         * cover the case where we are able to completely copy the entry while
         * the source backend is between increment steps.)  We use a volatile
         * pointer here to ensure the compiler doesn't try to get cute.
         */
        for (;;)
        {
            int         save_changecount = beentry->st_changecount;

            localentry->st_procpid = beentry->st_procpid;
            if (localentry->st_procpid > 0)
            {
                memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));

                /*
                 * strcpy is safe even if the string is modified concurrently,
                 * because there's always a \0 at the end of the buffer.
                 */
                strcpy(localappname, (char *) beentry->st_appname);
                localentry->st_appname = localappname;
                strcpy(localactivity, (char *) beentry->st_activity);
                localentry->st_activity = localactivity;
            }

            if (save_changecount == beentry->st_changecount &&
                (save_changecount & 1) == 0)
                break;

            /* Make sure we can break out of loop if stuck... */
            CHECK_FOR_INTERRUPTS();
        }

        beentry++;
        /* Only valid entries get included into the local array */
        if (localentry->st_procpid > 0)
        {
            localentry++;
            localappname += NAMEDATALEN;
            localactivity += pgstat_track_activity_query_size;
            localNumBackends++;
        }
    }

    /* Set the pointer only after completion of a valid table */
    localBackendStatusTable = localtable;
}

static void pgstat_read_db_statsfile ( Oid  databaseid,
HTAB tabhash,
HTAB funchash,
bool  permanent 
) [static]

Definition at line 3959 of file pgstat.c.

References AllocateFile(), DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStat_StatFuncEntry::functionid, get_dbstat_filename(), hash_search(), LOG, MAXPGPATH, NULL, PG_BINARY_R, PGSTAT_FILE_FORMAT_ID, pgStatRunningInCollector, PgStat_StatTabEntry::tableid, unlink(), and WARNING.

Referenced by pgstat_read_statsfiles().

{
    PgStat_StatTabEntry *tabentry;
    PgStat_StatTabEntry tabbuf;
    PgStat_StatFuncEntry funcbuf;
    PgStat_StatFuncEntry *funcentry;
    FILE       *fpin;
    int32       format_id;
    bool        found;
    char        statfile[MAXPGPATH];

    get_dbstat_filename(permanent, false, databaseid, statfile, MAXPGPATH);

    /*
     * Try to open the stats file. If it doesn't exist, the backends simply
     * return zero for anything and the collector simply starts from scratch
     * with empty counters.
     *
     * ENOENT is a possibility if the stats collector is not running or has
     * not yet written the stats file the first time.  Any other failure
     * condition is suspicious.
     */
    if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
    {
        if (errno != ENOENT)
            ereport(pgStatRunningInCollector ? LOG : WARNING,
                    (errcode_for_file_access(),
                     errmsg("could not open statistics file \"%s\": %m",
                            statfile)));
        return;
    }

    /*
     * Verify it's of the expected format.
     */
    if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
        format_id != PGSTAT_FILE_FORMAT_ID)
    {
        ereport(pgStatRunningInCollector ? LOG : WARNING,
                (errmsg("corrupted statistics file \"%s\"", statfile)));
        goto done;
    }

    /*
     * We found an existing collector stats file. Read it and put all the
     * hashtable entries into place.
     */
    for (;;)
    {
        switch (fgetc(fpin))
        {
                /*
                 * 'T'  A PgStat_StatTabEntry follows.
                 */
            case 'T':
                if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
                          fpin) != sizeof(PgStat_StatTabEntry))
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                /*
                 * Skip if table belongs to a not requested database.
                 */
                if (tabhash == NULL)
                    break;

                tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
                                                    (void *) &tabbuf.tableid,
                                                         HASH_ENTER, &found);

                if (found)
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                memcpy(tabentry, &tabbuf, sizeof(tabbuf));
                break;

                /*
                 * 'F'  A PgStat_StatFuncEntry follows.
                 */
            case 'F':
                if (fread(&funcbuf, 1, sizeof(PgStat_StatFuncEntry),
                          fpin) != sizeof(PgStat_StatFuncEntry))
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                /*
                 * Skip if function belongs to a not requested database.
                 */
                if (funchash == NULL)
                    break;

                funcentry = (PgStat_StatFuncEntry *) hash_search(funchash,
                                                (void *) &funcbuf.functionid,
                                                         HASH_ENTER, &found);

                if (found)
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                memcpy(funcentry, &funcbuf, sizeof(funcbuf));
                break;

                /*
                 * 'E'  The EOF marker of a complete stats file.
                 */
            case 'E':
                goto done;

            default:
                ereport(pgStatRunningInCollector ? LOG : WARNING,
                        (errmsg("corrupted statistics file \"%s\"",
                                statfile)));
                goto done;
        }
    }

done:
    FreeFile(fpin);

    if (permanent)
    {
        elog(DEBUG2, "removing permanent stats file '%s'", statfile);
        unlink(statfile);
    }

    return;
}

static bool pgstat_read_db_statsfile_timestamp ( Oid  databaseid,
bool  permanent,
TimestampTz ts 
) [static]

Definition at line 4123 of file pgstat.c.

References AllocateFile(), PgStat_StatDBEntry::databaseid, ereport, errcode_for_file_access(), errmsg(), FreeFile(), LOG, NULL, offsetof, PG_BINARY_R, PGSTAT_FILE_FORMAT_ID, pgstat_stat_filename, PGSTAT_STAT_PERMANENT_FILENAME, pgStatRunningInCollector, PgStat_StatDBEntry::stats_timestamp, PgStat_GlobalStats::stats_timestamp, and WARNING.

Referenced by backend_read_statsfile().

{
    PgStat_StatDBEntry dbentry;
    PgStat_GlobalStats myGlobalStats;
    FILE       *fpin;
    int32       format_id;
    const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;

    /*
     * Try to open the stats file.  As above, anything but ENOENT is worthy of
     * complaining about.
     */
    if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
    {
        if (errno != ENOENT)
            ereport(pgStatRunningInCollector ? LOG : WARNING,
                    (errcode_for_file_access(),
                     errmsg("could not open statistics file \"%s\": %m",
                            statfile)));
        return false;
    }

    /*
     * Verify it's of the expected format.
     */
    if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
        format_id != PGSTAT_FILE_FORMAT_ID)
    {
        ereport(pgStatRunningInCollector ? LOG : WARNING,
                (errmsg("corrupted statistics file \"%s\"", statfile)));
        FreeFile(fpin);
        return false;
    }

    /*
     * Read global stats struct
     */
    if (fread(&myGlobalStats, 1, sizeof(myGlobalStats),
              fpin) != sizeof(myGlobalStats))
    {
        ereport(pgStatRunningInCollector ? LOG : WARNING,
                (errmsg("corrupted statistics file \"%s\"", statfile)));
        FreeFile(fpin);
        return false;
    }

    /* By default, we're going to return the timestamp of the global file. */
    *ts = myGlobalStats.stats_timestamp;

    /*
     * We found an existing collector stats file.  Read it and look for a
     * record for the requested database.  If found, use its timestamp.
     */
    for (;;)
    {
        switch (fgetc(fpin))
        {
                /*
                 * 'D'  A PgStat_StatDBEntry struct describing a database
                 * follows.
                 */
            case 'D':
                if (fread(&dbentry, 1, offsetof(PgStat_StatDBEntry, tables),
                          fpin) != offsetof(PgStat_StatDBEntry, tables))
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                /*
                 * If this is the DB we're looking for, save its timestamp and
                 * we're done.
                 */
                if (dbentry.databaseid == databaseid)
                {
                    *ts = dbentry.stats_timestamp;
                    goto done;
                }

                break;

            case 'E':
                goto done;

            default:
                ereport(pgStatRunningInCollector ? LOG : WARNING,
                        (errmsg("corrupted statistics file \"%s\"",
                                statfile)));
                goto done;
        }
    }

done:
    FreeFile(fpin);
    return true;
}

static HTAB * pgstat_read_statsfiles ( Oid  onlydb,
bool  permanent,
bool  deep 
) [static]

Definition at line 3759 of file pgstat.c.

References AllocateFile(), PgStat_StatDBEntry::databaseid, DEBUG2, elog, HASHCTL::entrysize, ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStat_StatDBEntry::functions, GetCurrentTimestamp(), HASHCTL::hash, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_FUNCTION, hash_search(), HASHCTL::hcxt, InvalidOid, HASHCTL::keysize, LOG, NULL, offsetof, PG_BINARY_R, PGSTAT_DB_HASH_SIZE, PGSTAT_FILE_FORMAT_ID, PGSTAT_FUNCTION_HASH_SIZE, pgstat_read_db_statsfile(), pgstat_setup_memcxt(), pgstat_stat_filename, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_TAB_HASH_SIZE, pgStatRunningInCollector, PgStat_GlobalStats::stat_reset_timestamp, PgStat_StatDBEntry::tables, unlink(), and WARNING.

Referenced by backend_read_statsfile(), and PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;
    PgStat_StatDBEntry dbbuf;
    HASHCTL     hash_ctl;
    HTAB       *dbhash;
    FILE       *fpin;
    int32       format_id;
    bool        found;
    const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;

    /*
     * The tables will live in pgStatLocalContext.
     */
    pgstat_setup_memcxt();

    /*
     * Create the DB hashtable
     */
    memset(&hash_ctl, 0, sizeof(hash_ctl));
    hash_ctl.keysize = sizeof(Oid);
    hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
    hash_ctl.hash = oid_hash;
    hash_ctl.hcxt = pgStatLocalContext;
    dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
                         HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);

    /*
     * Clear out global statistics so they start from zero in case we can't
     * load an existing statsfile.
     */
    memset(&globalStats, 0, sizeof(globalStats));

    /*
     * Set the current timestamp (will be kept only in case we can't load an
     * existing statsfile).
     */
    globalStats.stat_reset_timestamp = GetCurrentTimestamp();

    /*
     * Try to open the stats file. If it doesn't exist, the backends simply
     * return zero for anything and the collector simply starts from scratch
     * with empty counters.
     *
     * ENOENT is a possibility if the stats collector is not running or has
     * not yet written the stats file the first time.  Any other failure
     * condition is suspicious.
     */
    if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
    {
        if (errno != ENOENT)
            ereport(pgStatRunningInCollector ? LOG : WARNING,
                    (errcode_for_file_access(),
                     errmsg("could not open statistics file \"%s\": %m",
                            statfile)));
        return dbhash;
    }

    /*
     * Verify it's of the expected format.
     */
    if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
        format_id != PGSTAT_FILE_FORMAT_ID)
    {
        ereport(pgStatRunningInCollector ? LOG : WARNING,
                (errmsg("corrupted statistics file \"%s\"", statfile)));
        goto done;
    }

    /*
     * Read global stats struct
     */
    if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
    {
        ereport(pgStatRunningInCollector ? LOG : WARNING,
                (errmsg("corrupted statistics file \"%s\"", statfile)));
        goto done;
    }

    /*
     * We found an existing collector stats file. Read it and put all the
     * hashtable entries into place.
     */
    for (;;)
    {
        switch (fgetc(fpin))
        {
                /*
                 * 'D'  A PgStat_StatDBEntry struct describing a database
                 * follows.
                 */
            case 'D':
                if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
                          fpin) != offsetof(PgStat_StatDBEntry, tables))
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                /*
                 * Add to the DB hash
                 */
                dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
                                                  (void *) &dbbuf.databaseid,
                                                             HASH_ENTER,
                                                             &found);
                if (found)
                {
                    ereport(pgStatRunningInCollector ? LOG : WARNING,
                            (errmsg("corrupted statistics file \"%s\"",
                                    statfile)));
                    goto done;
                }

                memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
                dbentry->tables = NULL;
                dbentry->functions = NULL;

                /*
                 * Don't collect tables if not the requested DB (or the
                 * shared-table info)
                 */
                if (onlydb != InvalidOid)
                {
                    if (dbbuf.databaseid != onlydb &&
                        dbbuf.databaseid != InvalidOid)
                        break;
                }

                memset(&hash_ctl, 0, sizeof(hash_ctl));
                hash_ctl.keysize = sizeof(Oid);
                hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
                hash_ctl.hash = oid_hash;
                hash_ctl.hcxt = pgStatLocalContext;
                dbentry->tables = hash_create("Per-database table",
                                              PGSTAT_TAB_HASH_SIZE,
                                              &hash_ctl,
                                   HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);

                hash_ctl.keysize = sizeof(Oid);
                hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
                hash_ctl.hash = oid_hash;
                hash_ctl.hcxt = pgStatLocalContext;
                dbentry->functions = hash_create("Per-database function",
                                                 PGSTAT_FUNCTION_HASH_SIZE,
                                                 &hash_ctl,
                                   HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);

                /*
                 * If requested, read the data from the database-specific
                 * file. If there was onlydb specified (!= InvalidOid), we
                 * would not get here because of a break above. So we don't
                 * need to recheck.
                 */
                if (deep)
                    pgstat_read_db_statsfile(dbentry->databaseid,
                                             dbentry->tables,
                                             dbentry->functions,
                                             permanent);

                break;

            case 'E':
                goto done;

            default:
                ereport(pgStatRunningInCollector ? LOG : WARNING,
                        (errmsg("corrupted statistics file \"%s\"",
                                statfile)));
                goto done;
        }
    }

done:
    FreeFile(fpin);

    /* If requested to read the permanent file, also get rid of it. */
    if (permanent)
    {
        elog(DEBUG2, "removing permanent stats file '%s'", statfile);
        unlink(statfile);
    }

    return dbhash;
}

static void pgstat_recv_analyze ( PgStat_MsgAnalyze msg,
int  len 
) [static]
static void pgstat_recv_autovac ( PgStat_MsgAutovacStart msg,
int  len 
) [static]

Definition at line 4739 of file pgstat.c.

References PgStat_StatDBEntry::last_autovac_time, PgStat_MsgAutovacStart::m_databaseid, PgStat_MsgAutovacStart::m_start_time, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;

    /*
     * Store the last autovacuum time in the database's hashtable entry.
     */
    dbentry = pgstat_get_db_entry(msg->m_databaseid, true);

    dbentry->last_autovac_time = msg->m_start_time;
}

static void pgstat_recv_bgwriter ( PgStat_MsgBgWriter msg,
int  len 
) [static]

Definition at line 4834 of file pgstat.c.

References PgStat_GlobalStats::buf_alloc, PgStat_GlobalStats::buf_fsync_backend, PgStat_GlobalStats::buf_written_backend, PgStat_GlobalStats::buf_written_checkpoints, PgStat_GlobalStats::buf_written_clean, PgStat_GlobalStats::checkpoint_sync_time, PgStat_GlobalStats::checkpoint_write_time, PgStat_MsgBgWriter::m_buf_alloc, PgStat_MsgBgWriter::m_buf_fsync_backend, PgStat_MsgBgWriter::m_buf_written_backend, PgStat_MsgBgWriter::m_buf_written_checkpoints, PgStat_MsgBgWriter::m_buf_written_clean, PgStat_MsgBgWriter::m_checkpoint_sync_time, PgStat_MsgBgWriter::m_checkpoint_write_time, PgStat_MsgBgWriter::m_maxwritten_clean, PgStat_MsgBgWriter::m_requested_checkpoints, PgStat_MsgBgWriter::m_timed_checkpoints, PgStat_GlobalStats::maxwritten_clean, PgStat_GlobalStats::requested_checkpoints, and PgStat_GlobalStats::timed_checkpoints.

Referenced by PgstatCollectorMain().

static void pgstat_recv_deadlock ( PgStat_MsgDeadlock msg,
int  len 
) [static]

Definition at line 4895 of file pgstat.c.

References PgStat_MsgDeadlock::m_databaseid, PgStat_StatDBEntry::n_deadlocks, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;

    dbentry = pgstat_get_db_entry(msg->m_databaseid, true);

    dbentry->n_deadlocks++;
}

static void pgstat_recv_dropdb ( PgStat_MsgDropdb msg,
int  len 
) [static]

Definition at line 4608 of file pgstat.c.

References DEBUG2, elog, ereport, errmsg(), ERROR, PgStat_StatDBEntry::functions, get_dbstat_filename(), hash_destroy(), HASH_REMOVE, hash_search(), PgStat_MsgDropdb::m_databaseid, MAXPGPATH, NULL, pgstat_get_db_entry(), PgStat_StatDBEntry::tables, and unlink().

Referenced by PgstatCollectorMain().

{
    Oid         dbid = msg->m_databaseid;
    PgStat_StatDBEntry *dbentry;

    /*
     * Lookup the database in the hashtable.
     */
    dbentry = pgstat_get_db_entry(dbid, false);

    /*
     * If found, remove it (along with the db statfile).
     */
    if (dbentry)
    {
        char        statfile[MAXPGPATH];

        get_dbstat_filename(true, false, dbid, statfile, MAXPGPATH);

        elog(DEBUG2, "removing %s", statfile);
        unlink(statfile);

        if (dbentry->tables != NULL)
            hash_destroy(dbentry->tables);
        if (dbentry->functions != NULL)
            hash_destroy(dbentry->functions);

        if (hash_search(pgStatDBHash,
                        (void *) &dbid,
                        HASH_REMOVE, NULL) == NULL)
            ereport(ERROR,
                    (errmsg("database hash table corrupted during cleanup --- abort")));
    }
}

static void pgstat_recv_funcpurge ( PgStat_MsgFuncpurge msg,
int  len 
) [static]

Definition at line 4976 of file pgstat.c.

References PgStat_StatDBEntry::functions, HASH_REMOVE, hash_search(), i, PgStat_MsgFuncpurge::m_databaseid, PgStat_MsgFuncpurge::m_functionid, PgStat_MsgFuncpurge::m_nentries, NULL, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;
    int         i;

    dbentry = pgstat_get_db_entry(msg->m_databaseid, false);

    /*
     * No need to purge if we don't even know the database.
     */
    if (!dbentry || !dbentry->functions)
        return;

    /*
     * Process all function entries in the message.
     */
    for (i = 0; i < msg->m_nentries; i++)
    {
        /* Remove from hashtable if present; we don't care if it's not. */
        (void) hash_search(dbentry->functions,
                           (void *) &(msg->m_functionid[i]),
                           HASH_REMOVE, NULL);
    }
}

static void pgstat_recv_funcstat ( PgStat_MsgFuncstat msg,
int  len 
) [static]

Definition at line 4928 of file pgstat.c.

References PgStat_FunctionEntry::f_id, PgStat_FunctionEntry::f_numcalls, PgStat_StatFuncEntry::f_numcalls, PgStat_FunctionEntry::f_self_time, PgStat_StatFuncEntry::f_self_time, PgStat_FunctionEntry::f_total_time, PgStat_StatFuncEntry::f_total_time, PgStat_StatDBEntry::functions, HASH_ENTER, hash_search(), i, PgStat_MsgFuncstat::m_databaseid, PgStat_MsgFuncstat::m_entry, PgStat_MsgFuncstat::m_nentries, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

{
    PgStat_FunctionEntry *funcmsg = &(msg->m_entry[0]);
    PgStat_StatDBEntry *dbentry;
    PgStat_StatFuncEntry *funcentry;
    int         i;
    bool        found;

    dbentry = pgstat_get_db_entry(msg->m_databaseid, true);

    /*
     * Process all function entries in the message.
     */
    for (i = 0; i < msg->m_nentries; i++, funcmsg++)
    {
        funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
                                                   (void *) &(funcmsg->f_id),
                                                         HASH_ENTER, &found);

        if (!found)
        {
            /*
             * If it's a new function entry, initialize counters to the values
             * we just got.
             */
            funcentry->f_numcalls = funcmsg->f_numcalls;
            funcentry->f_total_time = funcmsg->f_total_time;
            funcentry->f_self_time = funcmsg->f_self_time;
        }
        else
        {
            /*
             * Otherwise add the values to the existing entry.
             */
            funcentry->f_numcalls += funcmsg->f_numcalls;
            funcentry->f_total_time += funcmsg->f_total_time;
            funcentry->f_self_time += funcmsg->f_self_time;
        }
    }
}

static void pgstat_recv_inquiry ( PgStat_MsgInquiry msg,
int  len 
) [static]

Definition at line 4391 of file pgstat.c.

References PgStat_MsgInquiry::clock_time, slist_iter::cur, PgStat_MsgInquiry::cutoff_time, PgStat_StatDBEntry::databaseid, DBWriteRequest::databaseid, PgStat_MsgInquiry::databaseid, DEBUG2, elog, GetCurrentTimestamp(), LOG, DBWriteRequest::next, next(), NULL, palloc(), pfree(), pgstat_get_db_entry(), pstrdup(), DBWriteRequest::request_time, slist_container, slist_foreach, slist_push_head(), PgStat_StatDBEntry::stats_timestamp, and timestamptz_to_str().

Referenced by PgstatCollectorMain().

{
    slist_iter  iter;
    DBWriteRequest *newreq;
    PgStat_StatDBEntry *dbentry;

    elog(DEBUG2, "received inquiry for %d", msg->databaseid);

    /*
     * Find the last write request for this DB.  If it's older than the
     * request's cutoff time, update it; otherwise there's nothing to do.
     *
     * Note that if a request is found, we return early and skip the below
     * check for clock skew.  This is okay, since the only way for a DB request
     * to be present in the list is that we have been here since the last write
     * round.
     */
    slist_foreach(iter, &last_statrequests)
    {
        DBWriteRequest *req = slist_container(DBWriteRequest, next, iter.cur);

        if (req->databaseid != msg->databaseid)
            continue;

        if (msg->cutoff_time > req->request_time)
            req->request_time = msg->cutoff_time;
        return;
    }

    /*
     * There's no request for this DB yet, so create one.
     */
    newreq = palloc(sizeof(DBWriteRequest));

    newreq->databaseid = msg->databaseid;
    newreq->request_time = msg->clock_time;
    slist_push_head(&last_statrequests, &newreq->next);

    /*
     * If the requestor's local clock time is older than stats_timestamp, we
     * should suspect a clock glitch, ie system time going backwards; though
     * the more likely explanation is just delayed message receipt.  It is
     * worth expending a GetCurrentTimestamp call to be sure, since a large
     * retreat in the system clock reading could otherwise cause us to neglect
     * to update the stats file for a long time.
     */
    dbentry = pgstat_get_db_entry(msg->databaseid, false);
    if ((dbentry != NULL) && (msg->clock_time < dbentry->stats_timestamp))
    {
        TimestampTz cur_ts = GetCurrentTimestamp();

        if (cur_ts < dbentry->stats_timestamp)
        {
            /*
             * Sure enough, time went backwards.  Force a new stats file write
             * to get back in sync; but first, log a complaint.
             */
            char       *writetime;
            char       *mytime;

            /* Copy because timestamptz_to_str returns a static buffer */
            writetime = pstrdup(timestamptz_to_str(dbentry->stats_timestamp));
            mytime = pstrdup(timestamptz_to_str(cur_ts));
            elog(LOG,
            "stats_timestamp %s is later than collector's time %s for db %d",
                 writetime, mytime, dbentry->databaseid);
            pfree(writetime);
            pfree(mytime);

            newreq->request_time = cur_ts;
            dbentry->stats_timestamp = cur_ts - 1;
        }
    }
}

static void pgstat_recv_recoveryconflict ( PgStat_MsgRecoveryConflict msg,
int  len 
) [static]
static void pgstat_recv_resetcounter ( PgStat_MsgResetcounter msg,
int  len 
) [static]

Definition at line 4651 of file pgstat.c.

References PgStat_StatDBEntry::functions, hash_destroy(), PgStat_MsgResetcounter::m_databaseid, NULL, pgstat_get_db_entry(), reset_dbentry_counters(), and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;

    /*
     * Lookup the database in the hashtable.  Nothing to do if not there.
     */
    dbentry = pgstat_get_db_entry(msg->m_databaseid, false);

    if (!dbentry)
        return;

    /*
     * We simply throw away all the database's table entries by recreating a
     * new hash table for them.
     */
    if (dbentry->tables != NULL)
        hash_destroy(dbentry->tables);
    if (dbentry->functions != NULL)
        hash_destroy(dbentry->functions);

    dbentry->tables = NULL;
    dbentry->functions = NULL;

    /*
     * Reset database-level stats, too.  This creates empty hash tables for
     * tables and functions.
     */
    reset_dbentry_counters(dbentry);
}

static void pgstat_recv_resetsharedcounter ( PgStat_MsgResetsharedcounter msg,
int  len 
) [static]

Definition at line 4689 of file pgstat.c.

References GetCurrentTimestamp(), PgStat_MsgResetsharedcounter::m_resettarget, RESET_BGWRITER, and PgStat_GlobalStats::stat_reset_timestamp.

Referenced by PgstatCollectorMain().

{
    if (msg->m_resettarget == RESET_BGWRITER)
    {
        /* Reset the global background writer statistics for the cluster. */
        memset(&globalStats, 0, sizeof(globalStats));
        globalStats.stat_reset_timestamp = GetCurrentTimestamp();
    }

    /*
     * Presumably the sender of this message validated the target, don't
     * complain here if it's not valid
     */
}

static void pgstat_recv_resetsinglecounter ( PgStat_MsgResetsinglecounter msg,
int  len 
) [static]

Definition at line 4711 of file pgstat.c.

References PgStat_StatDBEntry::functions, GetCurrentTimestamp(), HASH_REMOVE, hash_search(), PgStat_MsgResetsinglecounter::m_databaseid, PgStat_MsgResetsinglecounter::m_objectid, PgStat_MsgResetsinglecounter::m_resettype, NULL, pgstat_get_db_entry(), RESET_FUNCTION, RESET_TABLE, PgStat_StatDBEntry::stat_reset_timestamp, and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;

    dbentry = pgstat_get_db_entry(msg->m_databaseid, false);

    if (!dbentry)
        return;

    /* Set the reset timestamp for the whole database */
    dbentry->stat_reset_timestamp = GetCurrentTimestamp();

    /* Remove object if it exists, ignore it if not */
    if (msg->m_resettype == RESET_TABLE)
        (void) hash_search(dbentry->tables, (void *) &(msg->m_objectid),
                           HASH_REMOVE, NULL);
    else if (msg->m_resettype == RESET_FUNCTION)
        (void) hash_search(dbentry->functions, (void *) &(msg->m_objectid),
                           HASH_REMOVE, NULL);
}

static void pgstat_recv_tabpurge ( PgStat_MsgTabpurge msg,
int  len 
) [static]

Definition at line 4575 of file pgstat.c.

References HASH_REMOVE, hash_search(), i, PgStat_MsgTabpurge::m_databaseid, PgStat_MsgTabpurge::m_nentries, PgStat_MsgTabpurge::m_tableid, NULL, pgstat_get_db_entry(), and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;
    int         i;

    dbentry = pgstat_get_db_entry(msg->m_databaseid, false);

    /*
     * No need to purge if we don't even know the database.
     */
    if (!dbentry || !dbentry->tables)
        return;

    /*
     * Process all table entries in the message.
     */
    for (i = 0; i < msg->m_nentries; i++)
    {
        /* Remove from hashtable if present; we don't care if it's not. */
        (void) hash_search(dbentry->tables,
                           (void *) &(msg->m_tableid[i]),
                           HASH_REMOVE, NULL);
    }
}

static void pgstat_recv_tabstat ( PgStat_MsgTabstat msg,
int  len 
) [static]

Definition at line 4474 of file pgstat.c.

References PgStat_StatTabEntry::analyze_count, PgStat_StatTabEntry::analyze_timestamp, PgStat_StatTabEntry::autovac_analyze_count, PgStat_StatTabEntry::autovac_analyze_timestamp, PgStat_StatTabEntry::autovac_vacuum_count, PgStat_StatTabEntry::autovac_vacuum_timestamp, PgStat_StatTabEntry::blocks_fetched, PgStat_StatTabEntry::blocks_hit, PgStat_StatTabEntry::changes_since_analyze, HASH_ENTER, hash_search(), i, PgStat_MsgTabstat::m_block_read_time, PgStat_MsgTabstat::m_block_write_time, PgStat_MsgTabstat::m_databaseid, PgStat_MsgTabstat::m_entry, PgStat_MsgTabstat::m_nentries, PgStat_MsgTabstat::m_xact_commit, PgStat_MsgTabstat::m_xact_rollback, Max, PgStat_StatDBEntry::n_block_read_time, PgStat_StatDBEntry::n_block_write_time, PgStat_StatDBEntry::n_blocks_fetched, PgStat_StatDBEntry::n_blocks_hit, PgStat_StatTabEntry::n_dead_tuples, PgStat_StatTabEntry::n_live_tuples, PgStat_StatDBEntry::n_tuples_deleted, PgStat_StatDBEntry::n_tuples_fetched, PgStat_StatDBEntry::n_tuples_inserted, PgStat_StatDBEntry::n_tuples_returned, PgStat_StatDBEntry::n_tuples_updated, PgStat_StatDBEntry::n_xact_commit, PgStat_StatDBEntry::n_xact_rollback, PgStat_StatTabEntry::numscans, pgstat_get_db_entry(), PgStat_TableCounts::t_blocks_fetched, PgStat_TableCounts::t_blocks_hit, PgStat_TableCounts::t_changed_tuples, PgStat_TableEntry::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, PgStat_TableEntry::t_id, PgStat_TableCounts::t_numscans, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_fetched, PgStat_TableCounts::t_tuples_hot_updated, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_returned, PgStat_TableCounts::t_tuples_updated, PgStat_StatDBEntry::tables, PgStat_StatTabEntry::tuples_deleted, PgStat_StatTabEntry::tuples_fetched, PgStat_StatTabEntry::tuples_hot_updated, PgStat_StatTabEntry::tuples_inserted, PgStat_StatTabEntry::tuples_returned, PgStat_StatTabEntry::tuples_updated, PgStat_StatTabEntry::vacuum_count, and PgStat_StatTabEntry::vacuum_timestamp.

Referenced by PgstatCollectorMain().

{
    PgStat_StatDBEntry *dbentry;
    PgStat_StatTabEntry *tabentry;
    int         i;
    bool        found;

    dbentry = pgstat_get_db_entry(msg->m_databaseid, true);

    /*
     * Update database-wide stats.
     */
    dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
    dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
    dbentry->n_block_read_time += msg->m_block_read_time;
    dbentry->n_block_write_time += msg->m_block_write_time;

    /*
     * Process all table entries in the message.
     */
    for (i = 0; i < msg->m_nentries; i++)
    {
        PgStat_TableEntry *tabmsg = &(msg->m_entry[i]);

        tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
                                                    (void *) &(tabmsg->t_id),
                                                       HASH_ENTER, &found);

        if (!found)
        {
            /*
             * If it's a new table entry, initialize counters to the values we
             * just got.
             */
            tabentry->numscans = tabmsg->t_counts.t_numscans;
            tabentry->tuples_returned = tabmsg->t_counts.t_tuples_returned;
            tabentry->tuples_fetched = tabmsg->t_counts.t_tuples_fetched;
            tabentry->tuples_inserted = tabmsg->t_counts.t_tuples_inserted;
            tabentry->tuples_updated = tabmsg->t_counts.t_tuples_updated;
            tabentry->tuples_deleted = tabmsg->t_counts.t_tuples_deleted;
            tabentry->tuples_hot_updated = tabmsg->t_counts.t_tuples_hot_updated;
            tabentry->n_live_tuples = tabmsg->t_counts.t_delta_live_tuples;
            tabentry->n_dead_tuples = tabmsg->t_counts.t_delta_dead_tuples;
            tabentry->changes_since_analyze = tabmsg->t_counts.t_changed_tuples;
            tabentry->blocks_fetched = tabmsg->t_counts.t_blocks_fetched;
            tabentry->blocks_hit = tabmsg->t_counts.t_blocks_hit;

            tabentry->vacuum_timestamp = 0;
            tabentry->vacuum_count = 0;
            tabentry->autovac_vacuum_timestamp = 0;
            tabentry->autovac_vacuum_count = 0;
            tabentry->analyze_timestamp = 0;
            tabentry->analyze_count = 0;
            tabentry->autovac_analyze_timestamp = 0;
            tabentry->autovac_analyze_count = 0;
        }
        else
        {
            /*
             * Otherwise add the values to the existing entry.
             */
            tabentry->numscans += tabmsg->t_counts.t_numscans;
            tabentry->tuples_returned += tabmsg->t_counts.t_tuples_returned;
            tabentry->tuples_fetched += tabmsg->t_counts.t_tuples_fetched;
            tabentry->tuples_inserted += tabmsg->t_counts.t_tuples_inserted;
            tabentry->tuples_updated += tabmsg->t_counts.t_tuples_updated;
            tabentry->tuples_deleted += tabmsg->t_counts.t_tuples_deleted;
            tabentry->tuples_hot_updated += tabmsg->t_counts.t_tuples_hot_updated;
            tabentry->n_live_tuples += tabmsg->t_counts.t_delta_live_tuples;
            tabentry->n_dead_tuples += tabmsg->t_counts.t_delta_dead_tuples;
            tabentry->changes_since_analyze += tabmsg->t_counts.t_changed_tuples;
            tabentry->blocks_fetched += tabmsg->t_counts.t_blocks_fetched;
            tabentry->blocks_hit += tabmsg->t_counts.t_blocks_hit;
        }

        /* Clamp n_live_tuples in case of negative delta_live_tuples */
        tabentry->n_live_tuples = Max(tabentry->n_live_tuples, 0);
        /* Likewise for n_dead_tuples */
        tabentry->n_dead_tuples = Max(tabentry->n_dead_tuples, 0);

        /*
         * Add per-table stats to the per-database entry, too.
         */
        dbentry->n_tuples_returned += tabmsg->t_counts.t_tuples_returned;
        dbentry->n_tuples_fetched += tabmsg->t_counts.t_tuples_fetched;
        dbentry->n_tuples_inserted += tabmsg->t_counts.t_tuples_inserted;
        dbentry->n_tuples_updated += tabmsg->t_counts.t_tuples_updated;
        dbentry->n_tuples_deleted += tabmsg->t_counts.t_tuples_deleted;
        dbentry->n_blocks_fetched += tabmsg->t_counts.t_blocks_fetched;
        dbentry->n_blocks_hit += tabmsg->t_counts.t_blocks_hit;
    }
}

static void pgstat_recv_tempfile ( PgStat_MsgTempFile msg,
int  len 
) [static]
static void pgstat_recv_vacuum ( PgStat_MsgVacuum msg,
int  len 
) [static]
void pgstat_report_activity ( BackendState  state,
const char *  cmd_str 
)

Definition at line 2571 of file pgstat.c.

References Assert, GetCurrentStatementStartTimestamp(), GetCurrentTimestamp(), NULL, pg_mbcliplen(), pgstat_track_activities, pgstat_track_activity_query_size, PgBackendStatus::st_activity, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_changecount, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_waiting, PgBackendStatus::st_xact_start_timestamp, and STATE_DISABLED.

Referenced by autovac_report_activity(), exec_bind_message(), exec_execute_message(), exec_parse_message(), exec_simple_query(), initialize_worker_spi(), PostgresMain(), and worker_spi_main().

{
    volatile PgBackendStatus *beentry = MyBEEntry;
    TimestampTz start_timestamp;
    TimestampTz current_timestamp;
    int         len = 0;

    TRACE_POSTGRESQL_STATEMENT_STATUS(cmd_str);

    if (!beentry)
        return;

    if (!pgstat_track_activities)
    {
        if (beentry->st_state != STATE_DISABLED)
        {
            /*
             * track_activities is disabled, but we last reported a
             * non-disabled state.  As our final update, change the state and
             * clear fields we will not be updating anymore.
             */
            beentry->st_changecount++;
            beentry->st_state = STATE_DISABLED;
            beentry->st_state_start_timestamp = 0;
            beentry->st_activity[0] = '\0';
            beentry->st_activity_start_timestamp = 0;
            /* st_xact_start_timestamp and st_waiting are also disabled */
            beentry->st_xact_start_timestamp = 0;
            beentry->st_waiting = false;
            beentry->st_changecount++;
            Assert((beentry->st_changecount & 1) == 0);
        }
        return;
    }

    /*
     * To minimize the time spent modifying the entry, fetch all the needed
     * data first.
     */
    start_timestamp = GetCurrentStatementStartTimestamp();
    if (cmd_str != NULL)
    {
        len = pg_mbcliplen(cmd_str, strlen(cmd_str),
                           pgstat_track_activity_query_size - 1);
    }
    current_timestamp = GetCurrentTimestamp();

    /*
     * Now update the status entry
     */
    beentry->st_changecount++;

    beentry->st_state = state;
    beentry->st_state_start_timestamp = current_timestamp;

    if (cmd_str != NULL)
    {
        memcpy((char *) beentry->st_activity, cmd_str, len);
        beentry->st_activity[len] = '\0';
        beentry->st_activity_start_timestamp = start_timestamp;
    }

    beentry->st_changecount++;
    Assert((beentry->st_changecount & 1) == 0);
}

void pgstat_report_analyze ( Relation  rel,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples 
)

Definition at line 1319 of file pgstat.c.

References GetCurrentTimestamp(), InvalidOid, IsAutoVacuumWorkerProcess(), PgStat_MsgAnalyze::m_analyzetime, PgStat_MsgAnalyze::m_autovacuum, PgStat_MsgAnalyze::m_databaseid, PgStat_MsgAnalyze::m_dead_tuples, PgStat_MsgAnalyze::m_hdr, PgStat_MsgAnalyze::m_live_tuples, PgStat_MsgAnalyze::m_tableoid, Max, MyDatabaseId, NULL, PGINVALID_SOCKET, RelationData::pgstat_info, PGSTAT_MTYPE_ANALYZE, pgstat_send(), pgstat_setheader(), pgstat_track_counts, pgStatSock, RelationData::rd_rel, RelationGetRelid, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, PgStat_TableXactStatus::tuples_updated, and PgStat_TableXactStatus::upper.

Referenced by do_analyze_rel().

{
    PgStat_MsgAnalyze msg;

    if (pgStatSock == PGINVALID_SOCKET || !pgstat_track_counts)
        return;

    /*
     * Unlike VACUUM, ANALYZE might be running inside a transaction that has
     * already inserted and/or deleted rows in the target table. ANALYZE will
     * have counted such rows as live or dead respectively. Because we will
     * report our counts of such rows at transaction end, we should subtract
     * off these counts from what we send to the collector now, else they'll
     * be double-counted after commit.  (This approach also ensures that the
     * collector ends up with the right numbers if we abort instead of
     * committing.)
     */
    if (rel->pgstat_info != NULL)
    {
        PgStat_TableXactStatus *trans;

        for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
        {
            livetuples -= trans->tuples_inserted - trans->tuples_deleted;
            deadtuples -= trans->tuples_updated + trans->tuples_deleted;
        }
        /* count stuff inserted by already-aborted subxacts, too */
        deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
        /* Since ANALYZE's counts are estimates, we could have underflowed */
        livetuples = Max(livetuples, 0);
        deadtuples = Max(deadtuples, 0);
    }

    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
    msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
    msg.m_tableoid = RelationGetRelid(rel);
    msg.m_autovacuum = IsAutoVacuumWorkerProcess();
    msg.m_analyzetime = GetCurrentTimestamp();
    msg.m_live_tuples = livetuples;
    msg.m_dead_tuples = deadtuples;
    pgstat_send(&msg, sizeof(msg));
}

void pgstat_report_appname ( const char *  appname  ) 

Definition at line 2644 of file pgstat.c.

References Assert, NAMEDATALEN, pg_mbcliplen(), PgBackendStatus::st_appname, and PgBackendStatus::st_changecount.

Referenced by assign_application_name(), and pgstat_bestart().

{
    volatile PgBackendStatus *beentry = MyBEEntry;
    int         len;

    if (!beentry)
        return;

    /* This should be unnecessary if GUC did its job, but be safe */
    len = pg_mbcliplen(appname, strlen(appname), NAMEDATALEN - 1);

    /*
     * Update my status entry, following the protocol of bumping
     * st_changecount before and after.  We use a volatile pointer here to
     * ensure the compiler doesn't try to get cute.
     */
    beentry->st_changecount++;

    memcpy((char *) beentry->st_appname, appname, len);
    beentry->st_appname[len] = '\0';

    beentry->st_changecount++;
    Assert((beentry->st_changecount & 1) == 0);
}

void pgstat_report_autovac ( Oid  dboid  ) 
void pgstat_report_deadlock ( void   ) 
void pgstat_report_recovery_conflict ( int  reason  ) 
void pgstat_report_stat ( bool  force  ) 

Definition at line 713 of file pgstat.c.

References Assert, GetCurrentTransactionStopTimestamp(), have_function_stats, i, PgStat_MsgTabstat::m_databaseid, PgStat_MsgTabstat::m_entry, PgStat_MsgTabstat::m_nentries, memcmp(), MemSet, MyDatabaseId, NULL, PGSTAT_NUM_TABENTRIES, pgstat_send_funcstats(), pgstat_send_tabstat(), PGSTAT_STAT_INTERVAL, pgStatXactCommit, pgStatXactRollback, PgStat_TableEntry::t_counts, PgStat_TableStatus::t_counts, PgStat_TableStatus::t_id, PgStat_TableEntry::t_id, PgStat_TableStatus::t_shared, TimestampDifferenceExceeds(), PgStat_TableStatus::trans, TabStatusArray::tsa_entries, TabStatusArray::tsa_next, and TabStatusArray::tsa_used.

Referenced by pgstat_beshutdown_hook(), and PostgresMain().

{
    /* we assume this inits to all zeroes: */
    static const PgStat_TableCounts all_zeroes;
    static TimestampTz last_report = 0;

    TimestampTz now;
    PgStat_MsgTabstat regular_msg;
    PgStat_MsgTabstat shared_msg;
    TabStatusArray *tsa;
    int         i;

    /* Don't expend a clock check if nothing to do */
    if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
        !have_function_stats && !force)
        return;

    /*
     * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
     * msec since we last sent one, or the caller wants to force stats out.
     */
    now = GetCurrentTransactionStopTimestamp();
    if (!force &&
        !TimestampDifferenceExceeds(last_report, now, PGSTAT_STAT_INTERVAL))
        return;
    last_report = now;

    /*
     * Scan through the TabStatusArray struct(s) to find tables that actually
     * have counts, and build messages to send.  We have to separate shared
     * relations from regular ones because the databaseid field in the message
     * header has to depend on that.
     */
    regular_msg.m_databaseid = MyDatabaseId;
    shared_msg.m_databaseid = InvalidOid;
    regular_msg.m_nentries = 0;
    shared_msg.m_nentries = 0;

    for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
    {
        for (i = 0; i < tsa->tsa_used; i++)
        {
            PgStat_TableStatus *entry = &tsa->tsa_entries[i];
            PgStat_MsgTabstat *this_msg;
            PgStat_TableEntry *this_ent;

            /* Shouldn't have any pending transaction-dependent counts */
            Assert(entry->trans == NULL);

            /*
             * Ignore entries that didn't accumulate any actual counts, such
             * as indexes that were opened by the planner but not used.
             */
            if (memcmp(&entry->t_counts, &all_zeroes,
                       sizeof(PgStat_TableCounts)) == 0)
                continue;

            /*
             * OK, insert data into the appropriate message, and send if full.
             */
            this_msg = entry->t_shared ? &shared_msg : &regular_msg;
            this_ent = &this_msg->m_entry[this_msg->m_nentries];
            this_ent->t_id = entry->t_id;
            memcpy(&this_ent->t_counts, &entry->t_counts,
                   sizeof(PgStat_TableCounts));
            if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
            {
                pgstat_send_tabstat(this_msg);
                this_msg->m_nentries = 0;
            }
        }
        /* zero out TableStatus structs after use */
        MemSet(tsa->tsa_entries, 0,
               tsa->tsa_used * sizeof(PgStat_TableStatus));
        tsa->tsa_used = 0;
    }

    /*
     * Send partial messages.  If force is true, make sure that any pending
     * xact commit/abort gets counted, even if no table stats to send.
     */
    if (regular_msg.m_nentries > 0 ||
        (force && (pgStatXactCommit > 0 || pgStatXactRollback > 0)))
        pgstat_send_tabstat(&regular_msg);
    if (shared_msg.m_nentries > 0)
        pgstat_send_tabstat(&shared_msg);

    /* Now, send function statistics */
    pgstat_send_funcstats();
}

void pgstat_report_tempfile ( size_t  filesize  ) 
void pgstat_report_vacuum ( Oid  tableoid,
bool  shared,
PgStat_Counter  tuples 
)
void pgstat_report_waiting ( bool  waiting  ) 

Definition at line 2702 of file pgstat.c.

References pgstat_track_activities, and PgBackendStatus::st_waiting.

Referenced by WaitOnLock().

{
    volatile PgBackendStatus *beentry = MyBEEntry;

    if (!pgstat_track_activities || !beentry)
        return;

    /*
     * Since this is a single-byte field in a struct that only this process
     * may modify, there seems no need to bother with the st_changecount
     * protocol.  The update must appear atomic in any case.
     */
    beentry->st_waiting = waiting;
}

void pgstat_report_xact_timestamp ( TimestampTz  tstamp  ) 

Definition at line 2674 of file pgstat.c.

References Assert, pgstat_track_activities, PgBackendStatus::st_changecount, and PgBackendStatus::st_xact_start_timestamp.

Referenced by AbortTransaction(), CommitTransaction(), and StartTransaction().

{
    volatile PgBackendStatus *beentry = MyBEEntry;

    if (!pgstat_track_activities || !beentry)
        return;

    /*
     * Update my status entry, following the protocol of bumping
     * st_changecount before and after.  We use a volatile pointer here to
     * ensure the compiler doesn't try to get cute.
     */
    beentry->st_changecount++;
    beentry->st_xact_start_timestamp = tstamp;
    beentry->st_changecount++;
    Assert((beentry->st_changecount & 1) == 0);
}

void pgstat_reset_all ( void   ) 
void pgstat_reset_counters ( void   ) 
static void pgstat_reset_remove_files ( const char *  directory  )  [static]

Definition at line 559 of file pgstat.c.

References AllocateDir(), dirent::d_name, FreeDir(), MAXPGPATH, NULL, pgstat_stat_directory, ReadDir(), snprintf(), and unlink().

Referenced by pgstat_reset_all().

{
    DIR        *dir;
    struct dirent *entry;
    char        fname[MAXPGPATH];

    dir = AllocateDir(pgstat_stat_directory);
    while ((entry = ReadDir(dir, pgstat_stat_directory)) != NULL)
    {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
            continue;

        /* XXX should we try to ignore files other than the ones we write? */

        snprintf(fname, MAXPGPATH, "%s/%s", pgstat_stat_directory,
                 entry->d_name);
        unlink(fname);
    }
    FreeDir(dir);
}

void pgstat_reset_shared_counters ( const char *  target  ) 

Definition at line 1214 of file pgstat.c.

References ereport, errcode(), errhint(), errmsg(), ERROR, PgStat_MsgResetsharedcounter::m_hdr, PgStat_MsgResetsharedcounter::m_resettarget, PGINVALID_SOCKET, PGSTAT_MTYPE_RESETSHAREDCOUNTER, pgstat_send(), pgstat_setheader(), pgStatSock, and superuser().

Referenced by pg_stat_reset_shared().

{
    PgStat_MsgResetsharedcounter msg;

    if (pgStatSock == PGINVALID_SOCKET)
        return;

    if (!superuser())
        ereport(ERROR,
                (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                 errmsg("must be superuser to reset statistics counters")));

    if (strcmp(target, "bgwriter") == 0)
        msg.m_resettarget = RESET_BGWRITER;
    else
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("unrecognized reset target: \"%s\"", target),
                 errhint("Target must be \"bgwriter\".")));

    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETSHAREDCOUNTER);
    pgstat_send(&msg, sizeof(msg));
}

void pgstat_reset_single_counter ( Oid  objoid,
PgStat_Single_Reset_Type  type 
)
static void pgstat_send ( void *  msg,
int  len 
) [static]

Definition at line 2980 of file pgstat.c.

References EINTR, elog, LOG, PGINVALID_SOCKET, pgStatSock, and send.

Referenced by pgstat_drop_database(), pgstat_ping(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_deadlock(), pgstat_report_recovery_conflict(), pgstat_report_tempfile(), pgstat_report_vacuum(), pgstat_reset_counters(), pgstat_reset_shared_counters(), pgstat_reset_single_counter(), pgstat_send_bgwriter(), pgstat_send_funcstats(), pgstat_send_inquiry(), pgstat_send_tabstat(), and pgstat_vacuum_stat().

{
    int         rc;

    if (pgStatSock == PGINVALID_SOCKET)
        return;

    ((PgStat_MsgHdr *) msg)->m_size = len;

    /* We'll retry after EINTR, but ignore all other failures */
    do
    {
        rc = send(pgStatSock, msg, len, 0);
    } while (rc < 0 && errno == EINTR);

#ifdef USE_ASSERT_CHECKING
    /* In debug builds, log send failures ... */
    if (rc < 0)
        elog(LOG, "could not send to statistics collector: %m");
#endif
}

void pgstat_send_bgwriter ( void   ) 

Definition at line 3009 of file pgstat.c.

References PgStat_MsgBgWriter::m_hdr, memcmp(), MemSet, PGSTAT_MTYPE_BGWRITER, pgstat_send(), and pgstat_setheader().

Referenced by BackgroundWriterMain(), CheckpointerMain(), and CheckpointWriteDelay().

{
    /* We assume this initializes to zeroes */
    static const PgStat_MsgBgWriter all_zeroes;

    /*
     * This function can be called even if nothing at all has happened. In
     * this case, avoid sending a completely empty message to the stats
     * collector.
     */
    if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
        return;

    /*
     * Prepare and send the message
     */
    pgstat_setheader(&BgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
    pgstat_send(&BgWriterStats, sizeof(BgWriterStats));

    /*
     * Clear out the statistics buffer, so it can be re-used.
     */
    MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
}

static void pgstat_send_funcstats ( void   )  [static]

Definition at line 852 of file pgstat.c.

References PgStat_BackendFunctionEntry::f_counts, PgStat_BackendFunctionEntry::f_id, PgStat_FunctionEntry::f_id, PgStat_FunctionCounts::f_numcalls, PgStat_FunctionEntry::f_numcalls, PgStat_FunctionCounts::f_self_time, PgStat_FunctionEntry::f_self_time, PgStat_FunctionCounts::f_total_time, PgStat_FunctionEntry::f_total_time, hash_seq_init(), hash_seq_search(), have_function_stats, INSTR_TIME_GET_MICROSEC, PgStat_MsgFuncstat::m_databaseid, PgStat_MsgFuncstat::m_entry, PgStat_MsgFuncstat::m_hdr, PgStat_MsgFuncstat::m_nentries, memcmp(), MemSet, MyDatabaseId, NULL, offsetof, PGSTAT_MTYPE_FUNCSTAT, PGSTAT_NUM_FUNCENTRIES, pgstat_send(), and pgstat_setheader().

Referenced by pgstat_report_stat().

{
    /* we assume this inits to all zeroes: */
    static const PgStat_FunctionCounts all_zeroes;

    PgStat_MsgFuncstat msg;
    PgStat_BackendFunctionEntry *entry;
    HASH_SEQ_STATUS fstat;

    if (pgStatFunctions == NULL)
        return;

    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_FUNCSTAT);
    msg.m_databaseid = MyDatabaseId;
    msg.m_nentries = 0;

    hash_seq_init(&fstat, pgStatFunctions);
    while ((entry = (PgStat_BackendFunctionEntry *) hash_seq_search(&fstat)) != NULL)
    {
        PgStat_FunctionEntry *m_ent;

        /* Skip it if no counts accumulated since last time */
        if (memcmp(&entry->f_counts, &all_zeroes,
                   sizeof(PgStat_FunctionCounts)) == 0)
            continue;

        /* need to convert format of time accumulators */
        m_ent = &msg.m_entry[msg.m_nentries];
        m_ent->f_id = entry->f_id;
        m_ent->f_numcalls = entry->f_counts.f_numcalls;
        m_ent->f_total_time = INSTR_TIME_GET_MICROSEC(entry->f_counts.f_total_time);
        m_ent->f_self_time = INSTR_TIME_GET_MICROSEC(entry->f_counts.f_self_time);

        if (++msg.m_nentries >= PGSTAT_NUM_FUNCENTRIES)
        {
            pgstat_send(&msg, offsetof(PgStat_MsgFuncstat, m_entry[0]) +
                        msg.m_nentries * sizeof(PgStat_FunctionEntry));
            msg.m_nentries = 0;
        }

        /* reset the entry's counts */
        MemSet(&entry->f_counts, 0, sizeof(PgStat_FunctionCounts));
    }

    if (msg.m_nentries > 0)
        pgstat_send(&msg, offsetof(PgStat_MsgFuncstat, m_entry[0]) +
                    msg.m_nentries * sizeof(PgStat_FunctionEntry));

    have_function_stats = false;
}

static void pgstat_send_inquiry ( TimestampTz  clock_time,
TimestampTz  cutoff_time,
Oid  databaseid 
) [static]
static void pgstat_send_tabstat ( PgStat_MsgTabstat tsmsg  )  [static]
static void pgstat_setheader ( PgStat_MsgHdr hdr,
StatMsgType  mtype 
) [static]
static void pgstat_setup_memcxt ( void   )  [static]
static void pgstat_sighup_handler ( SIGNAL_ARGS   )  [static]

Definition at line 3329 of file pgstat.c.

References got_SIGHUP, and SetLatch().

Referenced by PgstatCollectorMain().

{
    int         save_errno = errno;

    got_SIGHUP = true;
    SetLatch(&pgStatLatch);

    errno = save_errno;
}

int pgstat_start ( void   ) 

Definition at line 629 of file pgstat.c.

References ClosePostmasterPorts(), ereport, errmsg(), fork_process(), last_pgstat_start_time, LOG, NULL, on_exit_reset(), PGINVALID_SOCKET, PGSharedMemoryDetach(), PGSTAT_RESTART_INTERVAL, PgstatCollectorMain(), and pgStatSock.

Referenced by reaper(), ServerLoop(), and sigusr1_handler().

{
    time_t      curtime;
    pid_t       pgStatPid;

    /*
     * Check that the socket is there, else pgstat_init failed and we can do
     * nothing useful.
     */
    if (pgStatSock == PGINVALID_SOCKET)
        return 0;

    /*
     * Do nothing if too soon since last collector start.  This is a safety
     * valve to protect against continuous respawn attempts if the collector
     * is dying immediately at launch.  Note that since we will be re-called
     * from the postmaster main loop, we will get another chance later.
     */
    curtime = time(NULL);
    if ((unsigned int) (curtime - last_pgstat_start_time) <
        (unsigned int) PGSTAT_RESTART_INTERVAL)
        return 0;
    last_pgstat_start_time = curtime;

    /*
     * Okay, fork off the collector.
     */
#ifdef EXEC_BACKEND
    switch ((pgStatPid = pgstat_forkexec()))
#else
    switch ((pgStatPid = fork_process()))
#endif
    {
        case -1:
            ereport(LOG,
                    (errmsg("could not fork statistics collector: %m")));
            return 0;

#ifndef EXEC_BACKEND
        case 0:
            /* in postmaster child ... */
            /* Close the postmaster's sockets */
            ClosePostmasterPorts(false);

            /* Lose the postmaster's on-exit routines */
            on_exit_reset();

            /* Drop our connection to postmaster's shared memory, as well */
            PGSharedMemoryDetach();

            PgstatCollectorMain(0, NULL);
            break;
#endif

        default:
            return (int) pgStatPid;
    }

    /* shouldn't get here */
    return 0;
}

void pgstat_twophase_postabort ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)
void pgstat_twophase_postcommit ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)
void pgstat_update_heap_dead_tuples ( Relation  rel,
int  delta 
)

Definition at line 1838 of file pgstat.c.

References NULL, RelationData::pgstat_info, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_delta_dead_tuples.

Referenced by heap_page_prune().

{
    PgStat_TableStatus *pgstat_info = rel->pgstat_info;

    if (pgstat_info != NULL)
        pgstat_info->t_counts.t_delta_dead_tuples -= delta;
}

void pgstat_vacuum_stat ( void   ) 

Definition at line 911 of file pgstat.c.

References backend_read_statsfile(), CHECK_FOR_INTERRUPTS, PgStat_StatDBEntry::databaseid, DatabaseRelationId, PgStat_StatFuncEntry::functionid, PgStat_StatDBEntry::functions, hash_destroy(), HASH_FIND, hash_get_num_entries(), hash_search(), hash_seq_init(), hash_seq_search(), PgStat_MsgFuncpurge::m_databaseid, PgStat_MsgTabpurge::m_databaseid, PgStat_MsgFuncpurge::m_functionid, PgStat_MsgFuncpurge::m_hdr, PgStat_MsgTabpurge::m_hdr, PgStat_MsgFuncpurge::m_nentries, PgStat_MsgTabpurge::m_nentries, PgStat_MsgTabpurge::m_tableid, MyDatabaseId, NULL, offsetof, OidIsValid, PGINVALID_SOCKET, pgstat_collect_oids(), pgstat_drop_database(), PGSTAT_MTYPE_FUNCPURGE, PGSTAT_MTYPE_TABPURGE, PGSTAT_NUM_FUNCPURGE, PGSTAT_NUM_TABPURGE, pgstat_send(), pgstat_setheader(), pgStatSock, ProcedureRelationId, RelationRelationId, PgStat_StatTabEntry::tableid, and PgStat_StatDBEntry::tables.

Referenced by do_autovacuum(), and vacuum().

{
    HTAB       *htab;
    PgStat_MsgTabpurge msg;
    PgStat_MsgFuncpurge f_msg;
    HASH_SEQ_STATUS hstat;
    PgStat_StatDBEntry *dbentry;
    PgStat_StatTabEntry *tabentry;
    PgStat_StatFuncEntry *funcentry;
    int         len;

    if (pgStatSock == PGINVALID_SOCKET)
        return;

    /*
     * If not done for this transaction, read the statistics collector stats
     * file into some hash tables.
     */
    backend_read_statsfile();

    /*
     * Read pg_database and make a list of OIDs of all existing databases
     */
    htab = pgstat_collect_oids(DatabaseRelationId);

    /*
     * Search the database hash table for dead databases and tell the
     * collector to drop them.
     */
    hash_seq_init(&hstat, pgStatDBHash);
    while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
    {
        Oid         dbid = dbentry->databaseid;

        CHECK_FOR_INTERRUPTS();

        /* the DB entry for shared tables (with InvalidOid) is never dropped */
        if (OidIsValid(dbid) &&
            hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
            pgstat_drop_database(dbid);
    }

    /* Clean up */
    hash_destroy(htab);

    /*
     * Lookup our own database entry; if not found, nothing more to do.
     */
    dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
                                                 (void *) &MyDatabaseId,
                                                 HASH_FIND, NULL);
    if (dbentry == NULL || dbentry->tables == NULL)
        return;

    /*
     * Similarly to above, make a list of all known relations in this DB.
     */
    htab = pgstat_collect_oids(RelationRelationId);

    /*
     * Initialize our messages table counter to zero
     */
    msg.m_nentries = 0;

    /*
     * Check for all tables listed in stats hashtable if they still exist.
     */
    hash_seq_init(&hstat, dbentry->tables);
    while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
    {
        Oid         tabid = tabentry->tableid;

        CHECK_FOR_INTERRUPTS();

        if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
            continue;

        /*
         * Not there, so add this table's Oid to the message
         */
        msg.m_tableid[msg.m_nentries++] = tabid;

        /*
         * If the message is full, send it out and reinitialize to empty
         */
        if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
        {
            len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
                +msg.m_nentries * sizeof(Oid);

            pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
            msg.m_databaseid = MyDatabaseId;
            pgstat_send(&msg, len);

            msg.m_nentries = 0;
        }
    }

    /*
     * Send the rest
     */
    if (msg.m_nentries > 0)
    {
        len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
            +msg.m_nentries * sizeof(Oid);

        pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
        msg.m_databaseid = MyDatabaseId;
        pgstat_send(&msg, len);
    }

    /* Clean up */
    hash_destroy(htab);

    /*
     * Now repeat the above steps for functions.  However, we needn't bother
     * in the common case where no function stats are being collected.
     */
    if (dbentry->functions != NULL &&
        hash_get_num_entries(dbentry->functions) > 0)
    {
        htab = pgstat_collect_oids(ProcedureRelationId);

        pgstat_setheader(&f_msg.m_hdr, PGSTAT_MTYPE_FUNCPURGE);
        f_msg.m_databaseid = MyDatabaseId;
        f_msg.m_nentries = 0;

        hash_seq_init(&hstat, dbentry->functions);
        while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
        {
            Oid         funcid = funcentry->functionid;

            CHECK_FOR_INTERRUPTS();

            if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
                continue;

            /*
             * Not there, so add this function's Oid to the message
             */
            f_msg.m_functionid[f_msg.m_nentries++] = funcid;

            /*
             * If the message is full, send it out and reinitialize to empty
             */
            if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
            {
                len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
                    +f_msg.m_nentries * sizeof(Oid);

                pgstat_send(&f_msg, len);

                f_msg.m_nentries = 0;
            }
        }

        /*
         * Send the rest
         */
        if (f_msg.m_nentries > 0)
        {
            len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
                +f_msg.m_nentries * sizeof(Oid);

            pgstat_send(&f_msg, len);
        }

        hash_destroy(htab);
    }
}

static void pgstat_write_db_statsfile ( PgStat_StatDBEntry dbentry,
bool  permanent 
) [static]

Definition at line 3643 of file pgstat.c.

References AllocateFile(), PgStat_StatDBEntry::databaseid, DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStat_StatDBEntry::functions, get_dbstat_filename(), hash_seq_init(), hash_seq_search(), LOG, MAXPGPATH, NULL, PG_BINARY_W, PgStat_StatDBEntry::tables, and unlink().

Referenced by pgstat_write_statsfiles().

{
    HASH_SEQ_STATUS tstat;
    HASH_SEQ_STATUS fstat;
    PgStat_StatTabEntry *tabentry;
    PgStat_StatFuncEntry *funcentry;
    FILE       *fpout;
    int32       format_id;
    Oid         dbid = dbentry->databaseid;
    int         rc;
    char        tmpfile[MAXPGPATH];
    char        statfile[MAXPGPATH];

    get_dbstat_filename(permanent, true, dbid, tmpfile, MAXPGPATH);
    get_dbstat_filename(permanent, false, dbid, statfile, MAXPGPATH);

    elog(DEBUG2, "writing statsfile '%s'", statfile);

    /*
     * Open the statistics temp file to write out the current values.
     */
    fpout = AllocateFile(tmpfile, PG_BINARY_W);
    if (fpout == NULL)
    {
        ereport(LOG,
                (errcode_for_file_access(),
                 errmsg("could not open temporary statistics file \"%s\": %m",
                        tmpfile)));
        return;
    }

    /*
     * Write the file header --- currently just a format ID.
     */
    format_id = PGSTAT_FILE_FORMAT_ID;
    rc = fwrite(&format_id, sizeof(format_id), 1, fpout);
    (void) rc;                  /* we'll check for error with ferror */

    /*
     * Walk through the database's access stats per table.
     */
    hash_seq_init(&tstat, dbentry->tables);
    while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
    {
        fputc('T', fpout);
        rc = fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
        (void) rc;              /* we'll check for error with ferror */
    }

    /*
     * Walk through the database's function stats table.
     */
    hash_seq_init(&fstat, dbentry->functions);
    while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&fstat)) != NULL)
    {
        fputc('F', fpout);
        rc = fwrite(funcentry, sizeof(PgStat_StatFuncEntry), 1, fpout);
        (void) rc;              /* we'll check for error with ferror */
    }

    /*
     * No more output to be done. Close the temp file and replace the old
     * pgstat.stat with it.  The ferror() check replaces testing for error
     * after each individual fputc or fwrite above.
     */
    fputc('E', fpout);

    if (ferror(fpout))
    {
        ereport(LOG,
                (errcode_for_file_access(),
               errmsg("could not write temporary statistics file \"%s\": %m",
                      tmpfile)));
        FreeFile(fpout);
        unlink(tmpfile);
    }
    else if (FreeFile(fpout) < 0)
    {
        ereport(LOG,
                (errcode_for_file_access(),
               errmsg("could not close temporary statistics file \"%s\": %m",
                      tmpfile)));
        unlink(tmpfile);
    }
    else if (rename(tmpfile, statfile) < 0)
    {
        ereport(LOG,
                (errcode_for_file_access(),
                 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
                        tmpfile, statfile)));
        unlink(tmpfile);
    }

    if (permanent)
    {
        get_dbstat_filename(false, false, dbid, statfile, MAXPGPATH);

        elog(DEBUG2, "removing temporary stat file '%s'", statfile);
        unlink(statfile);
    }
}

static bool pgstat_write_statsfile_needed ( void   )  [static]

Definition at line 5008 of file pgstat.c.

References slist_is_empty().

Referenced by PgstatCollectorMain().

{
    if (!slist_is_empty(&last_statrequests))
        return true;

    /* Everything was written recently */
    return false;
}

static void pgstat_write_statsfiles ( bool  permanent,
bool  allDbs 
) [static]

Definition at line 3486 of file pgstat.c.

References AllocateFile(), slist_mutable_iter::cur, PgStat_StatDBEntry::databaseid, DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), FreeFile(), GetCurrentTimestamp(), hash_seq_init(), hash_seq_search(), LOG, next(), NULL, offsetof, pfree(), PG_BINARY_W, pgstat_db_requested(), pgstat_stat_filename, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_STAT_PERMANENT_TMPFILE, pgstat_stat_tmpname, pgstat_write_db_statsfile(), slist_container, slist_foreach_modify, slist_init(), slist_is_empty(), PgStat_StatDBEntry::stats_timestamp, PgStat_GlobalStats::stats_timestamp, and unlink().

Referenced by PgstatCollectorMain().

{
    HASH_SEQ_STATUS hstat;
    PgStat_StatDBEntry *dbentry;
    FILE       *fpout;
    int32       format_id;
    const char *tmpfile = permanent ? PGSTAT_STAT_PERMANENT_TMPFILE : pgstat_stat_tmpname;
    const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;
    int         rc;

    elog(DEBUG2, "writing statsfile '%s'", statfile);

    /*
     * Open the statistics temp file to write out the current values.
     */
    fpout = AllocateFile(tmpfile, PG_BINARY_W);
    if (fpout == NULL)
    {
        ereport(LOG,
                (errcode_for_file_access(),
                 errmsg("could not open temporary statistics file \"%s\": %m",
                        tmpfile)));
        return;
    }

    /*
     * Set the timestamp of the stats file.
     */
    globalStats.stats_timestamp = GetCurrentTimestamp();

    /*
     * Write the file header --- currently just a format ID.
     */
    format_id = PGSTAT_FILE_FORMAT_ID;
    rc = fwrite(&format_id, sizeof(format_id), 1, fpout);
    (void) rc;                  /* we'll check for error with ferror */

    /*
     * Write global stats struct
     */
    rc = fwrite(&globalStats, sizeof(globalStats), 1, fpout);
    (void) rc;                  /* we'll check for error with ferror */

    /*
     * Walk through the database table.
     */
    hash_seq_init(&hstat, pgStatDBHash);
    while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
    {
        /*
         * Write out the tables and functions into the DB stat file, if
         * required.
         *
         * We need to do this before the dbentry write, to ensure the
         * timestamps written to both are consistent.
         */
        if (allDbs || pgstat_db_requested(dbentry->databaseid))
        {
            dbentry->stats_timestamp = globalStats.stats_timestamp;
            pgstat_write_db_statsfile(dbentry, permanent);
        }

        /*
         * Write out the DB entry. We don't write the tables or functions
         * pointers, since they're of no use to any other process.
         */
        fputc('D', fpout);
        rc = fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
        (void) rc;              /* we'll check for error with ferror */
    }

    /*
     * No more output to be done. Close the temp file and replace the old
     * pgstat.stat with it.  The ferror() check replaces testing for error
     * after each individual fputc or fwrite above.
     */
    fputc('E', fpout);

    if (ferror(fpout))
    {
        ereport(LOG,
                (errcode_for_file_access(),
               errmsg("could not write temporary statistics file \"%s\": %m",
                      tmpfile)));
        FreeFile(fpout);
        unlink(tmpfile);
    }
    else if (FreeFile(fpout) < 0)
    {
        ereport(LOG,
                (errcode_for_file_access(),
               errmsg("could not close temporary statistics file \"%s\": %m",
                      tmpfile)));
        unlink(tmpfile);
    }
    else if (rename(tmpfile, statfile) < 0)
    {
        ereport(LOG,
                (errcode_for_file_access(),
                 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
                        tmpfile, statfile)));
        unlink(tmpfile);
    }

    if (permanent)
        unlink(pgstat_stat_filename);

    /*
     * Now throw away the list of requests.  Note that requests sent after we
     * started the write are still waiting on the network socket.
     */
    if (!slist_is_empty(&last_statrequests))
    {
        slist_mutable_iter iter;

        slist_foreach_modify(iter, &last_statrequests)
        {
            DBWriteRequest *req;

            req = slist_container(DBWriteRequest, next, iter.cur);
            pfree(req);
        }

        slist_init(&last_statrequests);
    }
}

NON_EXEC_STATIC void PgstatCollectorMain ( int  argc,
char *  argv[] 
)

Definition at line 3045 of file pgstat.c.

References EAGAIN, EINTR, elog, ereport, errcode_for_socket_access(), errmsg(), ERROR, EWOULDBLOCK, FATAL, got_SIGHUP, init_ps_display(), InitializeLatchSupport(), InitLatch(), InvalidOid, IsUnderPostmaster, MyProcPid, MyStartTime, need_exit, NULL, PG_SETMASK, PGC_SIGHUP, pgstat_exit(), PGSTAT_MTYPE_ANALYZE, PGSTAT_MTYPE_AUTOVAC_START, PGSTAT_MTYPE_BGWRITER, PGSTAT_MTYPE_DEADLOCK, PGSTAT_MTYPE_DROPDB, PGSTAT_MTYPE_DUMMY, PGSTAT_MTYPE_FUNCPURGE, PGSTAT_MTYPE_FUNCSTAT, PGSTAT_MTYPE_INQUIRY, PGSTAT_MTYPE_RECOVERYCONFLICT, PGSTAT_MTYPE_RESETCOUNTER, PGSTAT_MTYPE_RESETSHAREDCOUNTER, PGSTAT_MTYPE_RESETSINGLECOUNTER, PGSTAT_MTYPE_TABPURGE, PGSTAT_MTYPE_TABSTAT, PGSTAT_MTYPE_TEMPFILE, PGSTAT_MTYPE_VACUUM, pgstat_read_statsfiles(), pgstat_recv_analyze(), pgstat_recv_autovac(), pgstat_recv_bgwriter(), pgstat_recv_deadlock(), pgstat_recv_dropdb(), pgstat_recv_funcpurge(), pgstat_recv_funcstat(), pgstat_recv_inquiry(), pgstat_recv_recoveryconflict(), pgstat_recv_resetcounter(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_recv_tabpurge(), pgstat_recv_tabstat(), pgstat_recv_tempfile(), pgstat_recv_vacuum(), pgstat_sighup_handler(), pgstat_write_statsfile_needed(), pgstat_write_statsfiles(), pgStatRunningInCollector, pgStatSock, pgwin32_noblock, pqsignal(), ProcessConfigFile(), recv, ResetLatch(), SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGCONT, SIGHUP, SIGPIPE, SIGQUIT, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGWINCH, UnBlockSig, WaitLatchOrSocket(), WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_READABLE, and WL_TIMEOUT.

Referenced by pgstat_start().

{
    int         len;
    PgStat_Msg  msg;
    int         wr;

    IsUnderPostmaster = true;   /* we are a postmaster subprocess now */

    MyProcPid = getpid();       /* reset MyProcPid */

    MyStartTime = time(NULL);   /* record Start Time for logging */

    /*
     * If possible, make this process a group leader, so that the postmaster
     * can signal any child processes too.  (pgstat probably never has any
     * child processes, but for consistency we make all postmaster child
     * processes do this.)
     */
#ifdef HAVE_SETSID
    if (setsid() < 0)
        elog(FATAL, "setsid() failed: %m");
#endif

    InitializeLatchSupport();   /* needed for latch waits */

    /* Initialize private latch for use by signal handlers */
    InitLatch(&pgStatLatch);

    /*
     * Ignore all signals usually bound to some action in the postmaster,
     * except SIGHUP and SIGQUIT.  Note we don't need a SIGUSR1 handler to
     * support latch operations, because pgStatLatch is local not shared.
     */
    pqsignal(SIGHUP, pgstat_sighup_handler);
    pqsignal(SIGINT, SIG_IGN);
    pqsignal(SIGTERM, SIG_IGN);
    pqsignal(SIGQUIT, pgstat_exit);
    pqsignal(SIGALRM, SIG_IGN);
    pqsignal(SIGPIPE, SIG_IGN);
    pqsignal(SIGUSR1, SIG_IGN);
    pqsignal(SIGUSR2, SIG_IGN);
    pqsignal(SIGCHLD, SIG_DFL);
    pqsignal(SIGTTIN, SIG_DFL);
    pqsignal(SIGTTOU, SIG_DFL);
    pqsignal(SIGCONT, SIG_DFL);
    pqsignal(SIGWINCH, SIG_DFL);
    PG_SETMASK(&UnBlockSig);

    /*
     * Identify myself via ps
     */
    init_ps_display("stats collector process", "", "", "");

    /*
     * Read in an existing statistics stats file or initialize the stats to
     * zero.
     */
    pgStatRunningInCollector = true;
    pgStatDBHash = pgstat_read_statsfiles(InvalidOid, true, true);

    /*
     * Loop to process messages until we get SIGQUIT or detect ungraceful
     * death of our parent postmaster.
     *
     * For performance reasons, we don't want to do ResetLatch/WaitLatch after
     * every message; instead, do that only after a recv() fails to obtain a
     * message.  (This effectively means that if backends are sending us stuff
     * like mad, we won't notice postmaster death until things slack off a
     * bit; which seems fine.)  To do that, we have an inner loop that
     * iterates as long as recv() succeeds.  We do recognize got_SIGHUP inside
     * the inner loop, which means that such interrupts will get serviced but
     * the latch won't get cleared until next time there is a break in the
     * action.
     */
    for (;;)
    {
        /* Clear any already-pending wakeups */
        ResetLatch(&pgStatLatch);

        /*
         * Quit if we get SIGQUIT from the postmaster.
         */
        if (need_exit)
            break;

        /*
         * Inner loop iterates as long as we keep getting messages, or until
         * need_exit becomes set.
         */
        while (!need_exit)
        {
            /*
             * Reload configuration if we got SIGHUP from the postmaster.
             */
            if (got_SIGHUP)
            {
                got_SIGHUP = false;
                ProcessConfigFile(PGC_SIGHUP);
            }

            /*
             * Write the stats file if a new request has arrived that is not
             * satisfied by existing file.
             */
            if (pgstat_write_statsfile_needed())
                pgstat_write_statsfiles(false, false);

            /*
             * Try to receive and process a message.  This will not block,
             * since the socket is set to non-blocking mode.
             *
             * XXX On Windows, we have to force pgwin32_recv to cooperate,
             * despite the previous use of pg_set_noblock() on the socket.
             * This is extremely broken and should be fixed someday.
             */
#ifdef WIN32
            pgwin32_noblock = 1;
#endif

            len = recv(pgStatSock, (char *) &msg,
                       sizeof(PgStat_Msg), 0);

#ifdef WIN32
            pgwin32_noblock = 0;
#endif

            if (len < 0)
            {
                if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
                    break;      /* out of inner loop */
                ereport(ERROR,
                        (errcode_for_socket_access(),
                         errmsg("could not read statistics message: %m")));
            }

            /*
             * We ignore messages that are smaller than our common header
             */
            if (len < sizeof(PgStat_MsgHdr))
                continue;

            /*
             * The received length must match the length in the header
             */
            if (msg.msg_hdr.m_size != len)
                continue;

            /*
             * O.K. - we accept this message.  Process it.
             */
            switch (msg.msg_hdr.m_type)
            {
                case PGSTAT_MTYPE_DUMMY:
                    break;

                case PGSTAT_MTYPE_INQUIRY:
                    pgstat_recv_inquiry((PgStat_MsgInquiry *) &msg, len);
                    break;

                case PGSTAT_MTYPE_TABSTAT:
                    pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
                    break;

                case PGSTAT_MTYPE_TABPURGE:
                    pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
                    break;

                case PGSTAT_MTYPE_DROPDB:
                    pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
                    break;

                case PGSTAT_MTYPE_RESETCOUNTER:
                    pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
                                             len);
                    break;

                case PGSTAT_MTYPE_RESETSHAREDCOUNTER:
                    pgstat_recv_resetsharedcounter(
                                       (PgStat_MsgResetsharedcounter *) &msg,
                                                   len);
                    break;

                case PGSTAT_MTYPE_RESETSINGLECOUNTER:
                    pgstat_recv_resetsinglecounter(
                                       (PgStat_MsgResetsinglecounter *) &msg,
                                                   len);
                    break;

                case PGSTAT_MTYPE_AUTOVAC_START:
                    pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
                    break;

                case PGSTAT_MTYPE_VACUUM:
                    pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
                    break;

                case PGSTAT_MTYPE_ANALYZE:
                    pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
                    break;

                case PGSTAT_MTYPE_BGWRITER:
                    pgstat_recv_bgwriter((PgStat_MsgBgWriter *) &msg, len);
                    break;

                case PGSTAT_MTYPE_FUNCSTAT:
                    pgstat_recv_funcstat((PgStat_MsgFuncstat *) &msg, len);
                    break;

                case PGSTAT_MTYPE_FUNCPURGE:
                    pgstat_recv_funcpurge((PgStat_MsgFuncpurge *) &msg, len);
                    break;

                case PGSTAT_MTYPE_RECOVERYCONFLICT:
                    pgstat_recv_recoveryconflict((PgStat_MsgRecoveryConflict *) &msg, len);
                    break;

                case PGSTAT_MTYPE_DEADLOCK:
                    pgstat_recv_deadlock((PgStat_MsgDeadlock *) &msg, len);
                    break;

                case PGSTAT_MTYPE_TEMPFILE:
                    pgstat_recv_tempfile((PgStat_MsgTempFile *) &msg, len);
                    break;

                default:
                    break;
            }
        }                       /* end of inner message-processing loop */

        /* Sleep until there's something to do */
#ifndef WIN32
        wr = WaitLatchOrSocket(&pgStatLatch,
                     WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE,
                               pgStatSock,
                               -1L);
#else

        /*
         * Windows, at least in its Windows Server 2003 R2 incarnation,
         * sometimes loses FD_READ events.  Waking up and retrying the recv()
         * fixes that, so don't sleep indefinitely.  This is a crock of the
         * first water, but until somebody wants to debug exactly what's
         * happening there, this is the best we can do.  The two-second
         * timeout matches our pre-9.2 behavior, and needs to be short enough
         * to not provoke "pgstat wait timeout" complaints from
         * backend_read_statsfile.
         */
        wr = WaitLatchOrSocket(&pgStatLatch,
        WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE | WL_TIMEOUT,
                               pgStatSock,
                               2 * 1000L /* msec */ );
#endif

        /*
         * Emergency bailout if postmaster has died.  This is to avoid the
         * necessity for manual cleanup of all postmaster children.
         */
        if (wr & WL_POSTMASTER_DEATH)
            break;
    }                           /* end of outer loop */

    /*
     * Save the final stats to reuse at next startup.
     */
    pgstat_write_statsfiles(true, true);

    exit(0);
}

void PostPrepare_PgStat ( void   ) 

Definition at line 2057 of file pgstat.c.

References PgStat_SubXactStatus::first, PgStat_TableXactStatus::next, NULL, PgStat_TableXactStatus::parent, pgstat_clear_snapshot(), PgStat_TableStatus::trans, and trans.

Referenced by PrepareTransaction().

{
    PgStat_SubXactStatus *xact_state;

    /*
     * We don't bother to free any of the transactional state, since it's all
     * in TopTransactionContext and will go away anyway.
     */
    xact_state = pgStatXactStack;
    if (xact_state != NULL)
    {
        PgStat_TableXactStatus *trans;

        for (trans = xact_state->first; trans != NULL; trans = trans->next)
        {
            PgStat_TableStatus *tabstat;

            tabstat = trans->parent;
            tabstat->trans = NULL;
        }
    }
    pgStatXactStack = NULL;

    /* Make sure any stats snapshot is thrown away */
    pgstat_clear_snapshot();
}

static void reset_dbentry_counters ( PgStat_StatDBEntry dbentry  )  [static]

Definition at line 3345 of file pgstat.c.

References HASHCTL::entrysize, PgStat_StatDBEntry::functions, GetCurrentTimestamp(), HASHCTL::hash, hash_create(), HASH_ELEM, HASH_FUNCTION, HASHCTL::keysize, PgStat_StatDBEntry::last_autovac_time, PgStat_StatDBEntry::n_block_read_time, PgStat_StatDBEntry::n_block_write_time, PgStat_StatDBEntry::n_blocks_fetched, PgStat_StatDBEntry::n_blocks_hit, PgStat_StatDBEntry::n_conflict_bufferpin, PgStat_StatDBEntry::n_conflict_lock, PgStat_StatDBEntry::n_conflict_snapshot, PgStat_StatDBEntry::n_conflict_startup_deadlock, PgStat_StatDBEntry::n_conflict_tablespace, PgStat_StatDBEntry::n_deadlocks, PgStat_StatDBEntry::n_temp_bytes, PgStat_StatDBEntry::n_temp_files, PgStat_StatDBEntry::n_tuples_deleted, PgStat_StatDBEntry::n_tuples_fetched, PgStat_StatDBEntry::n_tuples_inserted, PgStat_StatDBEntry::n_tuples_returned, PgStat_StatDBEntry::n_tuples_updated, PgStat_StatDBEntry::n_xact_commit, PgStat_StatDBEntry::n_xact_rollback, PGSTAT_FUNCTION_HASH_SIZE, PGSTAT_TAB_HASH_SIZE, PgStat_StatDBEntry::stat_reset_timestamp, PgStat_StatDBEntry::stats_timestamp, and PgStat_StatDBEntry::tables.

Referenced by pgstat_get_db_entry(), and pgstat_recv_resetcounter().

{
    HASHCTL     hash_ctl;

    dbentry->n_xact_commit = 0;
    dbentry->n_xact_rollback = 0;
    dbentry->n_blocks_fetched = 0;
    dbentry->n_blocks_hit = 0;
    dbentry->n_tuples_returned = 0;
    dbentry->n_tuples_fetched = 0;
    dbentry->n_tuples_inserted = 0;
    dbentry->n_tuples_updated = 0;
    dbentry->n_tuples_deleted = 0;
    dbentry->last_autovac_time = 0;
    dbentry->n_conflict_tablespace = 0;
    dbentry->n_conflict_lock = 0;
    dbentry->n_conflict_snapshot = 0;
    dbentry->n_conflict_bufferpin = 0;
    dbentry->n_conflict_startup_deadlock = 0;
    dbentry->n_temp_files = 0;
    dbentry->n_temp_bytes = 0;
    dbentry->n_deadlocks = 0;
    dbentry->n_block_read_time = 0;
    dbentry->n_block_write_time = 0;

    dbentry->stat_reset_timestamp = GetCurrentTimestamp();
    dbentry->stats_timestamp = 0;

    memset(&hash_ctl, 0, sizeof(hash_ctl));
    hash_ctl.keysize = sizeof(Oid);
    hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
    hash_ctl.hash = oid_hash;
    dbentry->tables = hash_create("Per-database table",
                                  PGSTAT_TAB_HASH_SIZE,
                                  &hash_ctl,
                                  HASH_ELEM | HASH_FUNCTION);

    hash_ctl.keysize = sizeof(Oid);
    hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
    hash_ctl.hash = oid_hash;
    dbentry->functions = hash_create("Per-database function",
                                     PGSTAT_FUNCTION_HASH_SIZE,
                                     &hash_ctl,
                                     HASH_ELEM | HASH_FUNCTION);
}


Variable Documentation

char* BackendActivityBuffer = NULL [static]

Definition at line 2314 of file pgstat.c.

Definition at line 2315 of file pgstat.c.

char* BackendAppnameBuffer = NULL [static]

Definition at line 2313 of file pgstat.c.

char* BackendClientHostnameBuffer = NULL [static]

Definition at line 2312 of file pgstat.c.

Definition at line 2310 of file pgstat.c.

Definition at line 223 of file pgstat.c.

volatile bool got_SIGHUP = false [static]

Definition at line 237 of file pgstat.c.

Referenced by pgstat_sighup_handler(), and PgstatCollectorMain().

bool have_function_stats = false [static]

Definition at line 177 of file pgstat.c.

Referenced by pgstat_end_function_usage(), pgstat_report_stat(), and pgstat_send_funcstats().

time_t last_pgstat_start_time [static]

Definition at line 141 of file pgstat.c.

Referenced by allow_immediate_pgstat_restart(), and pgstat_start().

slist_head last_statrequests = SLIST_STATIC_INIT(last_statrequests) [static]

Definition at line 234 of file pgstat.c.

Definition at line 215 of file pgstat.c.

int localNumBackends = 0 [static]
PgBackendStatus* MyBEEntry = NULL [static]

Definition at line 2311 of file pgstat.c.

volatile bool need_exit = false [static]

Definition at line 236 of file pgstat.c.

Referenced by pgstat_exit(), and PgstatCollectorMain().

char* pgstat_stat_directory = NULL
char* pgstat_stat_filename = NULL
char* pgstat_stat_tmpname = NULL

Definition at line 122 of file pgstat.c.

Referenced by assign_pgstat_temp_directory(), and pgstat_write_statsfiles().

int pgstat_track_functions = TRACK_FUNC_OFF

Definition at line 113 of file pgstat.c.

struct sockaddr_storage pgStatAddr [static]

Definition at line 139 of file pgstat.c.

Referenced by pgstat_init().

Definition at line 197 of file pgstat.c.

Referenced by pgstat_send_tabstat().

Definition at line 198 of file pgstat.c.

Referenced by pgstat_send_tabstat().

HTAB* pgStatDBHash = NULL [static]

Definition at line 214 of file pgstat.c.

HTAB* pgStatFunctions = NULL [static]

Definition at line 171 of file pgstat.c.

Latch pgStatLatch [static]

Definition at line 137 of file pgstat.c.

Definition at line 213 of file pgstat.c.

bool pgStatRunningInCollector = false [static]
NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET
TabStatusArray* pgStatTabList = NULL [static]

Definition at line 165 of file pgstat.c.

int pgStatXactCommit = 0 [static]

Definition at line 195 of file pgstat.c.

Referenced by AtEOXact_PgStat(), pgstat_report_stat(), and pgstat_send_tabstat().

int pgStatXactRollback = 0 [static]

Definition at line 196 of file pgstat.c.

Referenced by AtEOXact_PgStat(), pgstat_report_stat(), and pgstat_send_tabstat().

Definition at line 193 of file pgstat.c.

Definition at line 244 of file pgstat.c.

Referenced by pgstat_end_function_usage(), and pgstat_init_function_usage().