一、[OR REPLACE] 更新函数介绍
OR REPLACE 的作用为函数存在时则替换,建立 PL/pgsql 函数时如果不带 OR REPLACE 关键字, 则遇到函数已经存在,系统会报错,如下所示:
postgres=# select prosrc from pg_proc where proname='f'; prosrc————————– + BEGIN + RAISE NOTICE 'tdsql_pg';+ END; +(1 行记录)postgres=# CREATE FUNCTION f() RETURNS VOID ASpostgres-# $$postgres$# BEGINpostgres$# RAISE NOTICE 'Hello ,tdsql_pg';postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;ERROR: function "f" already exists with same argument typespostgres=# CREATE OR REPLACE FUNCTION f() RETURNS VOID ASpostgres-# $$postgres$# BEGINpostgres$# RAISE NOTICE 'Hello ,tdsql_pg';postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=# select prosrc from pg_proc where proname='f'; prosrc——————————— + BEGIN + RAISE NOTICE 'Hello ,tdsql_pg';+ END; +(1 行记录)postgres=# SELECT f();NOTICE: Hello ,tdsql_pg f—(1 行记录)
二、[模式名.]函数名介绍
建立函数名称,模式名可以指定,也可以不指定,不指存则存放在当前模式下,如上面例子就没有指定模式名,则就存放在当前模式下,如下所示:
postgres=# select * from pg_namespace; nspname | nspowner | nspacl——————–+———-+————————————- pg_toast | 10 | pg_temp_1 | 10 | pg_toast_temp_1 | 10 | pg_catalog | 10 | {postgres=UC/postgres,=U/postgres} public | 10 | {postgres=UC/postgres,=UC/postgres} information_schema | 10 | {postgres=UC/postgres,=U/postgres}(6 行记录)postgres=# show search_path; search_path—————- "$user",public(1 行记录)postgres=# select pg_namespace.nspname,pg_proc.prosrc from pg_proc,pg_namespace where pg_proc.pronamespace=pg_namespace.oid and pg_proc.proname='f'; nspname | prosrc———+——————————— public | + | BEGIN + | RAISE NOTICE 'Hello ,tdsql_pg';+ | END; + |(1 行记录)
因为 $user 模式不存在,所以存在 public 模式下。