pg_stat_login
pg_stat_login · Cumulative authentication counters by user and database
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 102432 | pg_stat_login | pg_stat_login | 1.0 | STAT | PostgreSQL | C |
PostgreSQL · 18, 19
Usage
Sources:
pg_stat_login 1.0 records successful and failed authentication by user/database pair using PostgreSQL’s Custom Cumulative Statistics API. It requires PostgreSQL 18 or later. It is a distinct extension from the older pg_login_stat for PostgreSQL 15–17.
Enable and Inspect
Append the library to the existing preload list, restart PostgreSQL and install the SQL objects as a superuser:
Members of pg_read_all_stats can read the view and inspection functions. Only superusers can reset the statistics. The extension is not relocatable.
Capacity and Maintenance
The shared table has a fixed capacity of 10,000 distinct pairs. pg_stat_login_info() reports max_entries, num_entries, n_dropped and stats_reset. At capacity, existing pairs keep accumulating but events for new pairs are dropped. Failed attempts also occupy slots.
Reset clears all accumulated counts and reclaims slots. The reloadable pg_stat_login.enable defaults on. Counts survive clean restarts but are discarded after crash recovery; this is cumulative monitoring, not a durable audit trail.
Authentication Boundaries
login_ok means authentication passed, not that a usable session opened. Later failures, including an unknown database or reserved connection slots, can still count as successful authentication. Rejections before the authentication hook, including connection exhaustion and pg_hba.conf rejection, are not counted. A client disconnecting before supplying a password is also excluded.
Names are recorded as received, including nonexistent roles or databases. Invalid database-encoding bytes are escaped; valid names that cannot be represented in the client encoding can make inspection fail. Check server logs for events outside the hook and correlate names with PostgreSQL catalogs when interpreting counts. The extension uses custom statistics kind ID 30.