From 4f2fc27ea95da4444370b92b529a6e731fb63ac1 Mon Sep 17 00:00:00 2001 From: Bo Date: Wed, 4 Dec 2024 14:56:40 -0800 Subject: [PATCH] ExecProcessReturning is unexpectedly called twice for psql request causing overflow error (#489) 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 --- src/backend/executor/nodeModifyTable.c | 4 ++-- .../regress/expected/babel_5343_returning.out | 21 +++++++++++++++++++ src/test/regress/parallel_schedule | 3 +++ src/test/regress/sql/babel_5343_returning.sql | 5 +++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/test/regress/expected/babel_5343_returning.out create mode 100644 src/test/regress/sql/babel_5343_returning.sql diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index f5f2d4083cc..ced47f81a38 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -807,7 +807,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 */ @@ -1199,7 +1199,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) diff --git a/src/test/regress/expected/babel_5343_returning.out b/src/test/regress/expected/babel_5343_returning.out new file mode 100644 index 00000000000..8d9ac5d2c0d --- /dev/null +++ b/src/test/regress/expected/babel_5343_returning.out @@ -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; diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule index ca4af0ecab5..57c1211da1d 100644 --- a/src/test/regress/parallel_schedule +++ b/src/test/regress/parallel_schedule @@ -137,3 +137,6 @@ test: event_trigger oidjoins test: fast_default test: serializable + +# Test Babel returning issue BABEL-5343 +test: babel_5343_returning diff --git a/src/test/regress/sql/babel_5343_returning.sql b/src/test/regress/sql/babel_5343_returning.sql new file mode 100644 index 00000000000..455788f803b --- /dev/null +++ b/src/test/regress/sql/babel_5343_returning.sql @@ -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;