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

Segfault when executing IMMUTABLE functions #4493

Merged
merged 1 commit into from
Jul 5, 2022
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
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');