Skip to content

Commit

Permalink
Move partialize functions to _timescaledb_functions schema
Browse files Browse the repository at this point in the history
To increase schema security we do not want to mix our own internal
objects with user objects. Since chunks are created in the
_timescaledb_internal schema our internal functions should live in
a different dedicated schema. This patch make the necessary
adjustments for the following functions:

- finalize_agg_ffunc(internal,text,name,name,name[],bytea,anyelement)
- finalize_agg_sfunc(internal,text,name,name,name[],bytea,anyelement)
- partialize_agg(anyelement)
- finalize_agg(text,name,name,name[][],bytea,anyelement)
  • Loading branch information
svenklemm committed Aug 29, 2023
1 parent 623381c commit 3b6dc7d
Show file tree
Hide file tree
Showing 25 changed files with 708 additions and 2,683 deletions.
12 changes: 6 additions & 6 deletions sql/partialize_finalize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

CREATE OR REPLACE FUNCTION _timescaledb_internal.partialize_agg(arg ANYELEMENT)
CREATE OR REPLACE FUNCTION _timescaledb_functions.partialize_agg(arg ANYELEMENT)
RETURNS BYTEA AS '@MODULE_PATHNAME@', 'ts_partialize_agg' LANGUAGE C STABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION _timescaledb_internal.finalize_agg_sfunc(
CREATE OR REPLACE FUNCTION _timescaledb_functions.finalize_agg_sfunc(
tstate internal, aggfn TEXT, inner_agg_collation_schema NAME, inner_agg_collation_name NAME, inner_agg_input_types NAME[][], inner_agg_serialized_state BYTEA, return_type_dummy_val ANYELEMENT)
RETURNS internal
AS '@MODULE_PATHNAME@', 'ts_finalize_agg_sfunc'
LANGUAGE C IMMUTABLE;

CREATE OR REPLACE FUNCTION _timescaledb_internal.finalize_agg_ffunc(
CREATE OR REPLACE FUNCTION _timescaledb_functions.finalize_agg_ffunc(
tstate internal, aggfn TEXT, inner_agg_collation_schema NAME, inner_agg_collation_name NAME, inner_agg_input_types NAME[][], inner_agg_serialized_state BYTEA, return_type_dummy_val ANYELEMENT)
RETURNS anyelement
AS '@MODULE_PATHNAME@', 'ts_finalize_agg_ffunc'
LANGUAGE C IMMUTABLE;

CREATE OR REPLACE AGGREGATE _timescaledb_internal.finalize_agg(agg_name TEXT, inner_agg_collation_schema NAME, inner_agg_collation_name NAME, inner_agg_input_types NAME[][], inner_agg_serialized_state BYTEA, return_type_dummy_val anyelement) (
SFUNC = _timescaledb_internal.finalize_agg_sfunc,
CREATE OR REPLACE AGGREGATE _timescaledb_functions.finalize_agg(agg_name TEXT, inner_agg_collation_schema NAME, inner_agg_collation_name NAME, inner_agg_input_types NAME[][], inner_agg_serialized_state BYTEA, return_type_dummy_val anyelement) (
SFUNC = _timescaledb_functions.finalize_agg_sfunc,
STYPE = internal,
FINALFUNC = _timescaledb_internal.finalize_agg_ffunc,
FINALFUNC = _timescaledb_functions.finalize_agg_ffunc,
FINALFUNC_EXTRA
);
14 changes: 13 additions & 1 deletion sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ DECLARE
cagg_migrate_execute_refresh_new_cagg, cagg_migrate_execute_copy_data, cagg_migrate_execute_override_cagg,
cagg_migrate_execute_drop_old_cagg, cagg_migrate_execute_plan,
finalize_agg,
hypertable_invalidation_log_delete, invalidation_cagg_log_add_entry, invalidation_hyper_log_add_entry,
invalidation_process_cagg_log, invalidation_process_hypertable_log, materialization_invalidation_log_delete,
Expand All @@ -134,7 +136,13 @@ DECLARE
}';
BEGIN
FOR foid, kind IN
SELECT oid, CASE WHEN prokind = 'f' THEN 'FUNCTION' ELSE 'PROCEDURE' END FROM pg_proc WHERE proname = ANY(funcs) AND pronamespace = '_timescaledb_internal'::regnamespace
SELECT oid,
CASE
WHEN prokind = 'f' THEN 'FUNCTION'
WHEN prokind = 'a' THEN 'AGGREGATE'
ELSE 'PROCEDURE'
END
FROM pg_proc WHERE proname = ANY(funcs) AND pronamespace = '_timescaledb_internal'::regnamespace
LOOP
EXECUTE format('ALTER %s %s SET SCHEMA _timescaledb_functions', kind, foid);
END LOOP;
Expand All @@ -155,3 +163,7 @@ ALTER FUNCTION _timescaledb_internal.get_partition_hash(val anyelement) SET SCHE

UPDATE _timescaledb_catalog.dimension SET partitioning_func_schema = '_timescaledb_functions' WHERE partitioning_func_schema = '_timescaledb_internal' AND partitioning_func IN ('get_partition_for_key','get_partition_hash');

ALTER FUNCTION _timescaledb_internal.finalize_agg_ffunc(internal,text,name,name,name[],bytea,anyelement) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.finalize_agg_sfunc(internal,text,name,name,name[],bytea,anyelement) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.partialize_agg(anyelement) SET SCHEMA _timescaledb_functions;

5 changes: 5 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,8 @@ ALTER FUNCTION _timescaledb_functions.get_partition_hash(val anyelement) SET SCH

UPDATE _timescaledb_catalog.dimension SET partitioning_func_schema = '_timescaledb_internal' WHERE partitioning_func_schema = '_timescaledb_functions' AND partitioning_func IN ('get_partition_for_key','get_partition_hash');

ALTER FUNCTION _timescaledb_functions.finalize_agg_ffunc(internal,text,name,name,name[],bytea,anyelement) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.finalize_agg_sfunc(internal,text,name,name,name[],bytea,anyelement) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.partialize_agg(anyelement) SET SCHEMA _timescaledb_internal;
ALTER AGGREGATE _timescaledb_functions.finalize_agg(text,name,name,name[][],bytea,anyelement) SET SCHEMA _timescaledb_internal;

6 changes: 3 additions & 3 deletions src/planner/partialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef struct PartializeWalkerState
*
* The partialize function is an expression of the form:
*
* _timescaledb_internal.partialize_agg(avg(temp))
* _timescaledb_functions.partialize_agg(avg(temp))
*
* where avg(temp) can be replaced by any aggregate that can be partialized.
*
Expand Down Expand Up @@ -115,7 +115,7 @@ has_partialize_function(Node *node, PartializeAggFixAggref fix_aggref)
.looking_for_agg = false,
.fix_aggref = fix_aggref,
.fnoid = InvalidOid };
List *name = list_make2(makeString(INTERNAL_SCHEMA_NAME), makeString(TS_PARTIALFN));
List *name = list_make2(makeString(FUNCTIONS_SCHEMA_NAME), makeString(TS_PARTIALFN));

partialfnoid = LookupFuncName(name, lengthof(argtyp), argtyp, false);
Assert(OidIsValid(partialfnoid));
Expand Down Expand Up @@ -193,7 +193,7 @@ partialize_agg_paths(RelOptInfo *rel)
* instance:
*
* SELECT time_bucket('1 day', time), device,
* _timescaledb_internal.partialize_agg(avg(temp))
* _timescaledb_functions.partialize_agg(avg(temp))
* GROUP BY 1, 2;
*
* Would compute the partial aggregate of avg(temp).
Expand Down
1 change: 0 additions & 1 deletion tsl/src/continuous_aggs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "ts_catalog/catalog.h"
#include "ts_catalog/continuous_agg.h"

#define TS_PARTIALFN "partialize_agg"
#define CONTINUOUS_AGG_MAX_JOIN_RELATIONS 2
#define DEFAULT_MATPARTCOLUMN_NAME "time_partition_col"

Expand Down
13 changes: 7 additions & 6 deletions tsl/src/continuous_aggs/finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "finalize.h"
#include "create.h"
#include "common.h"
#include <partialize_finalize.h>

typedef struct CAggHavingCxt
{
Expand Down Expand Up @@ -53,11 +54,11 @@ get_partialize_funcexpr(Aggref *agg)
FuncExpr *partialize_fnexpr;
Oid partfnoid, partargtype;
partargtype = ANYELEMENTOID;
partfnoid =
LookupFuncName(list_make2(makeString(INTERNAL_SCHEMA_NAME), makeString(TS_PARTIALFN)),
1,
&partargtype,
false);
partfnoid = LookupFuncName(list_make2(makeString(FUNCTIONS_SCHEMA_NAME),
makeString(PARTIALIZE_FUNC_NAME)),
1,
&partargtype,
false);
partialize_fnexpr = makeFuncExpr(partfnoid,
BYTEAOID,
list_make1(agg), /*args*/
Expand Down Expand Up @@ -194,7 +195,7 @@ get_finalize_function_oid(void)
Oid finalfnoid;
Oid finalfnargtypes[] = { TEXTOID, NAMEOID, NAMEOID, get_array_type(NAMEOID),
BYTEAOID, ANYELEMENTOID };
List *funcname = list_make2(makeString(INTERNAL_SCHEMA_NAME), makeString(FINALFN));
List *funcname = list_make2(makeString(FUNCTIONS_SCHEMA_NAME), makeString(FINALFN));
int nargs = sizeof(finalfnargtypes) / sizeof(finalfnargtypes[0]);
finalfnoid = LookupFuncName(funcname, nargs, finalfnargtypes, false);
return finalfnoid;
Expand Down
2 changes: 1 addition & 1 deletion tsl/src/fdw/deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2973,7 +2973,7 @@ deparseAggref(Aggref *node, deparse_expr_cxt *context)

/* Find aggregate name from aggfnoid which is a pg_proc entry */
if (partial_agg)
appendStringInfoString(buf, INTERNAL_SCHEMA_NAME "." PARTIALIZE_FUNC_NAME "(");
appendStringInfoString(buf, FUNCTIONS_SCHEMA_NAME "." PARTIALIZE_FUNC_NAME "(");

appendFunctionName(node->aggfnoid, context);
appendStringInfoChar(buf, '(');
Expand Down
2 changes: 1 addition & 1 deletion tsl/src/partialize_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ collation_oid_from_name(char *schema_name, char *collation_name)
* partial state of numeric aggregates also changing format.
*
* If a user that has stored partials (by using Continuous Aggregates or calling
* _timescaledb_internal.finalize_agg()) upgrades to PG14 then the partial state deserialization
* _timescaledb_functions.finalize_agg()) upgrades to PG14 then the partial state deserialization
* will lead to errors due to the mismatch with the PG14 code.
*
* For F_NUMERIC_AVG_DESERIALIZE and F_NUMERIC_DESERIALIZE the length of the serialized aggregate
Expand Down
Loading

0 comments on commit 3b6dc7d

Please sign in to comment.