cockroachdb / cockroachdb/cockroach

plpgsql: allow assignment via SELECT INTO to element of composite variable

Open
#140,076 1 comment 0 reactions 1 assignee Claimed by @DrewKimball View on GitHub
A-sql-plpgsql A-sql-routine branch-release-25.1 C-enhancement O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Consider the following trigger definition:

```CREATE OR REPLACE FUNCTION update_cte_trigger()
RETURNS TRIGGER AS $$
BEGIN
WITH modify_cte AS (
SELECT (NEW).value * 3 AS new_value
)
SELECT new_value INTO (NEW).value FROM modify_cte;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_modify_update
BEFORE UPDATE ON test_table
FOR EACH ROW EXECUTE FUNCTION update_cte_trigger();
```

CockroachDB does not like this:

```
ERROR: at or near ";": syntax error: "(" is not a scalar variable
SQLSTATE: 42601
DETAIL: source SQL:
BEGIN
WITH modify_cte AS (
SELECT (NEW).value * 3 AS new_value
)
SELECT new_value INTO (NEW).value FROM modify_cte;
^
ERROR: unknown function: update_cte_trigger()
SQLSTATE: 42883
```

Whereas Postgres thinks its fine:

```
matt=# CREATE OR REPLACE FUNCTION update_cte_trigger()
RETURNS TRIGGER AS $$
BEGIN
WITH modify_cte AS (
SELECT NEW.value * 3 AS new_value
)
SELECT new_value INTO NEW.value FROM modify_cte;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_modify_update
BEFORE UPDATE ON test_table
FOR EACH ROW EXECUTE FUNCTION update_cte_trigger();
CREATE FUNCTION
CREATE TRIGGER
matt=# UPDATE test_table SET value = 100 WHERE rowid = 1;
UPDATE 1
matt=# select * from test_table;
rowid | value
-------+-------
1 | 300
(1 row)
```

We should match Postgres here.

Jira issue: CRDB-46990

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.