diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l
index 991b7de0b5..0fab48a382 100644
--- a/src/fe_utils/psqlscan.l
+++ b/src/fe_utils/psqlscan.l
@@ -1098,23 +1098,23 @@ psql_scan(PsqlScanState state,
 	{
 		case LEXRES_EOL:		/* end of input */
 			switch (state->start_state)
 			{
 				case INITIAL:
 				case xqs:		/* we treat this like INITIAL */
 					if (state->paren_depth > 0)
 					{
 						result = PSCAN_INCOMPLETE;
 						*prompt = PROMPT_PAREN;
 					}
-					if (state->begin_depth > 0)
+					else if (state->begin_depth > 0)
 					{
 						result = PSCAN_INCOMPLETE;
 						*prompt = PROMPT_CONTINUE;
 					}
 					else if (query_buf->len > 0)
 					{
 						result = PSCAN_EOL;
 						*prompt = PROMPT_CONTINUE;
 					}
 					else
 					{
