Skip to content

Commit 49b3462

Browse files
committed
Have autovacuum report its activities to the stat collector.
1 parent 4adab7e commit 49b3462

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.18 2006/05/03 22:45:26 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.19 2006/05/19 15:15:37 alvherre Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -45,6 +45,8 @@
4545
#include "utils/fmgroids.h"
4646
#include "utils/memutils.h"
4747
#include "utils/ps_status.h"
48+
#include "utils/lsyscache.h"
49+
#include "utils/rel.h"
4850
#include "utils/relcache.h"
4951
#include "utils/syscache.h"
5052

@@ -109,6 +111,8 @@ static void test_rel_for_autovac(Oid relid, PgStat_StatTabEntry *tabentry,
109111
List **toast_table_ids);
110112
static void autovacuum_do_vac_analyze(List *relids, bool dovacuum,
111113
bool doanalyze, bool freeze);
114+
static void autovac_report_activity(VacuumStmt *vacstmt,
115+
List *relids);
112116

113117

114118
/*
@@ -911,12 +915,75 @@ autovacuum_do_vac_analyze(List *relids, bool dovacuum, bool doanalyze,
911915
vacstmt->relation = NULL; /* all tables, or not used if relids != NIL */
912916
vacstmt->va_cols = NIL;
913917

918+
/* Let pgstat know what we're doing */
919+
autovac_report_activity(vacstmt, relids);
920+
914921
vacuum(vacstmt, relids);
915922

916923
pfree(vacstmt);
917924
MemoryContextSwitchTo(old_cxt);
918925
}
919926

927+
/*
928+
* autovac_report_activity
929+
* Report to pgstat what autovacuum is doing
930+
*
931+
* We send a SQL string corresponding to what the user would see if the
932+
* equivalent command was to be issued manually.
933+
*
934+
* Note we assume that we are going to report the next command as soon as we're
935+
* done with the current one, and exiting right after the last one, so we don't
936+
* bother to report "<IDLE>" or some such.
937+
*/
938+
#define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 32)
939+
static void
940+
autovac_report_activity(VacuumStmt *vacstmt, List *relids)
941+
{
942+
char activity[MAX_AUTOVAC_ACTIV_LEN];
943+
944+
/*
945+
* This case is not currently exercised by the autovac code. Fill it in
946+
* if needed.
947+
*/
948+
if (list_length(relids) > 1)
949+
elog(WARNING, "vacuuming >1 rel unsupported");
950+
951+
/* Report the command and possible options */
952+
if (vacstmt->vacuum)
953+
snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
954+
"VACUUM%s%s%s",
955+
vacstmt->full ? " FULL" : "",
956+
vacstmt->freeze ? " FREEZE" : "",
957+
vacstmt->analyze ? " ANALYZE" : "");
958+
else if (vacstmt->analyze)
959+
snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
960+
"ANALYZE");
961+
962+
/* Report the qualified name of the first relation, if any */
963+
if (list_length(relids) > 0)
964+
{
965+
Oid relid = linitial_oid(relids);
966+
Relation rel;
967+
968+
rel = RelationIdGetRelation(relid);
969+
if (rel == NULL)
970+
elog(WARNING, "cache lookup failed for relation %u", relid);
971+
else
972+
{
973+
char *nspname = get_namespace_name(RelationGetNamespace(rel));
974+
int len = strlen(activity);
975+
976+
snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
977+
" %s.%s", nspname, RelationGetRelationName(rel));
978+
979+
pfree(nspname);
980+
RelationClose(rel);
981+
}
982+
}
983+
984+
pgstat_report_activity(activity);
985+
}
986+
920987
/*
921988
* AutoVacuumingActive
922989
* Check GUC vars and report whether the autovacuum process should be

src/backend/postmaster/pgstat.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2006, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.124 2006/04/27 00:06:58 momjian Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.125 2006/05/19 15:15:37 alvherre Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -700,17 +700,17 @@ pgstat_bestart(void)
700700

701701
/*
702702
* We may not have a MyProcPort (eg, if this is the autovacuum process).
703-
* For the moment, punt and don't send BESTART --- would be better to work
704-
* out a clean way of handling "unknown clientaddr".
703+
* Send an all-zeroes client address, which is dealt with specially in
704+
* pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
705705
*/
706+
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_BESTART);
707+
msg.m_databaseid = MyDatabaseId;
708+
msg.m_userid = GetSessionUserId();
706709
if (MyProcPort)
707-
{
708-
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_BESTART);
709-
msg.m_databaseid = MyDatabaseId;
710-
msg.m_userid = GetSessionUserId();
711710
memcpy(&msg.m_clientaddr, &MyProcPort->raddr, sizeof(msg.m_clientaddr));
712-
pgstat_send(&msg, sizeof(msg));
713-
}
711+
else
712+
MemSet(&msg.m_clientaddr, 0, sizeof(msg.m_clientaddr));
713+
pgstat_send(&msg, sizeof(msg));
714714

715715
/*
716716
* Set up a process-exit hook to ensure we flush the last batch of

src/backend/postmaster/postmaster.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.483 2006/03/18 22:09:58 neilc Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.484 2006/05/19 15:15:37 alvherre Exp $
4141
*
4242
* NOTES
4343
*
@@ -2182,6 +2182,9 @@ reaper(SIGNAL_ARGS)
21822182
{
21832183
AutoVacPID = 0;
21842184
autovac_stopped();
2185+
/* Tell the collector about process termination */
2186+
pgstat_beterm(pid);
2187+
21852188
if (exitstatus != 0)
21862189
HandleChildCrash(pid, exitstatus,
21872190
_("autovacuum process"));

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.27 2006/03/05 15:58:43 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.28 2006/05/19 15:15:37 alvherre Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -389,6 +389,7 @@ Datum
389389
pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
390390
{
391391
PgStat_StatBeEntry *beentry;
392+
SockAddr zero_clientaddr;
392393
int32 beid;
393394
char remote_host[NI_MAXHOST];
394395
int ret;
@@ -405,6 +406,12 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
405406
if (!superuser() && beentry->userid != GetUserId())
406407
PG_RETURN_NULL();
407408

409+
/* A zeroed client addr means we don't know */
410+
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
411+
if (memcmp(&(beentry->clientaddr), &zero_clientaddr,
412+
sizeof(zero_clientaddr) == 0))
413+
PG_RETURN_NULL();
414+
408415
switch (beentry->clientaddr.addr.ss_family)
409416
{
410417
case AF_INET:
@@ -432,6 +439,7 @@ Datum
432439
pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
433440
{
434441
PgStat_StatBeEntry *beentry;
442+
SockAddr zero_clientaddr;
435443
int32 beid;
436444
char remote_port[NI_MAXSERV];
437445
int ret;
@@ -448,6 +456,12 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
448456
if (!superuser() && beentry->userid != GetUserId())
449457
PG_RETURN_NULL();
450458

459+
/* A zeroed client addr means we don't know */
460+
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
461+
if (memcmp(&(beentry->clientaddr), &zero_clientaddr,
462+
sizeof(zero_clientaddr) == 0))
463+
PG_RETURN_NULL();
464+
451465
switch (beentry->clientaddr.addr.ss_family)
452466
{
453467
case AF_INET:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy