postgresql/src/backend/catalog/system_functions.sql

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

369 lines
10 KiB
MySQL
Raw Normal View History

/*
* PostgreSQL System Functions
*
* Copyright (c) 1996-2026, PostgreSQL Global Development Group
*
* src/backend/catalog/system_functions.sql
*
* This file redefines certain built-in functions that are impractical
* to fully define in pg_proc.dat. In most cases that's because they use
Simplify creation of built-in functions with default arguments. Up to now, to create such a function, one had to make a pg_proc.dat entry and then overwrite it with a CREATE OR REPLACE command in system_functions.sql. That's error-prone (cf. bug #19409) and results in leaving dead rows in the initial contents of pg_proc. Manual maintenance of pg_node_tree strings seems entirely impractical, and parsing expressions during bootstrap would be extremely difficult as well. But Andres Freund observed that all the current use-cases are simple constants, and building a Const node is well within the capabilities of bootstrap mode. So this patch invents a special case: if bootstrap mode is asked to ingest a non-null value for pg_proc.proargdefaults (which would otherwise fail in pg_node_tree_in), it parses the value as an array literal and then feeds the element strings to the input functions for the corresponding parameter types. Then we can build a suitable pg_node_tree string with just a few more lines of code. This allows removing all the system_functions.sql entries that are just there to set up default arguments, replacing them with proargdefaults fields in pg_proc.dat entries. The old technique remains available in case someone needs a non-constant default. The initial contents of pg_proc are demonstrably the same after this patch, except that (1) json_strip_nulls and jsonb_strip_nulls now have the correct provolatile setting, as per bug #19409; (2) pg_terminate_backend, make_interval, and drandom_normal now have defaults that don't include a type coercion, which is how they should have been all along. In passing, remove some unused entries from bootstrap.c's TypInfo[] array. I had to add some new ones because we'll now need an entry for each default-possessing system function parameter, but we shouldn't carry more than we need there; it's just a maintenance gotcha. Bug: #19409 Reported-by: Lucio Chiessi <lucio.chiessi@trustly.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Author: Andrew Dunstan <andrew@dunslane.net> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/183292bb-4891-4c96-a3ca-e78b5e0e1358@dunslane.net Discussion: https://postgr.es/m/19409-e16cd2605e59a4af@postgresql.org
2026-02-18 14:14:44 -05:00
* SQL-standard function bodies and/or default expressions. (But defaults
* that are just constants can be entered in pg_proc.dat.) The node
* tree representations of those are too unreadable, platform-dependent,
* and changeable to want to deal with them manually. Hence, we put stub
* definitions of such functions into pg_proc.dat and then replace them
* here. The stub definitions would be unnecessary were it not that we'd
* like these functions to have stable OIDs, the same as other built-in
Simplify creation of built-in functions with non-default ACLs. Up to now, to create such a function, one had to make a pg_proc.dat entry and then modify it with GRANT/REVOKE commands, which we put in system_functions.sql. That seems a little ugly though, because it violates the idea of having a single source of truth about the initial contents of pg_proc, and it results in leaving dead rows in the initial contents of pg_proc. This patch improves matters by allowing aclitemin to work during early bootstrap, before pg_authid has been loaded. On the same principle that we use for early access to pg_type details, put a table of known built-in role names into bootstrap.c, and use that in bootstrap mode. To create a built-in function with a non-default ACL, one should write the desired ACL list in its pg_proc.dat entry, using a simplified version of aclitemout's notation: omit the grantor (if it is the bootstrap superuser, which it pretty much always should be) and spell the bootstrap superuser's name as POSTGRES, similarly to the notation used elsewhere in src/include/catalog. This results in entries like proacl => '{POSTGRES=X,pg_monitor=X}' which shows that we've revoked public execute permissions and instead granted that to pg_monitor. In addition to fixing up pg_proc.dat entries, I got rid of some role grants that had been stuck into system_functions.sql, and instead put them into a new file pg_auth_members.dat; that seems like a far less random place to put the information. The correctness of the data changes can be verified by comparing the initial contents of pg_proc and pg_auth_members before and after. pg_proc should match exactly, but the OID column of pg_auth_members will probably be different because those OIDs now get assigned a little earlier in bootstrap. (I forced a catversion bump out of caution, but it wasn't really necessary.) Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/183292bb-4891-4c96-a3ca-e78b5e0e1358@dunslane.net
2026-03-05 17:43:09 -05:00
* functions. (That's important, for example, to their treatment by
* postgres_fdw.)
*
* Note: this file is read in single-user -j mode, which means that the
* command terminator is semicolon-newline-newline; whenever the backend
* sees that, it stops and executes what it's got. If you write a lot of
* statements without empty lines between, they'll all get quoted to you
* in any error message about one of them, so don't do that. Also, you
* cannot write a semicolon immediately followed by an empty line in a
* string literal (including a function body!) or a multiline comment.
*/
CREATE OR REPLACE FUNCTION lpad(text, integer)
RETURNS text
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN lpad($1, $2, ' ');
CREATE OR REPLACE FUNCTION rpad(text, integer)
RETURNS text
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN rpad($1, $2, ' ');
CREATE OR REPLACE FUNCTION "substring"(text, text, text)
RETURNS text
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN substring($1, similar_to_escape($2, $3));
CREATE OR REPLACE FUNCTION bit_length(bit)
RETURNS integer
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN length($1);
CREATE OR REPLACE FUNCTION bit_length(bytea)
RETURNS integer
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN octet_length($1) * 8;
CREATE OR REPLACE FUNCTION bit_length(text)
RETURNS integer
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN octet_length($1) * 8;
CREATE OR REPLACE FUNCTION log(numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN log(10, $1);
CREATE OR REPLACE FUNCTION log10(numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN log(10, $1);
CREATE OR REPLACE FUNCTION round(numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN round($1, 0);
CREATE OR REPLACE FUNCTION trunc(numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN trunc($1, 0);
CREATE OR REPLACE FUNCTION numeric_pl_pg_lsn(numeric, pg_lsn)
RETURNS pg_lsn
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION path_contain_pt(path, point)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN on_ppath($2, $1);
CREATE OR REPLACE FUNCTION age(timestamptz)
RETURNS interval
LANGUAGE sql
STABLE PARALLEL SAFE STRICT COST 1
RETURN age(cast(current_date as timestamptz), $1);
CREATE OR REPLACE FUNCTION age(timestamp)
RETURNS interval
LANGUAGE sql
STABLE PARALLEL SAFE STRICT COST 1
RETURN age(cast(current_date as timestamp), $1);
CREATE OR REPLACE FUNCTION date_part(text, date)
RETURNS double precision
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN date_part($1, cast($2 as timestamp));
CREATE OR REPLACE FUNCTION timestamptz(date, time)
RETURNS timestamptz
LANGUAGE sql
STABLE PARALLEL SAFE STRICT COST 1
RETURN cast(($1 + $2) as timestamptz);
CREATE OR REPLACE FUNCTION timedate_pl(time, date)
RETURNS timestamp
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION timetzdate_pl(timetz, date)
RETURNS timestamptz
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION interval_pl_time(interval, time)
RETURNS time
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION interval_pl_date(interval, date)
RETURNS timestamp
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION interval_pl_timetz(interval, timetz)
RETURNS timetz
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION interval_pl_timestamp(interval, timestamp)
RETURNS timestamp
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION interval_pl_timestamptz(interval, timestamptz)
RETURNS timestamptz
LANGUAGE sql
STABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION integer_pl_date(integer, date)
RETURNS date
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION "overlaps"(timestamptz, timestamptz,
timestamptz, interval)
RETURNS boolean
LANGUAGE sql
STABLE PARALLEL SAFE COST 1
RETURN ($1, $2) overlaps ($3, ($3 + $4));
CREATE OR REPLACE FUNCTION "overlaps"(timestamptz, interval,
timestamptz, interval)
RETURNS boolean
LANGUAGE sql
STABLE PARALLEL SAFE COST 1
RETURN ($1, ($1 + $2)) overlaps ($3, ($3 + $4));
CREATE OR REPLACE FUNCTION "overlaps"(timestamptz, interval,
timestamptz, timestamptz)
RETURNS boolean
LANGUAGE sql
STABLE PARALLEL SAFE COST 1
RETURN ($1, ($1 + $2)) overlaps ($3, $4);
CREATE OR REPLACE FUNCTION "overlaps"(timestamp, timestamp,
timestamp, interval)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE COST 1
RETURN ($1, $2) overlaps ($3, ($3 + $4));
CREATE OR REPLACE FUNCTION "overlaps"(timestamp, interval,
timestamp, timestamp)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE COST 1
RETURN ($1, ($1 + $2)) overlaps ($3, $4);
CREATE OR REPLACE FUNCTION "overlaps"(timestamp, interval,
timestamp, interval)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE COST 1
RETURN ($1, ($1 + $2)) overlaps ($3, ($3 + $4));
CREATE OR REPLACE FUNCTION "overlaps"(time, interval,
time, interval)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE COST 1
RETURN ($1, ($1 + $2)) overlaps ($3, ($3 + $4));
CREATE OR REPLACE FUNCTION "overlaps"(time, time,
time, interval)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE COST 1
RETURN ($1, $2) overlaps ($3, ($3 + $4));
CREATE OR REPLACE FUNCTION "overlaps"(time, interval,
time, time)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE COST 1
RETURN ($1, ($1 + $2)) overlaps ($3, $4);
CREATE OR REPLACE FUNCTION int8pl_inet(bigint, inet)
RETURNS inet
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 + $1;
CREATE OR REPLACE FUNCTION xpath(text, xml)
RETURNS xml[]
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN xpath($1, $2, '{}'::text[]);
CREATE OR REPLACE FUNCTION xpath_exists(text, xml)
RETURNS boolean
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN xpath_exists($1, $2, '{}'::text[]);
CREATE OR REPLACE FUNCTION pg_sleep_for(interval)
RETURNS void
LANGUAGE sql
PARALLEL SAFE STRICT COST 1
RETURN pg_sleep(extract(epoch from clock_timestamp() + $1) -
extract(epoch from clock_timestamp()));
CREATE OR REPLACE FUNCTION pg_sleep_until(timestamptz)
RETURNS void
LANGUAGE sql
PARALLEL SAFE STRICT COST 1
RETURN pg_sleep(extract(epoch from $1) -
extract(epoch from clock_timestamp()));
CREATE OR REPLACE FUNCTION pg_relation_size(regclass)
RETURNS bigint
LANGUAGE sql
PARALLEL SAFE STRICT COST 1
RETURN pg_relation_size($1, 'main');
CREATE OR REPLACE FUNCTION obj_description(oid, name)
RETURNS text
LANGUAGE sql
STABLE PARALLEL SAFE STRICT
BEGIN ATOMIC
select description from pg_description
where objoid = $1 and
classoid = (select oid from pg_class where relname = $2 and
relnamespace = 'pg_catalog'::regnamespace) and
objsubid = 0;
END;
CREATE OR REPLACE FUNCTION shobj_description(oid, name)
RETURNS text
LANGUAGE sql
STABLE PARALLEL SAFE STRICT
BEGIN ATOMIC
select description from pg_shdescription
where objoid = $1 and
classoid = (select oid from pg_class where relname = $2 and
relnamespace = 'pg_catalog'::regnamespace);
END;
CREATE OR REPLACE FUNCTION obj_description(oid)
RETURNS text
LANGUAGE sql
STABLE PARALLEL SAFE STRICT
BEGIN ATOMIC
select description from pg_description where objoid = $1 and objsubid = 0;
END;
CREATE OR REPLACE FUNCTION col_description(oid, integer)
RETURNS text
LANGUAGE sql
STABLE PARALLEL SAFE STRICT
BEGIN ATOMIC
select description from pg_description
where objoid = $1 and classoid = 'pg_class'::regclass and objsubid = $2;
END;
CREATE OR REPLACE FUNCTION ts_debug(config regconfig, document text,
OUT alias text,
OUT description text,
OUT token text,
OUT dictionaries regdictionary[],
OUT dictionary regdictionary,
OUT lexemes text[])
RETURNS SETOF record
LANGUAGE sql
STABLE PARALLEL SAFE STRICT
BEGIN ATOMIC
select
tt.alias AS alias,
tt.description AS description,
parse.token AS token,
ARRAY ( SELECT m.mapdict::regdictionary
FROM pg_ts_config_map AS m
WHERE m.mapcfg = $1 AND m.maptokentype = parse.tokid
ORDER BY m.mapseqno )
AS dictionaries,
( SELECT mapdict::regdictionary
FROM pg_ts_config_map AS m
WHERE m.mapcfg = $1 AND m.maptokentype = parse.tokid
ORDER BY ts_lexize(mapdict, parse.token) IS NULL, m.mapseqno
LIMIT 1
) AS dictionary,
( SELECT ts_lexize(mapdict, parse.token)
FROM pg_ts_config_map AS m
WHERE m.mapcfg = $1 AND m.maptokentype = parse.tokid
ORDER BY ts_lexize(mapdict, parse.token) IS NULL, m.mapseqno
LIMIT 1
) AS lexemes
FROM ts_parse(
(SELECT cfgparser FROM pg_ts_config WHERE oid = $1 ), $2
) AS parse,
ts_token_type(
(SELECT cfgparser FROM pg_ts_config WHERE oid = $1 )
) AS tt
WHERE tt.tokid = parse.tokid;
END;
CREATE OR REPLACE FUNCTION ts_debug(document text,
OUT alias text,
OUT description text,
OUT token text,
OUT dictionaries regdictionary[],
OUT dictionary regdictionary,
OUT lexemes text[])
RETURNS SETOF record
LANGUAGE sql
STABLE PARALLEL SAFE STRICT
BEGIN ATOMIC
SELECT * FROM ts_debug(get_current_ts_config(), $1);
END;