【问题标题】:Why is postgres_fdw double-qualifying schemas?为什么 postgres_fdw 是双重限定模式?
【发布时间】:2014-01-17 18:53:41
【问题描述】:

使用 postgres_fdw,我需要在指定的模式中创建外部表以防止名称冲突。为了隔离我遇到的问题,我在同一个集群上设置了两个测试 postgres 数据库,import_test 和 export_test。 Export_test 有一个表 foreign_schema.aa。在服务器 import_test 上,在完成其他 FDW 先决条件后,我运行:

CREATE FOREIGN TABLE local_schema.aa(
    id serial NOT NULL,
    dat text
) SERVER export_server OPTIONS (table_name 'foreign_schema.aa');

然后:

SELECT * FROM local_schema.aa;

当我这样做时,我得到:

ERROR:  relation "local_schema.foreign_schema.aa" does not exist
CONTEXT:  Remote SQL command: SELECT id, dat FROM local_schema."foreign_schema.aa"

如果我不做任何架构限定,如:

CREATE FOREIGN TABLE aa(
    id serial NOT NULL,
    dat text
) SERVER export_server OPTIONS (table_name 'aa');

并将 aa 表移动到公共模式,选择工作正常。

如果命令 "SELECT id, dat FROM local_schema."foreign_schema.aa" 确实在远程服务器上运行,很明显它为什么不起作用:local_schema."foreign_schema.aa" 确实不存在远程服务器。出于某种原因,postgres_fdw 似乎在为 table_name 提供的名称前面加上外部表的模式。

我需要在选择查询中指定架构,因为如果我不这样做,它就看不到外部表。通过在选择之前设置搜索路径来实现模式限定也无济于事。

我做错了什么吗?如果没有,是否有一种解决方法可以让我对外部表进行模式限定?

编辑:根据@Craig Ringer 的建议,这是独立的 psql 输入:

CREATE USER test_user SUPERUSER PASSWORD 'password';
SET ROLE test_user;
CREATE DATABASE import;
CREATE DATABASE export;
\c export test_user
CREATE SCHEMA export_schema;
CREATE TABLE export_schema.aa (
    id serial PRIMARY KEY,
    dat text
);
\c import test_user
CREATE EXTENSION postgres_fdw;
CREATE SERVER export_server 
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (host 'localhost', dbname 'export', port '5432');
CREATE USER MAPPING FOR test_user 
    SERVER export_server
    OPTIONS (user 'test_user', password 'password');
CREATE SCHEMA import_schema;        
CREATE FOREIGN TABLE import_schema.aa(
    id serial NOT NULL,
    dat text
) SERVER export_server OPTIONS (table_name 'export_schema.aa');
SELECT * FROM import_schema.aa;

产生这个输出:

ERROR:  relation "import_schema.export_schema.aa" does not exist
CONTEXT:  Remote SQL command: SELECT id, dat FROM import_schema."export_schema.aa"

【问题讨论】:

  • 请生成一个独立的测试用例作为可以从psql 运行的单个 SQL 脚本,并将其作为错误报告提交给 pgsql-bugs 邮件列表。链接回这篇文章以了解上下文。
  • @CraigRinger 我遇到了同样的问题。这有没有得到解决?
  • 我什么都没注意到。试试archives.postgresql.org
  • @DavidS 查看我的回答。

标签: postgresql postgresql-9.3 foreign-data-wrapper


【解决方案1】:

忘记带分辨率回来了。结果是在我发布错误报告的时候,postgres_fdw 上的文档已经更新。请参阅“F.31.1.2. 对象名称选项”部分和此页面上的 schema_name 选项:http://www.postgresql.org/docs/current/static/postgres-fdw.html。 我引用邮件列表的回复:

 "Use: OPTIONS (schema_name 'export_schema', table_name 'aa'); above.

        Thanks,

                Stephen"

所以要解决,只需在 schema_name 选项参数中指定外部模式名称。

【讨论】:

  • OPTIONS之间应该有逗号。
猜你喜欢
  • 2023-02-22
  • 2016-05-24
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2015-08-05
  • 1970-01-01
  • 2012-11-10
相关资源
最近更新 更多