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

sql: PL/pgSQL routine prematurely exits when SELECT ... INTO statements produce zero rows #114826

Closed
mgartner opened this issue Nov 21, 2023 · 2 comments · Fixed by #115650
Closed
Assignees
Labels
branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. GA-blocker O-qa P-1 Issues/test failures with a fix SLA of 1 month S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. T-sql-queries SQL Queries Team

Comments

@mgartner
Copy link
Collaborator

mgartner commented Nov 21, 2023

See the example below.

CREATE TABLE t (a INT);

CREATE FUNCTION f() RETURNS INT AS $$
  DECLARE
    found INT;
  BEGIN
    RAISE NOTICE 'HERE 1';
    SELECT a INTO found FROM t WHERE a = 3;

    -- The function exits here and returns NULL when the SELECT
    -- above does not find any rows.

    RAISE NOTICE 'HERE 2';
    RETURN 10;
  END
$$ LANGUAGE PLpgSQL;

-- ‼️ The function incorrectly exits early when no rows match the
-- SELECT ... INTO query.
SELECT f();
-- NOTICE: HERE 1
--    f
-- --------
--   NULL

INSERT INTO t VALUES (3);

-- After inserting a matching row, the function executes correctly.
SELECT f();
-- NOTICE: HERE 1
-- NOTICE: HERE 2
--   f
-- ------
--   10

Jira issue: CRDB-33702

@mgartner mgartner added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. labels Nov 21, 2023
@mgartner
Copy link
Collaborator Author

I think this may be a GA blocker. I've added the label.

Copy link

blathers-crl bot commented Nov 21, 2023

Hi @mgartner, please add branch-* labels to identify which branch(es) this release-blocker affects.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@mgartner mgartner added the branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 label Nov 21, 2023
@yuzefovich yuzefovich added the T-sql-queries SQL Queries Team label Nov 21, 2023
@yuzefovich yuzefovich assigned yuzefovich and unassigned yuzefovich Nov 22, 2023
@mgartner mgartner added the P-1 Issues/test failures with a fix SLA of 1 month label Nov 22, 2023
@DrewKimball DrewKimball self-assigned this Nov 28, 2023
@mgartner mgartner changed the title sql: PL/pgSQL routine prematurely exit when SELECT ... INTO statements produce zero rows sql: PL/pgSQL routine prematurely exits when SELECT ... INTO statements produce zero rows Nov 28, 2023
DrewKimball added a commit to DrewKimball/cockroach that referenced this issue Dec 5, 2023
Previously, a SELECT INTO statement that returned zero rows wouldn't
call the continuation, since the continuation is called as a projection.
This patch adds a RIGHT JOIN with a zero-column one-row Values operator
to ensure that SELECT INTO statements are correctly null-extended when
the underlying SQL statement returns no rows. See the postgres docs:
```
https://www.postgresql.org/docs/16/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
```

Fixes cockroachdb#114826

Release note (bug fix): Fixed a bug that existed only in pre-release
versions v23.2.0-beta.1 and v23.2.0-beta.2 which could cause PLpgSQL
routines with SELECT INTO syntax to return early.
craig bot pushed a commit that referenced this issue Dec 6, 2023
115650: plpgsql: don't exit early when SELECT INTO returns no rows r=DrewKimball a=DrewKimball

Previously, a SELECT INTO statement that returned zero rows wouldn't call the continuation, since the continuation is called as a projection. This patch adds a RIGHT JOIN with a zero-column one-row Values operator to ensure that SELECT INTO statements are correctly null-extended when the underlying SQL statement returns no rows. See the postgres docs:
```
https://www.postgresql.org/docs/16/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
```

Fixes #114826

Release note (bug fix): Fixed a bug that existed only in pre-release versions v23.2.0-beta.1 and v23.2.0-beta.2 which could cause PLpgSQL routines with SELECT INTO syntax to return early.

Co-authored-by: Drew Kimball <drewk@cockroachlabs.com>
@craig craig bot closed this as completed in f83b13a Dec 6, 2023
blathers-crl bot pushed a commit that referenced this issue Dec 6, 2023
Previously, a SELECT INTO statement that returned zero rows wouldn't
call the continuation, since the continuation is called as a projection.
This patch adds a RIGHT JOIN with a zero-column one-row Values operator
to ensure that SELECT INTO statements are correctly null-extended when
the underlying SQL statement returns no rows. See the postgres docs:
```
https://www.postgresql.org/docs/16/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
```

Fixes #114826

Release note (bug fix): Fixed a bug that existed only in pre-release
versions v23.2.0-beta.1 and v23.2.0-beta.2 which could cause PLpgSQL
routines with SELECT INTO syntax to return early.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. GA-blocker O-qa P-1 Issues/test failures with a fix SLA of 1 month S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants