Skip to content

Commit d7744d5

Browse files
committed
Fix initialization of pg_stat_get_lastscan()
A NULL result should be reported when a stats timestamp is set to 0, but c037471 missed that, leading to a confusing timestamp value after for example a DML on a freshly-created relation with no scans done on it yet. This impacted the following attributes for two system views: - pg_stat_all_tables.last_idx_scan - pg_stat_all_tables.last_seq_scan - pg_stat_all_indexes.last_idx_scan Reported-by: Robert Treat Analyzed-by: Peter Eisentraut Author: Dave Page Discussion: https://postgr.es/m/CABV9wwPzMfSaz3EfKXXDxKmMprbxwF5r6WPuxqA=5mzRUqfTGg@mail.gmail.com
1 parent 1613de8 commit d7744d5

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ Datum
5656
pg_stat_get_lastscan(PG_FUNCTION_ARGS)
5757
{
5858
Oid relid = PG_GETARG_OID(0);
59+
TimestampTz result;
5960
PgStat_StatTabEntry *tabentry;
6061

6162
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
63+
result = 0;
64+
else
65+
result = tabentry->lastscan;
66+
67+
if (result == 0)
6268
PG_RETURN_NULL();
6369
else
64-
PG_RETURN_TIMESTAMPTZ(tabentry->lastscan);
70+
PG_RETURN_TIMESTAMPTZ(result);
6571
}
6672

6773

src/test/regress/expected/stats.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ SELECT pg_stat_force_next_flush();
573573

574574
(1 row)
575575

576+
SELECT last_seq_scan, last_idx_scan FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
577+
last_seq_scan | last_idx_scan
578+
---------------+---------------
579+
|
580+
(1 row)
581+
576582
COMMIT;
577583
SELECT pg_stat_reset_single_table_counters('test_last_scan'::regclass);
578584
pg_stat_reset_single_table_counters

src/test/regress/sql/stats.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ BEGIN;
303303
CREATE TEMPORARY TABLE test_last_scan(idx_col int primary key, noidx_col int);
304304
INSERT INTO test_last_scan(idx_col, noidx_col) VALUES(1, 1);
305305
SELECT pg_stat_force_next_flush();
306+
SELECT last_seq_scan, last_idx_scan FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
306307
COMMIT;
307308

308309
SELECT pg_stat_reset_single_table_counters('test_last_scan'::regclass);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy