PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

Поиск
Список
Период
Сортировка
Искать

PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:
Вложения:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.

Thanks

[1] https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQL-EXTRA-CHECKS

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Pavel Stehule <pavel.stehule@gmail.com>
Дата:
Hi

pá 30. 10. 2026 v 12:43 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.


Yes, this is my omission.

The dynamic SQL is more problematic - it allows multiquery 

(2026-10-30 14:24:06) postgres=# do $$
declare r record;
begin
  execute 'select 1 union select 2; select 2' into strict r;
  raise notice '%', r;
end;
$$;
NOTICE:  (2)
DO

Maybe extra errors should handle this case too.

regards

Pavel


 

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:


On Fri, Oct 30, 2026 at 6:57 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:
Hi

pá 30. 10. 2026 v 12:43 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.


Yes, this is my omission.

The dynamic SQL is more problematic - it allows multiquery 

(2026-10-30 14:24:06) postgres=# do $$
declare r record;
begin
  execute 'select 1 union select 2; select 2' into strict r;
  raise notice '%', r;
end;
$$;
NOTICE:  (2)
DO

Maybe extra errors should handle this case too.

Hi Pavel,

Thanks for looking at this.

I confirmed the multi-statement case.  With INTO, only the result of the last statement is used, so even STRICT passes for:

    do $$
    declare r record;
    begin
      execute 'select 1 union select 2; select 2' into strict r;
      raise notice '%', r;
    end;
    $$;
    NOTICE:  (2)

That behavior is independent of this patch, which counts the rows of the statement whose result is actually used, so I'd prefer to keep this patch scoped to that.

Seems like a separate feature altogether.

Detecting multiple statements would need a different check, since exec_stmt_dynexecute() currently hands the query string straight to SPI_execute_extended() and has no way to see how many statements it contained.

Thanks
 

regards

Pavel


 

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Pavel Stehule <pavel.stehule@gmail.com>
Дата:


po 2. 11. 2026 v 12:35 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:


On Fri, Oct 30, 2026 at 6:57 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:
Hi

pá 30. 10. 2026 v 12:43 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.


Yes, this is my omission.

The dynamic SQL is more problematic - it allows multiquery 

(2026-10-30 14:24:06) postgres=# do $$
declare r record;
begin
  execute 'select 1 union select 2; select 2' into strict r;
  raise notice '%', r;
end;
$$;
NOTICE:  (2)
DO

Maybe extra errors should handle this case too.

Hi Pavel,

Thanks for looking at this.

I confirmed the multi-statement case.  With INTO, only the result of the last statement is used, so even STRICT passes for:

    do $$
    declare r record;
    begin
      execute 'select 1 union select 2; select 2' into strict r;
      raise notice '%', r;
    end;
    $$;
    NOTICE:  (2)

That behavior is independent of this patch, which counts the rows of the statement whose result is actually used, so I'd prefer to keep this patch scoped to that.

Seems like a separate feature altogether.

Detecting multiple statements would need a different check, since exec_stmt_dynexecute() currently hands the query string straight to SPI_execute_extended() and has no way to see how many statements it contained.

Yes, it is hard to say what is expected behaviour in the mentioned case.

Maybe it should be documented, so this case is not covered by checks.

Regards

Pavel


Thanks
 

regards

Pavel


 

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:
Вложения:


On Mon, Nov 2, 2026 at 5:23 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:


po 2. 11. 2026 v 12:35 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:


On Fri, Oct 30, 2026 at 6:57 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:
Hi

pá 30. 10. 2026 v 12:43 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.


Yes, this is my omission.

The dynamic SQL is more problematic - it allows multiquery 

(2026-10-30 14:24:06) postgres=# do $$
declare r record;
begin
  execute 'select 1 union select 2; select 2' into strict r;
  raise notice '%', r;
end;
$$;
NOTICE:  (2)
DO

Maybe extra errors should handle this case too.

Hi Pavel,

Thanks for looking at this.

I confirmed the multi-statement case.  With INTO, only the result of the last statement is used, so even STRICT passes for:

    do $$
    declare r record;
    begin
      execute 'select 1 union select 2; select 2' into strict r;
      raise notice '%', r;
    end;
    $$;
    NOTICE:  (2)

That behavior is independent of this patch, which counts the rows of the statement whose result is actually used, so I'd prefer to keep this patch scoped to that.

Seems like a separate feature altogether.

Detecting multiple statements would need a different check, since exec_stmt_dynexecute() currently hands the query string straight to SPI_execute_extended() and has no way to see how many statements it contained.

Yes, it is hard to say what is expected behaviour in the mentioned case.

Maybe it should be documented, so this case is not covered by checks.

Hi Pavel,

Thanks for the follow-up.

I agree that it is hard to say what the expected behavior is for that case, so documenting it seems like the right thing to do for now.

Attached is v2, which is v1 plus a short paragraph in the EXECUTE section of plpgsql.sgml saying that when the command string contains more than one SQL command, only the result of the last one is assigned to the INTO variable(s), and that STRICT and the too_many_rows extra check likewise look only at that result.

There are no code changes compared to v1.

Thanks
 

Regards

Pavel


Thanks
 

regards

Pavel


 

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Pavel Stehule <pavel.stehule@gmail.com>
Дата:
Hi

po 2. 11. 2026 v 15:21 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:


On Mon, Nov 2, 2026 at 5:23 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:


po 2. 11. 2026 v 12:35 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:


On Fri, Oct 30, 2026 at 6:57 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:
Hi

pá 30. 10. 2026 v 12:43 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.


Yes, this is my omission.

The dynamic SQL is more problematic - it allows multiquery 

(2026-10-30 14:24:06) postgres=# do $$
declare r record;
begin
  execute 'select 1 union select 2; select 2' into strict r;
  raise notice '%', r;
end;
$$;
NOTICE:  (2)
DO

Maybe extra errors should handle this case too.

Hi Pavel,

Thanks for looking at this.

I confirmed the multi-statement case.  With INTO, only the result of the last statement is used, so even STRICT passes for:

    do $$
    declare r record;
    begin
      execute 'select 1 union select 2; select 2' into strict r;
      raise notice '%', r;
    end;
    $$;
    NOTICE:  (2)

That behavior is independent of this patch, which counts the rows of the statement whose result is actually used, so I'd prefer to keep this patch scoped to that.

Seems like a separate feature altogether.

Detecting multiple statements would need a different check, since exec_stmt_dynexecute() currently hands the query string straight to SPI_execute_extended() and has no way to see how many statements it contained.

Yes, it is hard to say what is expected behaviour in the mentioned case.

Maybe it should be documented, so this case is not covered by checks.

Hi Pavel,

Thanks for the follow-up.

I agree that it is hard to say what the expected behavior is for that case, so documenting it seems like the right thing to do for now.

Attached is v2, which is v1 plus a short paragraph in the EXECUTE section of plpgsql.sgml saying that when the command string contains more than one SQL command, only the result of the last one is assigned to the INTO variable(s), and that STRICT and the too_many_rows extra check likewise look only at that result.

There are no code changes compared to v1.

+1

I tested this patch, and I don't see any issue

all tests passed

I'll mark this patch as ready for committer

Regards

Pavel
 

Thanks
 

Regards

Pavel


Thanks
 

regards

Pavel


 

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:


On Tue, Nov 3, 2026 at 12:16 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:
Hi

po 2. 11. 2026 v 15:21 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:


On Mon, Nov 2, 2026 at 5:23 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:


po 2. 11. 2026 v 12:35 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:


On Fri, Oct 30, 2026 at 6:57 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:
Hi

pá 30. 10. 2026 v 12:43 odesílatel Jeevan Chalke <jeevan.chalke@enterprisedb.com> napsal:
Hi,

The documentation for plpgsql.extra_warnings / plpgsql.extra_errors says that the too_many_rows check will "check if a given query returns more than one row when an INTO clause is used" [1].  Only static SQL statements implement this (exec_stmt_execsql()).  exec_stmt_dynexecute() only looks at STRICT, so EXECUTE ... INTO silently uses the first row and nothing is reported:

    set plpgsql.extra_warnings = 'too_many_rows';
    do $$ declare x int;
    begin
      execute 'select g from generate_series(1,2) g' into x;
    end $$;
    DO

The same statement written as static SQL does warn (or fail, with extra_errors).

The attached patch computes too_many_rows_level in exec_stmt_dynexecute() the same way exec_stmt_execsql() does, and reports at that level when more than one row comes back and STRICT was not specified.  With it:

    WARNING:  query returned more than one row
    HINT:  Make sure the query returns a single row, or use LIMIT 1.
    DO

and with plpgsql.extra_errors the same message is raised as an ERROR. Checking costs nothing extra here, since EXECUTE already fetches all the rows (no row limit is passed to SPI).

While there, I added the "Make sure the query returns a single row, or use LIMIT 1." hint to the error raised for EXECUTE ... INTO STRICT, so that it matches the error for static SQL.  This changes the expected output of three existing STRICT tests, which now show the HINT line.

The new tests are in the "runtime extra checks" section of plpgsql.sql and cover EXECUTE ... INTO with both extra_warnings and extra_errors.

This is a behavior change: someone who has too_many_rows in extra_errors may start getting errors from EXECUTE ... INTO statements that used to pass.  So I think this should go to master only (or v19, as it is yet to release), but I'd like to hear what others think.


Yes, this is my omission.

The dynamic SQL is more problematic - it allows multiquery 

(2026-10-30 14:24:06) postgres=# do $$
declare r record;
begin
  execute 'select 1 union select 2; select 2' into strict r;
  raise notice '%', r;
end;
$$;
NOTICE:  (2)
DO

Maybe extra errors should handle this case too.

Hi Pavel,

Thanks for looking at this.

I confirmed the multi-statement case.  With INTO, only the result of the last statement is used, so even STRICT passes for:

    do $$
    declare r record;
    begin
      execute 'select 1 union select 2; select 2' into strict r;
      raise notice '%', r;
    end;
    $$;
    NOTICE:  (2)

That behavior is independent of this patch, which counts the rows of the statement whose result is actually used, so I'd prefer to keep this patch scoped to that.

Seems like a separate feature altogether.

Detecting multiple statements would need a different check, since exec_stmt_dynexecute() currently hands the query string straight to SPI_execute_extended() and has no way to see how many statements it contained.

Yes, it is hard to say what is expected behaviour in the mentioned case.

Maybe it should be documented, so this case is not covered by checks.

Hi Pavel,

Thanks for the follow-up.

I agree that it is hard to say what the expected behavior is for that case, so documenting it seems like the right thing to do for now.

Attached is v2, which is v1 plus a short paragraph in the EXECUTE section of plpgsql.sgml saying that when the command string contains more than one SQL command, only the result of the last one is assigned to the INTO variable(s), and that STRICT and the too_many_rows extra check likewise look only at that result.

There are no code changes compared to v1.

+1

I tested this patch, and I don't see any issue

all tests passed

I'll mark this patch as ready for committer

Thanks for the review, Pavel, and for marking it Ready for Committer.
 

Regards

Pavel
 

Thanks
 

Regards

Pavel


Thanks
 

regards

Pavel


 

--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Tue, Nov 3, 2026 at 4:27 PM Jeevan Chalke
 wrote:
> Thanks for the review, Pavel, and for marking it Ready for Committer.

Since the patch was marked Ready for Committer, I've just started reading it.

+     If the command string contains more than one SQL command, only the
+     result of the last one is assigned to the INTO
+     variable(s); the results of the earlier commands are discarded.

This does not seem to be always true. If the last command is an INSERT with
an unconditional DO INSTEAD NOTHING rule, the result of the preceding
SELECT is used instead. Is that right?

Also, since this describes existing behavior, isn't it better to leave this
paragraph out of the patch and address it separately?

> +                         errhint("Make sure the query returns a single row, or use LIMIT 1.")));

LIMIT 1 is not applicable to every command, such as UPDATE ... RETURNING.
So,perhaps it's better to remove "or use LIMIT 1" from the hint? But, since
the same issue seems to exist for static SQL, that might need to be addressed
in a separate patch anyway.

Regards,

-- 
Fujii Masao


Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:
Вложения:
Hi Fujii-san,

Thank you for the review.

On Tue, Nov 3, 2026 at 2:09 PM Fujii Masao <masao.fujii@gmail.com> wrote:
On Tue, Nov 3, 2026 at 4:27 PM Jeevan Chalke
<jeevan.chalke@enterprisedb.com> wrote:
> Thanks for the review, Pavel, and for marking it Ready for Committer.

Since the patch was marked Ready for Committer, I've just started reading it.

+     If the command string contains more than one SQL command, only the
+     result of the last one is assigned to the <literal>INTO</literal>
+     variable(s); the results of the earlier commands are discarded.

This does not seem to be always true. If the last command is an INSERT with
an unconditional DO INSTEAD NOTHING rule, the result of the preceding
SELECT is used instead. Is that right?

Yes, you are right.  I tried it, and with such a rule
     execute 'select 42; insert into u values (1)' into x;
assigns 42 to x, so the paragraph I added was not accurate in general.  (If the last command returns no data at all and there is no such rule, we instead get "INTO used with a command that cannot return data".)
 

Also, since this describes existing behavior, isn't it better to leave this
paragraph out of the patch and address it separately?

Agreed.  I have removed it from 0001, and 0001 is now the same as v1 (code and tests).  I have put a reworded version in a separate patch, 0003, in case you and Pavel still think it is worth documenting. 

It now says that the results of all but the last command are discarded and only the last command's result is assigned to the INTO variable(s), and it mentions your DO INSTEAD NOTHING case.  If you think this is not needed, please feel free to ignore 0003.
 

> +                         errhint("Make sure the query returns a single row, or use LIMIT 1.")));

LIMIT 1 is not applicable to every command, such as UPDATE ... RETURNING.
So,perhaps it's better to remove "or use LIMIT 1" from the hint? But, since
the same issue seems to exist for static SQL, that might need to be addressed
in a separate patch anyway.

Right, the hint here is the same text static SQL uses, so I kept them consistent in 0001.  Since the issue exists in both places, I have done what you suggested as a separate patch: 0002 removes "or use LIMIT 1" from the hint in both exec_stmt_execsql() and exec_stmt_dynexecute(), and updates the expected output accordingly.

Attached is v3 with three patches:
  0001: the too_many_rows check for EXECUTE ... INTO (same as v1)
  0002: the hint change
  0003: the documentation change

One more thing: I proposed this for master only, since it changes behavior for users who have too_many_rows in extra_errors, and I'd appreciate your view on that as well.

Thanks
 

Regards,

--
Fujii Masao


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
Вложения:
On Tue, Nov 3, 2026 at 10:27 PM Jeevan Chalke
 wrote:
> If you think this is not needed, please feel free to ignore 0003.

Yes, let's leave 0003 out for now.

> 0002 removes "or use LIMIT 1" from the hint in both exec_stmt_execsql() and exec_stmt_dynexecute(), and updates the expected output accordingly.

On second thought, LIMIT 1 would be useful advice for many SELECT queries.
So, how about keeping it as a SELECT-specific example instead of removing it?

  Make sure the query returns a single row, for example by using LIMIT
1 with SELECT.

> Attached is v3 with three patches:
>   0001: the too_many_rows check for EXECUTE ... INTO (same as v1)
>   0002: the hint change
>   0003: the documentation change

Thanks for updating the patches!

I think it's better to put the hint change first, so the too_many_rows patch
uses the revised hint from the start. So I revised the patches in that order.
Attached. 0001 updates the hint, and 0002 adds the too_many_rows check.

Barring any objections, I'm thinking to commit these to master.

> One more thing: I proposed this for master only, since it changes behavior for users who have too_many_rows in extra_errors, and I'd appreciate your view on that as well.

Agreed.

Regards,

-- 
Fujii Masao

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:


On Wed, Nov 11, 2026 at 4:08 PM Fujii Masao <masao.fujii@gmail.com> wrote:
On Tue, Nov 3, 2026 at 10:27 PM Jeevan Chalke
<jeevan.chalke@enterprisedb.com> wrote:
> If you think this is not needed, please feel free to ignore 0003.

Yes, let's leave 0003 out for now.

> 0002 removes "or use LIMIT 1" from the hint in both exec_stmt_execsql() and exec_stmt_dynexecute(), and updates the expected output accordingly.

On second thought, LIMIT 1 would be useful advice for many SELECT queries.
So, how about keeping it as a SELECT-specific example instead of removing it?

  Make sure the query returns a single row, for example by using LIMIT
1 with SELECT.

> Attached is v3 with three patches:
>   0001: the too_many_rows check for EXECUTE ... INTO (same as v1)
>   0002: the hint change
>   0003: the documentation change

Thanks for updating the patches!

I think it's better to put the hint change first, so the too_many_rows patch
uses the revised hint from the start. So I revised the patches in that order.
Attached. 0001 updates the hint, and 0002 adds the too_many_rows check.

Barring any objections, I'm thinking to commit these to master.

> One more thing: I proposed this for master only, since it changes behavior for users who have too_many_rows in extra_errors, and I'd appreciate your view on that as well.

Agreed.

Hi Fujii-san,

Thank you for the review and for reordering the patches.

I agree with keeping LIMIT 1 as a SELECT-specific example, and with putting the hint change first so that the too_many_rows patch uses the revised wording from the start.  I applied v4 on the latest HEAD, and it builds without warnings. All tests pass.

Just one small observation on the 0002 commit message: it does not mention that EXECUTE ... INTO STRICT errors also gain the hint now (the three existing stricttest cases in plpgsql.out change accordingly).  You may want to add a sentence about that.

Also, agreed on committing to master only.

Thanks again!
 

Regards,

--
Fujii Masao


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com

Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 1:50 AM Jeevan Chalke
 wrote:
> Just one small observation on the 0002 commit message: it does not mention that EXECUTE ... INTO STRICT errors also gain the hint now (the three existing stricttest cases in plpgsql.out change accordingly).  You may want to add a sentence about that.

OK, I'll add "Also add a hint to errors reported when EXECUTE ... INTO
STRICT returns multiple rows." to the commit message for the 0002 patch.
After one more CI run with both patches, I'll commit them.

Regards,

-- 
Fujii Masao


Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 9:46 AM Fujii Masao  wrote:
> OK, I'll add "Also add a hint to errors reported when EXECUTE ... INTO
> STRICT returns multiple rows." to the commit message for the 0002 patch.
> After one more CI run with both patches, I'll commit them.

I've pushed the patches. Thanks!

Regards,

-- 
Fujii Masao


Re: PL/pgSQL: apply the too_many_rows extra check to EXECUTE ... INTO

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:


On Mon, Nov 16, 2026 at 7:38 AM Fujii Masao <masao.fujii@gmail.com> wrote:
On Mon, Nov 16, 2026 at 9:46 AM Fujii Masao <masao.fujii@gmail.com> wrote:
> OK, I'll add "Also add a hint to errors reported when EXECUTE ... INTO
> STRICT returns multiple rows." to the commit message for the 0002 patch.
> After one more CI run with both patches, I'll commit them.

I've pushed the patches. Thanks!

Thank you, Fujii-san.


Regards,

--
Fujii Masao


--
Jeevan Chalke
Senior Principal Engineer, Engineering Manager
Product Development


enterprisedb.com
FAQ