Skip to content

Commit e813abe

Browse files
committed
Add compatibility API for OSM
Add some helper function to decouple OSM from direct catalog access: _timescaledb_functions.get_hypertable_id(hypertable REGCLASS) _timescaledb_functions.get_hypertable_id(schema_name name, table_name name) _timescaledb_functions.get_hypertable_name(IN hypertable_id INTEGER, OUT schema_name name, OUT table_name name) _timescaledb_functions.get_chunk_id(hypertable REGCLASS) _timescaledb_functions.get_chunk_id(schema_name name, table_name name) _timescaledb_functions.get_chunk_name(IN chunk_id INTEGER, OUT schema_name name, OUT table_name name)
1 parent 1dd7df9 commit e813abe

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

sql/osm_api.sql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,36 @@ CREATE OR REPLACE FUNCTION _timescaledb_functions.hypertable_osm_range_update(
1212
empty BOOL = false
1313
) RETURNS BOOL AS '@MODULE_PATHNAME@',
1414
'ts_hypertable_osm_range_update' LANGUAGE C VOLATILE;
15+
16+
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_hypertable_id(hypertable REGCLASS) RETURNS INTEGER
17+
AS $$
18+
SELECT ht.id
19+
FROM _timescaledb_catalog.hypertable ht
20+
JOIN pg_class c ON ht.table_name = c.relname AND ht.schema_name = c.relnamespace::regnamespace::name AND c.oid = hypertable;
21+
$$ LANGUAGE SQL STABLE;
22+
23+
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_hypertable_id(schema_name name, table_name name) RETURNS INTEGER
24+
AS $$
25+
SELECT ht.id FROM _timescaledb_catalog.hypertable ht WHERE ht.schema_name = $1 AND ht.table_name = $2;
26+
$$ LANGUAGE SQL STABLE;
27+
28+
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_hypertable_name(IN hypertable_id INTEGER, OUT schema_name name, OUT table_name name) AS $$
29+
SELECT ht.schema_name, ht.table_name FROM _timescaledb_catalog.hypertable ht WHERE ht.id = hypertable_id;
30+
$$ LANGUAGE SQL STABLE;
31+
32+
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_chunk_id(hypertable REGCLASS) RETURNS INTEGER
33+
AS $$
34+
SELECT ch.id
35+
FROM _timescaledb_catalog.chunk ch
36+
JOIN pg_class c ON ch.table_name = c.relname AND ch.schema_name = c.relnamespace::regnamespace::name AND c.oid = hypertable;
37+
$$ LANGUAGE SQL STABLE;
38+
39+
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_chunk_id(schema_name name, table_name name) RETURNS INTEGER
40+
AS $$
41+
SELECT ch.id FROM _timescaledb_catalog.chunk ch WHERE ch.schema_name = $1 AND ch.table_name = $2;
42+
$$ LANGUAGE SQL STABLE;
43+
44+
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_chunk_name(IN chunk_id INTEGER, OUT schema_name name, OUT table_name name) AS $$
45+
SELECT ch.schema_name, ch.table_name FROM _timescaledb_catalog.chunk ch WHERE ch.id = chunk_id;
46+
$$ LANGUAGE SQL STABLE;
47+

tsl/test/shared/expected/extension.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ ORDER BY pronamespace::regnamespace::text COLLATE "C", p.oid::regprocedure::text
6969
_timescaledb_functions.freeze_chunk(regclass)
7070
_timescaledb_functions.generate_uuid()
7171
_timescaledb_functions.get_approx_row_count(regclass)
72+
_timescaledb_functions.get_chunk_id(name,name)
73+
_timescaledb_functions.get_chunk_id(regclass)
74+
_timescaledb_functions.get_chunk_name(integer)
7275
_timescaledb_functions.get_compressed_chunk_index_for_recompression(regclass)
7376
_timescaledb_functions.get_create_command(name)
7477
_timescaledb_functions.get_git_commit()
78+
_timescaledb_functions.get_hypertable_id(name,name)
79+
_timescaledb_functions.get_hypertable_id(regclass)
80+
_timescaledb_functions.get_hypertable_name(integer)
7581
_timescaledb_functions.get_internal_time_max(regtype)
7682
_timescaledb_functions.get_internal_time_min(regtype)
7783
_timescaledb_functions.get_orderby_defaults(regclass,text[])
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- This file and its contents are licensed under the Timescale License.
2+
-- Please see the included NOTICE for copyright information and
3+
-- LICENSE-TIMESCALE for a copy of the license.
4+
SELECT _timescaledb_functions.get_hypertable_id(NULL);
5+
get_hypertable_id
6+
-------------------
7+
8+
9+
SELECT _timescaledb_functions.get_hypertable_id('public.metrics');
10+
get_hypertable_id
11+
-------------------
12+
1
13+
14+
SELECT _timescaledb_functions.get_hypertable_id('public.metrics'::regclass);
15+
get_hypertable_id
16+
-------------------
17+
1
18+
19+
SELECT _timescaledb_functions.get_hypertable_id('public','metrics');
20+
get_hypertable_id
21+
-------------------
22+
1
23+
24+
SELECT * FROM _timescaledb_functions.get_hypertable_name(1);
25+
schema_name | table_name
26+
-------------+------------
27+
public | metrics
28+
29+
SELECT * FROM _timescaledb_functions.get_hypertable_name(_timescaledb_functions.get_hypertable_id('public.metrics'));
30+
schema_name | table_name
31+
-------------+------------
32+
public | metrics
33+
34+
SELECT _timescaledb_functions.get_chunk_id(NULL);
35+
get_chunk_id
36+
--------------
37+
38+
39+
SELECT _timescaledb_functions.get_chunk_id('_timescaledb_internal._hyper_X_X_chunk');
40+
get_chunk_id
41+
--------------
42+
3
43+
44+
SELECT _timescaledb_functions.get_chunk_id('_timescaledb_internal._hyper_X_X_chunk'::regclass);
45+
get_chunk_id
46+
--------------
47+
3
48+
49+
SELECT _timescaledb_functions.get_chunk_id('_timescaledb_internal','_hyper_X_X_chunk');
50+
get_chunk_id
51+
--------------
52+
3
53+
54+
SELECT * FROM _timescaledb_functions.get_chunk_name(3);
55+
schema_name | table_name
56+
-----------------------+------------------
57+
_timescaledb_internal | _hyper_X_X_chunk
58+
59+
SELECT * FROM _timescaledb_functions.get_chunk_name(_timescaledb_functions.get_chunk_id('_timescaledb_internal._hyper_X_X_chunk'));
60+
schema_name | table_name
61+
-----------------------+------------------
62+
_timescaledb_internal | _hyper_X_X_chunk
63+

tsl/test/shared/sql/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(TEST_FILES_SHARED
1313
gapfill_bug.sql
1414
generated_columns.sql
1515
memoize.sql
16+
osm_api.sql
1617
parameterized_chunkappend.sql
1718
security_barrier.sql
1819
subtract_integer_from_now.sql)

tsl/test/shared/sql/osm_api.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- This file and its contents are licensed under the Timescale License.
2+
-- Please see the included NOTICE for copyright information and
3+
-- LICENSE-TIMESCALE for a copy of the license.
4+
5+
SELECT _timescaledb_functions.get_hypertable_id(NULL);
6+
SELECT _timescaledb_functions.get_hypertable_id('public.metrics');
7+
SELECT _timescaledb_functions.get_hypertable_id('public.metrics'::regclass);
8+
SELECT _timescaledb_functions.get_hypertable_id('public','metrics');
9+
10+
SELECT * FROM _timescaledb_functions.get_hypertable_name(1);
11+
SELECT * FROM _timescaledb_functions.get_hypertable_name(_timescaledb_functions.get_hypertable_id('public.metrics'));
12+
13+
SELECT _timescaledb_functions.get_chunk_id(NULL);
14+
SELECT _timescaledb_functions.get_chunk_id('_timescaledb_internal._hyper_1_3_chunk');
15+
SELECT _timescaledb_functions.get_chunk_id('_timescaledb_internal._hyper_1_3_chunk'::regclass);
16+
SELECT _timescaledb_functions.get_chunk_id('_timescaledb_internal','_hyper_1_3_chunk');
17+
18+
SELECT * FROM _timescaledb_functions.get_chunk_name(3);
19+
SELECT * FROM _timescaledb_functions.get_chunk_name(_timescaledb_functions.get_chunk_id('_timescaledb_internal._hyper_1_3_chunk'));
20+

0 commit comments

Comments
 (0)