Skip to content

Commit 6fde2d9

Browse files
committed
Fix pg_stat_reset_single_table_counters() for shared relations
This commit fixes the function of $subject for shared relations. This feature has been added by e042678. Unfortunately, this new behavior got removed by 5891c7a when moving statistics to shared memory. Reported-by: Mitsuru Hinata Author: Masahiro Ikeda Reviewed-by: Kyotaro Horiguchi, Masahiko Sawada Discussion: https://postgr.es/m/7cc69f863d9b1bc677544e3accd0e4b4@oss.nttdata.com Backpatch-through: 15
1 parent 1951d21 commit 6fde2d9

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "access/htup_details.h"
1818
#include "access/xlog.h"
1919
#include "access/xlogprefetcher.h"
20+
#include "catalog/catalog.h"
2021
#include "catalog/pg_authid.h"
2122
#include "catalog/pg_type.h"
2223
#include "common/ip.h"
@@ -1776,13 +1777,17 @@ pg_stat_reset_shared(PG_FUNCTION_ARGS)
17761777
PG_RETURN_VOID();
17771778
}
17781779

1779-
/* Reset a single counter in the current database */
1780+
/*
1781+
* Reset a statistics for a single object, which may be of current
1782+
* database or shared across all databases in the cluster.
1783+
*/
17801784
Datum
17811785
pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
17821786
{
17831787
Oid taboid = PG_GETARG_OID(0);
1788+
Oid dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId);
17841789

1785-
pgstat_reset(PGSTAT_KIND_RELATION, MyDatabaseId, taboid);
1790+
pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
17861791

17871792
PG_RETURN_VOID();
17881793
}

src/test/regress/expected/stats.out

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,52 @@ FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
764764
2 | t | 3 | t
765765
(1 row)
766766

767+
-----
768+
-- Test reset of some stats for shared table
769+
-----
770+
-- This updates the comment of the database currently in use in
771+
-- pg_shdescription with a fake value, then sets it back to its
772+
-- original value.
773+
SELECT shobj_description(d.oid, 'pg_database') as description_before
774+
FROM pg_database d WHERE datname = current_database() \gset
775+
-- force some stats in pg_shdescription.
776+
BEGIN;
777+
SELECT current_database() as datname \gset
778+
COMMENT ON DATABASE :"datname" IS 'This is a test comment';
779+
SELECT pg_stat_force_next_flush();
780+
pg_stat_force_next_flush
781+
--------------------------
782+
783+
(1 row)
784+
785+
COMMIT;
786+
-- check that the stats are reset.
787+
SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
788+
WHERE relid = 'pg_shdescription'::regclass;
789+
has_data
790+
----------
791+
t
792+
(1 row)
793+
794+
SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
795+
pg_stat_reset_single_table_counters
796+
-------------------------------------
797+
798+
(1 row)
799+
800+
SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
801+
WHERE relid = 'pg_shdescription'::regclass;
802+
has_data
803+
----------
804+
f
805+
(1 row)
806+
807+
-- set back comment
808+
\if :{?description_before}
809+
COMMENT ON DATABASE :"datname" IS :'description_before';
810+
\else
811+
COMMENT ON DATABASE :"datname" IS NULL;
812+
\endif
767813
-----
768814
-- Test that various stats views are being properly populated
769815
-----

src/test/regress/sql/stats.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,36 @@ COMMIT;
376376
SELECT seq_scan, :'test_last_seq' = last_seq_scan AS seq_ok, idx_scan, :'test_last_idx' < last_idx_scan AS idx_ok
377377
FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
378378

379+
-----
380+
-- Test reset of some stats for shared table
381+
-----
382+
383+
-- This updates the comment of the database currently in use in
384+
-- pg_shdescription with a fake value, then sets it back to its
385+
-- original value.
386+
SELECT shobj_description(d.oid, 'pg_database') as description_before
387+
FROM pg_database d WHERE datname = current_database() \gset
388+
389+
-- force some stats in pg_shdescription.
390+
BEGIN;
391+
SELECT current_database() as datname \gset
392+
COMMENT ON DATABASE :"datname" IS 'This is a test comment';
393+
SELECT pg_stat_force_next_flush();
394+
COMMIT;
395+
396+
-- check that the stats are reset.
397+
SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
398+
WHERE relid = 'pg_shdescription'::regclass;
399+
SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
400+
SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
401+
WHERE relid = 'pg_shdescription'::regclass;
402+
403+
-- set back comment
404+
\if :{?description_before}
405+
COMMENT ON DATABASE :"datname" IS :'description_before';
406+
\else
407+
COMMENT ON DATABASE :"datname" IS NULL;
408+
\endif
379409

380410
-----
381411
-- Test that various stats views are being properly populated

0 commit comments

Comments
 (0)