Tom*_*ico 2 postgresql plpgsql sql-function psql
我想通过psql在空数据库中加载一些SQL函数:
psql -d my_database -f fuctions.sql --set ON_ERROR_STOP=1
Run Code Online (Sandbox Code Playgroud)
我使用--set ON_ERROR_STOP=1是因为如果脚本包含错误,我希望psql失败.
内容functions.sql是:
CREATE or REPLACE FUNCTION test_function() RETURNS INT AS $$
SELECT id from test_table;
$$ LANGUAGE sql;
Run Code Online (Sandbox Code Playgroud)
我的问题是,psql检查test_table加载函数时是否存在并因此错误而失败:
Run Code Online (Sandbox Code Playgroud)ERROR: relation "test_table" does not exist LINE 2: SELECT id from test_table;
但我不希望psql检查表是否存在,因为我稍后会创建此表.
以下解决方法可行,但我无法使用它们:
Mar*_*our 10
您可以check_function_bodies在创建函数之前将配置变量设置为false.
例如,即使test_table不存在,也应该让您创建测试函数:
BEGIN;
SET LOCAL check_function_bodies TO FALSE;
CREATE or REPLACE FUNCTION test_function() RETURNS INT AS $$
SELECT id from test_table;
$$ LANGUAGE sql;
COMMIT;
Run Code Online (Sandbox Code Playgroud)
文档:http://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-CHECK-FUNCTION-BODIES
| 归档时间: |
|
| 查看次数: |
1653 次 |
| 最近记录: |