Skip to content

Commit

Permalink
Fix ordered append for partially compressed chunks
Browse files Browse the repository at this point in the history
In the exclusive presence of partially compressed chunks, this
optimization was not applied because no pathkeys were supplied.
Additionally, this patch makes sure that if applicable, the
`enable_decompression_sorted_merge` optimization is chosen for
the path, since it is more beneficial due to the ability to
push down the sort below DecompressChunk.
  • Loading branch information
konskov committed Aug 23, 2023
1 parent d1246ee commit 373c556
Show file tree
Hide file tree
Showing 10 changed files with 4,515 additions and 53 deletions.
51 changes: 24 additions & 27 deletions tsl/src/nodes/decompress_chunk/decompress_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,34 +772,31 @@ ts_decompress_chunk_generate_paths(PlannerInfo *root, RelOptInfo *chunk_rel, Hyp
*/
if (batch_merge_path != NULL)
{
Path *merge_append_path =
(Path *) create_merge_append_path_compat(root,
chunk_rel,
list_make2(batch_merge_path,
uncompressed_path),
root->query_pathkeys,
req_outer,
NIL);

add_path(chunk_rel, merge_append_path);
path = (Path *) create_merge_append_path_compat(root,
chunk_rel,
list_make2(batch_merge_path,
uncompressed_path),
root->query_pathkeys,
req_outer,
NIL);
}

/*
* Ideally, we would like for this to be a MergeAppend path.
* However, accumulate_append_subpath will cut out MergeAppend
* and directly add its children, so we have to combine the children
* into a MergeAppend node later, at the chunk append level.
*/
path = (Path *) create_append_path_compat(root,
chunk_rel,
list_make2(path, uncompressed_path),
NIL /* partial paths */,
NIL /* pathkeys */,
req_outer,
0,
false,
false,
path->rows + uncompressed_path->rows);
else
/*
* Ideally, we would like for this to be a MergeAppend path.
* However, accumulate_append_subpath will cut out MergeAppend
* and directly add its children, so we have to combine the children
* into a MergeAppend node later, at the chunk append level.
*/
path = (Path *) create_append_path_compat(root,
chunk_rel,
list_make2(path, uncompressed_path),
NIL /* partial paths */,
root->query_pathkeys /* pathkeys */,
req_outer,
0,
false,
false,
path->rows + uncompressed_path->rows);
}

/* this has to go after the path is copied for the ordered path since path can get freed in
Expand Down
37 changes: 23 additions & 14 deletions tsl/test/expected/compression.out
Original file line number Diff line number Diff line change
Expand Up @@ -1972,23 +1972,32 @@ SELECT * FROM ht_metrics_partially_compressed ORDER BY time DESC, device LIMIT 1
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
-> Gather Merge
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
Workers Planned: 2
Output: ht_metrics_partially_compressed."time", ht_metrics_partially_compressed.device, ht_metrics_partially_compressed.value
-> Custom Scan (ChunkAppend) on public.ht_metrics_partially_compressed
Output: ht_metrics_partially_compressed."time", ht_metrics_partially_compressed.device, ht_metrics_partially_compressed.value
Order: ht_metrics_partially_compressed."time" DESC, ht_metrics_partially_compressed.device
Startup Exclusion: false
Runtime Exclusion: false
-> Sort
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
Output: _hyper_41_76_chunk."time", _hyper_41_76_chunk.device, _hyper_41_76_chunk.value
Sort Key: _hyper_41_76_chunk."time" DESC, _hyper_41_76_chunk.device
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_41_76_chunk
Output: _hyper_41_76_chunk."time", _hyper_41_76_chunk.device, _hyper_41_76_chunk.value
-> Seq Scan on _timescaledb_internal.compress_hyper_42_78_chunk
Output: compress_hyper_42_78_chunk."time", compress_hyper_42_78_chunk.device, compress_hyper_42_78_chunk.value, compress_hyper_42_78_chunk._ts_meta_count, compress_hyper_42_78_chunk._ts_meta_sequence_num, compress_hyper_42_78_chunk._ts_meta_min_1, compress_hyper_42_78_chunk._ts_meta_max_1
-> Merge Append
Sort Key: _hyper_41_75_chunk."time" DESC, _hyper_41_75_chunk.device
-> Parallel Append
-> Parallel Seq Scan on _timescaledb_internal._hyper_41_75_chunk
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_41_76_chunk
Output: _hyper_41_76_chunk."time", _hyper_41_76_chunk.device, _hyper_41_76_chunk.value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_42_78_chunk
Output: compress_hyper_42_78_chunk."time", compress_hyper_42_78_chunk.device, compress_hyper_42_78_chunk.value, compress_hyper_42_78_chunk._ts_meta_count, compress_hyper_42_78_chunk._ts_meta_sequence_num, compress_hyper_42_78_chunk._ts_meta_min_1, compress_hyper_42_78_chunk._ts_meta_max_1
-> Sort
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
Sort Key: _hyper_41_75_chunk."time" DESC, _hyper_41_75_chunk.device
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_41_75_chunk
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_42_77_chunk
-> Seq Scan on _timescaledb_internal.compress_hyper_42_77_chunk
Output: compress_hyper_42_77_chunk."time", compress_hyper_42_77_chunk.device, compress_hyper_42_77_chunk.value, compress_hyper_42_77_chunk._ts_meta_count, compress_hyper_42_77_chunk._ts_meta_sequence_num, compress_hyper_42_77_chunk._ts_meta_min_1, compress_hyper_42_77_chunk._ts_meta_max_1
(19 rows)
-> Sort
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
Sort Key: _hyper_41_75_chunk."time" DESC, _hyper_41_75_chunk.device
-> Seq Scan on _timescaledb_internal._hyper_41_75_chunk
Output: _hyper_41_75_chunk."time", _hyper_41_75_chunk.device, _hyper_41_75_chunk.value
(28 rows)

19 changes: 11 additions & 8 deletions tsl/test/expected/compression_insert.out
Original file line number Diff line number Diff line change
Expand Up @@ -734,15 +734,15 @@ INSERT INTO trigger_test VALUES
( '2010-01-01',10,10, 'ten', 222);
NOTICE: Trigger t1_mod BEFORE INSERT ROW on _hyper_11_15_chunk: ("Sat Jan 01 00:00:00 2000 PST",1,11,eleven,111) <NULL>
NOTICE: Trigger t1_mod BEFORE INSERT ROW on _hyper_11_18_chunk: ("Fri Jan 01 00:00:00 2010 PST",10,10,ten,222) <NULL>
SELECT * FROM trigger_test ORDER BY 1 ,2, 5;
SELECT * FROM trigger_test ORDER BY 1 ,2, 3, 5;
time | device | value | addcolv | addcoli
------------------------------+--------+-------+---------+---------
Fri Dec 31 22:00:00 1999 PST | 1 | 0 | |
Fri Dec 31 22:00:00 1999 PST | 1 | 0 | |
Sat Jan 01 00:00:00 2000 PST | 1 | 111 | eleven | 111
Sat Jan 01 00:00:00 2000 PST | 1 | 1 | |
Sat Jan 01 00:00:00 2000 PST | 1 | 0 | |
Sat Jan 01 00:00:00 2000 PST | 1 | 0 | |
Sat Jan 01 00:00:00 2000 PST | 1 | 1 | |
Sat Jan 01 00:00:00 2000 PST | 1 | 111 | eleven | 111
Fri Jan 01 00:00:00 2010 PST | 10 | 10 | ten | 222
Fri Jan 01 00:00:00 2010 PST | 10 | 110 | ten | 222
(8 rows)
Expand Down Expand Up @@ -802,15 +802,18 @@ INSERT INTO test_ordering SELECT 1;
:PREFIX SELECT * FROM test_ordering ORDER BY 1;
QUERY PLAN
-------------------------------------------------------------------------------------
Sort
Sort Key: _hyper_13_20_chunk."time"
-> Append
Custom Scan (ChunkAppend) on test_ordering
Order: test_ordering."time"
-> Merge Append
Sort Key: _hyper_13_20_chunk."time"
-> Custom Scan (DecompressChunk) on _hyper_13_20_chunk
-> Sort
Sort Key: compress_hyper_14_21_chunk._ts_meta_sequence_num DESC
-> Seq Scan on compress_hyper_14_21_chunk
-> Seq Scan on _hyper_13_20_chunk
(8 rows)
-> Sort
Sort Key: _hyper_13_20_chunk."time"
-> Seq Scan on _hyper_13_20_chunk
(11 rows)

INSERT INTO test_ordering VALUES (105),(104),(103);
-- should be ordered append
Expand Down
Loading

0 comments on commit 373c556

Please sign in to comment.