From e3d4a7464efcb2e558c41527db5c9e6c6702d3ee Mon Sep 17 00:00:00 2001 From: laishenghao Date: Mon, 12 Jun 2023 11:46:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=AD=98=E5=9C=A8exception?= =?UTF-8?q?=E6=97=B6=E5=8C=BF=E5=90=8D=E4=BA=8B=E5=8A=A1=E5=8D=A0=E7=94=A8?= =?UTF-8?q?=E5=86=85=E5=AD=98=E4=B8=8D=E6=96=AD=E4=B8=8A=E5=8D=87=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20&=202=E4=B8=AAmemcheck=20=E5=86=85?= =?UTF-8?q?=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/parser/gram.y | 2 +- src/common/pl/plpgsql/src/pl_exec.cpp | 45 ++++++++++++------- .../optimizer/commands/indexcmds.cpp | 2 +- src/gausskernel/runtime/executor/spi.cpp | 9 ++++ src/include/executor/spi.h | 1 + 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/common/backend/parser/gram.y b/src/common/backend/parser/gram.y index d4b0fb3c5d..a2fbed363f 100644 --- a/src/common/backend/parser/gram.y +++ b/src/common/backend/parser/gram.y @@ -25009,7 +25009,7 @@ character_set: charset_collate_name: ColId { $$ = $1; } - | BINARY { $$ = pg_strdup($1); } + | BINARY { $$ = pstrdup($1); } | Sconst { $$ = $1; } ; diff --git a/src/common/pl/plpgsql/src/pl_exec.cpp b/src/common/pl/plpgsql/src/pl_exec.cpp index 89cb23271f..f3b9cb0c2e 100644 --- a/src/common/pl/plpgsql/src/pl_exec.cpp +++ b/src/common/pl/plpgsql/src/pl_exec.cpp @@ -2780,7 +2780,7 @@ static void exec_exception_cleanup(PLpgSQL_execstate* estate, ExceptionContext * * With subtransaction aborted or not, SPI may be in a disconnected state, * We need this hack to return to connected state. */ - SPI_restore_connection(); + SPI_restore_connection_on_exception(); t_thrd.xact_cxt.isSelectInto = false; @@ -6948,6 +6948,7 @@ static int exec_stmt_dynexecute(PLpgSQL_execstate* estate, PLpgSQL_stmt_dynexecu u_sess->plsql_cxt.curr_compile_context = save_compile_context; u_sess->plsql_cxt.compile_status = save_compile_status; clearCompileContextList(save_compile_list_length); + pfree_ext(querystr); PG_RE_THROW(); } PG_END_TRY(); @@ -6961,6 +6962,7 @@ static int exec_stmt_dynexecute(PLpgSQL_execstate* estate, PLpgSQL_stmt_dynexecu res = exchange_parameters(estate, stmt, func->action->body, &ppdindex, &datumindex); if (res < 0) { + pfree_ext(querystr); return -1; } @@ -6989,6 +6991,7 @@ static int exec_stmt_dynexecute(PLpgSQL_execstate* estate, PLpgSQL_stmt_dynexecu #endif /* Decrement package use-count */ DecreasePackageUseCount(func); + pfree_ext(querystr); PG_RE_THROW(); } PG_END_TRY(); @@ -7049,24 +7052,34 @@ static int exec_stmt_dynexecute(PLpgSQL_execstate* estate, PLpgSQL_stmt_dynexecu plpgsql_estate = estate; - /* - * Execute the query without preparing a saved plan. - */ - if (stmt->ppd != NULL) { - PreparedParamsData* ppd = (PreparedParamsData*)stmt->ppd; - exec_res = SPI_execute_with_args( - querystr, ppd->nargs, ppd->types, ppd->values, ppd->nulls, estate->readonly_func, tcount, ppd->cursor_data); - free_params_data(ppd); - stmt->ppd = NULL; - } else { - bool isCollectParam = false; + PG_TRY(); + { + /* + * Execute the query without preparing a saved plan. + */ + if (stmt->ppd != NULL) { + PreparedParamsData* ppd = (PreparedParamsData*)stmt->ppd; + exec_res = SPI_execute_with_args(querystr, ppd->nargs, ppd->types, ppd->values, ppd->nulls, + estate->readonly_func, tcount, ppd->cursor_data); + free_params_data(ppd); + stmt->ppd = NULL; + } else { + bool isCollectParam = false; #ifdef ENABLE_MULTIPLE_NODES - if (checkAdivsorState()) { - isCollectParam = true; - } + if (checkAdivsorState()) { + isCollectParam = true; + } #endif - exec_res = SPI_execute(querystr, estate->readonly_func, 0, isCollectParam); + exec_res = SPI_execute(querystr, estate->readonly_func, 0, isCollectParam); + } + } + PG_CATCH(); + { + pfree_ext(querystr); + PG_RE_THROW(); } + PG_END_TRY(); + /* * This is used for nested STP. If the transaction Id changed, * then need to create new econtext for the TopTransaction. diff --git a/src/gausskernel/optimizer/commands/indexcmds.cpp b/src/gausskernel/optimizer/commands/indexcmds.cpp index c81ed349d7..ced07352d2 100644 --- a/src/gausskernel/optimizer/commands/indexcmds.cpp +++ b/src/gausskernel/optimizer/commands/indexcmds.cpp @@ -390,7 +390,7 @@ static List *ExtractSubPartitionIdf(IndexStmt* stmt, List *subPartitionOidList, foreach(lc2, idxPartdef->sublist) { RangePartitionindexDefState *idxSubPartdef = (RangePartitionindexDefState*)lfirst(lc2); if (idxSubPartdef->tablespace == NULL) { - idxSubPartdef->tablespace = strdup(idxPartdef->tablespace); + idxSubPartdef->tablespace = pstrdup(idxPartdef->tablespace); } } } diff --git a/src/gausskernel/runtime/executor/spi.cpp b/src/gausskernel/runtime/executor/spi.cpp index 7bcf89fa7d..fb4f73f486 100644 --- a/src/gausskernel/runtime/executor/spi.cpp +++ b/src/gausskernel/runtime/executor/spi.cpp @@ -729,6 +729,15 @@ void SPI_restore_connection(void) u_sess->SPI_cxt._curid = u_sess->SPI_cxt._connected - 1; } +void SPI_restore_connection_on_exception(void) +{ + Assert(u_sess->SPI_cxt._connected >= 0); + if (u_sess->SPI_cxt._current && u_sess->SPI_cxt._curid > u_sess->SPI_cxt._connected - 1) { + MemoryContextResetAndDeleteChildren(u_sess->SPI_cxt._current->execCxt); + } + u_sess->SPI_cxt._curid = u_sess->SPI_cxt._connected - 1; +} + #ifdef PGXC /* SPI_execute_direct: * Runs the 'remote_sql' query string on the node 'nodename' diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h index 87c8a89eb4..1fe2e0ae15 100644 --- a/src/include/executor/spi.h +++ b/src/include/executor/spi.h @@ -95,6 +95,7 @@ extern void SPI_pop(void); extern bool SPI_push_conditional(void); extern void SPI_pop_conditional(bool pushed); extern void SPI_restore_connection(void); +extern void SPI_restore_connection_on_exception(void); extern int SPI_execute(const char* src, bool read_only, long tcount, bool isCollectParam = false, parse_query_func parser = GetRawParser()); extern int SPI_execute_plan(SPIPlanPtr plan, Datum* Values, const char* Nulls, bool read_only, long tcount); extern int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount); -- Gitee