Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit8fb580a

Browse files
committed
pgstat: prepare APIs used by pgstatfuncs for shared memory stats.
With the introduction of PgStat_Kind PgStat_Single_Reset_Type,PgStat_Shared_Reset_Target don't make sense anymore. Replace them withPgStat_Kind.Instead of having dedicated reset functions for different kinds of stats, usetwo generic helper routines (one to reset all stats of a kind, one to resetone stats entry).A number of reset functions were named pgstat_reset_*_counter(), despiteaffecting multiple counters. The generic helper routines get rid ofpgstat_reset_single_counter(), pgstat_reset_subscription_counter().Rename pgstat_reset_slru_counter(), pgstat_reset_replslot_counter() topgstat_reset_slru(), pgstat_reset_replslot() respectively, and have them onlydeal with a single SLRU/slot. Resetting all SLRUs/slots goes through thegeneric pgstat_reset_of_kind().Previously pg_stat_reset_replication_slot() used SearchNamedReplicationSlot()to check if a slot exists. API wise it seems better to move that topgstat_replslot.c.This is done separately from the - quite large - shared memory statisticspatch to make review easier.Reviewed-By: Kyotaro Horiguchi <horikyota.ntt@gmail.com>Discussion:https://postgr.es/m/20220404041516.cctrvpadhuriawlq@alap3.anarazel.de
1 parent997afad commit8fb580a

File tree

7 files changed

+161
-131
lines changed

7 files changed

+161
-131
lines changed

‎src/backend/postmaster/pgstat.c

Lines changed: 92 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static bool pgstat_write_statsfile_needed(void);
124124
staticboolpgstat_db_requested(Oiddatabaseid);
125125

126126
staticPgStat_StatReplSlotEntry*pgstat_get_replslot_entry(NameDataname,boolcreate_it);
127-
staticvoidpgstat_reset_replslot(PgStat_StatReplSlotEntry*slotstats,TimestampTzts);
127+
staticvoidpgstat_reset_replslot_entry(PgStat_StatReplSlotEntry*slotstats,TimestampTzts);
128128

129129
staticHTAB*pgstat_collect_oids(Oidcatalogid,AttrNumberanum_oid);
130130

@@ -1084,55 +1084,110 @@ pgstat_reset_counters(void)
10841084
}
10851085

10861086
/*
1087-
* Reset a single counter.
1087+
* Reset a single variable-numbered entry.
1088+
*
1089+
* If the stats kind is within a database, also reset the database's
1090+
* stat_reset_timestamp.
10881091
*
10891092
* Permission checking for this function is managed through the normal
10901093
* GRANT system.
10911094
*/
10921095
void
1093-
pgstat_reset_single_counter(Oidobjoid,PgStat_Single_Reset_Typetype)
1096+
pgstat_reset(PgStat_Kindkind,Oiddboid,Oidobjoid)
10941097
{
1095-
PgStat_MsgResetsinglecountermsg;
10961098

10971099
if (pgStatSock==PGINVALID_SOCKET)
10981100
return;
10991101

1100-
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSINGLECOUNTER);
1101-
msg.m_databaseid=MyDatabaseId;
1102-
msg.m_resettype=type;
1103-
msg.m_objectid=objoid;
1102+
switch (kind)
1103+
{
1104+
casePGSTAT_KIND_FUNCTION:
1105+
casePGSTAT_KIND_RELATION:
1106+
{
1107+
PgStat_MsgResetsinglecountermsg;
11041108

1105-
pgstat_send(&msg,sizeof(msg));
1109+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSINGLECOUNTER);
1110+
msg.m_databaseid=dboid;
1111+
msg.m_resettype=kind;
1112+
msg.m_objectid=objoid;
1113+
pgstat_send(&msg,sizeof(msg));
1114+
}
1115+
break;
1116+
1117+
casePGSTAT_KIND_SUBSCRIPTION:
1118+
{
1119+
PgStat_MsgResetsubcountermsg;
1120+
1121+
Assert(dboid==InvalidOid);
1122+
msg.m_subid=objoid;
1123+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSUBCOUNTER);
1124+
}
1125+
break;
1126+
1127+
default:
1128+
elog(ERROR,"unexpected");
1129+
}
11061130
}
11071131

11081132
/*
1109-
* Resetcluster-wide shared counters.
1133+
* Resetstats for all entries of a kind.
11101134
*
11111135
* Permission checking for this function is managed through the normal
11121136
* GRANT system.
11131137
*/
11141138
void
1115-
pgstat_reset_shared_counters(constchar*target)
1139+
pgstat_reset_of_kind(PgStat_Kindkind)
11161140
{
1117-
PgStat_MsgResetsharedcountermsg;
1118-
11191141
if (pgStatSock==PGINVALID_SOCKET)
11201142
return;
11211143

1122-
if (strcmp(target,"archiver")==0)
1123-
msg.m_resettarget=RESET_ARCHIVER;
1124-
elseif (strcmp(target,"bgwriter")==0)
1125-
msg.m_resettarget=RESET_BGWRITER;
1126-
elseif (strcmp(target,"wal")==0)
1127-
msg.m_resettarget=RESET_WAL;
1128-
else
1129-
ereport(ERROR,
1130-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1131-
errmsg("unrecognized reset target: \"%s\"",target),
1132-
errhint("Target must be \"archiver\", \"bgwriter\", or \"wal\".")));
1144+
switch (kind)
1145+
{
1146+
casePGSTAT_KIND_ARCHIVER:
1147+
casePGSTAT_KIND_BGWRITER:
1148+
casePGSTAT_KIND_CHECKPOINTER:
1149+
casePGSTAT_KIND_WAL:
1150+
{
1151+
PgStat_MsgResetsharedcountermsg;
11331152

1134-
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSHAREDCOUNTER);
1135-
pgstat_send(&msg,sizeof(msg));
1153+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSHAREDCOUNTER);
1154+
msg.m_resettarget=kind;
1155+
pgstat_send(&msg,sizeof(msg));
1156+
}
1157+
break;
1158+
casePGSTAT_KIND_SLRU:
1159+
{
1160+
PgStat_MsgResetslrucountermsg;
1161+
1162+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSLRUCOUNTER);
1163+
msg.m_index=-1;
1164+
pgstat_send(&msg,sizeof(msg));
1165+
}
1166+
break;
1167+
casePGSTAT_KIND_REPLSLOT:
1168+
{
1169+
PgStat_MsgResetreplslotcountermsg;
1170+
1171+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETREPLSLOTCOUNTER);
1172+
msg.clearall= true;
1173+
pgstat_send(&msg,sizeof(msg));
1174+
}
1175+
break;
1176+
1177+
casePGSTAT_KIND_SUBSCRIPTION:
1178+
{
1179+
PgStat_MsgResetsubcountermsg;
1180+
1181+
msg.m_subid=InvalidOid;
1182+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSUBCOUNTER);
1183+
1184+
pgstat_send(&msg,sizeof(msg));
1185+
}
1186+
break;
1187+
1188+
default:
1189+
elog(ERROR,"unexpected");
1190+
}
11361191
}
11371192

11381193
/*
@@ -1954,7 +2009,7 @@ pgstat_get_replslot_entry(NameData name, bool create)
19542009
if (create&& !found)
19552010
{
19562011
namestrcpy(&(slotent->slotname),NameStr(name));
1957-
pgstat_reset_replslot(slotent,0);
2012+
pgstat_reset_replslot_entry(slotent,0);
19582013
}
19592014

19602015
returnslotent;
@@ -1964,7 +2019,7 @@ pgstat_get_replslot_entry(NameData name, bool create)
19642019
* Reset the given replication slot stats.
19652020
*/
19662021
staticvoid
1967-
pgstat_reset_replslot(PgStat_StatReplSlotEntry*slotent,TimestampTzts)
2022+
pgstat_reset_replslot_entry(PgStat_StatReplSlotEntry*slotent,TimestampTzts)
19682023
{
19692024
/* reset only counters. Don't clear slot name */
19702025
slotent->spill_txns=0;
@@ -3528,7 +3583,8 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
35283583
staticvoid
35293584
pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter*msg,intlen)
35303585
{
3531-
if (msg->m_resettarget==RESET_BGWRITER)
3586+
if (msg->m_resettarget==PGSTAT_KIND_BGWRITER||
3587+
msg->m_resettarget==PGSTAT_KIND_CHECKPOINTER)
35323588
{
35333589
/*
35343590
* Reset the global, bgwriter and checkpointer statistics for the
@@ -3537,13 +3593,13 @@ pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len)
35373593
memset(&globalStats,0,sizeof(globalStats));
35383594
globalStats.bgwriter.stat_reset_timestamp=GetCurrentTimestamp();
35393595
}
3540-
elseif (msg->m_resettarget==RESET_ARCHIVER)
3596+
elseif (msg->m_resettarget==PGSTAT_KIND_ARCHIVER)
35413597
{
35423598
/* Reset the archiver statistics for the cluster. */
35433599
memset(&archiverStats,0,sizeof(archiverStats));
35443600
archiverStats.stat_reset_timestamp=GetCurrentTimestamp();
35453601
}
3546-
elseif (msg->m_resettarget==RESET_WAL)
3602+
elseif (msg->m_resettarget==PGSTAT_KIND_WAL)
35473603
{
35483604
/* Reset the WAL statistics for the cluster. */
35493605
memset(&walStats,0,sizeof(walStats));
@@ -3577,10 +3633,10 @@ pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len)
35773633
dbentry->stat_reset_timestamp=GetCurrentTimestamp();
35783634

35793635
/* Remove object if it exists, ignore it if not */
3580-
if (msg->m_resettype==RESET_TABLE)
3636+
if (msg->m_resettype==PGSTAT_KIND_RELATION)
35813637
(void)hash_search(dbentry->tables, (void*)&(msg->m_objectid),
35823638
HASH_REMOVE,NULL);
3583-
elseif (msg->m_resettype==RESET_FUNCTION)
3639+
elseif (msg->m_resettype==PGSTAT_KIND_FUNCTION)
35843640
(void)hash_search(dbentry->functions, (void*)&(msg->m_objectid),
35853641
HASH_REMOVE,NULL);
35863642
}
@@ -3626,7 +3682,7 @@ pgstat_recv_resetreplslotcounter(PgStat_MsgResetreplslotcounter *msg,
36263682

36273683
hash_seq_init(&sstat,replSlotStatHash);
36283684
while ((slotent= (PgStat_StatReplSlotEntry*)hash_seq_search(&sstat))!=NULL)
3629-
pgstat_reset_replslot(slotent,ts);
3685+
pgstat_reset_replslot_entry(slotent,ts);
36303686
}
36313687
else
36323688
{
@@ -3643,7 +3699,7 @@ pgstat_recv_resetreplslotcounter(PgStat_MsgResetreplslotcounter *msg,
36433699
return;
36443700

36453701
/* Reset the stats for the requested replication slot */
3646-
pgstat_reset_replslot(slotent,ts);
3702+
pgstat_reset_replslot_entry(slotent,ts);
36473703
}
36483704
}
36493705

@@ -3963,7 +4019,7 @@ pgstat_recv_replslot(PgStat_MsgReplSlot *msg, int len)
39634019
* lost, slotent has stats for the old slot. So we initialize all
39644020
* counters at slot creation.
39654021
*/
3966-
pgstat_reset_replslot(slotent,0);
4022+
pgstat_reset_replslot_entry(slotent,0);
39674023
}
39684024
else
39694025
{

‎src/backend/utils/activity/pgstat_replslot.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,45 @@
2323

2424

2525
/*
26-
* Reset counters for a single replication slot, or all replication slots
27-
* (when name is null).
26+
* Reset counters for a single replication slot.
2827
*
2928
* Permission checking for this function is managed through the normal
3029
* GRANT system.
3130
*/
3231
void
33-
pgstat_reset_replslot_counter(constchar*name)
32+
pgstat_reset_replslot(constchar*name)
3433
{
34+
ReplicationSlot*slot;
3535
PgStat_MsgResetreplslotcountermsg;
3636

37+
AssertArg(name!=NULL);
38+
3739
if (pgStatSock==PGINVALID_SOCKET)
3840
return;
3941

40-
if (name)
41-
{
42-
namestrcpy(&msg.m_slotname,name);
43-
msg.clearall= false;
44-
}
45-
else
46-
msg.clearall= true;
42+
/*
43+
* Check if the slot exists with the given name. It is possible that by
44+
* the time this message is executed the slot is dropped but at least this
45+
* check will ensure that the given name is for a valid slot.
46+
*/
47+
slot=SearchNamedReplicationSlot(name, true);
4748

48-
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETREPLSLOTCOUNTER);
49+
if (!slot)
50+
ereport(ERROR,
51+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
52+
errmsg("replication slot \"%s\" does not exist",
53+
name)));
4954

55+
/*
56+
* Nothing to do for physical slots as we collect stats only for logical
57+
* slots.
58+
*/
59+
if (SlotIsPhysical(slot))
60+
return;
61+
62+
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETREPLSLOTCOUNTER);
63+
namestrcpy(&msg.m_slotname,name);
64+
msg.clearall= false;
5065
pgstat_send(&msg,sizeof(msg));
5166
}
5267

‎src/backend/utils/activity/pgstat_slru.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ static PgStat_MsgSLRU SLRUStats[SLRU_NUM_ELEMENTS];
3333

3434

3535
/*
36-
* Reset counters for a single SLRU, or all SLRUs (when name is null).
36+
* Reset counters for a single SLRU.
3737
*
3838
* Permission checking for this function is managed through the normal
3939
* GRANT system.
4040
*/
4141
void
42-
pgstat_reset_slru_counter(constchar*name)
42+
pgstat_reset_slru(constchar*name)
4343
{
4444
PgStat_MsgResetslrucountermsg;
4545

46+
AssertArg(name!=NULL);
47+
4648
if (pgStatSock==PGINVALID_SOCKET)
4749
return;
4850

4951
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSLRUCOUNTER);
50-
msg.m_index=(name) ?pgstat_slru_index(name) :-1;
52+
msg.m_index=pgstat_slru_index(name);
5153

5254
pgstat_send(&msg,sizeof(msg));
5355
}

‎src/backend/utils/activity/pgstat_subscription.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,6 @@
2020
#include"utils/pgstat_internal.h"
2121

2222

23-
/*
24-
* Reset counters for a single subscription, or all subscriptions (when subid
25-
* is InvalidOid).
26-
*
27-
* Permission checking for this function is managed through the normal
28-
* GRANT system.
29-
*/
30-
void
31-
pgstat_reset_subscription_counter(Oidsubid)
32-
{
33-
PgStat_MsgResetsubcountermsg;
34-
35-
if (pgStatSock==PGINVALID_SOCKET)
36-
return;
37-
38-
msg.m_subid=subid;
39-
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_RESETSUBCOUNTER);
40-
41-
pgstat_send(&msg,sizeof(msg));
42-
}
43-
4423
/*
4524
* Report a subscription error.
4625
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp