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

Commit 115f414

Browse files
committed
Fix VACUUM's reporting of dead-tuple counts to the stats collector.
Historically, VACUUM has just reported its new_rel_tuples estimate (the same thing it puts into pg_class.reltuples) to the stats collector. That number counts both live and dead-but-not-yet-reclaimable tuples. This behavior may once have been right, but modern versions of the pgstats code track live and dead tuple counts separately, so putting the total into n_live_tuples and zero into n_dead_tuples is surely pretty bogus. Fix it to report live and dead tuple counts separately. This doesn't really do much for situations where updating transactions commit concurrently with a VACUUM scan (possibly causing double-counting or omission of the tuples they add or delete); but it's clearly an improvement over what we were doing before. Hari Babu, reviewed by Amit Kapila
1 parent 76e91b3 commit 115f414

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/backend/commands/vacuumlazy.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ typedef struct LVRelStats
106106
double scanned_tuples; /* counts only tuples on scanned pages */
107107
double old_rel_tuples; /* previous value of pg_class.reltuples */
108108
double new_rel_tuples; /* new estimated total # of tuples */
109+
double new_dead_tuples; /* new estimated total # of dead tuples */
109110
BlockNumber pages_removed;
110111
double tuples_deleted;
111112
BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */
@@ -185,6 +186,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
185186
BlockNumber new_rel_pages;
186187
double new_rel_tuples;
187188
BlockNumber new_rel_allvisible;
189+
double new_live_tuples;
188190
TransactionId new_frozen_xid;
189191
MultiXactId new_min_multi;
190192

@@ -307,9 +309,14 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
307309
new_min_multi);
308310

309311
/* report results to the stats collector, too */
312+
new_live_tuples = new_rel_tuples - vacrelstats->new_dead_tuples;
313+
if (new_live_tuples < 0)
314+
new_live_tuples = 0; /* just in case */
315+
310316
pgstat_report_vacuum(RelationGetRelid(onerel),
311317
onerel->rd_rel->relisshared,
312-
new_rel_tuples);
318+
new_live_tuples,
319+
vacrelstats->new_dead_tuples);
313320

314321
/* and log the action if appropriate */
315322
if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
@@ -334,7 +341,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
334341
ereport(LOG,
335342
(errmsg("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"
336343
"pages: %d removed, %d remain\n"
337-
"tuples: %.0f removed, %.0f remain\n"
344+
"tuples: %.0f removed, %.0f remain, %.0f are dead but not yet removable\n"
338345
"buffer usage: %d hits, %d misses, %d dirtied\n"
339346
"avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"
340347
"system usage: %s",
@@ -346,6 +353,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
346353
vacrelstats->rel_pages,
347354
vacrelstats->tuples_deleted,
348355
vacrelstats->new_rel_tuples,
356+
vacrelstats->new_dead_tuples,
349357
VacuumPageHit,
350358
VacuumPageMiss,
351359
VacuumPageDirty,
@@ -1036,6 +1044,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
10361044
/* save stats for use later */
10371045
vacrelstats->scanned_tuples = num_tuples;
10381046
vacrelstats->tuples_deleted = tups_vacuumed;
1047+
vacrelstats->new_dead_tuples = nkeep;
10391048

10401049
/* now we can compute the new value for pg_class.reltuples */
10411050
vacrelstats->new_rel_tuples = vac_estimate_reltuples(onerel, false,

src/backend/postmaster/pgstat.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,8 @@ pgstat_report_autovac(Oid dboid)
13271327
* ---------
13281328
*/
13291329
void
1330-
pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter tuples)
1330+
pgstat_report_vacuum(Oid tableoid, bool shared,
1331+
PgStat_Counter livetuples, PgStat_Counter deadtuples)
13311332
{
13321333
PgStat_MsgVacuum msg;
13331334

@@ -1339,7 +1340,8 @@ pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter tuples)
13391340
msg.m_tableoid = tableoid;
13401341
msg.m_autovacuum = IsAutoVacuumWorkerProcess();
13411342
msg.m_vacuumtime = GetCurrentTimestamp();
1342-
msg.m_tuples = tuples;
1343+
msg.m_live_tuples = livetuples;
1344+
msg.m_dead_tuples = deadtuples;
13431345
pgstat_send(&msg, sizeof(msg));
13441346
}
13451347

@@ -4809,9 +4811,8 @@ pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
48094811

48104812
tabentry = pgstat_get_tab_entry(dbentry, msg->m_tableoid, true);
48114813

4812-
tabentry->n_live_tuples = msg->m_tuples;
4813-
/* Resetting dead_tuples to 0 is an approximation ... */
4814-
tabentry->n_dead_tuples = 0;
4814+
tabentry->n_live_tuples = msg->m_live_tuples;
4815+
tabentry->n_dead_tuples = msg->m_dead_tuples;
48154816

48164817
if (msg->m_autovacuum)
48174818
{

src/include/pgstat.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ typedef struct PgStat_MsgVacuum
333333
Oid m_tableoid;
334334
bool m_autovacuum;
335335
TimestampTz m_vacuumtime;
336-
PgStat_Counter m_tuples;
336+
PgStat_Counter m_live_tuples;
337+
PgStat_Counter m_dead_tuples;
337338
} PgStat_MsgVacuum;
338339

339340

@@ -775,7 +776,7 @@ extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type t
775776

776777
extern void pgstat_report_autovac(Oid dboid);
777778
extern void pgstat_report_vacuum(Oid tableoid, bool shared,
778-
PgStat_Counter tuples);
779+
PgStat_Counter livetuples, PgStat_Counter deadtuples);
779780
extern void pgstat_report_analyze(Relation rel,
780781
PgStat_Counter livetuples, PgStat_Counter deadtuples);
781782

0 commit comments

Comments
 (0)