Skip to content

Commit 4cd6bdf

Browse files
author
hackorum
committed
Apply no_data_found.patch
1 parent b597835 commit 4cd6bdf

5 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/pl/plpgsql/src/pl_exec.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,8 +4245,14 @@ exec_stmt_execsql(PLpgSQL_execstate *estate,
42454245
long tcount;
42464246
int rc;
42474247
PLpgSQL_expr *expr = stmt->sqlstmt;
4248+
int no_data_found_level = 0;
42484249
int too_many_rows_level = 0;
42494250

4251+
if (plpgsql_extra_errors & PLPGSQL_XCHECK_NODATAFOUND)
4252+
no_data_found_level = ERROR;
4253+
else if (plpgsql_extra_warnings & PLPGSQL_XCHECK_NODATAFOUND)
4254+
no_data_found_level = WARNING;
4255+
42504256
if (plpgsql_extra_errors & PLPGSQL_XCHECK_TOOMANYROWS)
42514257
too_many_rows_level = ERROR;
42524258
else if (plpgsql_extra_warnings & PLPGSQL_XCHECK_TOOMANYROWS)
@@ -4473,16 +4479,19 @@ exec_stmt_execsql(PLpgSQL_execstate *estate,
44734479
*/
44744480
if (n == 0)
44754481
{
4476-
if (stmt->strict)
4482+
if (stmt->strict || no_data_found_level)
44774483
{
44784484
char *errdetail;
4485+
int errlevel;
44794486

44804487
if (estate->func->print_strict_params)
44814488
errdetail = format_expr_params(estate, expr);
44824489
else
44834490
errdetail = NULL;
44844491

4485-
ereport(ERROR,
4492+
errlevel = stmt->strict ? ERROR : no_data_found_level;
4493+
4494+
ereport(errlevel,
44864495
(errcode(ERRCODE_NO_DATA_FOUND),
44874496
errmsg("query returned no rows"),
44884497
errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));

src/pl/plpgsql/src/pl_handler.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ plpgsql_extra_checks_check_hook(char **newvalue, void **extra, GucSource source)
9393

9494
if (pg_strcasecmp(tok, "shadowed_variables") == 0)
9595
extrachecks |= PLPGSQL_XCHECK_SHADOWVAR;
96+
else if (pg_strcasecmp(tok, "no_data_found") == 0)
97+
extrachecks |= PLPGSQL_XCHECK_NODATAFOUND;
9698
else if (pg_strcasecmp(tok, "too_many_rows") == 0)
9799
extrachecks |= PLPGSQL_XCHECK_TOOMANYROWS;
98100
else if (pg_strcasecmp(tok, "strict_multi_assignment") == 0)

src/pl/plpgsql/src/plpgsql.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,9 @@ extern bool plpgsql_check_asserts;
11931193
/* extra compile-time and run-time checks */
11941194
#define PLPGSQL_XCHECK_NONE 0
11951195
#define PLPGSQL_XCHECK_SHADOWVAR (1 << 1)
1196-
#define PLPGSQL_XCHECK_TOOMANYROWS (1 << 2)
1197-
#define PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT (1 << 3)
1196+
#define PLPGSQL_XCHECK_NODATAFOUND (1 << 2)
1197+
#define PLPGSQL_XCHECK_TOOMANYROWS (1 << 3)
1198+
#define PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT (1 << 4)
11981199
#define PLPGSQL_XCHECK_ALL ((int) ~0)
11991200

12001201
extern int plpgsql_extra_warnings;

src/test/regress/expected/plpgsql.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,25 @@ select shadowtest(1);
30883088
(1 row)
30893089

30903090
-- runtime extra checks
3091+
set plpgsql.extra_warnings to 'no_data_found';
3092+
do $$
3093+
declare x int;
3094+
begin
3095+
select 1 into x where 0 = 1;
3096+
end;
3097+
$$;
3098+
WARNING: query returned no rows
3099+
set plpgsql.extra_errors to 'no_data_found';
3100+
do $$
3101+
declare x int;
3102+
begin
3103+
select 1 into x where 0 = 1;
3104+
end;
3105+
$$;
3106+
ERROR: query returned no rows
3107+
CONTEXT: PL/pgSQL function inline_code_block line 4 at SQL statement
3108+
reset plpgsql.extra_errors;
3109+
reset plpgsql.extra_warnings;
30913110
set plpgsql.extra_warnings to 'too_many_rows';
30923111
do $$
30933112
declare x int;

src/test/regress/sql/plpgsql.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,27 @@ declare f1 int; begin return 1; end $$ language plpgsql;
26192619
select shadowtest(1);
26202620

26212621
-- runtime extra checks
2622+
set plpgsql.extra_warnings to 'no_data_found';
2623+
2624+
do $$
2625+
declare x int;
2626+
begin
2627+
select 1 into x where 0 = 1;
2628+
end;
2629+
$$;
2630+
2631+
set plpgsql.extra_errors to 'no_data_found';
2632+
2633+
do $$
2634+
declare x int;
2635+
begin
2636+
select 1 into x where 0 = 1;
2637+
end;
2638+
$$;
2639+
2640+
reset plpgsql.extra_errors;
2641+
reset plpgsql.extra_warnings;
2642+
26222643
set plpgsql.extra_warnings to 'too_many_rows';
26232644

26242645
do $$

0 commit comments

Comments
 (0)