Thread: postgres FDW doesn't support sequences?
I've got two 9.3 clusters, with a postgres foreign data wrapper (FDW) setup to point from one cluster to the other. One of the (foreign) tables associated with the foreign server has a bigint sequence for its primary key, defined as: id | bigint | not null default nextval('nppsmoke_id_seq1'::regclass) If I INSERT a new row into the local table (not the foreign table version), without specifying the 'id' column explicitly, it automatically is assigned the nextval in the sequence counter. However, if I attempt to run the same INSERT using the foreign table, it always fails complaining that null value in column "id" violates not-null constraint. It seems like the FDW is somehow ignoring the existence of the sequence default value, and rewriting the SQL query to explicitly attempt to insert a NULL value. Here's the full query & resulting error output: nightly=# INSERT into nppsmoke (date_created,last_update,build_type,current_status,info,cudacode,gpu,subtest,os,arch,cl,dispvers,branch,totdriver,ddcl,testdcmd,osversion) VALUES ((date_trunc('second',now())),(date_trunc('second',now())),'release','Building','npp-release-gpu-buildCUDA-2013-09-24-1380041350.log','2013-09-24.cuda-linux64-test42.release.gpu','380','CUDA build','Linux','x86_64','16935289','CBS_cuda_a_2013-09-24_16935289','cuda_a','1','16935289','./npp-smoke.sh --testtype release --amodel f --vc g --drvpath /home/lfriedman/cuda-stuff/sw/dev/gpu_drv/cuda_a/drivers/gpgpu --cudaroot /home/lfriedman/cuda-stuff/sw/gpgpu --totdriver t --email lfriedman','2.6.32-358.el6.x86_64'); ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 2013-09-25 08:00:46, 2013-09-25 08:00:46, release, Building, npp-release-gpu-buildCUDA-2013-09-24-1380041350.log, 2013-09-24.cuda-linux64-test42.release.gpu, 380, CUDA build, Linux, 2.6.32-358.el6.x86_64, x86_64, 16935289, CBS_cuda_a_2013-09-24_16935289, cuda_a, null, null, null, null, null, t, 16935289, null, ./npp-smoke.sh --testtype release --amodel f --vc g --drvpath /h..., null, null, null, null, g). CONTEXT: Remote SQL command: INSERT INTO public.nppsmoke(id, date_created, last_update, build_type, current_status, info, cudacode, gpu, subtest, os, osversion, arch, cl, dispvers, branch, pass, fail, oldfail, newfail, failureslog, totdriver, ddcl, buildid, testdcmd, pclog, filtercount, filterlog, error) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28) I tried to recreate the foreign table definition with a primary key, and that failed: ERROR: constraints are not supported on foreign tables Are sequences supported with the postgres FDW? If not, is there any workaround for inserting into a foreign table that doesn't require me to explicitly specify a value for the primary key sequence column in my INSERT statements? thanks!
Lonni J Friedman <netllama@gmail.com> writes: > If I INSERT a new row into the local table (not the foreign table > version), without specifying the 'id' column explicitly, it > automatically is assigned the nextval in the sequence counter. > However, if I attempt to run the same INSERT using the foreign table, > it always fails complaining that null value in column "id" violates > not-null constraint. It seems like the FDW is somehow ignoring the > existence of the sequence default value, and rewriting the SQL query > to explicitly attempt to insert a NULL value. Yeah, there was quite a bit of discussion about that back in February or so. The short of it is that column default values that are defined on the foreign server are not respected by operations on a foreign table; rather, you have to attach a DEFAULT specification to the foreign table definition if you want inserts into the foreign table to use that default. The default expression is executed locally, too, which means that if you'd like it to read like "nextval('some_seq')" then some_seq has to be a local sequence, not one on the foreign server. I realize that this isn't ideal for serial-like columns, but honoring default expressions that would execute on the foreign server turned out to be a huge can of worms. We might figure out how to fix that some day; but if we'd insisted on a solution now, there wouldn't be writable foreign tables at all in 9.3. regards, tom lane
On Wed, Sep 25, 2013 at 2:47 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote: > Lonni J Friedman <netllama@gmail.com> writes: >> If I INSERT a new row into the local table (not the foreign table >> version), without specifying the 'id' column explicitly, it >> automatically is assigned the nextval in the sequence counter. >> However, if I attempt to run the same INSERT using the foreign table, >> it always fails complaining that null value in column "id" violates >> not-null constraint. It seems like the FDW is somehow ignoring the >> existence of the sequence default value, and rewriting the SQL query >> to explicitly attempt to insert a NULL value. > > Yeah, there was quite a bit of discussion about that back in February or > so. The short of it is that column default values that are defined on the > foreign server are not respected by operations on a foreign table; rather, > you have to attach a DEFAULT specification to the foreign table definition > if you want inserts into the foreign table to use that default. > > The default expression is executed locally, too, which means that if you'd > like it to read like "nextval('some_seq')" then some_seq has to be a local > sequence, not one on the foreign server. Is there some elegant mechanism for keeping the local & foreign sequences in sync? > > I realize that this isn't ideal for serial-like columns, but honoring > default expressions that would execute on the foreign server turned out > to be a huge can of worms. We might figure out how to fix that some day; > but if we'd insisted on a solution now, there wouldn't be writable foreign > tables at all in 9.3. Understood. Other than reading the code, is there somewhere that these limitations are documented that I overlooked?
On Wed, Sep 25, 2013 at 4:47 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote: > Lonni J Friedman <netllama@gmail.com> writes: >> If I INSERT a new row into the local table (not the foreign table >> version), without specifying the 'id' column explicitly, it >> automatically is assigned the nextval in the sequence counter. >> However, if I attempt to run the same INSERT using the foreign table, >> it always fails complaining that null value in column "id" violates >> not-null constraint. It seems like the FDW is somehow ignoring the >> existence of the sequence default value, and rewriting the SQL query >> to explicitly attempt to insert a NULL value. > > Yeah, there was quite a bit of discussion about that back in February or > so. The short of it is that column default values that are defined on the > foreign server are not respected by operations on a foreign table; rather, > you have to attach a DEFAULT specification to the foreign table definition > if you want inserts into the foreign table to use that default. > > The default expression is executed locally, too, which means that if you'd > like it to read like "nextval('some_seq')" then some_seq has to be a local > sequence, not one on the foreign server. > > I realize that this isn't ideal for serial-like columns, but honoring > default expressions that would execute on the foreign server turned out > to be a huge can of worms. We might figure out how to fix that some day; > but if we'd insisted on a solution now, there wouldn't be writable foreign > tables at all in 9.3. This situation as well as a lot of other things be worked around if there was a mechanic to pass SQL (or whatever language the foreign server accepts) directly through to the foreign server, especially if it could be optionally parameterized. Until that happens for most real world work you're better off using dblink for postgres to postgres communication. merlin