Skip to content

Commit

Permalink
Segfault when executing IMMUTABLE functions
Browse files Browse the repository at this point in the history
Executing an IMMUTABLE function that has parameters and exception
handling block multiple times in the same transaction causes a null
pointer segfault when try to reset a non-initialized ts_baserel_info.

Fixed it by preventing to reset a non-initialized `ts_baserel_info`.

Fixes #4489
  • Loading branch information
fabriziomello committed Jul 5, 2022
1 parent a608d7d commit 68e33d7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ accidentally triggering the load of a previous DB version.**

**Thanks**
@nikugogoi for reporting a bug with CTEs and upserts on distributed hypertables
@jflambert for reporting a bug with IMMUTABLE functions

## 2.7.0 (2022-05-24)

Expand Down
2 changes: 1 addition & 1 deletion src/planner/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ timescaledb_planner(Query *parse, int cursor_opts, ParamListInfo bound_params)
ts_data_node_fetcher_scan_type = AutoFetcherType;
}

if (reset_baserel_info)
if (reset_baserel_info && ts_baserel_info)
{
BaserelInfo_destroy(ts_baserel_info);
ts_baserel_info = NULL;
Expand Down
31 changes: 31 additions & 0 deletions test/expected/ddl-12.out
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,34 @@ SELECT create_hypertable('part_custom_dim', 'time', 'combo', 4);
NOTICE: adding not-null constraint to column "time"
ERROR: could not find hash function for type tuple
\set ON_ERROR_STOP 1
-- immutable functions with sub-transaction (issue #4489)
CREATE FUNCTION i4489(value TEXT DEFAULT '') RETURNS INTEGER
AS
$$
BEGIN
RETURN value::INTEGER;
EXCEPTION WHEN invalid_text_representation THEN
RETURN 0;
END;
$$
LANGUAGE PLPGSQL IMMUTABLE;
-- should return 1 (one) in both cases
SELECT i4489('1'), i4489('1');
i4489 | i4489
-------+-------
1 | 1
(1 row)

-- should return 0 (zero) in all cases handled by the exception
SELECT i4489(), i4489();
i4489 | i4489
-------+-------
0 | 0
(1 row)

SELECT i4489('a'), i4489('a');
i4489 | i4489
-------+-------
0 | 0
(1 row)

31 changes: 31 additions & 0 deletions test/expected/ddl-13.out
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,34 @@ SELECT create_hypertable('part_custom_dim', 'time', 'combo', 4);
NOTICE: adding not-null constraint to column "time"
ERROR: could not find hash function for type tuple
\set ON_ERROR_STOP 1
-- immutable functions with sub-transaction (issue #4489)
CREATE FUNCTION i4489(value TEXT DEFAULT '') RETURNS INTEGER
AS
$$
BEGIN
RETURN value::INTEGER;
EXCEPTION WHEN invalid_text_representation THEN
RETURN 0;
END;
$$
LANGUAGE PLPGSQL IMMUTABLE;
-- should return 1 (one) in both cases
SELECT i4489('1'), i4489('1');
i4489 | i4489
-------+-------
1 | 1
(1 row)

-- should return 0 (zero) in all cases handled by the exception
SELECT i4489(), i4489();
i4489 | i4489
-------+-------
0 | 0
(1 row)

SELECT i4489('a'), i4489('a');
i4489 | i4489
-------+-------
0 | 0
(1 row)

31 changes: 31 additions & 0 deletions test/expected/ddl-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,34 @@ NOTICE: adding not-null constraint to column "time"
(1 row)

\set ON_ERROR_STOP 1
-- immutable functions with sub-transaction (issue #4489)
CREATE FUNCTION i4489(value TEXT DEFAULT '') RETURNS INTEGER
AS
$$
BEGIN
RETURN value::INTEGER;
EXCEPTION WHEN invalid_text_representation THEN
RETURN 0;
END;
$$
LANGUAGE PLPGSQL IMMUTABLE;
-- should return 1 (one) in both cases
SELECT i4489('1'), i4489('1');
i4489 | i4489
-------+-------
1 | 1
(1 row)

-- should return 0 (zero) in all cases handled by the exception
SELECT i4489(), i4489();
i4489 | i4489
-------+-------
0 | 0
(1 row)

SELECT i4489('a'), i4489('a');
i4489 | i4489
-------+-------
0 | 0
(1 row)

18 changes: 18 additions & 0 deletions test/sql/ddl.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ CREATE TABLE part_custom_dim (time TIMESTAMPTZ, combo TUPLE, device TEXT);
-- on PG14 custom types are hashable
SELECT create_hypertable('part_custom_dim', 'time', 'combo', 4);
\set ON_ERROR_STOP 1

-- immutable functions with sub-transaction (issue #4489)
CREATE FUNCTION i4489(value TEXT DEFAULT '') RETURNS INTEGER
AS
$$
BEGIN
RETURN value::INTEGER;
EXCEPTION WHEN invalid_text_representation THEN
RETURN 0;
END;
$$
LANGUAGE PLPGSQL IMMUTABLE;

-- should return 1 (one) in both cases
SELECT i4489('1'), i4489('1');
-- should return 0 (zero) in all cases handled by the exception
SELECT i4489(), i4489();
SELECT i4489('a'), i4489('a');

0 comments on commit 68e33d7

Please sign in to comment.