Skip to content

Commit

Permalink
Add a GUC variable to enable/disable OSM for
Browse files Browse the repository at this point in the history
SELECTs
  • Loading branch information
zilder committed Dec 12, 2022
1 parent d927390 commit 358133f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/chunk_scan.c
Expand Up @@ -11,6 +11,7 @@

#include "debug_point.h"
#include "dimension_vector.h"
#include "guc.h"
#include "hypertable.h"
#include "hypercube.h"
#include "scan_iterator.h"
Expand Down
13 changes: 13 additions & 0 deletions src/guc.c
Expand Up @@ -73,6 +73,7 @@ bool ts_guc_enable_constraint_exclusion = true;
bool ts_guc_enable_qual_propagation = true;
bool ts_guc_enable_cagg_reorder_groupby = true;
bool ts_guc_enable_now_constify = true;
bool ts_guc_enable_osm_reads = true;
TSDLLEXPORT bool ts_guc_enable_transparent_decompression = true;
bool ts_guc_enable_per_data_node_queries = true;
bool ts_guc_enable_async_append = true;
Expand Down Expand Up @@ -292,6 +293,18 @@ _guc_init(void)
NULL,
NULL);

DefineCustomBoolVariable("timescaledb.enable_tiered_reads",
"Enable tiered data reads",
"Enable reading of tiered data by including a foreign table "
"representing the data in the object storage into the query plan",
&ts_guc_enable_osm_reads,
true,
PGC_USERSET,
0,
NULL,
NULL,
NULL);

DefineCustomIntVariable("timescaledb.max_insert_batch_size",
"The max number of tuples to batch before sending to a data node",
"When acting as a access node, TimescaleDB splits batches of "
Expand Down
1 change: 1 addition & 0 deletions src/guc.h
Expand Up @@ -25,6 +25,7 @@ extern bool ts_guc_enable_runtime_exclusion;
extern bool ts_guc_enable_constraint_exclusion;
extern bool ts_guc_enable_cagg_reorder_groupby;
extern bool ts_guc_enable_now_constify;
extern bool ts_guc_enable_osm_reads;
extern TSDLLEXPORT bool ts_guc_enable_transparent_decompression;
extern TSDLLEXPORT bool ts_guc_enable_per_data_node_queries;
extern TSDLLEXPORT bool ts_guc_enable_async_append;
Expand Down
36 changes: 29 additions & 7 deletions src/hypertable_restrict_info.c
Expand Up @@ -20,6 +20,7 @@
#include "dimension.h"
#include "dimension_slice.h"
#include "dimension_vector.h"
#include "guc.h"
#include "hypercube.h"
#include "partitioning.h"
#include "scan_iterator.h"
Expand Down Expand Up @@ -664,6 +665,18 @@ ts_hypertable_restrict_info_get_chunks(HypertableRestrictInfo *hri, Hypertable *
* No restrictions on hyperspace. Just enumerate all the chunks.
*/
chunk_ids = ts_chunk_get_chunk_ids_by_hypertable_id(ht->fd.id);

/*
* If the hypertable has an OSM chunk it would end up in the list
* as well. We need to remove it when OSM reads are disabled via GUC
* variable.
*/
if (!ts_guc_enable_osm_reads)
{
int32 osm_chunk_id = ts_chunk_get_osm_chunk_id(ht->fd.id);

chunk_ids = list_delete_int(chunk_ids, osm_chunk_id);
}
}
else
{
Expand All @@ -686,16 +699,25 @@ ts_hypertable_restrict_info_get_chunks(HypertableRestrictInfo *hri, Hypertable *
}

/*
* Always include the OSM chunk if we have one. It has some virtual
* dimension slices (at the moment, (+inf, +inf) slice for time, but it
* used to be different and might change again.) So sometimes it will
* match and sometimes it won't, so we have to check if it's already
* there not to add a duplicate.
* Always include the OSM chunk if we have one and OSM reads are
* enabled. It has some virtual dimension slices (at the moment,
* (+inf, +inf) slice for time, but it used to be different and might
* change again.) So sometimes it will match and sometimes it won't,
* so we have to check if it's already there not to add a duplicate.
* Similarly if OSM reads are disabled then we exclude the OSM chunk.
*/
int32 osm_chunk_id = ts_chunk_get_osm_chunk_id(ht->fd.id);
if (osm_chunk_id != 0 && !list_member_int(chunk_ids, osm_chunk_id))

if (osm_chunk_id != INVALID_CHUNK_ID)
{
chunk_ids = lappend_int(chunk_ids, osm_chunk_id);
if (!ts_guc_enable_osm_reads)
{
chunk_ids = list_delete_int(chunk_ids, osm_chunk_id);
}
else if (!list_member_int(chunk_ids, osm_chunk_id))
{
chunk_ids = lappend_int(chunk_ids, osm_chunk_id);
}
}
}

Expand Down
49 changes: 49 additions & 0 deletions tsl/test/expected/chunk_utils_internal.out
Expand Up @@ -569,6 +569,55 @@ SELECT * from ht_try WHERE timec > '2020-01-01 01:00' ORDER BY 1;
Thu May 05 01:00:00 2022 PDT | 222 | 222
(1 row)

--TEST GUC variable to enable/disable OSM chunk
SET timescaledb.enable_tiered_reads=false;
EXPLAIN (COSTS OFF) SELECT * from ht_try;
QUERY PLAN
-------------------------------
Seq Scan on _hyper_5_10_chunk
(1 row)

EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec > '2022-01-01 01:00';
QUERY PLAN
----------------------------------------------------------------------------------
Index Scan using _hyper_5_10_chunk_ht_try_timec_idx on _hyper_5_10_chunk
Index Cond: (timec > 'Sat Jan 01 01:00:00 2022 PST'::timestamp with time zone)
(2 rows)

EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec < '2023-01-01 01:00';
QUERY PLAN
----------------------------------------------------------------------------------
Index Scan using _hyper_5_10_chunk_ht_try_timec_idx on _hyper_5_10_chunk
Index Cond: (timec < 'Sun Jan 01 01:00:00 2023 PST'::timestamp with time zone)
(2 rows)

SET timescaledb.enable_tiered_reads=true;
EXPLAIN (COSTS OFF) SELECT * from ht_try;
QUERY PLAN
---------------------------------------
Append
-> Foreign Scan on child_fdw_table
-> Seq Scan on _hyper_5_10_chunk
(3 rows)

EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec > '2022-01-01 01:00';
QUERY PLAN
----------------------------------------------------------------------------------------
Append
-> Foreign Scan on child_fdw_table
-> Index Scan using _hyper_5_10_chunk_ht_try_timec_idx on _hyper_5_10_chunk
Index Cond: (timec > 'Sat Jan 01 01:00:00 2022 PST'::timestamp with time zone)
(4 rows)

EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec < '2023-01-01 01:00';
QUERY PLAN
----------------------------------------------------------------------------------------
Append
-> Foreign Scan on child_fdw_table
-> Index Scan using _hyper_5_10_chunk_ht_try_timec_idx on _hyper_5_10_chunk
Index Cond: (timec < 'Sun Jan 01 01:00:00 2023 PST'::timestamp with time zone)
(4 rows)

--TEST insert into a OSM chunk fails. actually any insert will fail. But we just need
-- to mock the hook and make sure the timescaledb code works correctly.
SELECT ts_setup_osm_hook();
Expand Down
10 changes: 10 additions & 0 deletions tsl/test/sql/chunk_utils_internal.sql
Expand Up @@ -344,6 +344,16 @@ SELECT * from ht_try WHERE timec > '2000-01-01 01:00' and timec < '2022-01-01 0

SELECT * from ht_try WHERE timec > '2020-01-01 01:00' ORDER BY 1;

--TEST GUC variable to enable/disable OSM chunk
SET timescaledb.enable_tiered_reads=false;
EXPLAIN (COSTS OFF) SELECT * from ht_try;
EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec > '2022-01-01 01:00';
EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec < '2023-01-01 01:00';
SET timescaledb.enable_tiered_reads=true;
EXPLAIN (COSTS OFF) SELECT * from ht_try;
EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec > '2022-01-01 01:00';
EXPLAIN (COSTS OFF) SELECT * from ht_try WHERE timec < '2023-01-01 01:00';

--TEST insert into a OSM chunk fails. actually any insert will fail. But we just need
-- to mock the hook and make sure the timescaledb code works correctly.

Expand Down

0 comments on commit 358133f

Please sign in to comment.