Skip to content

Commit

Permalink
Count tuples correctly during GiST VACUUM.
Browse files Browse the repository at this point in the history
  • Loading branch information
michail-nikolaev committed Feb 25, 2018
1 parent c4ba1be commit ff5171b
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/backend/access/gist/gistvacuum.c
Expand Up @@ -16,6 +16,7 @@

#include "access/genam.h"
#include "access/gist_private.h"
#include "access/htup_details.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
Expand All @@ -32,7 +33,9 @@ gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
BlockNumber npages,
blkno;
BlockNumber totFreePages;
bool needLock;
bool needLock,
shouldCount = false;
uint64 tuplesCount = 0;

/* No-op in ANALYZE ONLY mode */
if (info->analyze_only)
Expand All @@ -45,11 +48,15 @@ gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
/* use heap's tuple count */
stats->num_index_tuples = info->num_heap_tuples;
stats->estimated_count = info->estimated_count;

/*
* XXX the above is wrong if index is partial. Would it be OK to just
* return NULL, or is there work we must do below?
*/
/* unless it is estimate or index is partial */
if (rel->rd_indextuple == NULL)
RelationInitIndexAccessInfo(rel);
shouldCount = !heap_attisnull(rel->rd_indextuple, Anum_pg_index_indpred) ||
info->estimated_count;
}
else
{
shouldCount = stats->estimated_count;
}

/*
Expand Down Expand Up @@ -82,12 +89,27 @@ gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
totFreePages++;
RecordFreeIndexPage(rel, blkno);
}
else
{
/* Count tuples in leaf pages if needed */
if (shouldCount && GistPageIsLeaf(page))
{
tuplesCount += PageGetMaxOffsetNumber(page);
}
}
UnlockReleaseBuffer(buffer);
}

/* Finally, vacuum the FSM */
IndexFreeSpaceMapVacuum(info->index);

/* Update index tuples stat to counted over leaf pages if needed */
if (shouldCount)
{
stats->num_index_tuples = tuplesCount;
stats->estimated_count = false;
}

/* return statistics */
stats->pages_free = totFreePages;
if (needLock)
Expand Down

0 comments on commit ff5171b

Please sign in to comment.