Skip to content

Commit

Permalink
Mark immutable functions in information schema as parallel safe
Browse files Browse the repository at this point in the history
Also add opr_sanity check that all preloaded immutable functions are
parallel safe.  (Per discussion, this does not necessarily have to be
true for all possible such functions, but deviations would be unlikely
enough that maintaining such a test is reasonable.)

Reported-by: David Rowley <[email protected]>
Reviewed-by: Robert Haas <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
  • Loading branch information
petere committed Apr 6, 2017
1 parent 4be613f commit 5f21f52
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/backend/catalog/information_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ SET search_path TO information_schema;
/* Expand any 1-D array into a set with integers 1..N */
CREATE FUNCTION _pg_expandarray(IN anyarray, OUT x anyelement, OUT n int)
RETURNS SETOF RECORD
LANGUAGE sql STRICT IMMUTABLE
LANGUAGE sql STRICT IMMUTABLE PARALLEL SAFE
AS 'select $1[s], s - pg_catalog.array_lower($1,1) + 1
from pg_catalog.generate_series(pg_catalog.array_lower($1,1),
pg_catalog.array_upper($1,1),
1) as g(s)';

CREATE FUNCTION _pg_keysequal(smallint[], smallint[]) RETURNS boolean
LANGUAGE sql IMMUTABLE -- intentionally not STRICT, to allow inlining
LANGUAGE sql IMMUTABLE PARALLEL SAFE -- intentionally not STRICT, to allow inlining
AS 'select $1 operator(pg_catalog.<@) $2 and $2 operator(pg_catalog.<@) $1';

/* Given an index's OID and an underlying-table column number, return the
Expand All @@ -66,13 +66,15 @@ $$;
CREATE FUNCTION _pg_truetypid(pg_attribute, pg_type) RETURNS oid
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT CASE WHEN $2.typtype = 'd' THEN $2.typbasetype ELSE $1.atttypid END$$;

CREATE FUNCTION _pg_truetypmod(pg_attribute, pg_type) RETURNS int4
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT CASE WHEN $2.typtype = 'd' THEN $2.typtypmod ELSE $1.atttypmod END$$;
Expand All @@ -82,6 +84,7 @@ $$SELECT CASE WHEN $2.typtype = 'd' THEN $2.typtypmod ELSE $1.atttypmod END$$;
CREATE FUNCTION _pg_char_max_length(typid oid, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand All @@ -97,6 +100,7 @@ $$SELECT
CREATE FUNCTION _pg_char_octet_length(typid oid, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand All @@ -112,6 +116,7 @@ $$SELECT
CREATE FUNCTION _pg_numeric_precision(typid oid, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand All @@ -132,6 +137,7 @@ $$SELECT
CREATE FUNCTION _pg_numeric_precision_radix(typid oid, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand All @@ -143,6 +149,7 @@ $$SELECT
CREATE FUNCTION _pg_numeric_scale(typid oid, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand All @@ -158,6 +165,7 @@ $$SELECT
CREATE FUNCTION _pg_datetime_precision(typid oid, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand All @@ -173,6 +181,7 @@ $$SELECT
CREATE FUNCTION _pg_interval_type(typid oid, mod int4) RETURNS text
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
Expand Down
2 changes: 1 addition & 1 deletion src/include/catalog/catversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
*/

/* yyyymmddN */
#define CATALOG_VERSION_NO 201704061
#define CATALOG_VERSION_NO 201704062

#endif
8 changes: 8 additions & 0 deletions src/test/regress/expected/opr_sanity.out
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,14 @@ order by 1;
lowrite | 955
(13 rows)

-- Check that all immutable functions are marked parallel safe
SELECT p1.oid, p1.proname
FROM pg_proc AS p1
WHERE provolatile = 'i' AND proparallel = 'u';
oid | proname
-----+---------
(0 rows)

-- **************** pg_cast ****************
-- Catch bogus values in pg_cast columns (other than cases detected by
-- oidjoins test).
Expand Down
5 changes: 5 additions & 0 deletions src/test/regress/sql/opr_sanity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ and pronamespace = (select oid from pg_catalog.pg_namespace
where nspname = 'pg_catalog')
order by 1;

-- Check that all immutable functions are marked parallel safe
SELECT p1.oid, p1.proname
FROM pg_proc AS p1
WHERE provolatile = 'i' AND proparallel = 'u';


-- **************** pg_cast ****************

Expand Down

0 comments on commit 5f21f52

Please sign in to comment.