Skip to content

new request #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions doc/src/sgml/monitoring.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,13 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
yet included in <structname>pg_stat_user_functions</>).</entry>
</row>

<row>
<entry><structname>pg_stat_vacuum_progress</><indexterm><primary>pg_stat_vacuum_progress</primary></indexterm></entry>
<entry>One row for each backend (including autovacuum worker processes) running
<command>VACUUM</>, showing current progress in terms of heap pages it
has finished processing. Backends running <command>VACUUM FULL</> are
not included.</entry>
</row>
</tbody>
</tgroup>
</table>
Expand Down Expand Up @@ -1822,6 +1829,74 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
controls exactly which functions are tracked.
</para>

<table id="pg-stat-vacuum-progress" xreflabel="pg_stat_vacuum_progress">
<title><structname>pg_stat_vacuum_progress</structname> View</title>
<tgroup cols="3">
<thead>
<row>
<entry>Column</entry>
<entry>Type</entry>
<entry>Description</entry>
</row>
</thead>

<tbody>
<row>
<entry><structfield>pid</></entry>
<entry><type>integer</></entry>
<entry>Process ID of backend</entry>
</row>
<row>
<entry><structfield>relid</></entry>
<entry><type>oid</></entry>
<entry>OID of a table</entry>
</row>
<row>
<entry><structfield>phase</></entry>
<entry><type>name</></entry>
<entry>Phase of vacuum</entry>
</row>
<row>
<entry><structfield>total_heap_pages</></entry>
<entry><type>integer</></entry>
<entry>Total number of pages in this table</entry>
</row>
<row>
<entry><structfield>current_heap_blkno</></entry>
<entry><type>integer</></entry>
<entry>Current heap block number</entry>
</row>
<row>
<entry><structfield>total_index_pages</></entry>
<entry><type>integer</></entry>
<entry>Total number of index pages in this table</entry>
</row>
<row>
<entry><structfield>scanned_index_pages</></entry>
<entry><type>integer</></entry>
<entry>Number of index pages processed</entry>
</row>
<row>
<entry><structfield>index_scan_count</></entry>
<entry><type>integer</></entry>
<entry>Number of times index scan has been performed so far</entry>
</row>
<row>
<entry><structfield>percent_complete</></entry>
<entry><type>double precision</></entry>
<entry>Amount of work done in percent</entry>
</row>
</tbody>
</tgroup>
</table>

<para>
The <structname>pg_stat_vacuum_progress</structname> view will contain
one row for each backend (including autovacuum worker processes), showing
progress of <command>VACUUM</> running in it. Note that the backends
running <command>VACUUM FULL</> are not shown.
</para>

</sect2>

<sect2 id="monitoring-stats-functions">
Expand Down
13 changes: 13 additions & 0 deletions src/backend/catalog/system_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,19 @@ CREATE VIEW pg_stat_activity AS
WHERE S.datid = D.oid AND
S.usesysid = U.oid;

CREATE VIEW pg_stat_vacuum_progress AS
SELECT
S.pid,
S.relid,
S.phase,
S.total_heap_blks,
S.current_heap_blkno,
S.total_index_pages,
S.scanned_index_pages,
S.index_scan_count,
S.percent_complete
FROM pg_stat_get_command_progress() AS S;

CREATE VIEW pg_stat_replication AS
SELECT
S.pid,
Expand Down
5 changes: 5 additions & 0 deletions src/backend/commands/vacuum.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ vacuum(int options, RangeVar *relation, Oid relid, VacuumParams *params,

if (options & VACOPT_VACUUM)
{
if (!(options & VACOPT_FULL))
pgstat_report_progress_set_command(COMMAND_LAZY_VACUUM);

if (!vacuum_rel(relid, relation, options, params))
continue;
}
Expand Down Expand Up @@ -325,6 +328,7 @@ vacuum(int options, RangeVar *relation, Oid relid, VacuumParams *params,
{
in_vacuum = false;
VacuumCostActive = false;
pgstat_reset_local_progress();
PG_RE_THROW();
}
PG_END_TRY();
Expand Down Expand Up @@ -355,6 +359,7 @@ vacuum(int options, RangeVar *relation, Oid relid, VacuumParams *params,
vac_update_datfrozenxid();
}

pgstat_reset_local_progress();
/*
* Clean up working storage --- note we must do this after
* StartTransactionCommand, else we might be trying to delete the active
Expand Down
74 changes: 69 additions & 5 deletions src/backend/commands/vacuumlazy.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*-------------------------------------------------------------------------
*
* vacuumlazy.c
* vacuumlazy.c hii
* Concurrent ("lazy") vacuuming.
*
*
Expand Down Expand Up @@ -433,7 +433,11 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
Relation *Irel, int nindexes, bool scan_all)
{
BlockNumber nblocks,
blkno;
blkno,
total_heap_blks,
current_heap_blkno = 0,
total_index_pages = 0,
scanned_index_pages = 0;
HeapTupleData tuple;
char *relname;
BlockNumber empty_pages,
Expand All @@ -450,22 +454,33 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
bool skipping_all_visible_blocks;
xl_heap_freeze_tuple *frozen;
StringInfoData buf;
char progress_message[N_PROGRESS_PARAM][PROGRESS_MESSAGE_LENGTH];
const char *phase1="Scanning Heap";
const char *phase2="Vacuuming Index and Heap";
Oid relid;

pg_rusage_init(&ru0);

relname = RelationGetRelationName(onerel);
relid = RelationGetRelid(onerel);
ereport(elevel,
(errmsg("vacuuming \"%s.%s\"",
get_namespace_name(RelationGetNamespace(onerel)),
relname)));
/* Report relid of the relation*/
pgstat_report_progress_set_command_target(relid);

empty_pages = vacuumed_pages = 0;
num_tuples = tups_vacuumed = nkeep = nunused = 0;

indstats = (IndexBulkDeleteResult **)
palloc0(nindexes * sizeof(IndexBulkDeleteResult *));

nblocks = RelationGetNumberOfBlocks(onerel);
total_heap_blks = nblocks = RelationGetNumberOfBlocks(onerel);

for (i = 0; i < nindexes; i++)
total_index_pages += RelationGetNumberOfBlocks(Irel[i]);

vacrelstats->rel_pages = nblocks;
vacrelstats->scanned_pages = 0;
vacrelstats->nonempty_pages = 0;
Expand All @@ -474,6 +489,10 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
lazy_space_alloc(vacrelstats, nblocks);
frozen = palloc(sizeof(xl_heap_freeze_tuple) * MaxHeapTuplesPerPage);

/* Report count of total heap blocks and total index pages of a relation*/
pgstat_report_progress_update_counter(0, total_heap_blks);
pgstat_report_progress_update_counter(2, total_index_pages);

/*
* We want to skip pages that don't require vacuuming according to the
* visibility map, but only when we can skip at least SKIP_PAGES_THRESHOLD
Expand Down Expand Up @@ -523,10 +542,16 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
vacuum_delay_point();
}
if (next_not_all_visible_block >= SKIP_PAGES_THRESHOLD)
{
skipping_all_visible_blocks = true;
/*if(!scan_all)
current_heap_blkno = current_heap_blkno + next_not_all_visible_block;*/
}
else
skipping_all_visible_blocks = false;

snprintf(progress_message[0], PROGRESS_MESSAGE_LENGTH, "%s", phase1);
pgstat_report_progress_update_message(0, progress_message);
for (blkno = 0; blkno < nblocks; blkno++)
{
Buffer buf;
Expand Down Expand Up @@ -566,7 +591,11 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
* following blocks.
*/
if (next_not_all_visible_block - blkno > SKIP_PAGES_THRESHOLD)
{
skipping_all_visible_blocks = true;
/*if(!scan_all)
current_heap_blkno = current_heap_blkno + next_not_all_visible_block;*/
}
else
skipping_all_visible_blocks = false;
all_visible_according_to_vm = false;
Expand Down Expand Up @@ -603,11 +632,19 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
/* Log cleanup info before we touch indexes */
vacuum_log_cleanup_info(onerel, vacrelstats);

snprintf(progress_message[0], PROGRESS_MESSAGE_LENGTH, "%s", phase2);
pgstat_report_progress_update_message(0, progress_message);

/* Remove index entries */
for (i = 0; i < nindexes; i++)
{
lazy_vacuum_index(Irel[i],
&indstats[i],
vacrelstats);
scanned_index_pages += RelationGetNumberOfBlocks(Irel[i]);
/* Update scanned index pages of a relation*/
pgstat_report_progress_update_counter(3, scanned_index_pages);
}
/* Remove tuples from heap */
lazy_vacuum_heap(onerel, vacrelstats);

Expand All @@ -617,8 +654,13 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
* valid.
*/
vacrelstats->num_dead_tuples = 0;
scanned_index_pages = 0;
vacrelstats->num_index_scans++;

pgstat_report_progress_update_counter(4, vacrelstats->num_index_scans);
}
snprintf(progress_message[0], PROGRESS_MESSAGE_LENGTH, "%s", phase1);
pgstat_report_progress_update_message(0, progress_message);

/*
* Pin the visibility map page in case we need to mark the page
Expand All @@ -645,7 +687,10 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
if (!scan_all && !FORCE_CHECK_PAGE())
{
ReleaseBuffer(buf);
current_heap_blkno++;
vacrelstats->pinskipped_pages++;
/* Report current heap block number */
pgstat_report_progress_update_counter(1, current_heap_blkno);
continue;
}

Expand All @@ -670,9 +715,13 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
{
UnlockReleaseBuffer(buf);
vacrelstats->scanned_pages++;
current_heap_blkno++;
vacrelstats->pinskipped_pages++;
if (hastup)
vacrelstats->nonempty_pages = blkno + 1;

/* Report current heap block number*/
pgstat_report_progress_update_counter(1, current_heap_blkno);
continue;
}
if (!scan_all)
Expand All @@ -693,6 +742,9 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
}

vacrelstats->scanned_pages++;
current_heap_blkno++;
/* Report current heap block number */
pgstat_report_progress_update_counter(1, current_heap_blkno);

page = BufferGetPage(buf);

Expand Down Expand Up @@ -1089,8 +1141,11 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
*/
if (vacrelstats->num_dead_tuples == prev_dead_count)
RecordPageWithFreeSpace(onerel, blkno, freespace);
}

if (blkno == nblocks - 1 && vacrelstats->num_dead_tuples == 0 && nindexes != 0
&& vacrelstats->num_index_scans == 0)
total_index_pages = 0;
}
pfree(frozen);

/* save stats for use later */
Expand Down Expand Up @@ -1120,16 +1175,25 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
/* Log cleanup info before we touch indexes */
vacuum_log_cleanup_info(onerel, vacrelstats);

snprintf(progress_message[0], PROGRESS_MESSAGE_LENGTH, "%s", phase2);
pgstat_report_progress_update_message(0, progress_message);

/* Remove index entries */
for (i = 0; i < nindexes; i++)
{
lazy_vacuum_index(Irel[i],
&indstats[i],
vacrelstats);
scanned_index_pages += RelationGetNumberOfBlocks(Irel[i]);
/* Update the scanned index pages and number of index scan */
pgstat_report_progress_update_counter(3, scanned_index_pages);
pgstat_report_progress_update_counter(4, vacrelstats->num_index_scans + 1);
}
/* Remove tuples from heap */
lazy_vacuum_heap(onerel, vacrelstats);
vacrelstats->num_index_scans++;
scanned_index_pages = 0;
}

/* Do post-vacuum cleanup and statistics update for each index */
for (i = 0; i < nindexes; i++)
lazy_cleanup_index(Irel[i], indstats[i], vacrelstats);
Expand Down
Loading