Skip to content

Commit

Permalink
Stats improvement for Uncompressed Chunks
Browse files Browse the repository at this point in the history
During the compression autovacuum use to be disabled for uncompressed
chunk and enable after decompression. This leads to postgres
maintainence issue. Let's not disable autovacuum for uncompressed
chunk anymore. Let postgres take care of the stats in its natural way.

Fixes #309
  • Loading branch information
shhnwz committed Jan 2, 2023
1 parent 1eacb35 commit 0337af2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 68 deletions.
59 changes: 4 additions & 55 deletions tsl/src/compression/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,47 +240,6 @@ compresschunkcxt_init(CompressChunkCxt *cxt, Cache *hcache, Oid hypertable_relid
cxt->srcht_chunk = srcchunk;
}

static void
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)),
};
ts_alter_table_with_event_trigger(chunk_relid, NULL, list_make1(&at_cmd), false);
}

/* This function is intended to undo the disabling of autovacuum done when we compressed a chunk.
* Note that we do not cache the previous value for this (as we don't expect users to toggle this
* for individual chunks), so we use the hypertable's setting to determine whether to enable this on
* the decompressed chunk.
*/
static void
restore_autovacuum_on_decompress(Oid uncompressed_hypertable_relid, Oid uncompressed_chunk_relid)
{
Relation tablerel = table_open(uncompressed_hypertable_relid, AccessShareLock);
bool ht_autovac_enabled =
tablerel->rd_options ? ((StdRdOptions *) (tablerel)->rd_options)->autovacuum.enabled : true;

table_close(tablerel, AccessShareLock);
if (ht_autovac_enabled)
{
AlterTableCmd at_cmd = {
.type = T_AlterTableCmd,
.subtype = AT_SetRelOptions,
.def = (Node *) list_make1(
makeDefElem("autovacuum_enabled", (Node *) makeString("true"), -1)),
};

ts_alter_table_with_event_trigger(uncompressed_chunk_relid,
NULL,
list_make1(&at_cmd),
false);
}
}

static Chunk *
find_chunk_to_merge_into(Hypertable *ht, Chunk *current_chunk)
{
Expand Down Expand Up @@ -415,10 +374,7 @@ compress_chunk_impl(Oid hypertable_relid, Oid chunk_relid)
/* acquire locks on src and compress hypertable and src chunk */
LockRelationOid(cxt.srcht->main_table_relid, AccessShareLock);
LockRelationOid(cxt.compress_ht->main_table_relid, AccessShareLock);
LockRelationOid(cxt.srcht_chunk->table_id, ShareLock);

/* Disabling autovacuum on chunk which should be empty while in compressed state */
disable_autovacuum_on_chunk(chunk_relid);
LockRelationOid(cxt.srcht_chunk->table_id, ExclusiveLock);

/* acquire locks on catalog tables to keep till end of txn */
LockRelationOid(catalog_get_table_id(ts_catalog_get(), HYPERTABLE_COMPRESSION),
Expand Down Expand Up @@ -637,10 +593,6 @@ decompress_chunk_impl(Oid uncompressed_hypertable_relid, Oid uncompressed_chunk_
*/
LockRelationOid(compressed_chunk->table_id, AccessExclusiveLock);
ts_chunk_drop(compressed_chunk, DROP_RESTRICT, -1);

/* reenable autovacuum if necessary */
restore_autovacuum_on_decompress(uncompressed_hypertable_relid, uncompressed_chunk_relid);

ts_cache_release(hcache);
return true;
}
Expand Down Expand Up @@ -738,8 +690,7 @@ decompress_remote_chunk(FunctionCallInfo fcinfo, const Chunk *chunk, bool if_com
* chunk_relid - non-compressed chunk relid
* chunk_table - table containing compressed data
*/
Datum
tsl_create_compressed_chunk(PG_FUNCTION_ARGS)
Datum tsl_create_compressed_chunk(PG_FUNCTION_ARGS)
{
Oid chunk_relid = PG_GETARG_OID(0);
Oid chunk_table = PG_GETARG_OID(1);
Expand Down Expand Up @@ -806,8 +757,7 @@ tsl_create_compressed_chunk(PG_FUNCTION_ARGS)
PG_RETURN_OID(chunk_relid);
}

Datum
tsl_compress_chunk(PG_FUNCTION_ARGS)
Datum tsl_compress_chunk(PG_FUNCTION_ARGS)
{
Oid uncompressed_chunk_id = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
bool if_not_compressed = PG_ARGISNULL(1) ? false : PG_GETARG_BOOL(1);
Expand Down Expand Up @@ -836,8 +786,7 @@ tsl_compress_chunk(PG_FUNCTION_ARGS)
PG_RETURN_OID(uncompressed_chunk_id);
}

Datum
tsl_decompress_chunk(PG_FUNCTION_ARGS)
Datum tsl_decompress_chunk(PG_FUNCTION_ARGS)
{
Oid uncompressed_chunk_id = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
bool if_compressed = PG_ARGISNULL(1) ? false : PG_GETARG_BOOL(1);
Expand Down
18 changes: 9 additions & 9 deletions tsl/test/expected/compression.out
Original file line number Diff line number Diff line change
Expand Up @@ -1341,9 +1341,9 @@ SELECT relpages, reltuples FROM pg_class WHERE relname = :statchunk;

-- Verify that decompressing the chunk restores autoanalyze to the hypertable's setting
SELECT reloptions FROM pg_class WHERE relname = :statchunk;
reloptions
----------------------------
{autovacuum_enabled=false}
reloptions
------------

(1 row)

SELECT decompress_chunk(c) FROM show_chunks('stattest') c;
Expand All @@ -1353,9 +1353,9 @@ SELECT decompress_chunk(c) FROM show_chunks('stattest') c;
(1 row)

SELECT reloptions FROM pg_class WHERE relname = :statchunk;
reloptions
---------------------------
{autovacuum_enabled=true}
reloptions
------------

(1 row)

SELECT compress_chunk(c) FROM show_chunks('stattest') c;
Expand All @@ -1365,9 +1365,9 @@ SELECT compress_chunk(c) FROM show_chunks('stattest') c;
(1 row)

SELECT reloptions FROM pg_class WHERE relname = :statchunk;
reloptions
----------------------------
{autovacuum_enabled=false}
reloptions
------------

(1 row)

ALTER TABLE stattest SET (autovacuum_enabled = false);
Expand Down
8 changes: 4 additions & 4 deletions tsl/test/isolation/expected/compression_chunk_race.out
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ step s3_unlock_compression:
SELECT locktype, mode, granted, objid FROM pg_locks WHERE not granted AND (locktype = 'advisory' or relation::regclass::text LIKE '%chunk') ORDER BY relation, locktype, mode, granted;
SELECT debug_waitpoint_release('compress_chunk_impl_start');

locktype|mode |granted| objid
--------+---------+-------+----------
relation|ShareLock|f |
advisory|ShareLock|f |3379597659
locktype|mode |granted| objid
--------+-------------+-------+----------
relation|ExclusiveLock|f |
advisory|ShareLock |f |3379597659
(2 rows)

debug_waitpoint_release
Expand Down

0 comments on commit 0337af2

Please sign in to comment.