From 8a6061345638501b0540436c69e6ad884b043535 Mon Sep 17 00:00:00 2001 From: TatsuyaKawata Date: Mon, 19 Jan 2026 23:20:23 +0900 Subject: [PATCH v5] Add sampling statistics to autoanalyze log output Buffer core sampling messages during automatic ANALYZE and include them in the completion log when the logging duration threshold is met. These messages report pages scanned, live and dead rows found, sample rows, and estimated total rows, including details for sampled inheritance children. Otherwise, report the buffered sampling details at DEBUG2, subject to normal message filtering, without enabling resource instrumentation. Buffered details are not reported if analysis fails before completion. Manual ANALYZE retains its immediate sampling messages and separate VERBOSE resource usage report. Manage the sampling buffer locally in do_analyze_rel() and pass it to the core sampling functions. Allocate it only when completion logging is enabled or DEBUG2 messages are of interest at the start of analysis. When collection is disabled, automatic ANALYZE does not emit immediate core sampling messages. FDW callbacks and interfaces remain unchanged: their sampling messages are emitted immediately as before and are not included in the automatic ANALYZE completion log. Author: Tatsuya Kawata Reviewed-by: Fujii Masao Reviewed-by: Sami Imseih Reviewed-by: Chao Li Discussion: https://www.postgresql.org/message-id/flat/CAHza6qcN%3DPaGqo8CGgrqd%2BnaOwY_pLGiwEq6u%3D%2BASZZNL9zi9A%40mail.gmail.com#26a70a815cc922b7513e71fc0c445ff3 --- src/backend/commands/analyze.c | 120 ++++++++++++++++++++++++--------- 1 file changed, 88 insertions(+), 32 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 6a405a6a6ff..e80c0c19b57 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -88,11 +88,13 @@ static VacAttrStats *examine_attribute(Relation onerel, int attnum, Node *index_expr); static int acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, - double *totalrows, double *totaldeadrows); + double *totalrows, double *totaldeadrows, + StringInfo sampling_info); static int compare_rows(const void *a, const void *b, void *arg); static int acquire_inherited_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, - double *totalrows, double *totaldeadrows); + double *totalrows, double *totaldeadrows, + StringInfo sampling_info); static void update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats); static Datum std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); @@ -213,9 +215,7 @@ analyze_rel(Oid relid, RangeVar *relation, if (onerel->rd_rel->relkind == RELKIND_RELATION || onerel->rd_rel->relkind == RELKIND_MATVIEW) { - /* Regular table, so we'll use the regular row acquisition function */ - acquirefunc = acquire_sample_rows; - /* Also get regular table's size */ + /* Get regular table's size */ relpages = RelationGetNumberOfBlocks(onerel); } else if (onerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE) @@ -321,9 +321,9 @@ out: /* * do_analyze_rel() -- analyze one relation, recursively or not * - * Note that "acquirefunc" is only relevant for the non-inherited case. - * For the inherited case, acquire_inherited_sample_rows() determines the - * appropriate acquirefunc for each child table. + * "acquirefunc" is used only for non-inherited analysis of foreign tables. + * For inherited analysis, acquire_inherited_sample_rows() determines how + * to sample each child table. */ static void do_analyze_rel(Relation onerel, const VacuumParams *params, @@ -351,6 +351,7 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, PGRUsage ru0; TimestampTz starttime = 0; MemoryContext caller_context; + StringInfo sampling_info = NULL; Oid save_userid; int save_sec_context; int save_nestlevel; @@ -383,6 +384,16 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, ALLOCSET_DEFAULT_SIZES); caller_context = MemoryContextSwitchTo(anl_context); + /* + * Buffer automatic sampling details if completion logging is enabled or + * DEBUG2 output is currently requested. The duration threshold is + * checked at completion. Manual ANALYZE reports sampling details + * immediately. + */ + if (AmAutoVacuumWorkerProcess() && + (instrument || message_level_is_interesting(DEBUG2))) + sampling_info = makeStringInfo(); + /* * Switch to the table owner's userid, so that any index functions are run * as that user. Also lock down security-restricted operations and @@ -562,7 +573,10 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, if (inh) numrows = acquire_inherited_sample_rows(onerel, elevel, rows, targrows, - &totalrows, &totaldeadrows); + &totalrows, &totaldeadrows, sampling_info); + else if (onerel->rd_rel->relkind != RELKIND_FOREIGN_TABLE) + numrows = acquire_sample_rows(onerel, elevel, rows, targrows, + &totalrows, &totaldeadrows, sampling_info); else numrows = (*acquirefunc) (onerel, elevel, rows, targrows, @@ -765,13 +779,14 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, vac_close_indexes(nindexes, Irel, NoLock); /* Log the action if appropriate */ - if (instrument) + if (instrument || sampling_info != NULL) { TimestampTz endtime = GetCurrentTimestamp(); - if (verbose || params->log_analyze_min_duration == 0 || - TimestampDifferenceExceeds(starttime, endtime, - params->log_analyze_min_duration)) + if (instrument && + (verbose || params->log_analyze_min_duration == 0 || + TimestampDifferenceExceeds(starttime, endtime, + params->log_analyze_min_duration))) { long delay_in_ms; WalUsage walusage; @@ -833,7 +848,12 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, initStringInfo(&buf); if (AmAutoVacuumWorkerProcess()) - msgfmt = _("automatic analyze of table \"%s.%s.%s\"\n"); + { + if (inh) + msgfmt = _("automatic analyze of table \"%s.%s.%s\" inheritance tree\n"); + else + msgfmt = _("automatic analyze of table \"%s.%s.%s\"\n"); + } else msgfmt = _("finished analyzing table \"%s.%s.%s\"\n"); @@ -841,6 +861,9 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, get_database_name(MyDatabaseId), get_namespace_name(RelationGetNamespace(onerel)), RelationGetRelationName(onerel)); + if (sampling_info != NULL) + appendStringInfoString(&buf, sampling_info->data); + if (track_cost_delay_timing) { /* @@ -878,6 +901,18 @@ do_analyze_rel(Relation onerel, const VacuumParams *params, pfree(buf.data); } + else if (sampling_info != NULL) + { + /* Report sampling details at DEBUG2 without resource usage. */ + ereport(DEBUG2, + (errmsg_internal(inh ? + _("automatic analyze of table \"%s.%s.%s\" inheritance tree\n%s") : + _("automatic analyze of table \"%s.%s.%s\"\n%s"), + get_database_name(MyDatabaseId), + get_namespace_name(RelationGetNamespace(onerel)), + RelationGetRelationName(onerel), + sampling_info->data))); + } } /* Roll back any GUC changes executed by index functions */ @@ -1265,6 +1300,10 @@ block_sampling_read_stream_next(ReadStream *stream, * We also estimate the total numbers of live and dead rows in the table, * and return them into *totalrows and *totaldeadrows, respectively. * + * If sampling_info is not NULL, append sampling details to it for the + * automatic ANALYZE completion log. Otherwise, emit them immediately + * at elevel for manual ANALYZE, or omit them for automatic ANALYZE. + * * The returned list of tuples is in order by physical position in the table. * (We will rely on this later to derive correlation estimates.) * @@ -1292,7 +1331,8 @@ block_sampling_read_stream_next(ReadStream *stream, static int acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, - double *totalrows, double *totaldeadrows) + double *totalrows, double *totaldeadrows, + StringInfo sampling_info) { int numrows = 0; /* # rows now in reservoir */ double samplerows = 0; /* total # rows collected */ @@ -1428,17 +1468,27 @@ acquire_sample_rows(Relation onerel, int elevel, *totaldeadrows = 0.0; } - /* - * Emit some interesting relation info - */ - ereport(elevel, - (errmsg("\"%s\": scanned %d of %u pages, " - "containing %.0f live rows and %.0f dead rows; " - "%d rows in sample, %.0f estimated total rows", - RelationGetRelationName(onerel), - bs.m, totalblocks, - liverows, deadrows, - numrows, *totalrows))); + /* Buffer automatic ANALYZE details; preserve manual ANALYZE output. */ + if (sampling_info != NULL) + { + appendStringInfo(sampling_info, + _("\"%s\": scanned %d of %u pages, " + "containing %.0f live rows and %.0f dead rows; " + "%d rows in sample, %.0f estimated total rows"), + RelationGetRelationName(onerel), + bs.m, totalblocks, liverows, deadrows, + numrows, *totalrows); + appendStringInfoChar(sampling_info, '\n'); + } + else if (!AmAutoVacuumWorkerProcess()) + ereport(elevel, + (errmsg("\"%s\": scanned %d of %u pages, " + "containing %.0f live rows and %.0f dead rows; " + "%d rows in sample, %.0f estimated total rows", + RelationGetRelationName(onerel), + bs.m, totalblocks, + liverows, deadrows, + numrows, *totalrows))); return numrows; } @@ -1473,13 +1523,16 @@ compare_rows(const void *a, const void *b, void *arg) * * This has the same API as acquire_sample_rows, except that rows are * collected from all inheritance children as well as the specified table. + * Only core sampling details are appended to sampling_info; FDWs retain + * their own logging behavior. * We fail and return zero if there are no inheritance children, or if all * children are foreign tables that don't support ANALYZE. */ static int acquire_inherited_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, - double *totalrows, double *totaldeadrows) + double *totalrows, double *totaldeadrows, + StringInfo sampling_info) { List *tableOIDs; Relation *rels; @@ -1555,8 +1608,6 @@ acquire_inherited_sample_rows(Relation onerel, int elevel, if (childrel->rd_rel->relkind == RELKIND_RELATION || childrel->rd_rel->relkind == RELKIND_MATVIEW) { - /* Regular table, so use the regular row acquisition function */ - acquirefunc = acquire_sample_rows; relpages = RelationGetNumberOfBlocks(childrel); } else if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE) @@ -1668,9 +1719,14 @@ acquire_inherited_sample_rows(Relation onerel, int elevel, tdrows; /* Fetch a random sample of the child's rows */ - childrows = (*acquirefunc) (childrel, elevel, - rows + numrows, childtargrows, - &trows, &tdrows); + if (childrel->rd_rel->relkind != RELKIND_FOREIGN_TABLE) + childrows = acquire_sample_rows(childrel, elevel, + rows + numrows, childtargrows, + &trows, &tdrows, sampling_info); + else + childrows = (*acquirefunc) (childrel, elevel, + rows + numrows, childtargrows, + &trows, &tdrows); /* We may need to convert from child's rowtype to parent's */ if (childrows > 0 && -- 2.56.1