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

Commit995fb74

Browse files
committed
Turn PGBE_ACTIVITY_SIZE into a GUC variable, track_activity_query_size.
As the buffer could now be a lot larger than before, and copying it couldthus be a lot more expensive than before, use strcpy instead of memcpy tocopy the query string, as was already suggested in comments. Also, only copythe PgBackendStatus struct and string if the slot is in use.Patch by Thomas Lee, with some changes by me.
1 parent7ea9b99 commit995fb74

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.180 2008/06/14 21:59:59 alvherre Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.181 2008/06/30 10:58:47 heikki Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -3331,6 +3331,22 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
33313331
</listitem>
33323332
</varlistentry>
33333333

3334+
<varlistentry id="guc-track-activity-query-size" xreflabel="track_activity_query_size">
3335+
<term><varname>track_activity_query_size</varname> (<type>integer</type>)</term>
3336+
<indexterm>
3337+
<primary><varname>track_activity_query_size</> configuration parameter</primary>
3338+
</indexterm>
3339+
<listitem>
3340+
<para>
3341+
Specifies the number of bytes reserved to track the currently
3342+
executing command for each active session, for the
3343+
<structname>pg_stat_activity</>.<structfield>current_query</> field.
3344+
The default value is 1024. This parameter can only be set at server
3345+
start.
3346+
</para>
3347+
</listitem>
3348+
</varlistentry>
3349+
33343350
<varlistentry id="guc-track-counts" xreflabel="track_counts">
33353351
<term><varname>track_counts</varname> (<type>boolean</type>)</term>
33363352
<indexterm>

‎src/backend/postmaster/pgstat.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*Copyright (c) 2001-2008, PostgreSQL Global Development Group
1515
*
16-
*$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.175 2008/06/19 00:46:05 alvherre Exp $
16+
*$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.176 2008/06/30 10:58:47 heikki Exp $
1717
* ----------
1818
*/
1919
#include"postgres.h"
@@ -101,6 +101,7 @@
101101
boolpgstat_track_activities= false;
102102
boolpgstat_track_counts= false;
103103
intpgstat_track_functions=TRACK_FUNC_OFF;
104+
intpgstat_track_activity_query_size=1024;
104105

105106
/*
106107
* BgWriter global statistics counters (unused in other processes).
@@ -2010,6 +2011,7 @@ pgstat_fetch_global(void)
20102011

20112012
staticPgBackendStatus*BackendStatusArray=NULL;
20122013
staticPgBackendStatus*MyBEEntry=NULL;
2014+
staticchar*BackendActivityBuffer=NULL;
20132015

20142016

20152017
/*
@@ -2020,20 +2022,25 @@ BackendStatusShmemSize(void)
20202022
{
20212023
Sizesize;
20222024

2023-
size=mul_size(sizeof(PgBackendStatus),MaxBackends);
2025+
size=add_size(mul_size(sizeof(PgBackendStatus),MaxBackends),
2026+
mul_size(pgstat_track_activity_query_size,MaxBackends));
20242027
returnsize;
20252028
}
20262029

20272030
/*
2028-
* Initialize the shared status array during postmaster startup.
2031+
* Initialize the shared status array and activity string buffer during
2032+
* postmaster startup.
20292033
*/
20302034
void
20312035
CreateSharedBackendStatus(void)
20322036
{
2033-
Sizesize=BackendStatusShmemSize();
2037+
Sizesize;
20342038
boolfound;
2039+
inti;
2040+
char*buffer;
20352041

20362042
/* Create or attach to the shared array */
2043+
size=mul_size(sizeof(PgBackendStatus),MaxBackends);
20372044
BackendStatusArray= (PgBackendStatus*)
20382045
ShmemInitStruct("Backend Status Array",size,&found);
20392046

@@ -2044,6 +2051,23 @@ CreateSharedBackendStatus(void)
20442051
*/
20452052
MemSet(BackendStatusArray,0,size);
20462053
}
2054+
2055+
/* Create or attach to the shared activity buffer */
2056+
size=mul_size(pgstat_track_activity_query_size,MaxBackends);
2057+
BackendActivityBuffer= (char*)
2058+
ShmemInitStruct("Backend Activity Buffer",size,&found);
2059+
2060+
if (!found)
2061+
{
2062+
MemSet(BackendActivityBuffer,0,size);
2063+
2064+
/* Initialize st_activity pointers. */
2065+
buffer=BackendActivityBuffer;
2066+
for (i=0;i<MaxBackends;i++) {
2067+
BackendStatusArray[i].st_activity=buffer;
2068+
buffer+=pgstat_track_activity_query_size;
2069+
}
2070+
}
20472071
}
20482072

20492073

@@ -2128,7 +2152,7 @@ pgstat_bestart(void)
21282152
beentry->st_waiting= false;
21292153
beentry->st_activity[0]='\0';
21302154
/* Also make sure the last byte in the string area is always 0 */
2131-
beentry->st_activity[PGBE_ACTIVITY_SIZE-1]='\0';
2155+
beentry->st_activity[pgstat_track_activity_query_size-1]='\0';
21322156

21332157
beentry->st_changecount++;
21342158
Assert((beentry->st_changecount&1)==0);
@@ -2188,7 +2212,7 @@ pgstat_report_activity(const char *cmd_str)
21882212
start_timestamp=GetCurrentStatementStartTimestamp();
21892213

21902214
len=strlen(cmd_str);
2191-
len=pg_mbcliplen(cmd_str,len,PGBE_ACTIVITY_SIZE-1);
2215+
len=pg_mbcliplen(cmd_str,len,pgstat_track_activity_query_size-1);
21922216

21932217
/*
21942218
* Update my status entry, following the protocol of bumping
@@ -2267,6 +2291,7 @@ pgstat_read_current_status(void)
22672291
volatilePgBackendStatus*beentry;
22682292
PgBackendStatus*localtable;
22692293
PgBackendStatus*localentry;
2294+
char*localactivity;
22702295
inti;
22712296

22722297
Assert(!pgStatRunningInCollector);
@@ -2278,6 +2303,9 @@ pgstat_read_current_status(void)
22782303
localtable= (PgBackendStatus*)
22792304
MemoryContextAlloc(pgStatLocalContext,
22802305
sizeof(PgBackendStatus)*MaxBackends);
2306+
localactivity= (char*)
2307+
MemoryContextAlloc(pgStatLocalContext,
2308+
pgstat_track_activity_query_size*MaxBackends);
22812309
localNumBackends=0;
22822310

22832311
beentry=BackendStatusArray;
@@ -2295,11 +2323,17 @@ pgstat_read_current_status(void)
22952323
{
22962324
intsave_changecount=beentry->st_changecount;
22972325

2298-
/*
2299-
* XXX if PGBE_ACTIVITY_SIZE is really large, it might be best to
2300-
* use strcpy not memcpy for copying the activity string?
2301-
*/
2302-
memcpy(localentry, (char*)beentry,sizeof(PgBackendStatus));
2326+
localentry->st_procpid=beentry->st_procpid;
2327+
if (localentry->st_procpid>0)
2328+
{
2329+
memcpy(localentry, (char*)beentry,sizeof(PgBackendStatus));
2330+
/*
2331+
* strcpy is safe even if the string is modified concurrently,
2332+
* because there's always a \0 at the end of the buffer.
2333+
*/
2334+
strcpy(localactivity, (char*)beentry->st_activity);
2335+
localentry->st_activity=localactivity;
2336+
}
23032337

23042338
if (save_changecount==beentry->st_changecount&&
23052339
(save_changecount&1)==0)
@@ -2314,6 +2348,7 @@ pgstat_read_current_status(void)
23142348
if (localentry->st_procpid>0)
23152349
{
23162350
localentry++;
2351+
localactivity+=pgstat_track_activity_query_size;
23172352
localNumBackends++;
23182353
}
23192354
}

‎src/backend/utils/misc/guc.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.456 2008/05/28 09:04:06 mha Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.457 2008/06/30 10:58:47 heikki Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -1848,6 +1848,15 @@ static struct config_int ConfigureNamesInt[] =
18481848
-1,-1,INT_MAX,NULL,NULL
18491849
},
18501850

1851+
{
1852+
{"track_activity_query_size",PGC_POSTMASTER,RESOURCES_MEM,
1853+
gettext_noop("Sets the size reserved for pg_stat_activity.current_query, in bytes."),
1854+
NULL,
1855+
},
1856+
&pgstat_track_activity_query_size,
1857+
1024,100,102400,NULL,NULL
1858+
},
1859+
18511860
/* End-of-list marker */
18521861
{
18531862
{NULL,0,0,NULL,NULL},NULL,0,0,0,NULL,NULL

‎src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@
364364
#track_activities = on
365365
#track_counts = on
366366
#track_functions = none# none, pl, all
367+
#track_activity_query_size = 1024
367368
#update_process_title = on
368369

369370

‎src/include/pgstat.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*Copyright (c) 2001-2008, PostgreSQL Global Development Group
77
*
8-
*$PostgreSQL: pgsql/src/include/pgstat.h,v 1.76 2008/06/19 00:46:05 alvherre Exp $
8+
*$PostgreSQL: pgsql/src/include/pgstat.h,v 1.77 2008/06/30 10:58:47 heikki Exp $
99
* ----------
1010
*/
1111
#ifndefPGSTAT_H
@@ -509,9 +509,6 @@ typedef struct PgStat_GlobalStats
509509
* ----------
510510
*/
511511

512-
/* Max length of st_activity string ... perhaps replace with a GUC var? */
513-
#definePGBE_ACTIVITY_SIZE1024
514-
515512
/* ----------
516513
* PgBackendStatus
517514
*
@@ -551,7 +548,7 @@ typedef struct PgBackendStatus
551548
boolst_waiting;
552549

553550
/* current command string; MUST be null-terminated */
554-
charst_activity[PGBE_ACTIVITY_SIZE];
551+
char*st_activity;
555552
}PgBackendStatus;
556553

557554
/*
@@ -578,6 +575,7 @@ typedef struct PgStat_FunctionCallUsage
578575
externboolpgstat_track_activities;
579576
externboolpgstat_track_counts;
580577
externintpgstat_track_functions;
578+
externintpgstat_track_activity_query_size;
581579

582580
/*
583581
* BgWriter statistics counters are updated directly by bgwriter and bufmgr

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp