Skip to content

Commit 1e07227

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 94a1864 commit 1e07227

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
@@ -460,6 +460,20 @@ SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.continuous_aggs
460460

461461
CREATE INDEX continuous_aggs_tenant_tracking_idx ON _timescaledb_catalog.continuous_aggs_tenant_tracking (hypertable_id, seqnum);
462462

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

sql/updates/latest-dev.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ GRANT SELECT ON _timescaledb_catalog.chunk TO PUBLIC;
215215
-- END add chunk.relid
216216
--
217217

218+
-- add hypertable_cagg_settings
219+
CREATE TABLE _timescaledb_catalog.hypertable_cagg_settings (
220+
hypertable_id integer NOT NULL,
221+
granular_refresh_column name NOT NULL,
222+
granular_refresh_start_offset text,
223+
granular_refresh_end_offset text,
224+
CONSTRAINT hypertable_cagg_settings_pkey PRIMARY KEY (hypertable_id),
225+
CONSTRAINT hypertable_cagg_settings_hypertable_id_fkey FOREIGN KEY (hypertable_id) REFERENCES _timescaledb_catalog.hypertable (id) ON DELETE CASCADE
226+
);
227+
218228
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.hypertable_cagg_settings', '');
219229

220230
-- Rebuild _timescaledb_catalog.continuous_agg to add granular_refresh_enabled.

sql/updates/reverse-dev.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.continuous_aggs
315315
-- drop continuous_aggs_tenant_tracking
316316
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.continuous_aggs_tenant_tracking;
317317
DROP TABLE _timescaledb_catalog.continuous_aggs_tenant_tracking;
318+
319+
-- drop hypertable_cagg_settings
320+
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.hypertable_cagg_settings;
321+
DROP TABLE _timescaledb_catalog.hypertable_cagg_settings;
322+
318323
-- Rebuild _timescaledb_catalog.continuous_agg to drop granular_refresh_enabled.
319324
DROP VIEW IF EXISTS timescaledb_experimental.policies;
320325

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,
@@ -1128,6 +1129,43 @@ typedef enum Anum_continuous_aggs_tenant_tracking_idx
11281129
#define Natts_continuous_aggs_tenant_tracking_idx \
11291130
(_Anum_continuous_aggs_tenant_tracking_idx_max - 1)
11301131

1132+
/****** HYPERTABLE_CAGG_SETTINGS definitions */
1133+
#define HYPERTABLE_CAGG_SETTINGS_TABLE_NAME "hypertable_cagg_settings"
1134+
1135+
typedef enum Anum_hypertable_cagg_settings
1136+
{
1137+
Anum_hypertable_cagg_settings_hypertable_id = 1,
1138+
Anum_hypertable_cagg_settings_granular_refresh_column,
1139+
Anum_hypertable_cagg_settings_granular_refresh_start_offset,
1140+
Anum_hypertable_cagg_settings_granular_refresh_end_offset,
1141+
_Anum_hypertable_cagg_settings_max,
1142+
} Anum_hypertable_cagg_settings;
1143+
1144+
#define Natts_hypertable_cagg_settings (_Anum_hypertable_cagg_settings_max - 1)
1145+
1146+
typedef struct FormData_hypertable_cagg_settings
1147+
{
1148+
int32 hypertable_id;
1149+
NameData granular_refresh_column;
1150+
text *granular_refresh_start_offset;
1151+
text *granular_refresh_end_offset;
1152+
} FormData_hypertable_cagg_settings;
1153+
1154+
typedef FormData_hypertable_cagg_settings *Form_hypertable_cagg_settings;
1155+
1156+
enum
1157+
{
1158+
HYPERTABLE_CAGG_SETTINGS_PKEY = 0,
1159+
_MAX_HYPERTABLE_CAGG_SETTINGS_INDEX,
1160+
};
1161+
typedef enum Anum_hypertable_cagg_settings_pkey
1162+
{
1163+
Anum_hypertable_cagg_settings_pkey_hypertable_id = 1,
1164+
_Anum_hypertable_cagg_settings_pkey_max,
1165+
} Anum_hypertable_cagg_settings_pkey;
1166+
1167+
#define Natts_hypertable_cagg_settings_pkey (_Anum_hypertable_cagg_settings_pkey_max - 1)
1168+
11311169
typedef enum Anum_continuous_aggs_jobs_refresh_ranges_idx
11321170
{
11331171
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)