Skip to content

Commit

Permalink
ExecProcessReturning is unexpectedly called twice for psql request ca…
Browse files Browse the repository at this point in the history
…using overflow error (#488)

In community postgresql, ExecProcessReturning is called once after the
main process of ExecInsert. But due to some merging errors related to
Babelfish and T-SQL, this function is wrongly called twice for psql
requests. At the first call, the xmax is not initialized causing
unexpected error raising.

The first intention of these pieces of codes is to call it in the
first place only for T-SQL(CR-52326143). And then got wrongly
cherry-picked in CR-57250041. Finally, the condition check gets
removed in CR-65026879. We should keep the first intention of the
codes, which also makes it consistent with the behaviour in
ExecUpdate.

Task: BABEL-5343
Signed-off-by: Bo Li <[email protected]>
  • Loading branch information
hopebo authored Dec 4, 2024
1 parent 58651b6 commit db4e138
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/backend/executor/nodeModifyTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ ExecInsert(ModifyTableContext *context,
}

/* Process RETURNING if present */
if (resultRelInfo->ri_projectReturning)
if (resultRelInfo->ri_projectReturning && sql_dialect == SQL_DIALECT_TSQL)
result = ExecProcessReturning(resultRelInfo, slot, planSlot);

/* INSTEAD OF ROW INSERT Triggers */
Expand Down Expand Up @@ -1218,7 +1218,7 @@ ExecInsert(ModifyTableContext *context,
ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);

/* Process RETURNING if present */
if (resultRelInfo->ri_projectReturning)
if (resultRelInfo->ri_projectReturning && sql_dialect != SQL_DIALECT_TSQL)
result = ExecProcessReturning(resultRelInfo, slot, planSlot);

if (inserted_tuple)
Expand Down
21 changes: 21 additions & 0 deletions src/test/regress/expected/babel_5343_returning.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE toverflow(id SERIAL, col1 VARCHAR);
INSERT INTO toverflow VALUES (default, 'hope') RETURNING xmax::text::int;
xmax
------
0
(1 row)

INSERT INTO toverflow VALUES (default, 'jack') RETURNING xmax;
xmax
------
0
(1 row)

SELECT * FROM toverflow ORDER BY id;
id | col1
----+------
1 | hope
2 | jack
(2 rows)

DROP TABLE toverflow;
3 changes: 3 additions & 0 deletions src/test/regress/parallel_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ test: serializable
# run tablespace test at the end because it drops the tablespace created during
# setup that other tests may use.
test: tablespace

# Test Babel returning issue BABEL-5343
test: babel_5343_returning
5 changes: 5 additions & 0 deletions src/test/regress/sql/babel_5343_returning.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE toverflow(id SERIAL, col1 VARCHAR);
INSERT INTO toverflow VALUES (default, 'hope') RETURNING xmax::text::int;
INSERT INTO toverflow VALUES (default, 'jack') RETURNING xmax;
SELECT * FROM toverflow ORDER BY id;
DROP TABLE toverflow;

0 comments on commit db4e138

Please sign in to comment.