我有以下 postgres 功能
CREATE OR REPLACE FUNCTION refresh_materialized_view(name)
RETURNS integer AS
$BODY$
DECLARE
_table_name ALIAS FOR $1;
_entry materialized_views%ROWTYPE;
_result INT;
BEGIN
SELECT * INTO _entry FROM materialized_views WHERE table_name = _table_name;
BEGIN;
EXECUTE 'CREATE TEMP TABLE new_materialized_view_rows ON COMMIT DROP AS SELECT * FROM ' || _entry.view_name;
EXECUTE 'TRUNCATE TABLE ' || _table_name;
EXECUTE 'INSERT INTO ' || _table_name || ' SELECT * FROM new_materialized_view_rows';
UPDATE materialized_views
SET last_refresh = CURRENT_TIMESTAMP
WHERE table_name = _table_name;
COMMIT;
EXECUTE 'ANALYZE ' || table_name;
RETURN 1;
END
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER;
ALTER FUNCTION refresh_materialized_view(name) OWNER TO portal;
该函数是从非事务性上下文中调用的,因此我附上了更新数据的语句
BEGIN;
COMMIT;
以便这些语句以原子方式执行。但是,当我执行上面的脚本时,出现以下错误:
ERROR: syntax error at or near ";"
LINE 16: BEGIN;