When / how are default value expression functions bound with regard to search_path?

后端 未结 3 1475
盖世英雄少女心
盖世英雄少女心 2021-01-25 16:06

For testing purposes, I provide my own implementation of the now() function which is public.now(). Using search_path to override the defau

3条回答
  •  难免孤独
    2021-01-25 16:41

    Default values are parsed at creation time (early binding!). What you see in psql, pgAdmin or other clients is a text representation but, in fact, the OID of the function now() at the time of creating the column default is stored in the system catalog pg_attrdef. I quote:

    adbin   pg_node_tree  The internal representation of the column default value
    adsrc   text          A human-readable representation of the default value
    

    When you change the search_path, that causes Postgres to display the name of the function schema-qualified, since it would not be resolved correctly any more with the current search_path.

    Dump and restore are not concerned with your custom search_path setting. They set it explicitly. So what you see is not related to the the dump / restore cycle.

    Override built-in functions

    Placing public before pg_catalog in the search_path is a game of hazard. Underprivileged users (including yourself) are often allowed to write there and create functions that may inadvertently overrule system functions - with arbitrary (or malicious) outcome.

    You want a dedicated schema with restricted access to override built-in functions. Use something like this instead:

    SET search_path = override, pg_catalog, public;
    

    Details in this related answer on dba.SE.

提交回复
热议问题