select * from $1 where ...;
create or replace function doSelect(query text) returns setof record as $code$ begin return query execute query; end; $code$ language plpgsql
work=# select * from doSelect('select relname::text from pg_class') as ds(name text) where name='aa'; name ------ aa (1 )
create or replace function doSelect(query text, p1 text) returns setof record as $code$ begin return query execute query using p1; end; $code$ language plpgsql; create or replace function doSelect(query text, p1 text, p2 text) returns setof record as $code$ begin return query execute query using p1, p2; end; $code$ language plpgsql;
work=# select * from doSelect(format('select table_catalog::text, table_schema::text, table_name::text from %s where table_schema=$1 limit $2::bigint', 'information_schema.tables'), 'public',1::text) as i(table_catalog text, table_schema text, table_name text); table_catalog | table_schema | table_name ---------------+--------------+------------ work | public | aa (1 )
create or replace function doSelect(query text, variadic param text[]) returns setof record as $code$ begin return query execute query using param; end; $code$ language plpgsql
work=# select * from doSelect(format('select table_catalog::text, table_schema::text, table_name::text from %s where table_schema=$1[1] limit $1[2]::bigint', 'information_schema.tables'), 'public',1::text) as i(table_catalog text, table_schema text, table_name text); table_catalog | table_schema | table_name ---------------+--------------+------------ work | public | aa (1 )
work=# select table_name, cnt.cnt from information_schema.tables t, doSelect(format('select count(*) from %s', t.table_name)) as cnt(cnt bigint) where table_schema='public'; table_name | cnt ---------------+---------- aa | 10000000 ttq | 3 report | 12 colltest | 100000 t2 | 100000 tpair | 10000 call | 0 XXXXXXX_locks | 1 t | 2 sbr | 273370 stest | 954 house | 21000 ttn | 1000000 addrobj | 21000 tt1 | 1 tt2 | 1 ttt | 100000 tt | 1 t1 | 10000 (19 )
create table tableasparameter as select n from generate_series(1,1000) as gs(n); work=# explain analyze select tt.* from generate_series(1,10000), tableasparameter tt; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.00..12526.50 rows=1000000 width=4) (actual time=1.919..1347.240 rows=10000000 loops=1) -> Function Scan on generate_series (cost=0.00..10.00 rows=1000 width=0) (actual time=1.896..2.508 rows=10000 loops=1) -> Materialize (cost=0.00..19.00 rows=1000 width=4) (actual time=0.000..0.042 rows=1000 loops=10000) -> Seq Scan on tableasparameter tt (cost=0.00..14.00 rows=1000 width=4) (actual time=0.017..0.219 rows=1000 loops=1) Planning time: 0.068 ms Execution time: 1648.586 ms (6 ) work=# explain analyze work-# select tt.* from generate_series(1,10000), doSelect('select * from tableasparameter') as tt(val int); QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.25..20010.25 rows=1000000 width=4) (actual time=1.294..1401.768 rows=10000000 loops=1) -> Function Scan on generate_series (cost=0.00..10.00 rows=1000 width=0) (actual time=1.033..1.590 rows=10000 loops=1) -> Function Scan on doselect tt (cost=0.25..10.25 rows=1000 width=4) (actual time=0.000..0.047 rows=1000 loops=10000) Planning time: 0.039 ms Execution time: 1705.056 ms (5 )
Source: https://habr.com/ru/post/278019/
All Articles