inet_client_addr()
is a system information function.
It is located in the schema pg_catalog
like other built-in functions (except for additional modules).
pg_catalog
is automatically part of the search_path
search_path
. Per documentation:
In addition to
public
and user-created schemas, each database contains apg_catalog
schema, which contains the system tables and all the built-in data types, functions, and operators.pg_catalog
is always effectively part of the search path. If it is not named explicitly in the path then it is implicitly searched before searching the path's schemas. This ensures that built-in names will always be findable. However, you can explicitly placepg_catalog
at the end of your search path if you prefer to have user-defined names override built-in names.
Bold emphasis mine.
So we create a dedicated schema and place it before pg_catalog
in the search_path
:
CREATE SCHEMA override;
CREATE OR REPLACE FUNCTION override.inet_client_addr()
RETURNS inet LANGUAGE sql IMMUTABLE PARALLEL SAFE AS
$func$
SELECT '127.0.0.1'::inet
$func$;
SET search_path = override, pg_catalog, public;
Then your call finds your custom override-function first:
SELECT inet_client_addr();
SQL Fiddledb<>fiddle here
Make sure, unprivileged users cannot create objects in the override
schema, or they can play all kinds of tricks on you. That's not the case by default. Per documentation:
No privileges are granted to PUBLIC by default on tables, columns, schemas or tablespaces.
Bold emphasis mine.
Care is needed if the same user should be allowed to create objects in the database.
Per documentation:
The first schema named in the search path is called the current schema. Aside from being the first schema searched, it is also the schema in which new tables will be created if the
CREATE TABLE
command does not specify a schema name.
Always specify a schema name for CREATE
commands, and disallow creating object to all by default to rule out mistakes.