Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y0EIP-0001fa-09 for pgsql-sql@arkaria.postgresql.org; Sun, 14 Dec 2014 18:53:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y0EIO-0004B4-G1 for pgsql-sql@arkaria.postgresql.org; Sun, 14 Dec 2014 18:53:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y0EIN-0004Ax-CM for pgsql-sql@postgresql.org; Sun, 14 Dec 2014 18:53:31 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y0EIJ-0005px-Vo for pgsql-sql@postgresql.org; Sun, 14 Dec 2014 18:53:29 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 4E61020743 for ; Sun, 14 Dec 2014 13:53:27 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute3.internal (MEProxy); Sun, 14 Dec 2014 13:53:27 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= x-sasl-enc:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; s=mesmtp; bh=OVI2bFGvTk4j64aoOUTrmD+F+ro=; b=YdIED4OMOXhXYdRG6R PBEHJ+8oW4ViljxRntR/s3nzX/DoP04jPe3KvoHUi+8rgxzwhwF8MF2nKdfn134W aAGrJMm/OJJ2+stEjYWJ+UzIQYbneATCu8xcCdbr8XZV4y8b1Gyet4oYmb8kdCm+ RCsN8/Kw2bsjO6j89X8iiGK8M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=x-sasl-enc:message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=OVI2bFGvTk4j64aoOUTrmD +F+ro=; b=IV9u/I0E/rUhU+tSDZShc6VPrsyYoxWwmsLKai2VNEZi7f9lXp9dSG TtmUX0vNTngn9fkZlmVYEUvK3JvXLiHmQ6FdlC2mMac/nnBFKDtFvSRjdcKzo3rP ZbtCqo4mh6ZpGbKvy7YUuqtvyxyDW9uZ/XJFJw+y/lFic2iN4KqSk= X-Sasl-enc: pJU5JXlkZi5DW0t/JlCJsitxHBYliMI/UgFCSKuoJW/a 1418583206 Received: from [192.168.1.2] (unknown [174.21.228.52]) by mail.messagingengine.com (Postfix) with ESMTPA id B9EE9C0027E; Sun, 14 Dec 2014 13:53:26 -0500 (EST) Message-ID: <548DDCA6.8030209@aklaver.com> Date: Sun, 14 Dec 2014 10:53:26 -0800 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Ed Rahn , pgsql-sql@postgresql.org Subject: Re: foregin table insert error References: <548D54C9.3040005@gmail.com> <548D9F19.9080405@aklaver.com> <548DBFB3.1090304@gmail.com> In-Reply-To: <548DBFB3.1090304@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 12/14/2014 08:49 AM, Ed Rahn wrote: > On 12/14/2014 09:30 AM, Adrian Klaver wrote: >> On 12/14/2014 01:13 AM, Ed Rahn wrote: >>> Hi, >>> I have a foreign table that I'm getting an insert error on: >>> >>> horsedata=# insert into remote_cache (entry_id, name_id) values(2,1); >>> ERROR: null value in column "id" violates not-null constraint >>> DETAIL: Failing row contains (null, 1, 2, null). >>> CONTEXT: Remote SQL command: INSERT INTO public.cache(id, name_id, >>> entry_id, value) VALUES ($1, $2, $3, $4) >>> >>> >>> Here is the remote table client side: >>> horsedata=# \d remote_cache >>> Foreign table "public.remote_cache" >>> Column | Type | Modifiers | FDW Options >>> ----------+---------+-----------+------------- >>> id | integer | | >>> name_id | integer | | >>> entry_id | integer | | >>> value | integer | | >>> Server: home >>> FDW Options: (table_name 'cache') >>> >>> >>> And here's cache server side: >>> horsedata=# \d cache; >>> Table "public.cache" >>> Column | Type | Modifiers >>> ----------+------------------+---------------------------------------------------- >>> >>> >>> id | integer | not null default >>> nextval('cache_id_seq'::regclass) >>> name_id | integer | >>> entry_id | integer | >>> value | double precision | >>> Indexes: >>> "cache_pkey" PRIMARY KEY, btree (id) >>> "cache_name_id_entry_id_key" UNIQUE CONSTRAINT, btree (name_id, >>> entry_id) >>> "ix_cache_entry_id" btree (entry_id) >>> "ix_cache_name_id" btree (name_id) >>> >>> >>> Any suggestions? >> >> Yes, see here: >> >> http://www.postgresql.org/message-id/CA+mi_8bfkaFPNPPx6_W_T_0J9OEMSfXQKCDZo=OMJpWWcCKtoA@mail.gmail.com >> >> > I tried something similar using -1 as well as the above: > On server I set: > create function inc_id_cache() returns trigger as $inc$ > begin > if NEW.id = NULL then > NEW.id := nextval('cache_id_seq'); > end if; > return NEW; > end; > $inc$ language plpgsql; > > create trigger inc before insert on cache for each row execute procedure > inc_id_cache(); > > > Now in both cases I get: > horsedata=# insert into remote_cache(name_id, entry_id) values (1, 8); > ERROR: null value in column "id" violates not-null constraint > DETAIL: Failing row contains (null, 1, 8, null). > CONTEXT: Remote SQL command: INSERT INTO public.cache(id, name_id, > entry_id, value) VALUES ($1, $2, $3, $4) Just realized that deals with remote side, but not with getting the value back to the local side. That stirred a memory which led me to this blog post: http://michael.otacoo.com/postgresql-2/global-sequences-with-postgres_fdw-and-postgres-core/ FYI if I follow correctly this: =# CREATE FOREIGN TABLE foreign_seq_table (a bigint) -# SERVER postgres_server OPTIONS (table_name 'seq_table') should be: =# CREATE FOREIGN TABLE foreign_seq_table (a bigint) -# SERVER postgres_server OPTIONS (table_name 'seq_view') > > thanks > Ed > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql