pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedQuery on postgres_fdw extension
8+ messages / 4 participants
[nested] [flat]
* Query on postgres_fdw extension
@ 2021-05-13 10:46 Swathi P <swathi.bluepearl@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Swathi P @ 2021-05-13 10:46 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org; +Cc: pgsql-bugs@lists.postgresql.org
Hello EveryOne,
Hope you are all doing well and staying safe.
Am Swathi, have been working with postgres for last 3 years. Currently we
are working on a project to build a sharding solution with the help of
native postgres_fdw extension. During this process, we have encountered an
issue with postgres_fdw. I tried to give as much as details below on the
issue we are facing, it would be of great help if you can help us overcome
this issue.
- We have Host_A and Host_B , where Host_A is out coordinator node and
Host_B is used as our data node.
- Host_B has a table "Table_B" with a sequence id column which auto
generates the series by default
CREATE TABLE public.table_a
(
id bigint NOT NULL DEFAULT nextval('table_a_id_seq'::regclass),
topic character varying(50) NOT NULL,
CONSTRAINT table_a_pk PRIMARY KEY (id)
)
- on Host_A we have a foreign table created with the ddl below
CREATE FOREIGN TABLE public.table_a
(
id bigint ,
topic character varying(50) NOT NULL,
) SERVER Host_A OPTIONS (schema_name 'public', table_name 'table_a');
- When we try to insert data directly on the table_a while connected to
Host_B, works fine with the auto incremented values for the id column
- But the same insert fails when run from the coordinator node with below
error.
poc=> insert into table_a(topic) values ('test');
ERROR: null value in column "id" of relation "table_a" violates not-null
constraint
DETAIL: Failing row contains (null, test).
CONTEXT: remote SQL command: INSERT INTO public.table_a(id, topic) VALUES
($1, $2)
- If we omit the primary key and not null constraints on the table_a on
remote host (Host_B) , inserts will work fine on Host_A but a NULL value
insertedd for the id column instead of sequence
We are looking for some help on understanding the issue here and the best
possible workaround for the same.
Your help will be greatly appreciated
Thanks
Swathi P
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-13 12:04 Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
parent: Swathi P <swathi.bluepearl@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Bharath Rupireddy @ 2021-05-13 12:04 UTC (permalink / raw)
To: Swathi P <swathi.bluepearl@gmail.com>; +Cc: pgsql-general@lists.postgresql.org; pgsql-bugs@lists.postgresql.org
On Thu, May 13, 2021 at 4:58 PM Swathi P <swathi.bluepearl@gmail.com> wrote:
>
> Hello EveryOne,
>
> Hope you are all doing well and staying safe.
>
> Am Swathi, have been working with postgres for last 3 years. Currently we are working on a project to build a sharding solution with the help of native postgres_fdw extension. During this process, we have encountered an issue with postgres_fdw. I tried to give as much as details below on the issue we are facing, it would be of great help if you can help us overcome this issue.
>
> - We have Host_A and Host_B , where Host_A is out coordinator node and Host_B is used as our data node.
>
> - Host_B has a table "Table_B" with a sequence id column which auto generates the series by default
> CREATE TABLE public.table_a
> (
> id bigint NOT NULL DEFAULT nextval('table_a_id_seq'::regclass),
> topic character varying(50) NOT NULL,
> CONSTRAINT table_a_pk PRIMARY KEY (id)
> )
>
> - on Host_A we have a foreign table created with the ddl below
> CREATE FOREIGN TABLE public.table_a
> (
> id bigint ,
> topic character varying(50) NOT NULL,
> ) SERVER Host_A OPTIONS (schema_name 'public', table_name 'table_a');
>
> - When we try to insert data directly on the table_a while connected to Host_B, works fine with the auto incremented values for the id column
>
> - But the same insert fails when run from the coordinator node with below error.
> poc=> insert into table_a(topic) values ('test');
> ERROR: null value in column "id" of relation "table_a" violates not-null constraint
> DETAIL: Failing row contains (null, test).
> CONTEXT: remote SQL command: INSERT INTO public.table_a(id, topic) VALUES ($1, $2)
>
> - If we omit the primary key and not null constraints on the table_a on remote host (Host_B) , inserts will work fine on Host_A but a NULL value insertedd for the id column instead of sequence
>
> We are looking for some help on understanding the issue here and the best possible workaround for the same.
>
> Your help will be greatly appreciated
I think you need to declare your foreign table column id as "serial"
type instead of "bigint". Below is what I tried from my end.
On remote server:
CREATE USER foreign_user;
DROP TABLE table_a;
CREATE TABLE table_a
(
id serial NOT NULL,
topic character varying(50) NOT NULL,
CONSTRAINT table_a_pk PRIMARY KEY (id)
);
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO foreign_user;
On local server:
DROP EXTENSION postgres_fdw CASCADE;
CREATE EXTENSION postgres_fdw;
CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', port '5433', dbname 'postgres');
CREATE USER MAPPING FOR public
SERVER foreign_server
OPTIONS (user 'foreign_user', password '');
CREATE FOREIGN TABLE table_a (id serial NOT NULL,
topic character varying(50) NOT NULL)
SERVER foreign_server
OPTIONS (schema_name 'public', table_name 'table_a');
SELECT * FROM table_a;
INSERT INTO table_a(topic) VALUES('row1');
INSERT INTO table_a(topic) VALUES('row2');
INSERT INTO table_a(topic) VALUES('row3');
With Regards,
Bharath Rupireddy.
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-14 07:07 Swathi P <swathi.bluepearl@gmail.com>
parent: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
0 siblings, 2 replies; 8+ messages in thread
From: Swathi P @ 2021-05-14 07:07 UTC (permalink / raw)
To: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; +Cc: pgsql-general@lists.postgresql.org; pgsql-bugs@lists.postgresql.org
Thanks Bharath for the details.
In our sharding solution, we have multiple coodinator nodes. If we declare
the table column as serial data type, we might end up having duplicate
values for id column in the table_a in host_b (data node) as cconnections
come from multiple coordinatoor nodes and might end up in duplicate key
violations.
Hence we decided to have the coordinator nodes as stateless and hence
declared the column with no serial/sequence. Let me know if this makes
sense.
Have come across multiple articles on the same issue, i would like to know
if we are doing something wrong here or we have bette workaround for this
issue,
https://www.postgresql.org/message-id/CAP=oouH=FccW4V2zm1VjGit=NZDCXzU2tYBoZe88v3mXrEA9Qg@mail.gmail...
https://stackoverflow.com/questions/66582694/how-to-get-the-generated-id-from-an-insert-to-a-postgre...
https://paquier.xyz/postgresql-2/global-sequences-with-postgres_fdw-and-postgres-core/
Thanks again for your time.
- Swathi
On Thu, May 13, 2021 at 5:34 PM Bharath Rupireddy <
bharath.rupireddyforpostgres@gmail.com> wrote:
> On Thu, May 13, 2021 at 4:58 PM Swathi P <swathi.bluepearl@gmail.com>
> wrote:
> >
> > Hello EveryOne,
> >
> > Hope you are all doing well and staying safe.
> >
> > Am Swathi, have been working with postgres for last 3 years. Currently
> we are working on a project to build a sharding solution with the help of
> native postgres_fdw extension. During this process, we have encountered an
> issue with postgres_fdw. I tried to give as much as details below on the
> issue we are facing, it would be of great help if you can help us overcome
> this issue.
> >
> > - We have Host_A and Host_B , where Host_A is out coordinator node and
> Host_B is used as our data node.
> >
> > - Host_B has a table "Table_B" with a sequence id column which auto
> generates the series by default
> > CREATE TABLE public.table_a
> > (
> > id bigint NOT NULL DEFAULT nextval('table_a_id_seq'::regclass),
> > topic character varying(50) NOT NULL,
> > CONSTRAINT table_a_pk PRIMARY KEY (id)
> > )
> >
> > - on Host_A we have a foreign table created with the ddl below
> > CREATE FOREIGN TABLE public.table_a
> > (
> > id bigint ,
> > topic character varying(50) NOT NULL,
> > ) SERVER Host_A OPTIONS (schema_name 'public', table_name 'table_a');
> >
> > - When we try to insert data directly on the table_a while connected to
> Host_B, works fine with the auto incremented values for the id column
> >
> > - But the same insert fails when run from the coordinator node with
> below error.
> > poc=> insert into table_a(topic) values ('test');
> > ERROR: null value in column "id" of relation "table_a" violates
> not-null constraint
> > DETAIL: Failing row contains (null, test).
> > CONTEXT: remote SQL command: INSERT INTO public.table_a(id, topic)
> VALUES ($1, $2)
> >
> > - If we omit the primary key and not null constraints on the table_a on
> remote host (Host_B) , inserts will work fine on Host_A but a NULL value
> insertedd for the id column instead of sequence
> >
> > We are looking for some help on understanding the issue here and the
> best possible workaround for the same.
> >
> > Your help will be greatly appreciated
>
> I think you need to declare your foreign table column id as "serial"
> type instead of "bigint". Below is what I tried from my end.
>
> On remote server:
> CREATE USER foreign_user;
> DROP TABLE table_a;
> CREATE TABLE table_a
> (
> id serial NOT NULL,
> topic character varying(50) NOT NULL,
> CONSTRAINT table_a_pk PRIMARY KEY (id)
> );
> GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO foreign_user;
>
> On local server:
> DROP EXTENSION postgres_fdw CASCADE;
> CREATE EXTENSION postgres_fdw;
> CREATE SERVER foreign_server
> FOREIGN DATA WRAPPER postgres_fdw
> OPTIONS (host 'localhost', port '5433', dbname 'postgres');
> CREATE USER MAPPING FOR public
> SERVER foreign_server
> OPTIONS (user 'foreign_user', password '');
> CREATE FOREIGN TABLE table_a (id serial NOT NULL,
> topic character varying(50) NOT NULL)
> SERVER foreign_server
> OPTIONS (schema_name 'public', table_name 'table_a');
> SELECT * FROM table_a;
> INSERT INTO table_a(topic) VALUES('row1');
> INSERT INTO table_a(topic) VALUES('row2');
> INSERT INTO table_a(topic) VALUES('row3');
>
> With Regards,
> Bharath Rupireddy.
> EnterpriseDB: http://www.enterprisedb.com
>
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-14 12:10 Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
parent: Swathi P <swathi.bluepearl@gmail.com>
1 sibling, 0 replies; 8+ messages in thread
From: Bharath Rupireddy @ 2021-05-14 12:10 UTC (permalink / raw)
To: Swathi P <swathi.bluepearl@gmail.com>; +Cc: pgsql-general@lists.postgresql.org; pgsql-bugs@lists.postgresql.org
On Fri, May 14, 2021 at 12:37 PM Swathi P <swathi.bluepearl@gmail.com> wrote:
>
> Thanks Bharath for the details.
>
> In our sharding solution, we have multiple coodinator nodes. If we declare the table column as serial data type, we might end up having duplicate values for id column in the table_a in host_b (data node) as cconnections come from multiple coordinatoor nodes and might end up in duplicate key violations.
>
> Hence we decided to have the coordinator nodes as stateless and hence declared the column with no serial/sequence. Let me know if this makes sense.
Hm.
> Have come across multiple articles on the same issue, i would like to know if we are doing something wrong here or we have bette workaround for this issue,
>
> https://www.postgresql.org/message-id/CAP=oouH=FccW4V2zm1VjGit=NZDCXzU2tYBoZe88v3mXrEA9Qg@mail.gmail...
> https://stackoverflow.com/questions/66582694/how-to-get-the-generated-id-from-an-insert-to-a-postgre...
> https://paquier.xyz/postgresql-2/global-sequences-with-postgres_fdw-and-postgres-core/
Did you try using the approach specified by Michael in [1]? Won't that help?
"As a conclusion, you can create tables using unique values across
multiple nodes by associating for example foreign_seq_nextval() with
DEFAULT for a column.
=# CREATE TABLE tab (a int DEFAULT foreign_seq_nextval());
CREATE TABLE
=# INSERT INTO tab VALUES (DEFAULT), (DEFAULT), (DEFAULT);
INSERT 0 3
=# SELECT * FROM tab;
a
----
9
10
11
(3 rows)
"
[1] - https://paquier.xyz/postgresql-2/global-sequences-with-postgres_fdw-and-postgres-core/
With Regards,
Bharath Rupireddy.
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-14 14:53 Tom Lane <tgl@sss.pgh.pa.us>
parent: Swathi P <swathi.bluepearl@gmail.com>
1 sibling, 2 replies; 8+ messages in thread
From: Tom Lane @ 2021-05-14 14:53 UTC (permalink / raw)
To: Swathi P <swathi.bluepearl@gmail.com>; +Cc: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-general@lists.postgresql.org
[ removing -bugs ]
Swathi P <swathi.bluepearl@gmail.com> writes:
> Hence we decided to have the coordinator nodes as stateless and hence
> declared the column with no serial/sequence. Let me know if this makes
> sense.
Attaching serial-sequence defaults on both sides would certainly not
work very well, because the sequences wouldn't stay in sync.
Unfortunately, postgres_fdw just doesn't have a good way right now
to make use of dynamically-generated defaults at the remote server.
If you leave out a column in your INSERT, it's going to compute
and send the locally-defined default (which is just null in this
case), so the remote's default expression is never used.
I remember that we spent a great deal of effort in postgres_fdw's
early days, trying to find a way that we could use the remote's
defaults in cases like this. But everything we tried ended up
causing horrible semantic inconsistencies, so we ended up with
the always-use-the-local-default approach. There was some feeling
that maybe this could be revisited later, but no one's done so.
One conceivable workaround is to do your insertions through a
foreign table that doesn't even have the serial column, so that
the INSERT command received by the remote server lacks that
column and the default gets applied. Probably too messy though.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-14 15:10 Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 8+ messages in thread
From: Bharath Rupireddy @ 2021-05-14 15:10 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Swathi P <swathi.bluepearl@gmail.com>; pgsql-general@lists.postgresql.org
On Fri, May 14, 2021 at 8:23 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> [ removing -bugs ]
>
> Swathi P <swathi.bluepearl@gmail.com> writes:
> > Hence we decided to have the coordinator nodes as stateless and hence
> > declared the column with no serial/sequence. Let me know if this makes
> > sense.
>
> Attaching serial-sequence defaults on both sides would certainly not
> work very well, because the sequences wouldn't stay in sync.
>
> Unfortunately, postgres_fdw just doesn't have a good way right now
> to make use of dynamically-generated defaults at the remote server.
> If you leave out a column in your INSERT, it's going to compute
> and send the locally-defined default (which is just null in this
> case), so the remote's default expression is never used.
>
> I remember that we spent a great deal of effort in postgres_fdw's
> early days, trying to find a way that we could use the remote's
> defaults in cases like this. But everything we tried ended up
> causing horrible semantic inconsistencies, so we ended up with
> the always-use-the-local-default approach. There was some feeling
> that maybe this could be revisited later, but no one's done so.
>
> One conceivable workaround is to do your insertions through a
> foreign table that doesn't even have the serial column, so that
> the INSERT command received by the remote server lacks that
> column and the default gets applied. Probably too messy though.
Then the serial column cannot be selected via the foreign table.
Maybe, drop the serial column from the foreign table before insertions
and add the serial column before the selects from the foreign table.
This is not elegant though.
With Regards,
Bharath Rupireddy.
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-14 15:32 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
0 siblings, 0 replies; 8+ messages in thread
From: Tom Lane @ 2021-05-14 15:32 UTC (permalink / raw)
To: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; +Cc: Swathi P <swathi.bluepearl@gmail.com>; pgsql-general@lists.postgresql.org
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> writes:
> On Fri, May 14, 2021 at 8:23 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> One conceivable workaround is to do your insertions through a
>> foreign table that doesn't even have the serial column, so that
>> the INSERT command received by the remote server lacks that
>> column and the default gets applied. Probably too messy though.
> Then the serial column cannot be selected via the foreign table.
Yeah, you'd probably need to use different tables for inserting
and reading, which is why I called it messy.
The idea you pointed to in Michael's blog might work though,
ie make a local function that reaches over to the remote server
to get a globally-valid next sequence value, and use that as
the default expression of the foreign table.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Query on postgres_fdw extension
@ 2021-05-14 16:04 Laurenz Albe <laurenz.albe@cybertec.at>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 8+ messages in thread
From: Laurenz Albe @ 2021-05-14 16:04 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Swathi P <swathi.bluepearl@gmail.com>; +Cc: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-general@lists.postgresql.org
On Fri, 2021-05-14 at 10:53 -0400, Tom Lane wrote:
> Swathi P <swathi.bluepearl@gmail.com> writes:
> > Hence we decided to have the coordinator nodes as stateless and hence
> > declared the column with no serial/sequence. Let me know if this makes
> > sense.
>
> Attaching serial-sequence defaults on both sides would certainly not
> work very well, because the sequences wouldn't stay in sync.
>
> Unfortunately, postgres_fdw just doesn't have a good way right now
> to make use of dynamically-generated defaults at the remote server.
> If you leave out a column in your INSERT, it's going to compute
> and send the locally-defined default (which is just null in this
> case), so the remote's default expression is never used.
>
> I remember that we spent a great deal of effort in postgres_fdw's
> early days, trying to find a way that we could use the remote's
> defaults in cases like this. But everything we tried ended up
> causing horrible semantic inconsistencies, so we ended up with
> the always-use-the-local-default approach. There was some feeling
> that maybe this could be revisited later, but no one's done so.
>
> One conceivable workaround is to do your insertions through a
> foreign table that doesn't even have the serial column, so that
> the INSERT command received by the remote server lacks that
> column and the default gets applied. Probably too messy though.
One possibility might be to define a trigger on the remote table
that fetches the next sequence value if you try to insert NULL.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2021-05-14 16:04 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-05-13 10:46 Query on postgres_fdw extension Swathi P <swathi.bluepearl@gmail.com>
2021-05-13 12:04 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2021-05-14 07:07 ` Swathi P <swathi.bluepearl@gmail.com>
2021-05-14 12:10 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2021-05-14 14:53 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-05-14 15:10 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2021-05-14 15:32 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-05-14 16:04 ` Laurenz Albe <laurenz.albe@cybertec.at>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox