Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6c7251d

Browse files
committed
Implement new optimization rule for updates of expanded variables.
If a read/write expanded variable is declared locally to the assignment statement that is updating it, and it is referenced exactly once in the assignment RHS, then we can optimize the operation as a direct update of the expanded value, whether or not the function(s) operating on it can be trusted not to modify the value before throwing an error. This works because if an error does get thrown, we no longer care what value the variable has. In cases where that doesn't work, fall back to the previous rule that checks for safety of the top-level function. In any case, postpone determination of whether these optimizations are feasible until we are executing a Param referencing the target variable and that variable holds a R/W expanded object. While the previous incarnation of exec_check_rw_parameter was pretty cheap, this is a bit less so, and our plan to invoke support functions will make it even less so. So avoiding the check for variables where it couldn't be useful should be a win. Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Andrey Borodin <x4mmm@yandex-team.ru> Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com> Discussion: https://postgr.es/m/CACxu=vJaKFNsYxooSnW1wEgsAO5u_v1XYBacfVJ14wgJV_PYeg@mail.gmail.com
1 parent 36fb9ef commit 6c7251d

File tree

6 files changed

+364
-62
lines changed

6 files changed

+364
-62
lines changed

src/include/executor/execExpr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ typedef struct ExprEvalStep
425425
{
426426
ExecEvalSubroutine paramfunc; /* add-on evaluation subroutine */
427427
void *paramarg; /* private data for same */
428+
void *paramarg2; /* more private data for same */
428429
int paramid; /* numeric ID for parameter */
429430
Oid paramtype; /* OID of parameter's datatype */
430431
} cparam;

src/pl/plpgsql/src/expected/plpgsql_array.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ NOTICE: a = ("{""(,11)""}",), a.c1[1].i = 11
5252
do $$ declare a int[];
5353
begin a := array_agg(x) from (values(1),(2),(3)) v(x); raise notice 'a = %', a; end$$;
5454
NOTICE: a = {1,2,3}
55+
do $$ declare a int[] := array[1,2,3];
56+
begin
57+
-- test scenarios for optimization of updates of R/W expanded objects
58+
a := array_append(a, 42); -- optimizable using "transfer" method
59+
a := a || a[3]; -- optimizable using "inplace" method
60+
a := a || a; -- not optimizable
61+
raise notice 'a = %', a;
62+
end$$;
63+
NOTICE: a = {1,2,3,42,3,1,2,3,42,3}
5564
create temp table onecol as select array[1,2] as f1;
5665
do $$ declare a int[];
5766
begin a := f1 from onecol; raise notice 'a = %', a; end$$;

0 commit comments

Comments
 (0)