I have a database with a table whose primary key is a serial
column, or a column with a locally-computed default value that prevents conflicts, for instance:
CREATE TABLE foo (
foo_id serial PRIMARY KEY,
foo_name text
);
I also have a second database, where I use the postgres_fdw
foreign data wrapper to access the table in the first database.
I’d like to insert a row in the foreign table in the second database, without specifying a value for the primary key, letting the remote server "choose" a value in a conflict-free way.
Unfortunately, whenever I try to insert data in the foreign table, without selecting the primary key column, postgres_fdw
tries to insert rows with NULL
s for the columns that weren’t selected, without using the server-defined default values. Hence it fails as the primary key is defined NOT NULL
.
As far as I can see, there is not way to use a foreign sequence. I’ve seen that I can define a default value when I create a foreign table but, as I understand it, it is implemented on the local side.
Is there a way I can insert a row in a foreign table and let the foreign server use its default values?
1 Answer 1
Yes, PostgreSQL always uses the local default value, and there is no way to change that. I can think of two remedies:
Define a special default value like -1 for the
id
column of the foreign table, and on the remote table define aBEFORE INSERT
triggerWHEN (NEW.id = -1)
that replaces the special value with the next sequence value. Using a trigger with aWHEN
clause has the advantage that the trigger function is only called when necessary.Define a foreign table that does not include the
id
column. If it is too awkward toINSERT
into one foreign table andSELECT
from another one, define a view on the latter that has anINSTEAD OF INSERT
trigger that inserts into the former table instead.
-
Thanks a lot for your help. I find your first option a little hackish, but I’m fine with the second option, that works fine.user2233709– user22337092023年03月31日 17:23:25 +00:00Commented Mar 31, 2023 at 17:23
Explore related questions
See similar questions with these tags.