target_ref_bugfix.patch

text/x-patch

Filename: target_ref_bugfix.patch
Type: text/x-patch
Part: 0
Message: Re: INSERT ... ON CONFLICT {UPDATE | IGNORE}

Patch

Format: unified
File+
src/backend/parser/analyze.c 10 0
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index e037c0f..8c25e82 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -2006,6 +2006,16 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
 		qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
 	}
 
+	/*
+	 * Having established that this is a speculative insertion's auxiliary
+	 * update, do not allow the query to access parent parse state.  This is a
+	 * simple way of making parent RTEs invisible -- otherwise, the parent's
+	 * target could spuriously become visible were the query to reference the
+	 * original target table name rather than the TARGET.* alias.
+	 */
+	if (pstate->p_is_speculative)
+		pstate->parentParseState = NULL;
+
 	qry->resultRelation = setTargetTable(pstate, stmt->relation,
 								  interpretInhOption(stmt->relation->inhOpt),
 										 true,