diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 9856968..dbec195 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -521,6 +521,13 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser VACUUM, showing current progress. See . + + + pg_stat_progress_analyzepg_stat_progress_analyze + One row for each backend running + ANALYZE, showing current progress. + See . + @@ -3186,9 +3193,8 @@ SELECT pg_stat_get_backend_pid(s.backendid) AS pid, PostgreSQL has the ability to report the progress of - certain commands during command execution. Currently, the only command - which supports progress reporting is VACUUM. This may be - expanded in the future. + certain commands during command execution. Currently, the supported progress + reporting commands are VACUUM and ANALYZE. @@ -3378,7 +3384,143 @@ SELECT pg_stat_get_backend_pid(s.backendid) AS pid, + + + + ANALYZE Progress Reporting + + + Whenever ANALYZE is running, the + pg_stat_progress_analyze view will contain + one row for each backend that is currently analyzing. + The tables below describe the information that will be reported and + provide information about how to interpret it. + + + + <structname>pg_stat_progress_analyze</structname> View + + + + Column + Type + Description + + + + + + pid + integer + Process ID of backend. + + + datid + oid + OID of the database to which this backend is connected. + + + datname + name + Name of the database to which this backend is connected. + + + relid + oid + OID of the table being analyzed. + + + phase + text + + Current processing phase of analyze. See . + + + + num_target_sample_rows + bigint + + Total number of sample rows of the relation. The sample it reads + is taken randomly. Its size depends on the + parameter value. + + + + num_rows_sampled + bigint + + Total number of rows sampled so far. If the table being analyzed + has one or more children, the target sample rows should be different + w.r.t number of rows sampled because ANALYZE will gather + sample rows twice. Once on the rows of parent table only and second + time on the rows of parent table with all of its children. + + + + +
+ + ANALYZE phases + + + + Phase + Description + + + + + + initializing + + ANALYZE is preparing to collect sample rows. + + + + collecting sample rows + + ANALYZE is currently collecting the sample rows of + single relation. The sample it reads is taken randomly. Its size + depends on the + parameter value. + + + + collecting inherited sample rows + + ANALYZE is currently collecting inherited sample rows. + If the table being analyzed has one or more children, this phase will + collect sample rows of parent table and all associated child relation. + The sample it reads is taken randomly. Its size depends on + the parameter value. + + + + computing statistics + + On the collected sample ANALYZE is currently computing some + statistical informations, such as the percentage of NULL values, + the average width of a row, the number of distinct values etc. + It also stores the most common values and their frequencies. + The number of these values depends on the value of the + parameter. + + + + computing inherited statistics + + On the collected sample ANALYZE is currently computing some + statistical informations of child tables, such as the percentage of NULL values, + the average width of a row, the number of distinct values etc. + It also stores the most common values and their frequencies. + The number of these values depends on the value of the + parameter. + + + + +
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index d357c8b..a74ea45 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -909,6 +909,21 @@ CREATE VIEW pg_stat_progress_vacuum AS FROM pg_stat_get_progress_info('VACUUM') AS S LEFT JOIN pg_database D ON S.datid = D.oid; +CREATE VIEW pg_stat_progress_analyze AS + SELECT + S.pid AS pid, S.datid AS datid, D.datname AS datname, + S.relid AS relid, + CASE S.param1 WHEN 0 THEN 'initializing' + WHEN 1 THEN 'collecting sample rows' + WHEN 2 THEN 'collecting inherited sample rows' + WHEN 3 THEN 'computing statistics' + WHEN 4 THEN 'computing inherited statistics' + END AS phase, + S.param2 AS num_target_sample_rows, + S.param3 AS num_rows_sampled + FROM pg_stat_get_progress_info('ANALYZE') AS S + LEFT JOIN pg_database D ON S.datid = D.oid; + CREATE VIEW pg_user_mappings AS SELECT U.oid AS umid, diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 404acb2..09c3f11 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -33,6 +33,7 @@ #include "commands/dbcommands.h" #include "commands/tablecmds.h" #include "commands/vacuum.h" +#include "commands/progress.h" #include "executor/executor.h" #include "foreign/fdwapi.h" #include "miscadmin.h" @@ -76,6 +77,7 @@ typedef struct AnlIndexData /* Default statistics target (GUC parameter) */ int default_statistics_target = 100; + /* A few variables that don't seem worth passing around as parameters */ static MemoryContext anl_context = NULL; static BufferAccessStrategy vac_strategy; @@ -263,10 +265,16 @@ analyze_rel(Oid relid, RangeVar *relation, int options, MyPgXact->vacuumFlags |= PROC_IN_ANALYZE; LWLockRelease(ProcArrayLock); + /* Report that we are going to start analyzing onerel. */ + pgstat_progress_start_command(PROGRESS_COMMAND_ANALYZE, + RelationGetRelid(onerel)); + /* * Do the normal non-recursive ANALYZE. We can skip this for partitioned * tables, which don't contain any rows. */ + pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE, + PROGRESS_ANALYZE_PHASE_COLLECT_SAMPLE_ROWS); if (onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE) do_analyze_rel(onerel, options, params, va_cols, acquirefunc, relpages, false, in_outer_xact, elevel); @@ -275,8 +283,15 @@ analyze_rel(Oid relid, RangeVar *relation, int options, * If there are child tables, do recursive ANALYZE. */ if (onerel->rd_rel->relhassubclass) + { + pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE, + PROGRESS_ANALYZE_PHASE_COLLECT_INH_SAMPLE_ROWS); do_analyze_rel(onerel, options, params, va_cols, acquirefunc, relpages, true, in_outer_xact, elevel); + } + + /* We're done analyzing. */ + pgstat_progress_end_command(); /* * Close source relation now, but keep lock so that no one deletes it @@ -493,6 +508,11 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, /* * Acquire the sample rows */ + + /* Report the number of target sample rows */ + pgstat_progress_update_param(PROGRESS_ANALYZE_NUM_TARGET_SAMPLE_ROWS, + targrows); + rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple)); if (inh) numrows = acquire_inherited_sample_rows(onerel, elevel, @@ -509,6 +529,15 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, * responsible to make sure that whatever they store into the VacAttrStats * structure is allocated in anl_context. */ + + /* Report that statistics will now be computed. */ + if (inh) + pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE, + PROGRESS_ANALYZE_PHASE_COMPUTE_INH_STATS); + else + pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE, + PROGRESS_ANALYZE_PHASE_COMPUTE_STATS); + if (numrows > 0) { MemoryContext col_context, @@ -1180,6 +1209,9 @@ acquire_sample_rows(Relation onerel, int elevel, } samplerows += 1; + + /* Report number of rows sampled so far */ + pgstat_progress_update_param(PROGRESS_ANALYZE_NUM_ROWS_SAMPLED, numrows); } } diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index dd2b924..d389907 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -466,6 +466,8 @@ pg_stat_get_progress_info(PG_FUNCTION_ARGS) /* Translate command name into command type code. */ if (pg_strcasecmp(cmd, "VACUUM") == 0) cmdtype = PROGRESS_COMMAND_VACUUM; + else if (pg_strcasecmp(cmd, "ANALYZE") == 0) + cmdtype = PROGRESS_COMMAND_ANALYZE; else ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), diff --git a/src/include/commands/progress.h b/src/include/commands/progress.h index 9472ecc..7668c44 100644 --- a/src/include/commands/progress.h +++ b/src/include/commands/progress.h @@ -34,4 +34,15 @@ #define PROGRESS_VACUUM_PHASE_TRUNCATE 5 #define PROGRESS_VACUUM_PHASE_FINAL_CLEANUP 6 +/* Progress parameters for analyze */ +#define PROGRESS_ANALYZE_PHASE 0 +#define PROGRESS_ANALYZE_NUM_TARGET_SAMPLE_ROWS 1 +#define PROGRESS_ANALYZE_NUM_ROWS_SAMPLED 2 + +/* Phases of analyze (as advertised via PROGRESS_ANALYZE_PHASE) */ +#define PROGRESS_ANALYZE_PHASE_COLLECT_SAMPLE_ROWS 1 +#define PROGRESS_ANALYZE_PHASE_COLLECT_INH_SAMPLE_ROWS 2 +#define PROGRESS_ANALYZE_PHASE_COMPUTE_STATS 3 +#define PROGRESS_ANALYZE_PHASE_COMPUTE_INH_STATS 4 + #endif diff --git a/src/include/pgstat.h b/src/include/pgstat.h index e29397f..2141593 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -911,7 +911,8 @@ typedef enum typedef enum ProgressCommandType { PROGRESS_COMMAND_INVALID, - PROGRESS_COMMAND_VACUUM + PROGRESS_COMMAND_VACUUM, + PROGRESS_COMMAND_ANALYZE } ProgressCommandType; #define PGSTAT_NUM_PROGRESS_PARAM 10 diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index d706f42..3400527 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -1819,6 +1819,22 @@ pg_stat_database_conflicts| SELECT d.oid AS datid, pg_stat_get_db_conflict_bufferpin(d.oid) AS confl_bufferpin, pg_stat_get_db_conflict_startup_deadlock(d.oid) AS confl_deadlock FROM pg_database d; +pg_stat_progress_analyze| SELECT s.pid, + s.datid, + d.datname, + s.relid, + CASE s.param1 + WHEN 0 THEN 'initializing'::text + WHEN 1 THEN 'collecting sample rows'::text + WHEN 2 THEN 'collecting inherited sample rows'::text + WHEN 3 THEN 'computing statistics'::text + WHEN 4 THEN 'computing inherited statistics'::text + ELSE NULL::text + END AS phase, + s.param2 AS num_target_sample_rows, + s.param3 AS num_rows_sampled + FROM (pg_stat_get_progress_info('ANALYZE'::text) s(pid, datid, relid, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10) + LEFT JOIN pg_database d ON ((s.datid = d.oid))); pg_stat_progress_vacuum| SELECT s.pid, s.datid, d.datname,