2016-09-26 17:39 GMT+02:00 dbyzaa@xxxxxxx <dbyzaa@xxxxxxx>:
test:create type h3 as (id int,name char(10));
CREATE or replace FUNCTIONproc17()
RETURNS SETOF h3 AS $$
DECLARE
v_rec h3;
BEGIN
create temp table abc(idint,name varchar) on commit drop;
insert into abc select 1,'lw';
insert into abc select 2,'lw2';
for v_rec in
select * from abc loop
return next v_rec;
end loop;
END;
$$
LANGUAGE plpgsql;
CREATE or replace FUNCTIONproc16()
RETURNS SETOF h3 AS $$
DECLARE
id_array int[];
name_arr varchar[];
v_rec h3;
BEGIN
id_array =array[1,2];
name_arr=array['lw','lw2'];
for v_rec in
select unnest(id_array) ,unnest(name_arr) loop
return next v_rec;
end loop;
END;
$$
LANGUAGE plpgsql;
postgres=# select * fromproc17();
id | name
----+------------
1 | lw
2 | lw2
(2 rows)
Time: 68.372 ms
postgres=# select * fromproc16();
id | name
----+------------
1 | lw
2 | lw2
(2 rows)
Time: 1.357 mstemp talbe result:[postgres@pg95 test_sql]$pgbench -M prepared -n -r -c 2 -j 2 -T 10 -f temporary_ test_1.sql
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 2
number of threads: 2
duration: 10 s
number of transactionsactually processed: 5173
latency average: 3.866 ms
tps = 517.229191 (includingconnections establishing)
tps = 517.367956 (excludingconnections establishing) statement latencies inmilliseconds:
3.863798 select * from proc17();array result:[postgres@pg95 test_sql]$pgbench -M prepared -n -r -c 2 -j 2 -T 10 -f arrary_test_1. sql
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 2
number of threads: 2
duration: 10 s
number of transactionsactually processed: 149381
latency average: 0.134 ms
tps = 14936.875176 (includingconnections establishing)
tps = 14940.234960 (excludingconnections establishing) statement latencies inmilliseconds:
0.132983 select * from proc16();Array is not convenient touse in function, whether there are other methods can be replaced temp table in function
Temporary tables are pretty expensive - from more reasons, and horrible when you use fresh table for two rows only. More if you recreate it every transaction.
More often pattern is create first and delete repeatedly. Better don't use temp tables when it is necessary. It is one reason why PostgreSQL supports a arrays. Partially - PostgreSQL arrays are analogy to T-SQL memory tables.
Regards
Pavel