Skip to content

Instantly share code, notes, and snippets.

@snaga
Created July 6, 2016 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snaga/362a965683fb2581bc693991b1fcf721 to your computer and use it in GitHub Desktop.
Save snaga/362a965683fb2581bc693991b1fcf721 to your computer and use it in GitHub Desktop.
parallel safe/unsafe functions
testdb=# select version();
version
-------------------------------------------------------------------------------------------------------------
PostgreSQL 9.6beta2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17), 64-bit
(1 row)
testdb=# create table t1 as
testdb-# select generate_series(1,10000000) c1;
SELECT 10000000
testdb=# set force_parallel_mode = true;
SET
testdb=# set max_parallel_workers_per_gather = 4;
SET
testdb=# create or replace function f1_safe(c int8)
testdb-# returns int8
testdb-# as
testdb-# $$
testdb$# begin
testdb$# return c;
testdb$# end
testdb$# $$
testdb-# parallel safe
testdb-# language 'plpgsql';
CREATE FUNCTION
testdb=# create or replace function f1_unsafe(c int8)
testdb-# returns int8
testdb-# as
testdb-# $$
testdb$# begin
testdb$# return c;
testdb$# end
testdb$# $$
testdb-# parallel unsafe
testdb-# language 'plpgsql';
CREATE FUNCTION
testdb=# create or replace function f1_safe_unsafe(c int8)
testdb-# returns int8
testdb-# as
testdb-# $$
testdb$# begin
testdb$# return f1_unsafe(c);
testdb$# end
testdb$# $$
testdb-# parallel safe
testdb-# language 'plpgsql';
CREATE FUNCTION
testdb=# create or replace function f1_unsafe_unsafe(c int8)
testdb-# returns int8
testdb-# as
testdb-# $$
testdb$# begin
testdb$# return f1_unsafe(c);
testdb$# end
testdb$# $$
testdb-# parallel unsafe
testdb-# language 'plpgsql';
CREATE FUNCTION
testdb=# explain verbose select f1_safe(c1) from t1;
QUERY PLAN
----------------------------------------------------------------------------
Gather (cost=0.00..0.00 rows=0 width=0)
Output: (f1_safe((c1)::bigint))
Workers Planned: 1
Single Copy: true
-> Seq Scan on public.t1 (cost=0.00..3006098.50 rows=11283240 width=8)
Output: f1_safe((c1)::bigint)
(6 rows)
testdb=# explain verbose select f1_unsafe(c1) from t1;
QUERY PLAN
----------------------------------------------------------------------
Seq Scan on public.t1 (cost=0.00..3006098.50 rows=11283240 width=8)
Output: f1_unsafe((c1)::bigint)
(2 rows)
testdb=# explain verbose select f1_safe_unsafe(c1) from t1;
QUERY PLAN
----------------------------------------------------------------------------
Gather (cost=0.00..0.00 rows=0 width=0)
Output: (f1_safe_unsafe((c1)::bigint))
Workers Planned: 1
Single Copy: true
-> Seq Scan on public.t1 (cost=0.00..3006098.50 rows=11283240 width=8)
Output: f1_safe_unsafe((c1)::bigint)
(6 rows)
testdb=# explain verbose select f1_unsafe_unsafe(c1) from t1;
QUERY PLAN
----------------------------------------------------------------------
Seq Scan on public.t1 (cost=0.00..3006098.50 rows=11283240 width=8)
Output: f1_unsafe_unsafe((c1)::bigint)
(2 rows)
testdb=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment