information_schema.routine_routine_usage reports planner SUPPORT functions as routine calls

Started by Noname6 days ago1 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Noname
wangyang25@otcaix.iscas.ac.cn

PostgreSQL server version: PostgreSQL 19.0, REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10
psql version: PostgreSQL 19.0
Platform: Ubuntu 24.04 LTS, Linux x86_64, kernel 7.0.0-38-generic, glibc 2.39; GCC 13.3.0
Build options: --without-readline --without-zlib --without-icu --without-llvm

Cluster setup: Run as the postgres operating-system user. Set PGDATA to a new empty data directory and SOCKET to a local socket directory, then run:

export PGDATA=/path/to/pgdata
export SOCKET=/path/to/socket
mkdir -p "$SOCKET"
initdb -D "$PGDATA" --no-locale -E UTF8 --auth-local=trust --auth-host=reject

Start the server with:

pg_ctl -D "$PGDATA" -l "$PGDATA/server.log" -o "-c listen_addresses= -c unix_socket_directories=$SOCKET -c port=55488 -c max_connections=10 -c shared_buffers=32MB -c wal_level=logical -c max_replication_slots=10 -c max_wal_senders=10" -w start

Connect to the postgres database with PGHOST=$SOCKET PGPORT=55488 psql -X -U postgres -d postgres.

Summary

information_schema.routine_routine_usage reports a planner support function as used by a SQL routine whose body does not call it. The view documentation says it identifies functions or procedures used in the SQL body or parameter default expressions.

Reproduction

Run as a superuser because creating a function with an internal language and the SUPPORT attribute requires superuser privileges. The transaction leaves no objects behind.

BEGIN;
CREATE SCHEMA rru_support_test;

CREATE FUNCTION rru_support_test.support_stub(internal)
RETURNS internal
LANGUAGE internal
AS 'array_unnest_support';

CREATE FUNCTION rru_support_test.no_body_call(integer)
RETURNS integer
LANGUAGE SQL IMMUTABLE
SUPPORT rru_support_test.support_stub
RETURN 1;

CREATE FUNCTION rru_support_test.body_helper(integer)
RETURNS integer
LANGUAGE SQL IMMUTABLE
RETURN $1;

CREATE FUNCTION rru_support_test.explicit_call(x integer)
RETURNS integer
LANGUAGE SQL IMMUTABLE
RETURN rru_support_test.body_helper(x);

SELECT using_r.routine_name AS using_routine,
used_r.routine_name AS reported_used_routine
FROM information_schema.routine_routine_usage AS u
JOIN information_schema.routines AS using_r
ON using_r.specific_schema = u.specific_schema
AND using_r.specific_name = u.specific_name
JOIN information_schema.routines AS used_r
ON used_r.routine_schema = u.routine_schema
AND used_r.specific_name = u.routine_name
WHERE using_r.routine_schema = 'rru_support_test'
ORDER BY using_r.routine_name, used_r.specific_name;

ROLLBACK;

Observed result

The result rows were:

explicit_call | body_helper
no_body_call | support_stub

The body of no_body_call is only RETURN 1 and contains no routine call or parameter default expression. The same output occurred in two independent psql sessions on PostgreSQL 18.7 (REL_18_7, commit f5c41f6681c910b3f69951bf6fb97c6d74605d25) and two independent psql sessions on PostgreSQL 19.0 (REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10).

Expected result

Only explicit_call -> body_helper should be listed. PostgreSQL 19 documents routine_routine_usage as identifying routine calls in SQL bodies or parameter default expressions; a planner support function is specified separately by the SUPPORT attribute and is used for optimization.

Documentation:
https://www.postgresql.org/docs/19/infoschema-routine-routine-usage.html
https://www.postgresql.org/docs/19/sql-createfunction.html
https://www.postgresql.org/docs/19/xfunc-optimization.html

Relevant source location: the routine_routine_usage view definition in src/backend/catalog/information_schema.sql joins routine dependencies without distinguishing a SUPPORT dependency from a call in the SQL body or a parameter default. Source at the tested commit: https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/catalog/information_schema.sql#L1448-L1468