Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d7744d5

Browse filesBrowse 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
Copy full SHA for d7744d5

File tree

3 files changed

+14
-1
lines changed
Filter options

3 files changed

+14
-1
lines changed

‎src/backend/utils/adt/pgstatfuncs.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/pgstatfuncs.c
+7-1Lines 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

Copy file name to clipboardExpand all lines: src/test/regress/expected/stats.out
+6Lines 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

Copy file name to clipboardExpand all lines: src/test/regress/sql/stats.sql
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.