Skip to content

Commit e905250

Browse files
committed
Add hypertable_cagg_settings catalog
Introdusce a new catalog table named `hypertable_cagg_settings` to store tenant tracking column, start and end offsets to calculate granular refresh window.
1 parent c4a71ff commit e905250

9 files changed

Lines changed: 142 additions & 0 deletions

File tree

sql/pre_install/tables.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,20 @@ SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.continuous_aggs
455455

456456
CREATE INDEX continuous_aggs_tenant_tracking_idx ON _timescaledb_catalog.continuous_aggs_tenant_tracking (hypertable_id, seqnum);
457457

458+
-- Per-hypertable settings for granular refresh of continuous aggregates.
459+
-- Row existence means granular refresh is configured for the hypertable.
460+
CREATE TABLE _timescaledb_catalog.hypertable_cagg_settings (
461+
hypertable_id integer NOT NULL,
462+
granular_refresh_column name NOT NULL,
463+
granular_refresh_start_offset text,
464+
granular_refresh_end_offset text,
465+
-- table constraints
466+
CONSTRAINT hypertable_cagg_settings_pkey PRIMARY KEY (hypertable_id),
467+
CONSTRAINT hypertable_cagg_settings_hypertable_id_fkey FOREIGN KEY (hypertable_id) REFERENCES _timescaledb_catalog.hypertable (id) ON DELETE CASCADE
468+
);
469+
470+
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.hypertable_cagg_settings', '');
471+
458472
/* the source of this data is the enum from the source code that lists
459473
* the algorithms. This table is NOT dumped.
460474
*/

sql/updates/latest-dev.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,14 @@ GRANT SELECT ON _timescaledb_catalog.chunk TO PUBLIC;
161161
-- END add chunk.relid
162162
--
163163

164+
-- add hypertable_cagg_settings
165+
CREATE TABLE _timescaledb_catalog.hypertable_cagg_settings (
166+
hypertable_id integer NOT NULL,
167+
granular_refresh_column name NOT NULL,
168+
granular_refresh_start_offset text,
169+
granular_refresh_end_offset text,
170+
CONSTRAINT hypertable_cagg_settings_pkey PRIMARY KEY (hypertable_id),
171+
CONSTRAINT hypertable_cagg_settings_hypertable_id_fkey FOREIGN KEY (hypertable_id) REFERENCES _timescaledb_catalog.hypertable (id) ON DELETE CASCADE
172+
);
173+
174+
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.hypertable_cagg_settings', '');

sql/updates/reverse-dev.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,7 @@ DROP FUNCTION IF EXISTS @extschema@.alter_job(job_id INTEGER, schedule_interval
263263
-- drop continuous_aggs_tenant_tracking
264264
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.continuous_aggs_tenant_tracking;
265265
DROP TABLE _timescaledb_catalog.continuous_aggs_tenant_tracking;
266+
267+
-- drop hypertable_cagg_settings
268+
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.hypertable_cagg_settings;
269+
DROP TABLE _timescaledb_catalog.hypertable_cagg_settings;

src/ts_catalog/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(SOURCES
99
${CMAKE_CURRENT_SOURCE_DIR}/continuous_aggs_jobs_refresh_ranges.c
1010
${CMAKE_CURRENT_SOURCE_DIR}/continuous_aggs_tenant_tracking.c
1111
${CMAKE_CURRENT_SOURCE_DIR}/continuous_aggs_watermark.c
12+
${CMAKE_CURRENT_SOURCE_DIR}/hypertable_cagg_settings.c
1213
${CMAKE_CURRENT_SOURCE_DIR}/metadata.c
1314
${CMAKE_CURRENT_SOURCE_DIR}/tablespace.c)
1415
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

src/ts_catalog/catalog.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ static const TableInfoDef catalog_table_names[_MAX_CATALOG_TABLES + 1] = {
9797
.schema_name = CATALOG_SCHEMA_NAME,
9898
.table_name = CONTINUOUS_AGGS_TENANT_TRACKING_TABLE_NAME,
9999
},
100+
[HYPERTABLE_CAGG_SETTINGS] = {
101+
.schema_name = CATALOG_SCHEMA_NAME,
102+
.table_name = HYPERTABLE_CAGG_SETTINGS_TABLE_NAME,
103+
},
100104
[COMPRESSION_SETTINGS] = {
101105
.schema_name = CATALOG_SCHEMA_NAME,
102106
.table_name = COMPRESSION_SETTINGS_TABLE_NAME,
@@ -252,6 +256,12 @@ static const TableIndexDef catalog_table_index_definitions[_MAX_CATALOG_TABLES]
252256
[CONTINUOUS_AGGS_TENANT_TRACKING_IDX] = "continuous_aggs_tenant_tracking_idx",
253257
},
254258
},
259+
[HYPERTABLE_CAGG_SETTINGS] = {
260+
.length = _MAX_HYPERTABLE_CAGG_SETTINGS_INDEX,
261+
.names = (char *[]) {
262+
[HYPERTABLE_CAGG_SETTINGS_PKEY] = "hypertable_cagg_settings_pkey",
263+
},
264+
},
255265
[CONTINUOUS_AGGS_WATERMARK] = {
256266
.length = _MAX_CONTINUOUS_AGGS_WATERMARK_INDEX,
257267
.names = (char *[]) {

src/ts_catalog/catalog.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef enum CatalogTable
5050
CONTINUOUS_AGGS_MATERIALIZATION_RANGES,
5151
CONTINUOUS_AGGS_JOBS_REFRESH_RANGES,
5252
CONTINUOUS_AGGS_TENANT_TRACKING,
53+
HYPERTABLE_CAGG_SETTINGS,
5354
COMPRESSION_SETTINGS,
5455
COMPRESSION_CHUNK_SIZE,
5556
CONTINUOUS_AGGS_BUCKET_FUNCTION,
@@ -1122,6 +1123,43 @@ typedef enum Anum_continuous_aggs_tenant_tracking_idx
11221123
#define Natts_continuous_aggs_tenant_tracking_idx \
11231124
(_Anum_continuous_aggs_tenant_tracking_idx_max - 1)
11241125

1126+
/****** HYPERTABLE_CAGG_SETTINGS definitions */
1127+
#define HYPERTABLE_CAGG_SETTINGS_TABLE_NAME "hypertable_cagg_settings"
1128+
1129+
typedef enum Anum_hypertable_cagg_settings
1130+
{
1131+
Anum_hypertable_cagg_settings_hypertable_id = 1,
1132+
Anum_hypertable_cagg_settings_granular_refresh_column,
1133+
Anum_hypertable_cagg_settings_granular_refresh_start_offset,
1134+
Anum_hypertable_cagg_settings_granular_refresh_end_offset,
1135+
_Anum_hypertable_cagg_settings_max,
1136+
} Anum_hypertable_cagg_settings;
1137+
1138+
#define Natts_hypertable_cagg_settings (_Anum_hypertable_cagg_settings_max - 1)
1139+
1140+
typedef struct FormData_hypertable_cagg_settings
1141+
{
1142+
int32 hypertable_id;
1143+
NameData granular_refresh_column;
1144+
text *granular_refresh_start_offset;
1145+
text *granular_refresh_end_offset;
1146+
} FormData_hypertable_cagg_settings;
1147+
1148+
typedef FormData_hypertable_cagg_settings *Form_hypertable_cagg_settings;
1149+
1150+
enum
1151+
{
1152+
HYPERTABLE_CAGG_SETTINGS_PKEY = 0,
1153+
_MAX_HYPERTABLE_CAGG_SETTINGS_INDEX,
1154+
};
1155+
typedef enum Anum_hypertable_cagg_settings_pkey
1156+
{
1157+
Anum_hypertable_cagg_settings_pkey_hypertable_id = 1,
1158+
_Anum_hypertable_cagg_settings_pkey_max,
1159+
} Anum_hypertable_cagg_settings_pkey;
1160+
1161+
#define Natts_hypertable_cagg_settings_pkey (_Anum_hypertable_cagg_settings_pkey_max - 1)
1162+
11251163
typedef enum Anum_continuous_aggs_jobs_refresh_ranges_idx
11261164
{
11271165
Anum_continuous_aggs_jobs_refresh_ranges_idx_materialization_id = 1,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file and its contents are licensed under the Apache License 2.0.
3+
* Please see the included NOTICE for copyright information and
4+
* LICENSE-APACHE for a copy of the license.
5+
*/
6+
7+
#include <postgres.h>
8+
9+
#include "scan_iterator.h"
10+
#include "ts_catalog/catalog.h"
11+
#include "ts_catalog/hypertable_cagg_settings.h"
12+
13+
static void
14+
init_scan_by_hypertable_id(ScanIterator *iterator, const int32 hypertable_id)
15+
{
16+
iterator->ctx.index = catalog_get_index(ts_catalog_get(),
17+
HYPERTABLE_CAGG_SETTINGS,
18+
HYPERTABLE_CAGG_SETTINGS_PKEY);
19+
20+
ts_scan_iterator_scan_key_init(iterator,
21+
Anum_hypertable_cagg_settings_pkey_hypertable_id,
22+
BTEqualStrategyNumber,
23+
F_INT4EQ,
24+
Int32GetDatum(hypertable_id));
25+
}
26+
27+
/* Delete the settings row for a hypertable, if any. */
28+
TSDLLEXPORT void
29+
ts_hypertable_cagg_settings_delete(int32 hypertable_id)
30+
{
31+
ScanIterator iterator =
32+
ts_scan_iterator_create(HYPERTABLE_CAGG_SETTINGS, RowExclusiveLock, CurrentMemoryContext);
33+
34+
init_scan_by_hypertable_id(&iterator, hypertable_id);
35+
36+
ts_scanner_foreach(&iterator)
37+
{
38+
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
39+
40+
ts_catalog_delete_tid(ti->scanrel, ts_scanner_get_tuple_tid(ti));
41+
}
42+
ts_scan_iterator_close(&iterator);
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This file and its contents are licensed under the Apache License 2.0.
3+
* Please see the included NOTICE for copyright information and
4+
* LICENSE-APACHE for a copy of the license.
5+
*/
6+
#pragma once
7+
8+
#include <postgres.h>
9+
10+
#include "export.h"
11+
12+
/*
13+
* Catalog access for _timescaledb_catalog.hypertable_cagg_settings.
14+
*
15+
* Per-hypertable settings for granular refresh of continuous
16+
* aggregates. Row existence means granular refresh is configured for the
17+
* hypertable.
18+
*/
19+
20+
extern TSDLLEXPORT void ts_hypertable_cagg_settings_delete(int32 hypertable_id);

test/expected/drop_rename_hypertable.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ SELECT schema, name FROM test.relation WHERE schema IN ('public', '_timescaledb_
200200
_timescaledb_catalog | dimension
201201
_timescaledb_catalog | dimension_slice
202202
_timescaledb_catalog | hypertable
203+
_timescaledb_catalog | hypertable_cagg_settings
203204
_timescaledb_catalog | metadata
204205
_timescaledb_catalog | tablespace
205206
_timescaledb_internal | bgw_job_stat

0 commit comments

Comments
 (0)