Вопрос

Using postgres_fdw, I need to create the foreign tables in a specified schema to prevent name collisions. To isolate the issue I've been having, I set up two test postgres databases on the same cluster, import_test and export_test. Export_test has a table, foreign_schema.aa. On the server import_test, after doing the other FDW prerequisites, I ran:

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

Then:

SELECT * FROM local_schema.aa;

When I do this, I get:

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

If I don't do any schema qualification, as in:

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

And move the aa table to the public schema, the select works just fine.

If the command "SELECT id, dat FROM local_schema."foreign_schema.aa" is literally being run on the remote server, it's obvious why it doesn't work: local_schema."foreign_schema.aa" really doesn't exist on the remote server. For some reason, the postgres_fdw appears to be prepending the name given for table_name with the schema of the foreign table.

I need to specify the schema in the select query, because if I don't it doesn't see the foreign table. Achieving the schema qualification by preceding the select with setting the search path doesn't help either.

Is there anything I am doing wrong? If not, is there a workaround that will let me schema-qualify the foreign table?

EDIT: Per @Craig Ringer's suggestions, here's the self-contained psql input:

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;

Which yields this output:

ERROR:  relation "import_schema.export_schema.aa" does not exist
CONTEXT:  Remote SQL command: SELECT id, dat FROM import_schema."export_schema.aa"
Это было полезно?

Решение

Forgot to come back with resolution. Turns out sometime around the time I posted my bug report, the docs on postgres_fdw were updated. See the section "F.31.1.2. Object Name Options" and the schema_name option on this page: http://www.postgresql.org/docs/current/static/postgres-fdw.html. I quote the mailing list reply:

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

        Thanks,

                Stephen"

So to resolve, just specify the foreign schema name in the schema_name option parameter.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top