From 8c95440905c01b8c9ed8b46ef01a24b01f24580d Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Fri, 9 Apr 2021 19:40:36 +0200
Subject: [PATCH] Improve psql's parsing of SQL standard function bodies

Treat BEGIN and CASE as the start of a function body only
if we are not inside parentheses.

That allows statements like the following to be parsed correctly:

ALTER FOREIGN TABLE x OPTIONS (ADD case 'upper');
---
 src/fe_utils/psqlscan.l | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l
index 4ec57e96a9..dbfcfc84e5 100644
--- a/src/fe_utils/psqlscan.l
+++ b/src/fe_utils/psqlscan.l
@@ -871,8 +871,9 @@ other			.
 
 {identifier}	{
 					cur_state->identifier_count++;
-					if (pg_strcasecmp(yytext, "begin") == 0
-						|| pg_strcasecmp(yytext, "case") == 0)
+					if (cur_state->paren_depth == 0
+						&& (pg_strcasecmp(yytext, "begin") == 0
+							|| pg_strcasecmp(yytext, "case") == 0))
 					{
 						if (cur_state->identifier_count > 1)
 							cur_state->begin_depth++;
-- 
2.26.3

