From b0aaf040a42e3a036ba43c9891e07bad73c35a5e Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Sat, 10 Apr 2021 03:35:39 +0200
Subject: [PATCH] Improve parsing of SQL standard functions in psql

Handle BEGIN, CASE and END as special 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 | 3 +++
 1 file changed, 3 insertions(+)

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

