Skip to content

Commit

Permalink
Move ANALYZE after heap scan during compression
Browse files Browse the repository at this point in the history
Depending on the statistics target, running ANALYZE on a chunk before
compression can cause a lot of random IO operations for chunks that
are bigger than the number of pages ANALYZE needs to read. By moving
that operation after the heap is loaded into memory for sorting,
we increase the chance of hitting cache and reducing disk operations
necessary to execute compression jobs.
  • Loading branch information
antekresic committed Sep 27, 2022
1 parent 3e32cec commit c0620a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
20 changes: 3 additions & 17 deletions tsl/src/compression/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,28 +159,14 @@ compresschunkcxt_init(CompressChunkCxt *cxt, Cache *hcache, Oid hypertable_relid
}

static void
preserve_uncompressed_chunk_stats(Oid chunk_relid)
disable_autovacuum_on_chunk(Oid chunk_relid)
{
AlterTableCmd at_cmd = {
.type = T_AlterTableCmd,
.subtype = AT_SetRelOptions,
.def = (Node *) list_make1(
makeDefElem("autovacuum_enabled", (Node *) makeString("false"), -1)),
};
VacuumRelation vr = {
.type = T_VacuumRelation,
.relation = NULL,
.oid = chunk_relid,
.va_cols = NIL,
};
VacuumStmt vs = {
.type = T_VacuumStmt,
.rels = list_make1(&vr),
.is_vacuumcmd = false,
.options = NIL,
};

ExecVacuum(NULL, &vs, true);
ts_alter_table_with_event_trigger(chunk_relid, NULL, list_make1(&at_cmd), false);
}

Expand Down Expand Up @@ -234,8 +220,8 @@ compress_chunk_impl(Oid hypertable_relid, Oid chunk_relid)
LockRelationOid(cxt.compress_ht->main_table_relid, AccessShareLock);
LockRelationOid(cxt.srcht_chunk->table_id, ShareLock);

/* Perform an analyze on the chunk to get up-to-date stats before compressing */
preserve_uncompressed_chunk_stats(chunk_relid);
/* Disabling autovacuum on chunk which should be empty while in compressed state */
disable_autovacuum_on_chunk(chunk_relid);

/* acquire locks on catalog tables to keep till end of txn */
LockRelationOid(catalog_get_table_id(ts_catalog_get(), HYPERTABLE_COMPRESSION),
Expand Down
27 changes: 27 additions & 0 deletions tsl/src/compression/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <funcapi.h>
#include <libpq/pqformat.h>
#include <miscadmin.h>
#include <nodes/pg_list.h>
#include <storage/lmgr.h>
#include <storage/predicate.h>
#include <utils/builtins.h>
Expand Down Expand Up @@ -390,6 +391,7 @@ static void compress_chunk_populate_sort_info_for_column(Oid table,
const ColumnCompressionInfo *column,
AttrNumber *att_nums, Oid *sort_operator,
Oid *collation, bool *nulls_first);
static void preserve_uncompressed_chunk_stats(Oid chunk_relid);

static Tuplesortstate *
compress_chunk_sort_relation(Relation in_rel, int n_keys, const ColumnCompressionInfo **keys)
Expand Down Expand Up @@ -441,6 +443,12 @@ compress_chunk_sort_relation(Relation in_rel, int n_keys, const ColumnCompressio

heap_endscan(heapScan);

/* Perform an analyze on the chunk to get up-to-date stats before compressing.
* We do it at this point to reduce the amount of random IO operations
* necessary in case the statistic target is larger than the chunk heap size.
*/
preserve_uncompressed_chunk_stats(in_rel->rd_id);

ExecDropSingleTupleTableSlot(heap_tuple_slot);

tuplesort_performsort(tuplesortstate);
Expand Down Expand Up @@ -488,6 +496,25 @@ compress_chunk_populate_sort_info_for_column(Oid table, const ColumnCompressionI
ReleaseSysCache(tp);
}

static void
preserve_uncompressed_chunk_stats(Oid chunk_relid)
{
VacuumRelation vr = {
.type = T_VacuumRelation,
.relation = NULL,
.oid = chunk_relid,
.va_cols = NIL,
};
VacuumStmt vs = {
.type = T_VacuumStmt,
.rels = list_make1(&vr),
.is_vacuumcmd = false,
.options = NIL,
};

ExecVacuum(NULL, &vs, true);
}

/********************
** row_compressor **
********************/
Expand Down

0 comments on commit c0620a3

Please sign in to comment.