[PATCH] postgres-fdw: column option to override foreign types

From: "Dian M Fay" <dian(dot)m(dot)fay(at)gmail(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] postgres-fdw: column option to override foreign types
Date: 2021-03-01 07:24:01
Message-ID: C9LU294V7K4F.34LRRDU449O45@lamia
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Full use of a custom data type with postgres_fdw currently requires the
type be maintained in both the local and remote databases. `CREATE
FOREIGN TABLE` does not check declared types against the remote table,
but declaring e.g. a remote enum to be local text works only partway, as
seen here. A simple select query against alpha_items returns the enum
values as text; however, *filtering* on the column yields an error.

create database alpha;
create database beta;

\c alpha

create type itemtype as enum ('one', 'two', 'three');
create table items (
id serial not null primary key,
type itemtype not null
);
insert into items (type) values ('one'), ('one'), ('two');

\c beta

create extension postgres_fdw;
create server alpha foreign data wrapper postgres_fdw options (dbname 'alpha', host 'localhost', port '5432');
create user mapping for postgres server alpha options (user 'postgres');

create foreign table alpha_items (
id int,
type text
) server alpha options (table_name 'items');
select * from alpha_items; -- ok
select * from alpha_items where type = 'one';

ERROR: operator does not exist: public.itemtype = text
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
CONTEXT: remote SQL command: SELECT id, type FROM public.items WHERE ((type = 'one'::text))

The attached changeset adds a new boolean option for postgres_fdw
foreign table columns, `use_local_type`. When true, ColumnRefs for the
relevant attribute will be deparsed with a cast to the type defined in
`CREATE FOREIGN TABLE`.

create foreign table alpha_items (
id int,
type text options (use_local_type 'true')
) server alpha options (table_name 'items');
select * from alpha_items where type = 'one'; -- succeeds

This builds and checks, with a new regression test and documentation.

Attachment Content-Type Size
0001-postgres_fdw-column-option-to-override-foreign-types.patch text/plain 6.3 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2021-03-01 07:36:53 Re: archive_command / pg_stat_archiver & documentation
Previous Message Peter Eisentraut 2021-03-01 07:15:42 Re: macOS SIP, next try