简体   繁体   English

pg_restore 错误:function raise_err(unknown) 不存在

[英]pg_restore error: function raise_err(unknown) does not exist

I run a daily backup of my database using pg_dump and pg_restore that recently stopped working after I pushed an update.我使用 pg_dump 和 pg_restore 运行我的数据库的每日备份,这些备份在我推送更新后最近停止工作。

I have a function validate_id that's a Case/When statement just as a quick check for some the data that has integrity issues.我有一个 function validate_id ,它是一个Case/When语句,用于快速检查一些存在完整性问题的数据。 Looks something like this:看起来像这样:

CREATE OR REPLACE FUNCTION validate_id(
    _string text,
    _type type
) RETURNS boolean AS
$$
SELECT
    CASE WHEN (stuff) THEN TRUE
    WHEN (other stuff) THEN TRUE
    When (more stuff) THEN raise_err('Not an accepted type, the accepted types are: x y z')
ELSE FALSE
$$
LANGUAGE SQL;

Since I added this function, when I dump using this command:由于我添加了这个 function,当我使用这个命令转储时:

pg_dump -U postgres -h ipaddress -p 5432 -w -F t databaseName > backupsfolder/databaseName.tar

When I use this command:当我使用这个命令时:

pg_restore -U postgres -h localhost -p 5432 -d postgres -C "backupsfolder/databaseName.tar"

As of two days ago, this now throws an error:截至两天前,这现在引发了一个错误:

pg_restore: error: could not execute query: ERROR: function raise_err(unknown) does not exist

I'm pretty lost on what to do.我很不知道该怎么做。 I think what might be going on is that it's trying to restore this function before it restores the raise_err function.我认为可能发生的事情是它试图在恢复 raise_err function 之前恢复这个raise_err Which I thought was built-in to postgres (I can SELECT raise_err('Hello, World'); ).我认为这是 postgres 内置的(我可以SELECT raise_err('Hello, World'); )。 Is this possible?这可能吗? Is it my CASE statement because I need to return only Booleans?这是我的CASE语句,因为我只需要返回布尔值吗? All of the permissions seem correct and restoring with previous backups works fine.所有权限似乎都是正确的,并且使用以前的备份进行恢复工作正常。

The problem is that raise_err is not schema qualified in your function code.问题是raise_err在您的 function 代码中不是架构限定的。

This is potentially dangerous: a malicious user could create his own function raise_err and set search_path so that the wrong function is called.这是潜在的危险:恶意用户可以创建自己的 function raise_err并设置search_path以便调用错误的 function。

Since pg_restore is typically run by a superuser, this can be a security problem.由于pg_restore通常由超级用户运行,这可能是一个安全问题。 Imagine such a function being used in an index definition!想象一下在索引定义中使用这样的 function!

For these reasons pg_dump and pg_restore set an empty search_path in current versions of PostgreSQL.由于这些原因, pg_dumppg_restore在 PostgreSQL 的当前版本中设置了一个空的search_path

The solution to your problem is to explicitly use the function's schema in your SQL statement.您的问题的解决方案是在 SQL 语句中明确使用函数的模式。

I ended up solving this issue by explicitly setting the search paths for both functions, raise_err() and validate_id() to public :我最终通过将raise_err()validate_id()这两个函数的搜索路径显式设置为public来解决这个问题:

ALTER FUNCTION validate_id(text,text) SET search_path=public;
ALTER FUNCTION raise_err(text,text) SET search_path=public;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM