Skip to content

Commit 8a55513

Browse files
svenklemmtimescale-automation
authored andcommitted
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. (cherry picked from commit 5773544)
1 parent d11585d commit 8a55513

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

.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
@@ -2186,3 +2186,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21862186
-------------------
21872187
cagg_hypertab_ddl
21882188

2189+
-- TEST continuous aggregate with functionally dependent columns
2190+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2191+
CREATE table sensor(id int PRIMARY KEY, name text);
2192+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2193+
NOTICE: using column "time" as partitioning column
2194+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2195+
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;
2196+
SELECT * FROM cagg_sensordata;
2197+
id | name | bucket | avg
2198+
----+------+--------+-----
2199+

tsl/test/expected/cagg_ddl-16.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,3 +2186,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21862186
-------------------
21872187
cagg_hypertab_ddl
21882188

2189+
-- TEST continuous aggregate with functionally dependent columns
2190+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2191+
CREATE table sensor(id int PRIMARY KEY, name text);
2192+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2193+
NOTICE: using column "time" as partitioning column
2194+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2195+
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;
2196+
SELECT * FROM cagg_sensordata;
2197+
id | name | bucket | avg
2198+
----+------+--------+-----
2199+

tsl/test/expected/cagg_ddl-17.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,3 +2186,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21862186
-------------------
21872187
cagg_hypertab_ddl
21882188

2189+
-- TEST continuous aggregate with functionally dependent columns
2190+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2191+
CREATE table sensor(id int PRIMARY KEY, name text);
2192+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2193+
NOTICE: using column "time" as partitioning column
2194+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2195+
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;
2196+
SELECT * FROM cagg_sensordata;
2197+
id | name | bucket | avg
2198+
----+------+--------+-----
2199+

tsl/test/expected/cagg_ddl-18.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,3 +2186,14 @@ SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypert
21862186
-------------------
21872187
cagg_hypertab_ddl
21882188

2189+
-- TEST continuous aggregate with functionally dependent columns
2190+
-- name column depends on id which is in GROUP BY so name doesnt have to be
2191+
CREATE table sensor(id int PRIMARY KEY, name text);
2192+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
2193+
NOTICE: using column "time" as partitioning column
2194+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
2195+
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;
2196+
SELECT * FROM cagg_sensordata;
2197+
id | name | bucket | avg
2198+
----+------+--------+-----
2199+

tsl/test/sql/cagg_ddl.sql.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,3 +1492,12 @@ SELECT ddl_function();
14921492
SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypertable_name='hypertab_ddl';
14931493
SELECT ddl_function();
14941494
SELECT view_name from timescaledb_information.continuous_aggregates WHERE hypertable_name='hypertab_ddl';
1495+
1496+
-- TEST continuous aggregate with functionally dependent columns
1497+
-- name column depends on id which is in GROUP BY so name doesnt have to be
1498+
CREATE table sensor(id int PRIMARY KEY, name text);
1499+
CREATE TABLE sensordata(time timestamptz,id int, value float) WITH (timescaledb.hypertable);
1500+
CREATE MATERIALIZED VIEW cagg_sensordata WITH (tsdb.continuous) AS
1501+
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;
1502+
1503+
SELECT * FROM cagg_sensordata;

0 commit comments

Comments
 (0)