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

Commit f64340e

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 c6dbf1f commit f64340e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/backend/commands/analyze.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,13 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
611611
/*
612612
* Report ANALYZE to the stats collector, too. However, if doing
613613
* inherited stats we shouldn't report, because the stats collector only
614-
* tracks per-table stats.
614+
* tracks per-table stats. Reset the changes_since_analyze counter only
615+
* if we analyzed all columns; otherwise, there is still work for
616+
* auto-analyze to do.
615617
*/
616618
if (!inh)
617-
pgstat_report_analyze(onerel, totalrows, totaldeadrows);
619+
pgstat_report_analyze(onerel, totalrows, totaldeadrows,
620+
(va_cols == NIL));
618621

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

src/backend/postmaster/pgstat.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -1340,11 +1340,15 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
13401340
* pgstat_report_analyze() -
13411341
*
13421342
* Tell the collector about the table we just analyzed.
1343+
*
1344+
* Caller must provide new live- and dead-tuples estimates, as well as a
1345+
* flag indicating whether to reset the changes_since_analyze counter.
13431346
* --------
13441347
*/
13451348
void
13461349
pgstat_report_analyze(Relation rel,
1347-
PgStat_Counter livetuples, PgStat_Counter deadtuples)
1350+
PgStat_Counter livetuples, PgStat_Counter deadtuples,
1351+
bool resetcounter)
13481352
{
13491353
PgStat_MsgAnalyze msg;
13501354

@@ -1381,6 +1385,7 @@ pgstat_report_analyze(Relation rel,
13811385
msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
13821386
msg.m_tableoid = RelationGetRelid(rel);
13831387
msg.m_autovacuum = IsAutoVacuumWorkerProcess();
1388+
msg.m_resetcounter = resetcounter;
13841389
msg.m_analyzetime = GetCurrentTimestamp();
13851390
msg.m_live_tuples = livetuples;
13861391
msg.m_dead_tuples = deadtuples;
@@ -5263,10 +5268,12 @@ pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
52635268
tabentry->n_dead_tuples = msg->m_dead_tuples;
52645269

52655270
/*
5266-
* We reset changes_since_analyze to zero, forgetting any changes that
5267-
* occurred while the ANALYZE was in progress.
5271+
* If commanded, reset changes_since_analyze to zero. This forgets any
5272+
* changes that were committed while the ANALYZE was in progress, but we
5273+
* have no good way to estimate how many of those there were.
52685274
*/
5269-
tabentry->changes_since_analyze = 0;
5275+
if (msg->m_resetcounter)
5276+
tabentry->changes_since_analyze = 0;
52705277

52715278
if (msg->m_autovacuum)
52725279
{

src/include/pgstat.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ typedef struct PgStat_MsgAnalyze
383383
Oid m_databaseid;
384384
Oid m_tableoid;
385385
bool m_autovacuum;
386+
bool m_resetcounter;
386387
TimestampTz m_analyzetime;
387388
PgStat_Counter m_live_tuples;
388389
PgStat_Counter m_dead_tuples;
@@ -970,7 +971,8 @@ extern void pgstat_report_autovac(Oid dboid);
970971
extern void pgstat_report_vacuum(Oid tableoid, bool shared,
971972
PgStat_Counter livetuples, PgStat_Counter deadtuples);
972973
extern void pgstat_report_analyze(Relation rel,
973-
PgStat_Counter livetuples, PgStat_Counter deadtuples);
974+
PgStat_Counter livetuples, PgStat_Counter deadtuples,
975+
bool resetcounter);
974976

975977
extern void pgstat_report_recovery_conflict(int reason);
976978
extern void pgstat_report_deadlock(void);

0 commit comments

Comments
 (0)