From 733f107f09130f4f9afed5cf217d350d0c8725fa Mon Sep 17 00:00:00 2001
From: Bjorn Pagen <hello@bjornpagen.com>
Date: Mon, 16 Nov 2026 01:23:59 -0600
Subject: [PATCH v1] Avoid quadratic rescans in plpgsql_location_to_lineno().

The PL/pgSQL grammar asks for the line number of an IF, ELSIF or CASE
WHEN only when it reduces the statement, after the statements in its
body have asked for theirs, so the location goes backwards.
plpgsql_location_to_lineno() handled that by rescanning the source
from the start, which made compiling a body of many such statements
quadratic: a CASE with 40000 WHEN arms took about 15 seconds to
compile, at CREATE FUNCTION and again at the first call in each
backend.  Instead, walk the current line back one line at a time.

Add a test of the line numbers reported for errors in such
statements, which nothing else in the regression tests covered.
---
 src/pl/plpgsql/src/expected/plpgsql_misc.out | 42 ++++++++++++++++++++
 src/pl/plpgsql/src/pl_scanner.c              | 20 +++++++---
 src/pl/plpgsql/src/sql/plpgsql_misc.sql      | 35 ++++++++++++++++
 3 files changed, 91 insertions(+), 6 deletions(-)

diff --git a/src/pl/plpgsql/src/expected/plpgsql_misc.out b/src/pl/plpgsql/src/expected/plpgsql_misc.out
index b3def30282..7dd29b9d82 100644
--- a/src/pl/plpgsql/src/expected/plpgsql_misc.out
+++ b/src/pl/plpgsql/src/expected/plpgsql_misc.out
@@ -110,3 +110,45 @@ do $$ declare x int; begin x := 1 $$;
 ERROR:  missing ";" at end of SQL statement
 LINE 1: do $$ declare x int; begin x := 1 $$;
                                           ^
+--
+-- Line numbers of statements whose location the grammar resolves only after
+-- their bodies (IF, ELSIF and CASE), so locations reach the scanner out of
+-- order
+--
+create function lineno_order(k int) returns int language plpgsql as $$
+declare
+  r int;
+begin
+  if k = 1 then
+    r := 1;
+  elsif 1 / (k - 2) = 0 then
+    r := 2;
+  end if;
+  case 6 / (k - 3)
+    when 0 then
+      r := 3;
+    else
+      if k = 5 then
+        r := 1 / (k - 5);
+      end if;
+      r := 4;
+  end case;
+  if 1 / (k - 4) = 0 then
+    r := 5;
+  end if;
+  return r;
+end
+$$;
+select lineno_order(2);
+ERROR:  division by zero
+CONTEXT:  PL/pgSQL function lineno_order(integer) line 5 at IF
+select lineno_order(3);
+ERROR:  division by zero
+CONTEXT:  PL/pgSQL function lineno_order(integer) line 10 at CASE
+select lineno_order(4);
+ERROR:  division by zero
+CONTEXT:  PL/pgSQL function lineno_order(integer) line 19 at IF
+select lineno_order(5);
+ERROR:  division by zero
+CONTEXT:  PL/pgSQL function lineno_order(integer) line 15 at assignment
+drop function lineno_order(int);
diff --git a/src/pl/plpgsql/src/pl_scanner.c b/src/pl/plpgsql/src/pl_scanner.c
index 344fb7b870..9e44faf232 100644
--- a/src/pl/plpgsql/src/pl_scanner.c
+++ b/src/pl/plpgsql/src/pl_scanner.c
@@ -563,9 +563,10 @@ plpgsql_yyerror(YYLTYPE *yyllocp, PLpgSQL_stmt_block **plpgsql_parse_result_p, y
  * Given a location (a byte offset in the function source text),
  * return a line number.
  *
- * We expect that this is typically called for a sequence of increasing
- * location values, so optimize accordingly by tracking the endpoints
- * of the "current" line.
+ * We track the endpoints of the "current" line, and move it forwards or
+ * backwards a line at a time.  Locations mostly increase, but the grammar
+ * asks for the line number of an IF, ELSIF or CASE WHEN only when it reduces
+ * the statement, after the statements in its body have asked for theirs.
  */
 int
 plpgsql_location_to_lineno(int location, yyscan_t yyscanner)
@@ -576,9 +577,16 @@ plpgsql_location_to_lineno(int location, yyscan_t yyscanner)
 		return 0;				/* garbage in, garbage out */
 	loc = yyextra->scanorig + location;
 
-	/* be correct, but not fast, if input location goes backwards */
-	if (loc < yyextra->cur_line_start)
-		location_lineno_init(yyscanner);
+	while (loc < yyextra->cur_line_start)
+	{
+		const char *p = yyextra->cur_line_start - 1;	/* previous line's \n */
+
+		yyextra->cur_line_end = p;
+		while (p > yyextra->scanorig && p[-1] != '\n')
+			p--;
+		yyextra->cur_line_start = p;
+		yyextra->cur_line_num--;
+	}
 
 	while (yyextra->cur_line_end != NULL && loc > yyextra->cur_line_end)
 	{
diff --git a/src/pl/plpgsql/src/sql/plpgsql_misc.sql b/src/pl/plpgsql/src/sql/plpgsql_misc.sql
index e2c3278a8d..e3bedcea6b 100644
--- a/src/pl/plpgsql/src/sql/plpgsql_misc.sql
+++ b/src/pl/plpgsql/src/sql/plpgsql_misc.sql
@@ -70,3 +70,38 @@ end $$;
 -- unterminated constructs should complain about the missing terminator
 do $$ begin if true $$;
 do $$ declare x int; begin x := 1 $$;
+
+--
+-- Line numbers of statements whose location the grammar resolves only after
+-- their bodies (IF, ELSIF and CASE), so locations reach the scanner out of
+-- order
+--
+create function lineno_order(k int) returns int language plpgsql as $$
+declare
+  r int;
+begin
+  if k = 1 then
+    r := 1;
+  elsif 1 / (k - 2) = 0 then
+    r := 2;
+  end if;
+  case 6 / (k - 3)
+    when 0 then
+      r := 3;
+    else
+      if k = 5 then
+        r := 1 / (k - 5);
+      end if;
+      r := 4;
+  end case;
+  if 1 / (k - 4) = 0 then
+    r := 5;
+  end if;
+  return r;
+end
+$$;
+select lineno_order(2);
+select lineno_order(3);
+select lineno_order(4);
+select lineno_order(5);
+drop function lineno_order(int);
-- 
2.54.0 (Apple Git-157)

