Skip to content

Commit 5f3e0e8

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 4a21c6f commit 5f3e0e8

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
@@ -616,10 +616,13 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
616616
/*
617617
* Report ANALYZE to the stats collector, too. However, if doing
618618
* inherited stats we shouldn't report, because the stats collector only
619-
* tracks per-table stats.
619+
* tracks per-table stats. Reset the changes_since_analyze counter only
620+
* if we analyzed all columns; otherwise, there is still work for
621+
* auto-analyze to do.
620622
*/
621623
if (!inh)
622-
pgstat_report_analyze(onerel, totalrows, totaldeadrows);
624+
pgstat_report_analyze(onerel, totalrows, totaldeadrows,
625+
(vacstmt->va_cols == NIL));
623626

624627
/* If this isn't part of VACUUM ANALYZE, let index AMs do cleanup */
625628
if (!(vacstmt->options & VACOPT_VACUUM))

src/backend/postmaster/pgstat.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,11 +1329,15 @@ pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter tuples)
13291329
* pgstat_report_analyze() -
13301330
*
13311331
* Tell the collector about the table we just analyzed.
1332+
*
1333+
* Caller must provide new live- and dead-tuples estimates, as well as a
1334+
* flag indicating whether to reset the changes_since_analyze counter.
13321335
* --------
13331336
*/
13341337
void
13351338
pgstat_report_analyze(Relation rel,
1336-
PgStat_Counter livetuples, PgStat_Counter deadtuples)
1339+
PgStat_Counter livetuples, PgStat_Counter deadtuples,
1340+
bool resetcounter)
13371341
{
13381342
PgStat_MsgAnalyze msg;
13391343

@@ -1370,6 +1374,7 @@ pgstat_report_analyze(Relation rel,
13701374
msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
13711375
msg.m_tableoid = RelationGetRelid(rel);
13721376
msg.m_autovacuum = IsAutoVacuumWorkerProcess();
1377+
msg.m_resetcounter = resetcounter;
13731378
msg.m_analyzetime = GetCurrentTimestamp();
13741379
msg.m_live_tuples = livetuples;
13751380
msg.m_dead_tuples = deadtuples;
@@ -4842,10 +4847,12 @@ pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
48424847
tabentry->n_dead_tuples = msg->m_dead_tuples;
48434848

48444849
/*
4845-
* We reset changes_since_analyze to zero, forgetting any changes that
4846-
* occurred while the ANALYZE was in progress.
4850+
* If commanded, reset changes_since_analyze to zero. This forgets any
4851+
* changes that were committed while the ANALYZE was in progress, but we
4852+
* have no good way to estimate how many of those there were.
48474853
*/
4848-
tabentry->changes_since_analyze = 0;
4854+
if (msg->m_resetcounter)
4855+
tabentry->changes_since_analyze = 0;
48494856

48504857
if (msg->m_autovacuum)
48514858
{

src/include/pgstat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ typedef struct PgStat_MsgAnalyze
359359
Oid m_databaseid;
360360
Oid m_tableoid;
361361
bool m_autovacuum;
362+
bool m_resetcounter;
362363
TimestampTz m_analyzetime;
363364
PgStat_Counter m_live_tuples;
364365
PgStat_Counter m_dead_tuples;
@@ -788,7 +789,8 @@ extern void pgstat_report_autovac(Oid dboid);
788789
extern void pgstat_report_vacuum(Oid tableoid, bool shared,
789790
PgStat_Counter tuples);
790791
extern void pgstat_report_analyze(Relation rel,
791-
PgStat_Counter livetuples, PgStat_Counter deadtuples);
792+
PgStat_Counter livetuples, PgStat_Counter deadtuples,
793+
bool resetcounter);
792794

793795
extern void pgstat_report_recovery_conflict(int reason);
794796
extern void pgstat_report_deadlock(void);

0 commit comments

Comments
 (0)