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 #ifndef TIMESCALEDB_CROSS_MODULE_FN_H
7 #define TIMESCALEDB_CROSS_MODULE_FN_H
8 
9 #include <postgres.h>
10 #include <fmgr.h>
11 #include <commands/event_trigger.h>
12 #include <optimizer/planner.h>
13 #include <utils/timestamp.h>
14 #include <utils/jsonb.h>
15 #include <utils/array.h>
16 
17 #include "export.h"
18 #include "compat/compat.h"
19 #include "bgw/job.h"
20 #include "process_utility.h"
21 #include "with_clause_parser.h"
22 #include "continuous_agg.h"
23 #include "plan_expand_hypertable.h"
24 #include "planner.h"
25 
26 /*
27  * To define a cross-module function add it to this struct, add a default
28  * version in to ts_cm_functions_default cross_module_fn.c, and the overridden
29  * version to tsl_cm_functions tsl/src/init.c.
30  * This will allow the function to be called from this codebase as
31  *     ts_cm_functions-><function name>
32  */
33 
34 typedef struct JsonbParseState JsonbParseState;
35 typedef struct Hypertable Hypertable;
36 typedef struct Chunk Chunk;
37 typedef struct CopyChunkState CopyChunkState;
38 typedef struct CompressSingleRowState CompressSingleRowState;
39 
40 typedef struct CrossModuleFunctions
41 {
42 	void (*add_tsl_telemetry_info)(JsonbParseState **parse_state);
43 
44 	PGFunction policy_compression_add;
45 	PGFunction policy_compression_remove;
46 	PGFunction policy_recompression_proc;
47 	PGFunction policy_refresh_cagg_add;
48 	PGFunction policy_refresh_cagg_proc;
49 	PGFunction policy_refresh_cagg_remove;
50 	PGFunction policy_reorder_add;
51 	PGFunction policy_reorder_proc;
52 	PGFunction policy_reorder_remove;
53 	PGFunction policy_retention_add;
54 	PGFunction policy_retention_proc;
55 	PGFunction policy_retention_remove;
56 
57 	PGFunction job_add;
58 	PGFunction job_alter;
59 	PGFunction job_delete;
60 	PGFunction job_run;
61 	bool (*job_execute)(BgwJob *job);
62 
63 	void (*create_upper_paths_hook)(PlannerInfo *, UpperRelationKind, RelOptInfo *, RelOptInfo *,
64 									TsRelType input_reltype, Hypertable *ht, void *extra);
65 	void (*set_rel_pathlist_dml)(PlannerInfo *, RelOptInfo *, Index, RangeTblEntry *, Hypertable *);
66 	void (*set_rel_pathlist_query)(PlannerInfo *, RelOptInfo *, Index, RangeTblEntry *,
67 								   Hypertable *);
68 	void (*set_rel_pathlist)(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte);
69 
70 	/* gapfill */
71 	PGFunction gapfill_marker;
72 	PGFunction gapfill_int16_time_bucket;
73 	PGFunction gapfill_int32_time_bucket;
74 	PGFunction gapfill_int64_time_bucket;
75 	PGFunction gapfill_date_time_bucket;
76 	PGFunction gapfill_timestamp_time_bucket;
77 	PGFunction gapfill_timestamptz_time_bucket;
78 
79 	PGFunction reorder_chunk;
80 	PGFunction move_chunk;
81 	PGFunction move_chunk_proc;
82 	PGFunction copy_chunk_proc;
83 	PGFunction copy_chunk_cleanup_proc;
84 	void (*ddl_command_start)(ProcessUtilityArgs *args);
85 	void (*ddl_command_end)(EventTriggerData *command);
86 	void (*sql_drop)(List *dropped_objects);
87 
88 	/* Continuous Aggregates */
89 	PGFunction partialize_agg;
90 	PGFunction finalize_agg_sfunc;
91 	PGFunction finalize_agg_ffunc;
92 	DDLResult (*process_cagg_viewstmt)(Node *stmt, const char *query_string, void *pstmt,
93 									   WithClauseResult *with_clause_options);
94 	PGFunction continuous_agg_invalidation_trigger;
95 	void (*continuous_agg_call_invalidation_trigger)(int32 hypertable_id, Relation chunk_rel,
96 													 HeapTuple chunk_tuple,
97 													 HeapTuple chunk_newtuple, bool update,
98 													 bool is_distributed_hypertable_trigger,
99 													 int32 parent_hypertable_id);
100 	PGFunction continuous_agg_refresh;
101 	PGFunction continuous_agg_refresh_chunk;
102 	void (*continuous_agg_invalidate)(const Hypertable *ht,
103 									  ContinuousAggHypertableStatus caggstatus, int32 entry_id,
104 									  int64 start, int64 end);
105 	void (*continuous_agg_update_options)(ContinuousAgg *cagg,
106 										  WithClauseResult *with_clause_options);
107 	PGFunction invalidation_cagg_log_add_entry;
108 	PGFunction invalidation_hyper_log_add_entry;
109 	void (*remote_invalidation_log_delete)(int32 raw_hypertable_id,
110 										   ContinuousAggHypertableStatus caggstatus);
111 	PGFunction drop_dist_ht_invalidation_trigger;
112 	void (*remote_drop_dist_ht_invalidation_trigger)(int32 raw_hypertable_id);
113 	PGFunction invalidation_process_hypertable_log;
114 	PGFunction invalidation_process_cagg_log;
115 
116 	PGFunction compressed_data_send;
117 	PGFunction compressed_data_recv;
118 	PGFunction compressed_data_in;
119 	PGFunction compressed_data_out;
120 	bool (*process_compress_table)(AlterTableCmd *cmd, Hypertable *ht,
121 								   WithClauseResult *with_clause_options);
122 	void (*process_altertable_cmd)(Hypertable *ht, const AlterTableCmd *cmd);
123 	void (*process_rename_cmd)(Oid relid, Cache *hcache, const RenameStmt *stmt);
124 	PGFunction compress_chunk;
125 	PGFunction decompress_chunk;
126 	PGFunction recompress_chunk;
127 	/* The compression functions below are not installed in SQL as part of create extension;
128 	 *  They are installed and tested during testing scripts. They are exposed in cross-module
129 	 *  functions because they may be very useful for debugging customer problems if the sql
130 	 *  stub is installed on the customer's machine.
131 	 */
132 	PGFunction compressed_data_decompress_forward;
133 	PGFunction compressed_data_decompress_reverse;
134 	PGFunction deltadelta_compressor_append;
135 	PGFunction deltadelta_compressor_finish;
136 	PGFunction gorilla_compressor_append;
137 	PGFunction gorilla_compressor_finish;
138 	PGFunction dictionary_compressor_append;
139 	PGFunction dictionary_compressor_finish;
140 	PGFunction array_compressor_append;
141 	PGFunction array_compressor_finish;
142 
143 	PGFunction data_node_add;
144 	PGFunction data_node_delete;
145 	PGFunction data_node_attach;
146 	PGFunction data_node_ping;
147 	PGFunction data_node_detach;
148 	PGFunction data_node_allow_new_chunks;
149 	PGFunction data_node_block_new_chunks;
150 
151 	PGFunction chunk_set_default_data_node;
152 	PGFunction create_chunk;
153 	PGFunction show_chunk;
154 
155 	List *(*get_and_validate_data_node_list)(ArrayType *nodearr);
156 	void (*hypertable_make_distributed)(Hypertable *ht, List *data_node_names);
157 	PGFunction timescaledb_fdw_handler;
158 	PGFunction timescaledb_fdw_validator;
159 	void (*cache_syscache_invalidate)(Datum arg, int cacheid, uint32 hashvalue);
160 	PGFunction remote_txn_id_in;
161 	PGFunction remote_txn_id_out;
162 	PGFunction remote_txn_heal_data_node;
163 	PGFunction remote_connection_cache_show;
164 	void (*create_chunk_on_data_nodes)(const Chunk *chunk, const Hypertable *ht,
165 									   const char *remote_chunk_name, List *data_nodes);
166 	Path *(*distributed_insert_path_create)(PlannerInfo *root, ModifyTablePath *mtpath,
167 											Index hypertable_rti, int subpath_index);
168 	uint64 (*distributed_copy)(const CopyStmt *stmt, CopyChunkState *ccstate, List *attnums);
169 	bool (*set_distributed_id)(Datum id);
170 	void (*set_distributed_peer_id)(Datum id);
171 	bool (*is_access_node_session)(void);
172 	bool (*remove_from_distributed_db)(void);
173 	PGFunction dist_remote_hypertable_info;
174 	PGFunction dist_remote_chunk_info;
175 	PGFunction dist_remote_compressed_chunk_info;
176 	PGFunction dist_remote_hypertable_index_info;
177 	void (*validate_as_data_node)(void);
178 	void (*func_call_on_data_nodes)(FunctionCallInfo fcinfo, List *data_node_oids);
179 	PGFunction distributed_exec;
180 	PGFunction create_distributed_restore_point;
181 	PGFunction chunk_get_relstats;
182 	PGFunction chunk_get_colstats;
183 	PGFunction hypertable_distributed_set_replication_factor;
184 	PGFunction chunk_create_empty_table;
185 	PGFunction chunk_create_replica_table;
186 	PGFunction chunk_drop_replica;
187 	void (*update_compressed_chunk_relstats)(Oid uncompressed_relid, Oid compressed_relid);
188 	CompressSingleRowState *(*compress_row_init)(int srcht_id, Relation in_rel, Relation out_rel);
189 	TupleTableSlot *(*compress_row_exec)(CompressSingleRowState *cr, TupleTableSlot *slot);
190 	void (*compress_row_end)(CompressSingleRowState *cr);
191 	void (*compress_row_destroy)(CompressSingleRowState *cr);
192 } CrossModuleFunctions;
193 
194 extern TSDLLEXPORT CrossModuleFunctions *ts_cm_functions;
195 extern TSDLLEXPORT CrossModuleFunctions ts_cm_functions_default;
196 
197 #endif /* TIMESCALEDB_CROSS_MODULE_FN_H */
198