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

Commit 5acc58c

Browse files
committed
Don't reset changes_since_analyze after a selective-columns ANALYZE.
If we ANALYZE only selected columns of a table, we should not postpone auto-analyze because of that; other columns may well still need stats updates. As committed, the counter is left alone if a column list is given, whether or not it includes all analyzable columns of the table. Per complaint from Tomasz Ostrowski. It's been like this a long time, so back-patch to all supported branches. Report: <ef99c1bd-ff60-5f32-2733-c7b504eb960c@ato.waw.pl>
1 parent a7aa61f commit 5acc58c

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/backend/commands/analyze.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,13 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
605605
/*
606606
* Report ANALYZE to the stats collector, too. However, if doing
607607
* inherited stats we shouldn't report, because the stats collector only
608-
* tracks per-table stats.
608+
* tracks per-table stats. Reset the changes_since_analyze counter only
609+
* if we analyzed all columns; otherwise, there is still work for
610+
* auto-analyze to do.
609611
*/
610612
if (!inh)
611-
pgstat_report_analyze(onerel, totalrows, totaldeadrows);
613+
pgstat_report_analyze(onerel, totalrows, totaldeadrows,
614+
(va_cols == NIL));
612615

613616
/* If this isn't part of VACUUM ANALYZE, let index AMs do cleanup */
614617
if (!(options & VACOPT_VACUUM))

src/backend/postmaster/pgstat.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,11 +1346,15 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
13461346
* pgstat_report_analyze() -
13471347
*
13481348
* Tell the collector about the table we just analyzed.
1349+
*
1350+
* Caller must provide new live- and dead-tuples estimates, as well as a
1351+
* flag indicating whether to reset the changes_since_analyze counter.
13491352
* --------
13501353
*/
13511354
void
13521355
pgstat_report_analyze(Relation rel,
1353-
PgStat_Counter livetuples, PgStat_Counter deadtuples)
1356+
PgStat_Counter livetuples, PgStat_Counter deadtuples,
1357+
bool resetcounter)
13541358
{
13551359
PgStat_MsgAnalyze msg;
13561360

@@ -1387,6 +1391,7 @@ pgstat_report_analyze(Relation rel,
13871391
msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
13881392
msg.m_tableoid = RelationGetRelid(rel);
13891393
msg.m_autovacuum = IsAutoVacuumWorkerProcess();
1394+
msg.m_resetcounter = resetcounter;
13901395
msg.m_analyzetime = GetCurrentTimestamp();
13911396
msg.m_live_tuples = livetuples;
13921397
msg.m_dead_tuples = deadtuples;
@@ -5110,10 +5115,12 @@ pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
51105115
tabentry->n_dead_tuples = msg->m_dead_tuples;
51115116

51125117
/*
5113-
* We reset changes_since_analyze to zero, forgetting any changes that
5114-
* occurred while the ANALYZE was in progress.
5118+
* If commanded, reset changes_since_analyze to zero. This forgets any
5119+
* changes that were committed while the ANALYZE was in progress, but we
5120+
* have no good way to estimate how many of those there were.
51155121
*/
5116-
tabentry->changes_since_analyze = 0;
5122+
if (msg->m_resetcounter)
5123+
tabentry->changes_since_analyze = 0;
51175124

51185125
if (msg->m_autovacuum)
51195126
{

src/include/pgstat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ typedef struct PgStat_MsgAnalyze
382382
Oid m_databaseid;
383383
Oid m_tableoid;
384384
bool m_autovacuum;
385+
bool m_resetcounter;
385386
TimestampTz m_analyzetime;
386387
PgStat_Counter m_live_tuples;
387388
PgStat_Counter m_dead_tuples;
@@ -932,7 +933,8 @@ extern void pgstat_report_autovac(Oid dboid);
932933
extern void pgstat_report_vacuum(Oid tableoid, bool shared,
933934
PgStat_Counter livetuples, PgStat_Counter deadtuples);
934935
extern void pgstat_report_analyze(Relation rel,
935-
PgStat_Counter livetuples, PgStat_Counter deadtuples);
936+
PgStat_Counter livetuples, PgStat_Counter deadtuples,
937+
bool resetcounter);
936938

937939
extern void pgstat_report_recovery_conflict(int reason);
938940
extern void pgstat_report_deadlock(void);

0 commit comments

Comments
 (0)