| From: | Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | postgres_fdw: virtual generated columns vs. IMPORT FOREIGN SCHEMA |
| Date: | 2026-11-06 13:20:54 |
| Message-ID: | CAPmGK15iokjftof+H31322JWK5ModPy91J-AAqeP3htDy+Orkg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
While working on something else, I noticed $SUBJECT:
create server loopback foreign data wrapper postgres_fdw options
(dbname 'postgres');
create user mapping for current_user server loopback;
create schema import_source;
create table import_source.import_table (a int, b int generated always
as (a * 2) virtual);
create schema import_dest;
import foreign schema import_source from server loopback into import_dest;
explain verbose insert into import_dest.import_table (a) values (1);
QUERY PLAN
----------------------------------------------------------------------------
Insert on import_dest.import_table (cost=0.00..0.01 rows=0 width=0)
Remote SQL: INSERT INTO import_source.import_table(a, b) VALUES ($1, $2)
Batch Size: 1
-> Result (cost=0.00..0.01 rows=1 width=8)
Output: 1, NULL::integer
(5 rows)
insert into import_dest.import_table (a) values (1);
ERROR: cannot insert a non-DEFAULT value into column "b"
DETAIL: Column "b" is a generated column.
CONTEXT: remote SQL command: INSERT INTO
import_source.import_table(a, b) VALUES ($1, $2)
The root cause of this is that we fail to include the virtual
generated expression for column "b" in the foreign table definition
like this:
\d+ import_dest.import_table
Foreign table "import_dest.import_table"
Column | Type | Collation | Nullable | Default | FDW options
| Storage | Stats target | Description
--------+---------+-----------+----------+---------+-------------------+---------+--------------+-------------
a | integer | | | | (column_name 'a')
| plain | |
b | integer | | | | (column_name 'b')
| plain | |
Server: loopback
FDW options: (schema_name 'import_source', table_name 'import_table')
This causes the remote query to try to insert NULL into the column,
despite not being allowed to do so, as shown in the error message.
We actually had the same issue for stored generated columns, and fixed
it by modifying IMPORT FOREIGN SCHEMA to include column (stored)
generated expressions in the definitions of foreign tables imported
from a foreign server (see commit aa769f80e). So I fixed this by just
expanding that handling to cover the virtual-generated-columns case.
Attached is a patch for that.
I don’t want to rush this, so I will push/backpatch the patch after
the freeze, if no objections.
Best regards,
Etsuro Fujita
| Attachment | Content-Type | Size |
|---|---|---|
| fix-IFS-virtual-gen-col-handling.patch | application/octet-stream | 5.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Ilia Evdokimov | 2026-11-06 13:17:01 | Re: Show 'Rows Removed by Filter' with two fractional digits in EXPLAIN |