Skip to content

Commit e054291

Browse files
committed
Hypertable invalidations logical decoding plugin
Implementation of a logical decoding plugin for retrieving hypertable invalidations from the WAL. This plugin will allow you to create a logical replication slot for hypertables that will process changes to the hypertables and return hypertable invalidation ranges. It also adds two functions to read and write hypertable invalidation ranges, allowing tests to be written that read the format.
1 parent 1f87302 commit e054291

22 files changed

Lines changed: 1547 additions & 16 deletions

.unreleased/pr_8207

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implements: #8207 Logical decoding plugin for continuous aggregate invalidations

sql/cagg_utils.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ CREATE OR REPLACE FUNCTION _timescaledb_functions.cagg_get_bucket_function_info(
2727
-- fixed or variable sized bucket
2828
OUT bucket_fixed_width BOOLEAN
2929
) RETURNS RECORD AS '@MODULE_PATHNAME@', 'ts_continuous_agg_get_bucket_function_info' LANGUAGE C STRICT VOLATILE;
30+
31+
CREATE OR REPLACE FUNCTION _timescaledb_functions.cagg_parse_invalidation_record(
32+
invalidation_record BYTEA,
33+
OUT hypertable_relid REGCLASS,
34+
OUT lowest_modified_value BIGINT,
35+
OUT greatest_modified_value BIGINT)
36+
RETURNS RECORD
37+
AS '@MODULE_PATHNAME@', 'ts_continuous_agg_read_invalidation_record'
38+
LANGUAGE C STRICT IMMUTABLE;

sql/updates/reverse-dev.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ DROP PROCEDURE @extschema@.add_process_hypertable_invalidations_policy(REGCLASS,
33
DROP PROCEDURE @extschema@.remove_process_hypertable_invalidations_policy(REGCLASS, BOOL);
44
DROP PROCEDURE _timescaledb_functions.policy_process_hypertable_invalidations(INTEGER, JSONB);
55
DROP FUNCTION _timescaledb_functions.policy_process_hypertable_invalidations_check(JSONB);
6+
DROP FUNCTION _timescaledb_functions.cagg_parse_invalidation_record(BYTEA);
67

78
DROP PROCEDURE IF EXISTS _timescaledb_functions.policy_compression(job_id INTEGER, config JSONB);
89
DROP PROCEDURE IF EXISTS _timescaledb_functions.policy_compression_execute(

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ if(USE_OPENSSL)
108108
endif(MSVC)
109109
endif(USE_OPENSSL)
110110

111+
set(TSL_CAGG_INVALIDATION_PLUGIN_NAME
112+
"${PROJECT_NAME}-invalidations"
113+
CACHE STRING "Continuous aggregates invalidation plugin name")
114+
111115
configure_file(config.h.in config.h)
112116
add_dependencies(${PROJECT_NAME} gitcheck)
113117
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

src/compat/compat.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,15 @@ pg_cmp_u32(uint32 a, uint32 b)
699699
#define make_range_compat(typcache, lower, upper, empty, escontext) \
700700
make_range(typcache, lower, upper, empty, escontext)
701701
#endif
702+
703+
/* Copied from PG17. We can remove it once we deprecate older versions. */
704+
#if PG17_LT
705+
static inline void
706+
initReadOnlyStringInfo(StringInfo str, char *data, int len)
707+
{
708+
str->data = data;
709+
str->len = len;
710+
str->maxlen = 0; /* read-only */
711+
str->cursor = 0;
712+
}
713+
#endif

src/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define BUILD_OS_VERSION "@CMAKE_SYSTEM_VERSION@"
1212
#define BUILD_PROCESSOR "@CMAKE_SYSTEM_PROCESSOR@"
1313
#define BUILD_POINTER_BYTES @CMAKE_SIZEOF_VOID_P@
14+
#define CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_PLUGIN_NAME "@TSL_CAGG_INVALIDATION_PLUGIN_NAME@"
1415

1516
/*
1617
* Value should be set in package release scripts. Otherwise

src/cross_module_fn.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ CROSSMODULE_WRAPPER(continuous_agg_validate_query);
9696
CROSSMODULE_WRAPPER(continuous_agg_get_bucket_function);
9797
CROSSMODULE_WRAPPER(continuous_agg_get_bucket_function_info);
9898
CROSSMODULE_WRAPPER(continuous_agg_migrate_to_time_bucket);
99+
CROSSMODULE_WRAPPER(continuous_agg_read_invalidation_record);
99100
CROSSMODULE_WRAPPER(cagg_try_repair);
100101

101102
CROSSMODULE_WRAPPER(chunk_freeze_chunk);
@@ -426,6 +427,7 @@ TSDLLEXPORT CrossModuleFunctions ts_cm_functions_default = {
426427
.continuous_agg_get_bucket_function = error_no_default_fn_pg_community,
427428
.continuous_agg_get_bucket_function_info = error_no_default_fn_pg_community,
428429
.continuous_agg_migrate_to_time_bucket = error_no_default_fn_pg_community,
430+
.continuous_agg_read_invalidation_record = error_no_default_fn_pg_community,
429431
.cagg_try_repair = process_cagg_try_repair,
430432

431433
/* compression */

src/cross_module_fn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ typedef struct CrossModuleFunctions
123123
PGFunction continuous_agg_get_bucket_function_info;
124124
PGFunction continuous_agg_migrate_to_time_bucket;
125125
PGFunction cagg_try_repair;
126+
PGFunction continuous_agg_read_invalidation_record;
126127

127128
PGFunction compressed_data_send;
128129
PGFunction compressed_data_recv;

src/ts_catalog/catalog.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,16 @@ typedef enum Anum_continuous_aggs_bucket_function_pkey
10041004
#define Natts_continuous_aggs_bucket_function_pkey \
10051005
(_Anum_continuous_aggs_bucket_function_pkey_max - 1)
10061006

1007-
/****** CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_LOG_TABLE definitions*/
1007+
/*
1008+
* CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_LOG_TABLE definitions
1009+
*
1010+
* The definition of CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_PLUGIN_NAME is
1011+
* generated from config.h.in and can be found in the generated file.
1012+
*/
10081013
#define CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_LOG_TABLE_NAME \
10091014
"continuous_aggs_hypertable_invalidation_log"
1015+
#define CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_SLOT_NAME "continuous_aggs_hypertable_invalidations"
1016+
10101017
typedef enum Anum_continuous_aggs_hypertable_invalidation_log
10111018
{
10121019
Anum_continuous_aggs_hypertable_invalidation_log_hypertable_id = 1,

tsl/src/continuous_aggs/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(SOURCES
33
${CMAKE_CURRENT_SOURCE_DIR}/create.c
44
${CMAKE_CURRENT_SOURCE_DIR}/finalize.c
55
${CMAKE_CURRENT_SOURCE_DIR}/insert.c
6+
${CMAKE_CURRENT_SOURCE_DIR}/invalidation_record.c
67
${CMAKE_CURRENT_SOURCE_DIR}/invalidation_threshold.c
78
${CMAKE_CURRENT_SOURCE_DIR}/invalidation.c
89
${CMAKE_CURRENT_SOURCE_DIR}/materialize.c
@@ -12,3 +13,21 @@ set(SOURCES
1213
${CMAKE_CURRENT_SOURCE_DIR}/repair.c
1314
${CMAKE_CURRENT_SOURCE_DIR}/utils.c)
1415
target_sources(${TSL_LIBRARY_NAME} PRIVATE ${SOURCES})
16+
17+
add_library(
18+
${TSL_CAGG_INVALIDATION_PLUGIN_NAME} MODULE
19+
invalidation_plugin.c invalidation_plugin_cache.c invalidation_record.c)
20+
21+
# VERSION and SOVERSION are not used since they do not make sense for modules,
22+
# and actually breaks the build on MacOS.
23+
#
24+
# See https://gitlab.kitware.com/cmake/cmake/-/issues/20782.
25+
set_target_properties(
26+
${TSL_CAGG_INVALIDATION_PLUGIN_NAME}
27+
PROPERTIES OUTPUT_NAME ${TSL_CAGG_INVALIDATION_PLUGIN_NAME}
28+
PREFIX ""
29+
POSITION_INDEPENDENT_CODE ON)
30+
target_include_directories(${TSL_CAGG_INVALIDATION_PLUGIN_NAME}
31+
PRIVATE ${PG_INCLUDEDIR})
32+
install(TARGETS ${TSL_CAGG_INVALIDATION_PLUGIN_NAME}
33+
LIBRARY DESTINATION ${PG_PKGLIBDIR})

0 commit comments

Comments
 (0)