Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions doc/src/sgml/monitoring.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,15 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
</entry>
</row>

<row>
<entry><structname>pg_stat_role</structname><indexterm><primary>pg_stat_role</primary></indexterm></entry>
<entry>One row per role, showing statistics about sessions and
transactions of that role. See
<link linkend="monitoring-pg-stat-role">
<structname>pg_stat_role</structname></link> for details.
</entry>
</row>

<row>
<entry><structname>pg_stat_slru</structname><indexterm><primary>pg_stat_slru</primary></indexterm></entry>
<entry>One row per SLRU, showing statistics of operations. See
Expand Down Expand Up @@ -2578,6 +2587,99 @@ description | Waiting for a newly initialized WAL file to reach durable storage

</sect2>

<sect2 id="monitoring-pg-stat-role">
<title><structname>pg_stat_role</structname></title>

<indexterm>
<primary>pg_stat_role</primary>
</indexterm>

<para>
The <structname>pg_stat_role</structname> view will contain one row per
role, showing cumulative statistics about the activity of sessions that
logged in as that role. Activity is attributed to the login role of a
session; <command>SET ROLE</command> and <literal>SECURITY
DEFINER</literal> functions do not change the role activity is counted
for. Only client sessions are counted, not background processes such as
autovacuum workers.
</para>

<table id="pg-stat-role" xreflabel="pg_stat_role">
<title><structname>pg_stat_role</structname> View</title>
<tgroup cols="1">
<thead>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
Column Type
</para>
<para>
Description
</para></entry>
</row>
</thead>

<tbody>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>roleid</structfield> <type>oid</type>
</para>
<para>
OID of the role
</para></entry>
</row>

<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>rolname</structfield> <type>name</type>
</para>
<para>
Name of the role
</para></entry>
</row>

<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>sessions</structfield> <type>bigint</type>
</para>
<para>
Number of sessions established by this role
</para></entry>
</row>

<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>xact_commit</structfield> <type>bigint</type>
</para>
<para>
Number of transactions in sessions of this role that have been
committed
</para></entry>
</row>

<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>xact_rollback</structfield> <type>bigint</type>
</para>
<para>
Number of transactions in sessions of this role that have been
rolled back
</para></entry>
</row>

<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>stats_reset</structfield> <type>timestamp with time zone</type>
</para>
<para>
Time at which these statistics were last reset
</para></entry>
</row>
</tbody>
</tgroup>
</table>

</sect2>

<sect2 id="monitoring-pg-stat-ssl-view">
<title><structname>pg_stat_ssl</structname></title>

Expand Down Expand Up @@ -5984,6 +6086,26 @@ description | Waiting for a newly initialized WAL file to reach durable storage
can be granted EXECUTE to run the function.
</para></entry>
</row>

<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_stat_reset_role_stats</primary>
</indexterm>
<function>pg_stat_reset_role_stats</function> ( <type>oid</type> )
<returnvalue>void</returnvalue>
</para>
<para>
Resets statistics for a single role shown in the
<structname>pg_stat_role</structname> view to zero. If
the argument is <literal>NULL</literal>, reset statistics for all
roles.
</para>
<para>
This function is restricted to superusers by default, but other users
can be granted EXECUTE to run the function.
</para></entry>
</row>
</tbody>
</tgroup>
</table>
Expand Down
11 changes: 11 additions & 0 deletions src/backend/catalog/system_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,17 @@ CREATE VIEW pg_stat_subscription_stats AS
FROM pg_subscription as s,
pg_stat_get_subscription_stats(s.oid) as ss;

CREATE VIEW pg_stat_role AS
SELECT
rs.roleid,
r.rolname,
rs.sessions,
rs.xact_commit,
rs.xact_rollback,
rs.stats_reset
FROM pg_roles as r,
pg_stat_get_role_stats(r.oid) as rs;

CREATE VIEW pg_wait_events AS
SELECT * FROM pg_get_wait_events();

Expand Down
7 changes: 7 additions & 0 deletions src/backend/commands/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "commands/user.h"
#include "libpq/crypt.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "storage/lmgr.h"
#include "utils/acl.h"
Expand Down Expand Up @@ -606,6 +607,9 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
/* Post creation hook for new role */
InvokeObjectPostCreateHook(AuthIdRelationId, roleid, 0);

/* Create the statistics entry for the new role */
pgstat_create_role(roleid);

/*
* Close pg_authid, but keep lock till commit.
*/
Expand Down Expand Up @@ -1325,6 +1329,9 @@ DropRole(DropRoleStmt *stmt)
* Remove settings for this role.
*/
DropSetting(InvalidOid, roleid);

/* Drop the statistics entry for this role */
pgstat_drop_role(roleid);
}

/*
Expand Down
1 change: 1 addition & 0 deletions src/backend/tcop/postgres.c
Original file line number Diff line number Diff line change
Expand Up @@ -4497,6 +4497,7 @@ PostgresMain(const char *dbname, const char *username)
on_proc_exit(log_disconnections, 0);

pgstat_report_connect(MyDatabaseId);
pgstat_report_role_connect(GetSessionUserId());

/* Perform initialization specific to a WAL sender process. */
if (am_walsender)
Expand Down
1 change: 1 addition & 0 deletions src/backend/utils/activity/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ OBJS = \
pgstat_lock.o \
pgstat_relation.o \
pgstat_replslot.o \
pgstat_role.o \
pgstat_shmem.o \
pgstat_slru.o \
pgstat_subscription.o \
Expand Down
1 change: 1 addition & 0 deletions src/backend/utils/activity/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ backend_sources += files(
'pgstat_lock.c',
'pgstat_relation.c',
'pgstat_replslot.c',
'pgstat_role.c',
'pgstat_shmem.c',
'pgstat_slru.c',
'pgstat_subscription.c',
Expand Down
18 changes: 18 additions & 0 deletions src/backend/utils/activity/pgstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,23 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
.reset_timestamp_cb = pgstat_backend_reset_timestamp_cb,
},

[PGSTAT_KIND_ROLE] = {
.name = "role",

.fixed_amount = false,
.write_to_file = true,
/* so pg_stat_role entries can be seen in all databases */
.accessed_across_databases = true,

.shared_size = sizeof(PgStatShared_Role),
.shared_data_off = offsetof(PgStatShared_Role, stats),
.shared_data_len = sizeof(((PgStatShared_Role *) 0)->stats),
.pending_size = sizeof(PgStat_StatRoleEntry),

.flush_pending_cb = pgstat_role_flush_cb,
.reset_timestamp_cb = pgstat_role_reset_timestamp_cb,
},

/* stats for fixed-numbered (mostly 1) objects */

[PGSTAT_KIND_ARCHIVER] = {
Expand Down Expand Up @@ -786,6 +803,7 @@ pgstat_report_stat(bool force)
}

pgstat_update_dbstats(now);
pgstat_update_role_stats();

/* don't wait for lock acquisition when !force */
nowait = !force;
Expand Down
Loading