Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing syntax to use MATERIALIZED VIEW for continuous aggregates #2243

Merged
merged 1 commit into from Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions sql/updates/post-update.sql
Expand Up @@ -3,12 +3,24 @@ DO $$
DECLARE
vname regclass;
altercmd text;
ts_version TEXT;
BEGIN
SELECT extversion INTO ts_version FROM pg_extension WHERE extname = 'timescaledb';

FOR vname IN select format('%I.%I', user_view_schema, user_view_name)::regclass from _timescaledb_catalog.continuous_agg where materialized_only = false
LOOP
-- the cast from oid to text returns quote_qualified_identifier
-- (see regclassout)
altercmd := format('ALTER VIEW %s SET (timescaledb.materialized_only=false) ', vname::text);
-- the cast from oid to text returns
-- quote_qualified_identifier (see regclassout).
--
-- We use the if statement to handle pre-2.0 as well as
-- post-2.0. This could be turned into a procedure if we want
-- to have something more generic, but right now it is just
-- this case.
IF ts_version < '2.0.0' THEN
altercmd := format('ALTER VIEW %s SET (timescaledb.materialized_only=false) ', vname::text);
ELSE
altercmd := format('ALTER MATERIALIZED VIEW %s SET (timescaledb.materialized_only=false) ', vname::text);
END IF;
RAISE INFO 'cmd executed: %', altercmd;
EXECUTE altercmd;
END LOOP;
Expand Down
35 changes: 33 additions & 2 deletions src/continuous_agg.c
Expand Up @@ -514,6 +514,21 @@ ts_continuous_agg_find_by_relid(Oid relid)
return ts_continuous_agg_find_userview_name(schemaname, relname);
}

/*
* Find a continuous aggregate by range var.
*/
ContinuousAgg *
ts_continuous_agg_find_by_rv(const RangeVar *rv)
{
Oid relid;
if (rv == NULL)
return NULL;
relid = RangeVarGetRelid(rv, NoLock, true);
if (!OidIsValid(relid))
return NULL;
return ts_continuous_agg_find_by_relid(relid);
}

/*
* Drops continuous aggs and all related objects.
*
Expand Down Expand Up @@ -834,11 +849,14 @@ ts_continuous_agg_rename_schema_name(char *old_schema, char *new_schema)
}

extern void
ts_continuous_agg_rename_view(char *old_schema, char *name, char *new_schema, char *new_name)
ts_continuous_agg_rename_view(const char *old_schema, const char *name, const char *new_schema,
const char *new_name, ObjectType *object_type)
{
ScanIterator iterator =
ts_scan_iterator_create(CONTINUOUS_AGG, RowExclusiveLock, CurrentMemoryContext);

Assert(object_type);

ts_scanner_foreach(&iterator)
{
TupleInfo *tinfo = ts_scan_iterator_tuple_info(&iterator);
Expand All @@ -847,11 +865,24 @@ ts_continuous_agg_rename_view(char *old_schema, char *name, char *new_schema, ch
FormData_continuous_agg *data = (FormData_continuous_agg *) GETSTRUCT(tuple);
HeapTuple new_tuple = NULL;
ContinuousAggViewType vtyp = ts_continuous_agg_view_type(data, old_schema, name);

switch (vtyp)
{
case ContinuousAggUserView:
{
FormData_continuous_agg *new_data = ensure_new_tuple(tuple, &new_tuple);
FormData_continuous_agg *new_data;

if (*object_type == OBJECT_VIEW)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot alter continuous aggregate using ALTER VIEW"),
errhint(
"Use ALTER MATERIALIZED VIEW to alter a continuous aggregate.")));

Assert(*object_type == OBJECT_MATVIEW);
*object_type = OBJECT_VIEW;

new_data = ensure_new_tuple(tuple, &new_tuple);
namestrcpy(&new_data->user_view_schema, new_schema);
namestrcpy(&new_data->user_view_name, new_name);
break;
Expand Down
6 changes: 4 additions & 2 deletions src/continuous_agg.h
Expand Up @@ -77,6 +77,7 @@ int64 ts_continuous_agg_get_completed_threshold(int32 materialization_id);
extern TSDLLEXPORT ContinuousAgg *ts_continuous_agg_find_by_view_name(const char *schema,
const char *name);
extern TSDLLEXPORT ContinuousAgg *ts_continuous_agg_find_by_relid(Oid relid);
extern TSDLLEXPORT ContinuousAgg *ts_continuous_agg_find_by_rv(const RangeVar *rv);

extern void ts_continuous_agg_drop_view_callback(ContinuousAgg *ca, const char *schema,
const char *name);
Expand All @@ -87,8 +88,9 @@ extern TSDLLEXPORT ContinuousAggViewType ts_continuous_agg_view_type(FormData_co
const char *schema,
const char *name);
extern void ts_continuous_agg_rename_schema_name(char *old_schema, char *new_schema);
extern void ts_continuous_agg_rename_view(char *old_schema, char *name, char *new_schema,
char *new_name);
extern void ts_continuous_agg_rename_view(const char *old_schema, const char *name,
const char *new_schema, const char *new_name,
ObjectType *object_type);

extern TSDLLEXPORT int32 ts_number_of_continuous_aggs(void);

Expand Down
2 changes: 1 addition & 1 deletion src/cross_module_fn.c
Expand Up @@ -245,7 +245,7 @@ error_no_default_fn_pg_enterprise(PG_FUNCTION_ARGS)
}

static DDLResult
process_cagg_viewstmt_default(ViewStmt *stmt, const char *query_string, void *pstmt,
process_cagg_viewstmt_default(Node *stmt, const char *query_string, void *pstmt,
WithClauseResult *with_clause_options)
{
return error_no_default_fn_bool_void_community();
Expand Down
2 changes: 1 addition & 1 deletion src/cross_module_fn.h
Expand Up @@ -88,7 +88,7 @@ typedef struct CrossModuleFunctions
PGFunction partialize_agg;
PGFunction finalize_agg_sfunc;
PGFunction finalize_agg_ffunc;
DDLResult (*process_cagg_viewstmt)(ViewStmt *stmt, const char *query_string, void *pstmt,
DDLResult (*process_cagg_viewstmt)(Node *stmt, const char *query_string, void *pstmt,
WithClauseResult *with_clause_options);
PGFunction continuous_agg_invalidation_trigger;
PGFunction continuous_agg_refresh;
Expand Down
2 changes: 2 additions & 0 deletions src/hypertable.c
Expand Up @@ -2038,8 +2038,10 @@ ts_hypertable_create_from_info(Oid table_relid, int32 hypertable_id, uint32 flag
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("table \"%s\" is already partitioned", get_rel_name(table_relid)),
errdetail("It is not possible to turn partitioned tables into hypertables.")));
case RELKIND_MATVIEW:
case RELKIND_RELATION:
break;

default:
ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("invalid relation type")));
}
Expand Down