Skip to content

Commit

Permalink
Fix index att number calculation
Browse files Browse the repository at this point in the history
Attribute offset was used by mistake where attribute number was
needed causing wrong values to be fetched when scanning
compressed chunk index.
  • Loading branch information
antekresic authored and svenklemm committed Dec 15, 2022
1 parent b40b1b8 commit 97c05fe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
4 changes: 2 additions & 2 deletions tsl/src/compression/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ index_scan_sequence_number(Relation table_rel, Oid index_oid, ScanKeyData *scank
if (index_getnext_tid(index_scan, BackwardScanDirection))
{
result = index_getattr(index_scan->xs_itup,
index_scan->xs_itupdesc->natts -
1, /* Last attribute of the index is sequence number. */
index_scan->xs_itupdesc
->natts, /* Last attribute of the index is sequence number. */
index_scan->xs_itupdesc,
&is_null);
if (is_null)
Expand Down
52 changes: 45 additions & 7 deletions tsl/test/expected/compression_merge.out
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,28 @@ NOTICE: adding not-null constraint to column "Time"
INSERT INTO test5 SELECT t, 1, gen_rand_minstd() FROM generate_series('2018-03-02 1:00'::TIMESTAMPTZ, '2018-03-03 0:59', '1 minute') t;
-- Compression is set to merge those 24 chunks into 1 24 hour chunk
ALTER TABLE test5 set (timescaledb.compress, timescaledb.compress_segmentby='i', timescaledb.compress_orderby='"Time"', timescaledb.compress_chunk_time_interval='24 hours');
SELECT compress_chunk(i) FROM show_chunks('test5') i LIMIT 1;
SELECT
$$
SELECT * FROM test5 ORDER BY i, "Time"
$$ AS "QUERY" \gset
SELECT compress_chunk(i) FROM show_chunks('test5') i LIMIT 4;
compress_chunk
------------------------------------------
_timescaledb_internal._hyper_9_163_chunk
(1 row)
_timescaledb_internal._hyper_9_163_chunk
_timescaledb_internal._hyper_9_163_chunk
_timescaledb_internal._hyper_9_163_chunk
(4 rows)

-- Make sure sequence numbers are correctly fetched from index.
SELECT _ts_meta_sequence_num FROM _timescaledb_internal.compress_hyper_10_187_chunk where i = 1;
_ts_meta_sequence_num
-----------------------
10
20
30
40
(4 rows)

SELECT schemaname || '.' || indexname AS "INDEXNAME"
FROM pg_indexes i
Expand All @@ -267,10 +284,31 @@ LIMIT 1 \gset
DROP INDEX :INDEXNAME;
-- We dropped the index from compressed chunk thats needed to determine sequence numbers
-- during merge, merging will fallback to doing heap scans and work just fine.
SELECT
$$
SELECT * FROM test5 ORDER BY i, "Time"
$$ AS "QUERY" \gset
SELECT compress_chunk(i, true) FROM show_chunks('test5') i LIMIT 5;
NOTICE: chunk "_hyper_9_163_chunk" is already compressed
compress_chunk
------------------------------------------
_timescaledb_internal._hyper_9_163_chunk
_timescaledb_internal._hyper_9_163_chunk
_timescaledb_internal._hyper_9_163_chunk
_timescaledb_internal._hyper_9_163_chunk
_timescaledb_internal._hyper_9_163_chunk
(5 rows)

-- Make sure sequence numbers are correctly fetched from heap.
SELECT _ts_meta_sequence_num FROM _timescaledb_internal.compress_hyper_10_187_chunk where i = 1;
_ts_meta_sequence_num
-----------------------
10
20
30
40
50
60
70
80
(8 rows)

SELECT 'test5' AS "HYPERTABLE_NAME" \gset
\ir include/compression_test_merge.sql
-- This file and its contents are licensed under the Timescale License.
Expand All @@ -280,7 +318,7 @@ SELECT 'test5' AS "HYPERTABLE_NAME" \gset
psql:include/compression_test_merge.sql:12: NOTICE: chunk "_hyper_9_163_chunk" is already compressed
count_compressed
------------------
24
17
(1 row)

?column? | count
Expand Down
19 changes: 14 additions & 5 deletions tsl/test/sql/compression_merge.sql
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,31 @@ INSERT INTO test5 SELECT t, 1, gen_rand_minstd() FROM generate_series('2018-03-0
-- Compression is set to merge those 24 chunks into 1 24 hour chunk
ALTER TABLE test5 set (timescaledb.compress, timescaledb.compress_segmentby='i', timescaledb.compress_orderby='"Time"', timescaledb.compress_chunk_time_interval='24 hours');

SELECT compress_chunk(i) FROM show_chunks('test5') i LIMIT 1;
SELECT
$$
SELECT * FROM test5 ORDER BY i, "Time"
$$ AS "QUERY" \gset

SELECT compress_chunk(i) FROM show_chunks('test5') i LIMIT 4;

-- Make sure sequence numbers are correctly fetched from index.
SELECT _ts_meta_sequence_num FROM _timescaledb_internal.compress_hyper_10_187_chunk where i = 1;

SELECT schemaname || '.' || indexname AS "INDEXNAME"
FROM pg_indexes i
INNER JOIN _timescaledb_catalog.chunk cc ON i.schemaname = cc.schema_name and i.tablename = cc.table_name
INNER JOIN _timescaledb_catalog.chunk c ON (cc.id = c.compressed_chunk_id)
LIMIT 1 \gset


DROP INDEX :INDEXNAME;

-- We dropped the index from compressed chunk thats needed to determine sequence numbers
-- during merge, merging will fallback to doing heap scans and work just fine.
SELECT
$$
SELECT * FROM test5 ORDER BY i, "Time"
$$ AS "QUERY" \gset
SELECT compress_chunk(i, true) FROM show_chunks('test5') i LIMIT 5;

-- Make sure sequence numbers are correctly fetched from heap.
SELECT _ts_meta_sequence_num FROM _timescaledb_internal.compress_hyper_10_187_chunk where i = 1;

SELECT 'test5' AS "HYPERTABLE_NAME" \gset
\ir include/compression_test_merge.sql
Expand Down

0 comments on commit 97c05fe

Please sign in to comment.