Skip to content

Commit 5773544

Browse files
committed
Fix segfault in continuous aggregate creation on PG18
When a continuous aggregate query had functionally dependant columns that were not part of the GROUP BY clause the create statement would segfault.
1 parent 7c15939 commit 5773544

7 files changed

Lines changed: 71 additions & 10 deletions

File tree

.unreleased/pr_9303

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #9303 Fix segfault in continuous aggregate creation on PG18

tsl/src/continuous_aggs/finalize.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,27 @@ mattablecolumninfo_addentry(MaterializationHypertableColumnInfo *out, Node *inpu
257257
Var *var = castNode(Var, tle->expr);
258258
Assert((int) var->varno <= list_length(rtable));
259259
RangeTblEntry *rte = list_nth(rtable, var->varno - 1);
260-
Assert(rte->rtekind == RTE_GROUP);
261260
Assert(var->varattno > 0);
262-
Node *node = list_nth(rte->groupexprs, var->varattno - 1);
263-
if (IsA(node, FuncExpr))
261+
/*
262+
* Var might not be part of the GROUP BY clause
263+
* eg for functionally dependent columns on tables with primary key
264+
*/
265+
if (rte->rtekind == RTE_GROUP)
264266
{
265-
if (contain_mutable_functions(node))
267+
Node *node = list_nth(rte->groupexprs, var->varattno - 1);
268+
if (IsA(node, FuncExpr))
266269
{
267-
ereport(WARNING,
268-
(errmsg("using non-immutable functions in continuous aggregate "
269-
"view may lead to "
270-
"inconsistent results on rematerialization")));
270+
if (contain_mutable_functions(node))
271+
{
272+
ereport(WARNING,
273+
(errmsg("using non-immutable functions in continuous aggregate "
274+
"view may lead to "
275+
"inconsistent results on rematerialization")));
276+
}
277+
FuncExpr *expr = (FuncExpr *) node;
278+
timebkt_chk =
279+
function_allowed_in_cagg_definition(((FuncExpr *) expr)->funcid);
271280
}
272-
FuncExpr *expr = (FuncExpr *) node;
273-
timebkt_chk = function_allowed_in_cagg_definition(((FuncExpr *) expr)->funcid);
274281
}
275282
}
276283
#endif

tsl/test/expected/cagg_ddl-15.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,3 +2181,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21812181
-------------------
21822182
cagg_hypertab_ddl
21832183

2184+
-- TEST continuous aggregate with functionally dependent columns
2185+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2186+
CREATE table sensor(id int PRIMARY KEY, name text);
2187+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2188+
NOTICE: using column "time" as partitioning column
2189+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2190+
SELECT s.id, s.name,time_bucket('15 minutes', sd.time) as bucket, avg(sd.value) FROM sensordata sd JOIN sensor s USING(id) GROUP BY s.id, bucket WITH NO DATA;
2191+
SELECT * FROM cagg_sensordata;
2192+
id | name | bucket | avg
2193+
----+------+--------+-----
2194+

tsl/test/expected/cagg_ddl-16.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,3 +2181,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21812181
-------------------
21822182
cagg_hypertab_ddl
21832183

2184+
-- TEST continuous aggregate with functionally dependent columns
2185+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2186+
CREATE table sensor(id int PRIMARY KEY, name text);
2187+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2188+
NOTICE: using column "time" as partitioning column
2189+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2190+
SELECT s.id, s.name,time_bucket('15 minutes', sd.time) as bucket, avg(sd.value) FROM sensordata sd JOIN sensor s USING(id) GROUP BY s.id, bucket WITH NO DATA;
2191+
SELECT * FROM cagg_sensordata;
2192+
id | name | bucket | avg
2193+
----+------+--------+-----
2194+

tsl/test/expected/cagg_ddl-17.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,3 +2181,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21812181
-------------------
21822182
cagg_hypertab_ddl
21832183

2184+
-- TEST continuous aggregate with functionally dependent columns
2185+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2186+
CREATE table sensor(id int PRIMARY KEY, name text);
2187+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2188+
NOTICE: using column "time" as partitioning column
2189+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2190+
SELECT s.id, s.name,time_bucket('15 minutes', sd.time) as bucket, avg(sd.value) FROM sensordata sd JOIN sensor s USING(id) GROUP BY s.id, bucket WITH NO DATA;
2191+
SELECT * FROM cagg_sensordata;
2192+
id | name | bucket | avg
2193+
----+------+--------+-----
2194+

tsl/test/expected/cagg_ddl-18.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,3 +2181,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21812181
-------------------
21822182
cagg_hypertab_ddl
21832183

2184+
-- TEST continuous aggregate with functionally dependent columns
2185+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2186+
CREATE table sensor(id int PRIMARY KEY, name text);
2187+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2188+
NOTICE: using column "time" as partitioning column
2189+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2190+
SELECT s.id, s.name,time_bucket('15 minutes', sd.time) as bucket, avg(sd.value) FROM sensordata sd JOIN sensor s USING(id) GROUP BY s.id, bucket WITH NO DATA;
2191+
SELECT * FROM cagg_sensordata;
2192+
id | name | bucket | avg
2193+
----+------+--------+-----
2194+

tsl/test/sql/cagg_ddl.sql.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,3 +1490,12 @@ SELECT ddl_function();
14901490
SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypertable_name='hypertab_ddl';
14911491
SELECT ddl_function();
14921492
SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypertable_name='hypertab_ddl';
1493+
1494+
-- TEST continuous aggregate with functionally dependent columns
1495+
-- name column depends on id which is in GROUP BY so name doesnt have to be
1496+
CREATE table sensor(id int PRIMARY KEY, name text);
1497+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
1498+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
1499+
SELECT s.id, s.name,time_bucket('15 minutes', sd.time) as bucket, avg(sd.value) FROM sensordata sd JOIN sensor s USING(id) GROUP BY s.id, bucket WITH NO DATA;
1500+
1501+
SELECT * FROM cagg_sensordata;

0 commit comments

Comments
 (0)