diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 9eaf43a..f03a657 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 . + @@ -2824,8 +2831,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 + certain commands during command execution. Currently, the command + which supports progress reporting is VACUUM and ANALYZE. This may be expanded in the future. @@ -3016,7 +3023,116 @@ 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. The sample it reads is taken randomly. + Its size depends on the default_statistics_target parameter value. + + + + num_rows_sampled + bigint + + Total number of rows sampled so far. + + + + +
+ + ANALYZE phases + + + + Phase + Description + + + + + + initializing + + ANALYZE is preparing to collect sample rows. + + + + collecting sample rows + + ANALYZE is currently collecting the sample rows. + The sample it reads is taken randomly. Its size depends on + the default_statistics_target parameter value. + + + + computing stats + + ANALYZE is currently computing heap statistics + and index statistics, 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 + default_statistics_target parameter. + + + + +
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index b6552da..63451ba 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -872,6 +872,19 @@ 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 'computing 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 b91df98..aa59a6e 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -31,6 +31,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" @@ -322,6 +323,11 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, Oid save_userid; int save_sec_context; int save_nestlevel; + const int initprog_index[] = { + PROGRESS_ANALYZE_PHASE, + PROGRESS_ANALYZE_NUM_TARGET_SAMPLE_ROWS + }; + int64 initprog_val[2]; if (inh) ereport(elevel, @@ -334,6 +340,8 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, get_namespace_name(RelationGetNamespace(onerel)), RelationGetRelationName(onerel)))); + pgstat_progress_start_command(PROGRESS_COMMAND_ANALYZE, RelationGetRelid(onerel)); + /* * Set up a working context so that we can easily free whatever junk gets * created. @@ -487,6 +495,12 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, /* * Acquire the sample rows */ + + /* Report collecting sample rows phase and number of target sample rows */ + initprog_val[0] = PROGRESS_ANALYZE_PHASE_COLLECT_SAMPLE_ROWS; + initprog_val[1] = targrows; + pgstat_progress_update_multi_param(2, initprog_index, initprog_val); + rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple)); if (inh) numrows = acquire_inherited_sample_rows(onerel, elevel, @@ -503,6 +517,10 @@ 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 compute statistics phase */ + pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE, + PROGRESS_ANALYZE_PHASE_COMPUTE_STATS); if (numrows > 0) { MemoryContext col_context, @@ -672,6 +690,8 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, MemoryContextSwitchTo(caller_context); MemoryContextDelete(anl_context); anl_context = NULL; + + pgstat_progress_end_command(); } /* @@ -1172,6 +1192,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 a987d0d..eb1dd63 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -465,6 +465,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..c32b4eb 100644 --- a/src/include/commands/progress.h +++ b/src/include/commands/progress.h @@ -34,4 +34,13 @@ #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_COMPUTE_STATS 2 + #endif diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 60c78d1..2b51d76 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -812,7 +812,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 bd13ae6..8a8caeb 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -1817,6 +1817,20 @@ pg_stat_progress_vacuum| SELECT s.pid, s.param7 AS num_dead_tuples FROM (pg_stat_get_progress_info('VACUUM'::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_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 'computing 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_replication| SELECT s.pid, s.usesysid, u.rolname AS usename,