Skip to content

Commit

Permalink
Create compressed chunks at insert time
Browse files Browse the repository at this point in the history
To avoid lock contention during compression operation, we are moving
compressed chunk creation together with uncompressed chunk creation
during insert time. Now compressed chunks live while the uncompressed
chunk is alive, we don't remove them during decompression but rather
truncate them. This moves lock contention over compressed hypertable
to coincide with lock contention over uncompressed hypertable.
  • Loading branch information
antekresic committed Sep 4, 2023
1 parent c6a9308 commit e35d68a
Show file tree
Hide file tree
Showing 105 changed files with 8,584 additions and 8,186 deletions.
1 change: 1 addition & 0 deletions .unreleased/PR_5849
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implements: #5849 Create compressed chunks at insert time
4 changes: 4 additions & 0 deletions sql/maintenance_utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ CREATE OR REPLACE FUNCTION _timescaledb_functions.create_compressed_chunk(
numrows_post_compression BIGINT
) RETURNS REGCLASS AS '@MODULE_PATHNAME@', 'ts_create_compressed_chunk' LANGUAGE C STRICT VOLATILE;

CREATE OR REPLACE FUNCTION _timescaledb_functions.create_compressed_chunks_for_hypertable(
hypertable REGCLASS
) RETURNS VOID AS '@MODULE_PATHNAME@', 'ts_create_compressed_chunks_for_hypertable' LANGUAGE C STRICT VOLATILE;

CREATE OR REPLACE FUNCTION @[email protected]_chunk(
uncompressed_chunk REGCLASS,
if_not_compressed BOOLEAN = false
Expand Down
6 changes: 3 additions & 3 deletions sql/size_utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,10 @@ SELECT
srcht.table_name AS hypertable_name,
srcch.schema_name AS chunk_schema,
srcch.table_name AS chunk_name,
CASE WHEN srcch.compressed_chunk_id IS NULL THEN
'Uncompressed'::text
ELSE
CASE WHEN srcch.status & 1 > 0 THEN
'Compressed'::text
ELSE
'Uncompressed'::text
END AS compression_status,
map.uncompressed_heap_size,
map.uncompressed_index_size,
Expand Down
3 changes: 3 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,6 @@ ALTER FUNCTION _timescaledb_internal.finalize_agg_ffunc(internal,text,name,name,
ALTER FUNCTION _timescaledb_internal.finalize_agg_sfunc(internal,text,name,name,name[],bytea,anyelement) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.partialize_agg(anyelement) SET SCHEMA _timescaledb_functions;

CREATE OR REPLACE FUNCTION _timescaledb_functions.create_compressed_chunks_for_hypertable(
hypertable REGCLASS
) RETURNS VOID AS '@MODULE_PATHNAME@', 'ts_create_compressed_chunks_for_hypertable' LANGUAGE C STRICT VOLATILE;
14 changes: 14 additions & 0 deletions sql/updates/post-update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,17 @@ BEGIN
END;
$$;

-- create compressed chunks for all uncompressed chunks for all hypertables that have compression enabled
DO $$
DECLARE
schema name;
ht_name name;
BEGIN
FOR schema, ht_name IN
SELECT schema_name, table_name FROM _timescaledb_catalog.hypertable
WHERE compressed_hypertable_id IS NOT NULL
LOOP
PERFORM _timescaledb_functions.create_compressed_chunks_for_hypertable(schema || '.' || ht_name);
END LOOP;
END;
$$;
24 changes: 24 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,27 @@ ALTER FUNCTION _timescaledb_functions.finalize_agg_sfunc(internal,text,name,name
ALTER FUNCTION _timescaledb_functions.partialize_agg(anyelement) SET SCHEMA _timescaledb_internal;
ALTER AGGREGATE _timescaledb_functions.finalize_agg(text,name,name,name[][],bytea,anyelement) SET SCHEMA _timescaledb_internal;

-- remove compressed chunks for all uncompressed chunks for all hypertables that have compression enabled
DO $$
DECLARE
schema name;
chunk_name name;
BEGIN
FOR schema, chunk_name IN
SELECT c.schema_name, c.table_name FROM _timescaledb_catalog.hypertable h
INNER JOIN _timescaledb_catalog.chunk c ON c.hypertable_id = h.id
WHERE h.compressed_hypertable_id IS NOT NULL
AND c.status & 1 = 0
LOOP
EXECUTE format('DROP TABLE %s.%s', schema, chunk_name);
END LOOP;

UPDATE _timescaledb_catalog.chunk SET compressed_chunk_id = NULL
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON c.hypertable_id = h.id
WHERE h.compressed_hypertable_id IS NOT NULL
AND c.status & 1 = 0;
END;
$$;

DROP FUNCTION IF EXISTS _timescaledb_functions.create_compressed_chunks_for_hypertable(REGCLASS);
Loading

0 comments on commit e35d68a

Please sign in to comment.