diff --git a/src/common/backend/parser/parse_expr.cpp b/src/common/backend/parser/parse_expr.cpp index c70aabb16..c66a9dad8 100644 --- a/src/common/backend/parser/parse_expr.cpp +++ b/src/common/backend/parser/parse_expr.cpp @@ -966,10 +966,15 @@ Node* transformColumnRef(ParseState* pstate, ColumnRef* cref) if (node == NULL) { node = hookresult; } else if (hookresult != NULL) { - ereport(ERROR, - (errcode(ERRCODE_AMBIGUOUS_COLUMN), - errmsg("column reference \"%s\" is ambiguous", NameListToString(cref->fields)), - parser_errposition(pstate, cref->location))); + if (IS_SUPPORT_RIGHT_REF(pstate->rightRefState)) { + node = hookresult; + } else { + ereport(ERROR, + (errcode(ERRCODE_AMBIGUOUS_COLUMN), + errmsg("column reference \"%s\" is ambiguous", NameListToString(cref->fields)), + parser_errposition(pstate, cref->location))); + } + } } diff --git a/src/common/pl/plpgsql/src/pl_comp.cpp b/src/common/pl/plpgsql/src/pl_comp.cpp index 23c69f27f..241a081d9 100644 --- a/src/common/pl/plpgsql/src/pl_comp.cpp +++ b/src/common/pl/plpgsql/src/pl_comp.cpp @@ -1621,7 +1621,7 @@ static Node* plpgsql_post_column_ref(ParseState* pstate, ColumnRef* cref, Node* */ Node* myvar = resolve_column_ref(pstate, expr, cref, (var == NULL)); - if (myvar != NULL && var != NULL) { + if (myvar != NULL && var != NULL && !IS_SUPPORT_RIGHT_REF(pstate->rightRefState)) { /* * We could leave it to the core parser to throw this error, but we * can add a more useful detail message than the core could. diff --git a/src/test/regress/expected/insert_right_ref.out b/src/test/regress/expected/insert_right_ref.out index 644e14a90..ea1fe5bb8 100644 --- a/src/test/regress/expected/insert_right_ref.out +++ b/src/test/regress/expected/insert_right_ref.out @@ -148,5 +148,29 @@ select * from upser order by c1; (15 rows) drop table upser; +-- test var +create table with_var(a int default 999); +create function with_var_func() return int as +declare + a int := 666; +begin + insert into with_var values(a); + return a; +end; +/ +call with_var_func(); + with_var_func +--------------- + 666 +(1 row) + +select * from with_var; + a +----- + 666 +(1 row) + +drop function with_var_func; +drop table with_var; \c postgres drop database rightref; diff --git a/src/test/regress/sql/insert_right_ref.sql b/src/test/regress/sql/insert_right_ref.sql index d34c43682..b5fddab49 100644 --- a/src/test/regress/sql/insert_right_ref.sql +++ b/src/test/regress/sql/insert_right_ref.sql @@ -65,6 +65,23 @@ select * from upser order by c1; drop table upser; +-- test var +create table with_var(a int default 999); +create function with_var_func() return int as +declare + a int := 666; +begin + insert into with_var values(a); + return a; +end; +/ + +call with_var_func(); +select * from with_var; + +drop function with_var_func; +drop table with_var; + \c postgres drop database rightref;