在创建默认约束时,默认函数是“绑定的”。显示非限定名称的视图只是对其进行缩写。
这可以通过在遮蔽函数之前和之后插入行来证明:
Set search_path to public,pg_catalog;
Create Temp Table foo (
test date not null default now()
);
Insert Into foo default values;
Create Function public.now() Returns timestamp with time zone Language SQL As $$
-- No idea why I chose this date.
Select '1942-05-09'::timestamp with time zone;
$$;
Insert Into foo default values;
Select * from foo;
请注意,这两行(在函数创建之前和之后插入)都包含今天的日期,而不是假日期。
此外,使用已在范围内的上述函数创建表,然后尝试删除该函数,会导致依赖错误:
Set search_path to public,pg_catalog;
Create Function public.now() Returns timestamp with time zone Language SQL As $$
Select '1942-05-09'::timestamp with time zone;
$$;
Create Temp Table bar (
test date not null default now()
);
Insert Into bar default values;
Select * from bar;
-- Single row containing the dummy date rather than today
Drop Function public.now();
-- ERROR: cannot drop function now() because other objects depend on it
如果绑定只发生在插入时,就不会有这种依赖。