Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

+7-1
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

+6
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

+1
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)