!4249 【bugfixed】解决 insert select 宕机问题

Merge pull request !4249 from laishenghao/master
This commit is contained in:
opengauss_bot
2023-10-09 03:06:34 +00:00
committed by Gitee
3 changed files with 57 additions and 7 deletions

View File

@ -1651,11 +1651,15 @@ static void SetUpsertAttrnoState(ParseState* pstate, List *targetList)
}
}
static RightRefState* MakeRightRefState()
static RightRefState* MakeRightRefStateIfSupported(SelectStmt* selectStmt)
{
RightRefState* refState = (RightRefState*)palloc0(sizeof(RightRefState));
refState->isSupported = !IsInitdb && DB_IS_CMPT(B_FORMAT);
return refState;
bool isSupported = DB_IS_CMPT(B_FORMAT) && selectStmt && selectStmt->valuesLists && !IsInitdb;
if (isSupported) {
RightRefState* refState = (RightRefState*)palloc0(sizeof(RightRefState));
refState->isSupported = true;
return refState;
}
return nullptr;
}
/*
@ -1684,7 +1688,7 @@ static Query* transformInsertStmt(ParseState* pstate, InsertStmt* stmt)
/* There can't be any outer WITH to worry about */
AssertEreport(pstate->p_ctenamespace == NIL, MOD_OPT, "para should be NIL");
RightRefState* rightRefState = MakeRightRefState();
RightRefState* rightRefState = MakeRightRefStateIfSupported((SelectStmt*)stmt->selectStmt);
qry->commandType = CMD_INSERT;
pstate->p_is_insert = true;
@ -2176,7 +2180,7 @@ static Query* transformInsertStmt(ParseState* pstate, InsertStmt* stmt)
exprList = transformInsertRow(pstate, exprList, stmt->cols, icolumns, attrnos);
}
if (rightRefState->isSupported) {
if (IS_SUPPORT_RIGHT_REF(rightRefState)) {
SetInsertAttrnoState(pstate, attrnos, list_length(exprList));
}
@ -2273,7 +2277,7 @@ static Query* transformInsertStmt(ParseState* pstate, InsertStmt* stmt)
} else {
qry->rightRefState = nullptr;
pstate->rightRefState = nullptr;
pfree(rightRefState);
pfree_ext(rightRefState);
}
return qry;

View File

@ -468,6 +468,21 @@ create table t_multi_values3(f1 int primary key, f2 int, f3 int);
insert into t_multi_values3(f1, f3) VALUES(1, f1 + 2),(2, f1 + 2),(3, f1 + 2),(4, f1 + 2);
select * from t_multi_values3 order by f1;
-- test insert select
create table ins_sel_t2 (a1 int);
create table ins_sel_t3 (a int);
create table ins_sel_t4 (a1 int);
insert into ins_sel_t3 select * from ins_sel_t2 where a < 800; -- should error
insert into ins_sel_t2 values(generate_series(20, 30));
insert into ins_sel_t3 select * from ins_sel_t2 where a1 < 25;
select * from ins_sel_t3 order by a;
delete from ins_sel_t2;
insert into ins_sel_t4 values(generate_series(1, 10));
insert into ins_sel_t2 select * from ins_sel_t4 where a1 < 3;
select * from ins_sel_t2 order by a1;
-- jdbc case
DROP USER IF EXISTS rightref CASCADE;
CREATE USER rightref WITH PASSWORD 'rightref@123';

View File

@ -671,6 +671,37 @@ select * from t_multi_values3 order by f1;
4 | | 6
(4 rows)
-- test insert select
create table ins_sel_t2 (a1 int);
create table ins_sel_t3 (a int);
create table ins_sel_t4 (a1 int);
insert into ins_sel_t3 select * from ins_sel_t2 where a < 800; -- should error
ERROR: column "a" does not exist
LINE 1: ...sert into ins_sel_t3 select * from ins_sel_t2 where a < 800;
^
HINT: There is a column named "a" in table "ins_sel_t3", but it cannot be referenced from this part of the query.
insert into ins_sel_t2 values(generate_series(20, 30));
insert into ins_sel_t3 select * from ins_sel_t2 where a1 < 25;
select * from ins_sel_t3 order by a;
a
----
20
21
22
23
24
(5 rows)
delete from ins_sel_t2;
insert into ins_sel_t4 values(generate_series(1, 10));
insert into ins_sel_t2 select * from ins_sel_t4 where a1 < 3;
select * from ins_sel_t2 order by a1;
a1
----
1
2
(2 rows)
-- jdbc case
DROP USER IF EXISTS rightref CASCADE;
NOTICE: role "rightref" does not exist, skipping