Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e0b0142

Browse files
committed
Create subscription stats entry at CREATE SUBSCRIPTION time
Previously, the subscription stats entry was created when the first stats, i.e., an error on apply worker or tablesync worker, were reported. Therefore, the stats_reset field was not updated by pg_stat_reset_subscription_stats() if the stats entry was not populated yet, which was different behavior than other statistics. This change creates the subscription stats entry and initializes it at CREATE SUBSCRIPTION time. Reviewed-by: Andres Freund <andres@anarazel.de> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Author: Masahiko Sawada <sawada.mshk@gmail.com> Discussion: https://postgr.es/m/CAAKRu_Zqd-e5imT_3-ZiQv1cfsWuy16OJTiUaCvqpq4V7GVdSg@mail.gmail.com
1 parent cd4e8ca commit e0b0142

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/backend/utils/activity/pgstat_subscription.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ pgstat_report_subscription_error(Oid subid, bool is_apply_error)
4141

4242
/*
4343
* Report creating the subscription.
44-
*
45-
* Ensures that stats are dropped if transaction rolls back.
4644
*/
4745
void
4846
pgstat_create_subscription(Oid subid)
4947
{
48+
/* Ensures that stats are dropped if transaction rolls back */
5049
pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION,
5150
InvalidOid, subid);
51+
52+
/* Create and initialize the subscription stats entry */
53+
pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid,
54+
true, NULL);
55+
pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0);
5256
}
5357

5458
/*

src/test/regress/expected/subscription.out

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,40 @@ SELECT obj_description(s.oid, 'pg_subscription') FROM pg_subscription s;
3737
test subscription
3838
(1 row)
3939

40+
-- Check if the subscription stats are created and stats_reset is updated
41+
-- by pg_stat_reset_subscription_stats().
42+
SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
43+
subname | stats_reset_is_null
44+
-----------------+---------------------
45+
regress_testsub | t
46+
(1 row)
47+
48+
SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
49+
pg_stat_reset_subscription_stats
50+
----------------------------------
51+
52+
(1 row)
53+
54+
SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
55+
subname | stats_reset_is_null
56+
-----------------+---------------------
57+
regress_testsub | f
58+
(1 row)
59+
60+
-- Reset the stats again and check if the new reset_stats is updated.
61+
SELECT stats_reset as prev_stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub' \gset
62+
SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
63+
pg_stat_reset_subscription_stats
64+
----------------------------------
65+
66+
(1 row)
67+
68+
SELECT :'prev_stats_reset' < stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
69+
?column?
70+
----------
71+
t
72+
(1 row)
73+
4074
-- fail - name already exists
4175
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false);
4276
ERROR: subscription "regress_testsub" already exists

src/test/regress/sql/subscription.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUB
3030
COMMENT ON SUBSCRIPTION regress_testsub IS 'test subscription';
3131
SELECT obj_description(s.oid, 'pg_subscription') FROM pg_subscription s;
3232

33+
-- Check if the subscription stats are created and stats_reset is updated
34+
-- by pg_stat_reset_subscription_stats().
35+
SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
36+
SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
37+
SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
38+
39+
-- Reset the stats again and check if the new reset_stats is updated.
40+
SELECT stats_reset as prev_stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub' \gset
41+
SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
42+
SELECT :'prev_stats_reset' < stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
43+
3344
-- fail - name already exists
3445
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false);
3546

0 commit comments

Comments
 (0)