【fix】解决触发器中设置自定义变量报错的问题

This commit is contained in:
yuhuanhuan
2022-10-10 10:21:17 +08:00
parent 6f87123c7a
commit aa5ffec6a3
5 changed files with 185 additions and 2 deletions

View File

@ -2414,7 +2414,13 @@ Node* eval_const_expressions_mutator(Node* node, eval_const_expressions_context*
/* Look to see if we've been given a value for this Param */
if (param->paramkind == PARAM_EXTERN && context->boundParams != NULL && param->paramid > 0 &&
param->paramid <= context->boundParams->numParams) {
ParamExternData* prm = &context->boundParams->params[param->paramid - 1];
ParamListInfo paramInfo = context->boundParams;
ParamExternData* prm = &paramInfo->params[param->paramid - 1];
/* give hook a chance in case parameter in dynamic */
if (!OidIsValid(prm->ptype) && paramInfo->paramFetch != NULL) {
(*paramInfo->paramFetch)(paramInfo, param->paramid);
}
if (OidIsValid(prm->ptype)) {
/* OK to substitute parameter value? */

View File

@ -1625,6 +1625,9 @@ static void pgaudit_ProcessUtility(Node* parsetree, const char* queryString, Par
VariableSetStmt* variablesetstmt = (VariableSetStmt*)(parsetree);
pgaudit_process_set_parameter(variablesetstmt->name, queryString);
} break;
case T_VariableMultiSetStmt: {
pgaudit_process_set_parameter("set stmt", queryString);
} break;
#ifndef ENABLE_MULTIPLE_NODES
case T_AlterSystemStmt: {
AlterSystemStmt* altersystemstmt = (AlterSystemStmt*)(parsetree);

View File

@ -8624,7 +8624,7 @@ const char* CreateCommandTag(Node* parse_tree)
case T_VariableMultiSetStmt:
{
tag = "Set";
tag = "SET";
} break;
case T_VariableShowStmt:

View File

@ -454,6 +454,68 @@ ROLLBACK;
show client_encoding;
select @v1;
\c test_set
drop table if exists t1;
create table t1(a int);
create or replace function tri_func1() returns trigger as
$$
declare
begin
set @num = @num + NEW.a;
return NEW;
end
$$ LANGUAGE PLPGSQL;
create trigger tri_insert_before before insert on t1 for each row execute procedure tri_func1();
set @num := 0;
select @num;
insert into t1 values(100);
select @num;
insert into t1 values(200);
select @num;
drop trigger tri_insert_before on t1;
create trigger tri_update_before before update on t1 for each row execute procedure tri_func1();
set @num := 0;
select @num;
update t1 set a = 1000 where a = 100;
select @num;
update t1 set a = 2000 where a = 200;
select @num;
drop trigger tri_update_before on t1;
create or replace function tri_func2() returns trigger as
$$
declare
begin
set @num = @num + OLD.a;
return OLD;
end
$$ LANGUAGE PLPGSQL;
create trigger tri_update_after after update on t1 for each row execute procedure tri_func2();
set @num := 0;
select @num;
update t1 set a = 100 where a = 1000;
select @num;
update t1 set a = 200 where a = 2000;
select @num;
drop trigger tri_update_after on t1;
create trigger tri_delete_after after delete on t1 for each row execute procedure tri_func2();
set @num := 0;
select @num;
delete t1 where a = 100;
select @num;
delete t1 where a = 200;
select @num;
drop trigger tri_delete_after on t1;
-- exception scenario
\c test_set
set @v2 := 2;

View File

@ -967,6 +967,118 @@ select @v1;
1
(1 row)
\c test_set
drop table if exists t1;
NOTICE: table "t1" does not exist, skipping
create table t1(a int);
create or replace function tri_func1() returns trigger as
$$
declare
begin
set @num = @num + NEW.a;
return NEW;
end
$$ LANGUAGE PLPGSQL;
create trigger tri_insert_before before insert on t1 for each row execute procedure tri_func1();
set @num := 0;
select @num;
@num
------
0
(1 row)
insert into t1 values(100);
select @num;
@num
------
100
(1 row)
insert into t1 values(200);
select @num;
@num
------
300
(1 row)
drop trigger tri_insert_before on t1;
create trigger tri_update_before before update on t1 for each row execute procedure tri_func1();
set @num := 0;
select @num;
@num
------
0
(1 row)
update t1 set a = 1000 where a = 100;
select @num;
@num
------
1000
(1 row)
update t1 set a = 2000 where a = 200;
select @num;
@num
------
3000
(1 row)
drop trigger tri_update_before on t1;
create or replace function tri_func2() returns trigger as
$$
declare
begin
set @num = @num + OLD.a;
return OLD;
end
$$ LANGUAGE PLPGSQL;
create trigger tri_update_after after update on t1 for each row execute procedure tri_func2();
set @num := 0;
select @num;
@num
------
0
(1 row)
update t1 set a = 100 where a = 1000;
select @num;
@num
------
1000
(1 row)
update t1 set a = 200 where a = 2000;
select @num;
@num
------
3000
(1 row)
drop trigger tri_update_after on t1;
create trigger tri_delete_after after delete on t1 for each row execute procedure tri_func2();
set @num := 0;
select @num;
@num
------
0
(1 row)
delete t1 where a = 100;
select @num;
@num
------
100
(1 row)
delete t1 where a = 200;
select @num;
@num
------
300
(1 row)
drop trigger tri_delete_after on t1;
-- exception scenario
\c test_set
set @v2 := 2;