From peter.neu@gmx.net Mon Dec 18 09:54:35 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 8D82C9FA2FD for ; Mon, 18 Dec 2006 05:54:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 88204-07 for ; Mon, 18 Dec 2006 05:54:21 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id E8D2E9FA28E for ; Mon, 18 Dec 2006 05:54:29 -0400 (AST) Received: (qmail invoked by alias); 18 Dec 2006 09:54:26 -0000 Received: from ktbl.heagmedianet.de (EHLO PCMELZER) [213.188.107.46] by mail.gmx.net (mp038) with SMTP; 18 Dec 2006 10:54:26 +0100 X-Authenticated: #17979613 From: "Peter Neu" To: Subject: Efficient Insert statement for one record into multiple tables? Date: Mon, 18 Dec 2006 10:53:39 +0100 Message-ID: <01cd01c7228a$6680d960$bcc8a8c0@PCMELZER> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_01CE_01C72292.C8454160" X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 Thread-Index: AcciimTkhT5zWTGjRqazg1oVz1c3Ow== X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200612/81 X-Sequence-Number: 16313 This is a multi-part message in MIME format. ------=_NextPart_000_01CE_01C72292.C8454160 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Hello, I have this task that I need to insert one user record into to tables A and B. User table A contains primary key and main data. Table B contains extra data for statistics. Table A has one field Id which is a sequence so it gets a unique value for each new insert. I would like to insert this value into the other table B as well as foreign key. So I'd like to do something like this insert into A (user_name, value2 ) values ( "foo", "foo2") AND insert into B ( id_from_a , statistic_data) values ( 23, "bla") How is this done the best way? I would like to do this in one call. Sorry, for his dumb question but I'm not very experienced in Postgresql. :o( Cheers, Pete ------=_NextPart_000_01CE_01C72292.C8454160 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable

Hello,

 

I have this task that I need to insert one = user record into to tables A and B.

 

User table A contains primary key and main = data. Table B contains extra data for

statistics. Table A has one field Id which is = a sequence so it gets a unique value

for each new insert. I would like to insert = this value into the other table B as well as

foreign key.

 

So I’d like to do something like this =

 

insert into A (user_name, value2 ) values ( = “foo”, “foo2”)

 

AND

 

insert into B ( id_from_a , statistic_data) = values ( 23, “bla”)

 

How is this done the best way? I would like to = do this in one call.

 

Sorry, for his dumb question but I’m not = very experienced in Postgresql. :o(

 

 

Cheers,

 

Pete

 

 

 

 

 

 

 

 

------=_NextPart_000_01CE_01C72292.C8454160-- From roland.walter.rwa@gmx.net Mon Dec 18 11:28:24 2006 Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 479519FA111 for ; Mon, 18 Dec 2006 07:28:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 43318-08 for ; Mon, 18 Dec 2006 07:28:15 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id AA23C9F931E for ; Mon, 18 Dec 2006 07:28:14 -0400 (AST) Received: (qmail invoked by alias); 18 Dec 2006 11:28:13 -0000 Received: from xdsl-213-196-246-72.netcologne.de (EHLO [192.168.3.10]) [213.196.246.72] by mail.gmx.net (mp041) with SMTP; 18 Dec 2006 12:28:13 +0100 X-Authenticated: #2870507 Message-ID: <45867B49.2090905@gmx.net> Date: Mon, 18 Dec 2006 12:28:09 +0100 From: Roland Walter User-Agent: Thunderbird 1.5.0.8 (X11/20060911) MIME-Version: 1.0 To: Peter Neu CC: pgsql-jdbc@postgresql.org Subject: Re: Efficient Insert statement for one record into multiple References: <01cd01c7228a$6680d960$bcc8a8c0@PCMELZER> In-Reply-To: <01cd01c7228a$6680d960$bcc8a8c0@PCMELZER> X-Enigmail-Version: 0.94.1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/82 X-Sequence-Number: 16314 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Peter Neu wrote: > Hello, > > I have this task that I need to insert one user record into to tables A > and B. > > User table A contains primary key and main data. Table B contains extra > data for > > statistics. Table A has one field Id which is a sequence so it gets a > unique value > > for each new insert. I would like to insert this value into the other > table B as well as > > foreign key. > > So I’d like to do something like this > > insert into A (user_name, value2 ) values ( “foo”, “foo2”) > > AND > > insert into B ( id_from_a , statistic_data) values ( 23, “bla”) > > How is this done the best way? I would like to do this in one call. > > > > Sorry, for his dumb question but I’m not very experienced in Postgresql. :o( > If you know the name of the sequence for the primary key in table A, say "primary_A", then use the following to insert into table B direct after you inserted into table A in the same connection: insert into B (id_from_a, statistic_data) values( currval("primary_A"), bla") This works only if your insertion into table A called in some form nextval("primary_A"), may it be as default value or in the insertion-statement itself. Regards, Roland. - -- Dipl.-Phys. Roland Walter mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFFhntJxoOEAkary1ERAljXAJ9DASPP58I74Cj6Nt59rDzaIGlHjQCdFZRr lFQGZaGY24/Sj77M6Le9xbc= =Yr/X -----END PGP SIGNATURE----- From peter.neu@gmx.net Mon Dec 18 12:06:20 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id EB8489F9B05 for ; Mon, 18 Dec 2006 08:06:20 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 67640-09 for ; Mon, 18 Dec 2006 08:05:58 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id 1F0079F9C35 for ; Mon, 18 Dec 2006 08:05:55 -0400 (AST) Received: (qmail invoked by alias); 18 Dec 2006 12:05:54 -0000 Received: from ktbl.heagmedianet.de (EHLO PCMELZER) [213.188.107.46] by mail.gmx.net (mp038) with SMTP; 18 Dec 2006 13:05:54 +0100 X-Authenticated: #17979613 From: "Peter Neu" To: Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 13:05:12 +0100 Message-ID: <01fb01c7229c$c59e0aa0$bcc8a8c0@PCMELZER> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <45867B49.2090905@gmx.net> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 Thread-Index: Accil7JKGFpfHQnPTDK+FZeOvk1BTwABPXEA X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_PASS X-Spam-Level: X-Archive-Number: 200612/83 X-Sequence-Number: 16315 Hi, in my create table statement I got this line: id int4 DEFAULT nextval('public.user_auth_id2_seq'::text) So according to your suggestions this would be insert into B (id_from_a, statistic_data) values( currval("public.user_auth_id2_seq"),bla") ,right? If I got you right I cannot link this two insert statements together. So I need two times to open a prepared statement and execute it. Would I need to do this in a transaction or is it safe to do this without it? Cheers, Pete From pg@fastcrypt.com Mon Dec 18 12:29:27 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 183569F9C35 for ; Mon, 18 Dec 2006 08:29:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 75761-08 for ; Mon, 18 Dec 2006 08:29:02 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 04E649F946C for ; Mon, 18 Dec 2006 08:29:11 -0400 (AST) Received: (qmail 24868 invoked from network); 18 Dec 2006 12:29:09 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 18 Dec 2006 12:29:09 -0000 In-Reply-To: <01fb01c7229c$c59e0aa0$bcc8a8c0@PCMELZER> References: <01fb01c7229c$c59e0aa0$bcc8a8c0@PCMELZER> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 07:29:32 -0500 To: "Peter Neu" X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/84 X-Sequence-Number: 16316 On 18-Dec-06, at 7:05 AM, Peter Neu wrote: > Hi, > > in my create table statement I got this line: > > id int4 DEFAULT nextval('public.user_auth_id2_seq'::text) > > So according to your suggestions this would be > > insert into B (id_from_a, statistic_data) values( > currval("public.user_auth_id2_seq"),bla") > > ,right? > > If I got you right I cannot link this two insert statements together. > So I need two times to open a prepared statement and execute it. > Would I need to do this in a transaction or is it safe to do this > without it? It's safe to do it without a transaction, what you have to make sure is that you do it with the same connection object. Dave > > Cheers, > Pete > > > > > > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that > your > message can get through to the mailing list cleanly > From roland.walter.rwa@gmx.net Mon Dec 18 14:30:35 2006 Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id D19DF9FA280 for ; Mon, 18 Dec 2006 10:30:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 86013-02 for ; Mon, 18 Dec 2006 10:30:18 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id 3E1BA9F9357 for ; Mon, 18 Dec 2006 10:30:18 -0400 (AST) Received: (qmail invoked by alias); 18 Dec 2006 14:30:13 -0000 Received: from xdsl-213-196-246-72.netcologne.de (EHLO [192.168.3.10]) [213.196.246.72] by mail.gmx.net (mp020) with SMTP; 18 Dec 2006 15:30:13 +0100 X-Authenticated: #2870507 Message-ID: <4586A5F4.7070305@gmx.net> Date: Mon, 18 Dec 2006 15:30:12 +0100 From: Roland Walter User-Agent: Thunderbird 1.5.0.8 (X11/20060911) MIME-Version: 1.0 To: Peter Neu CC: pgsql-jdbc@postgresql.org Subject: Re: Efficient Insert statement for one record into multiple References: <01fa01c7229b$104e2320$bcc8a8c0@PCMELZER> In-Reply-To: <01fa01c7229b$104e2320$bcc8a8c0@PCMELZER> X-Enigmail-Version: 0.94.1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/85 X-Sequence-Number: 16317 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Peter Neu wrote: > Hi, > > in my create table statement I got this line: > > id int4 DEFAULT nextval('public.user_auth_id2_seq'::text) > > So according to your suggestions this would be > > insert into B (id_from_a, statistic_data) values( > currval("public.user_auth_id2_seq"),bla") > > ,right? > Yes. (But beware, I forgot the double-qoute in front of bla.) > If I got you right I cannot link this two insert statements together. > So I need two times to open a prepared statement and execute it. > Would I need to do this in a transaction or is it safe to do this > without it? As Dave Cramer wrote already, you must use the same connection object. If you put in a transaction depends of your application. If you do many insertions in table A and B at the same time, you should put all insertions in one transaction to avoid performance issues. Regards, Roland. - -- Dipl.-Phys. Roland Walter mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFFhqX0xoOEAkary1ERAoVPAJ4krT4+7k8YnzX3cRgmqTd9bMllawCfVJuo KIHe0I0jK0lgwcuhJvy4ztc= =T1t5 -----END PGP SIGNATURE----- From all@adv.magwien.gv.at Mon Dec 18 14:38:34 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 923009F9398 for ; Mon, 18 Dec 2006 10:38:34 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 12587-05 for ; Mon, 18 Dec 2006 10:38:18 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from fr112.ext.magwien.gv.at (relay-fr112.magwien.gv.at [217.149.225.66]) by postgresql.org (Postfix) with ESMTP id ADCF79F9357 for ; Mon, 18 Dec 2006 10:38:29 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by fr112.ext.magwien.gv.at (Postfix) with ESMTP id 1AC936EF68; Mon, 18 Dec 2006 15:38:28 +0100 (CET) Received: from exrat0.host.magwien.gv.at (exrat0.host.magwien.gv.at [10.136.132.84]) by fr112.ext.magwien.gv.at (Postfix) with ESMTP id 56BAC6EF84; Mon, 18 Dec 2006 15:38:27 +0100 (CET) Received: from EXADV1.host.magwien.gv.at ([10.152.101.185]) by exrat0.host.magwien.gv.at with Microsoft SMTPSVC(6.0.3790.0); Mon, 18 Dec 2006 15:36:52 +0100 X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 15:36:50 +0100 Message-ID: <52EF20B2E3209443BC37736D00C3C1380BF08073@EXADV1.host.magwien.gv.at> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [JDBC] Efficient Insert statement for one record into multiple Thread-Index: AccisRrwHE6MaUSBTgmRMte6Rrj3ngAAFjYg From: "Albe Laurenz" To: "Roland Walter *EXTERN*" , "Peter Neu" Cc: X-OriginalArrivalTime: 18 Dec 2006 14:36:52.0024 (UTC) FILETIME=[F51D4380:01C722B1] X-Virus-Scanned: by amavisd-new using Sophie and ClamAV at magwien.gv.at X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/86 X-Sequence-Number: 16318 >> I have this task that I need to insert one user record into to tables A >> and B. >>=20 >> So I'd like to do something like this >>=20 >> insert into A (user_name, value2 ) values ( "foo", "foo2") >>=20 >> AND >>=20 >> insert into B ( id_from_a , statistic_data) values ( 23, "bla") >>=20 >> How is this done the best way? I would like to do this in one call. >=20 > If you know the name of the sequence for the primary key in table A, say > "primary_A", then use the following to insert into table B direct after > you inserted into table A in the same connection: >=20 > insert into B (id_from_a, statistic_data) values( currval("primary_A"), > bla") >=20 > This works only if your insertion into table A called in some form > nextval("primary_A"), may it be as default value or in the > insertion-statement itself. This is _bad_. What if somebody did a nextval("primary_A") between the insert into A and the insert into B? If there is a sequence "primary_A" for table A, you should proceed as follows: - select nextval("primary_A") - then insert into A with this primary key - then insert into B with the same key That should all be in a single transaction! Yours, Laurenz Albe From pg@fastcrypt.com Mon Dec 18 14:51:14 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E70FB9F9398 for ; Mon, 18 Dec 2006 10:51:14 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 18064-01-6 for ; Mon, 18 Dec 2006 10:50:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 871389FB20D for ; Mon, 18 Dec 2006 10:49:52 -0400 (AST) Received: (qmail 26327 invoked from network); 18 Dec 2006 14:49:50 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 18 Dec 2006 14:49:50 -0000 In-Reply-To: <52EF20B2E3209443BC37736D00C3C1380BF08073@EXADV1.host.magwien.gv.at> References: <52EF20B2E3209443BC37736D00C3C1380BF08073@EXADV1.host.magwien.gv.at> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <3D7E9990-2CC9-4129-8732-69DDAA6817CD@fastcrypt.com> Cc: "Roland Walter *EXTERN*" , "Peter Neu" , Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 09:50:13 -0500 To: Albe Laurenz X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/87 X-Sequence-Number: 16319 On 18-Dec-06, at 9:36 AM, Albe Laurenz wrote: >>> I have this task that I need to insert one user record into to >>> tables > A >>> and B. >>> >>> So I'd like to do something like this >>> >>> insert into A (user_name, value2 ) values ( "foo", "foo2") >>> >>> AND >>> >>> insert into B ( id_from_a , statistic_data) values ( 23, "bla") >>> >>> How is this done the best way? I would like to do this in one call. >> >> If you know the name of the sequence for the primary key in table A, > say >> "primary_A", then use the following to insert into table B direct > after >> you inserted into table A in the same connection: >> >> insert into B (id_from_a, statistic_data) values( > currval("primary_A"), >> bla") >> >> This works only if your insertion into table A called in some form >> nextval("primary_A"), may it be as default value or in the >> insertion-statement itself. > > This is _bad_. No, this works > > What if somebody did a nextval("primary_A") between the insert into A > and the insert into B? as long as you use currval then it works as advertised > > If there is a sequence "primary_A" for table A, you should proceed > as follows: This requires two trips to the db. Roland's solution doesn't, and it does work. > > - select nextval("primary_A") > - then insert into A with this primary key > - then insert into B with the same key > > That should all be in a single transaction! This doesn't even have to be in a single transaction. Dave > > Yours, > Laurenz Albe > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > From all@adv.magwien.gv.at Mon Dec 18 15:07:29 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 8CF3E9F9FB2 for ; Mon, 18 Dec 2006 11:07:29 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 19042-10 for ; Mon, 18 Dec 2006 11:07:10 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from fr112.ext.magwien.gv.at (relay-fr112.magwien.gv.at [217.149.225.66]) by postgresql.org (Postfix) with ESMTP id 690139F9357 for ; Mon, 18 Dec 2006 11:07:22 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by fr112.ext.magwien.gv.at (Postfix) with ESMTP id 696526EF70; Mon, 18 Dec 2006 16:07:20 +0100 (CET) Received: from exrat0.host.magwien.gv.at (exrat0.host.magwien.gv.at [10.136.132.84]) by fr112.ext.magwien.gv.at (Postfix) with ESMTP id 0CE566EF5C; Mon, 18 Dec 2006 16:07:19 +0100 (CET) Received: from EXADV1.host.magwien.gv.at ([10.152.101.185]) by exrat0.host.magwien.gv.at with Microsoft SMTPSVC(6.0.3790.0); Mon, 18 Dec 2006 16:06:04 +0100 X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 16:06:03 +0100 Message-ID: <52EF20B2E3209443BC37736D00C3C1380BF080CF@EXADV1.host.magwien.gv.at> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [JDBC] Efficient Insert statement for one record into multiple Thread-Index: AccitLrqlZHcaa/tRSW/d/qO8tUoywAART8g From: "Albe Laurenz" To: "Dave Cramer *EXTERN*" Cc: "Roland Walter *EXTERN*" , "Peter Neu" , X-OriginalArrivalTime: 18 Dec 2006 15:06:04.0892 (UTC) FILETIME=[09E7B1C0:01C722B6] X-Virus-Scanned: by amavisd-new using Sophie and ClamAV at magwien.gv.at X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/88 X-Sequence-Number: 16320 >> This is _bad_. > No, this works Oops, indeed. >> What if somebody did a nextval("primary_A") between the insert into A >> and the insert into B? > as long as you use currval then it works as advertised Sorry, I didn't realize that currval returns the last value retrieved _in this session_. YOurs, Laurenz Albe From all@adv.magwien.gv.at Mon Dec 18 15:37:08 2006 Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 7274B9F9F08 for ; Mon, 18 Dec 2006 11:37:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 11203-01 for ; Mon, 18 Dec 2006 11:36:53 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from fr112.ext.magwien.gv.at (relay-fr112.magwien.gv.at [217.149.225.66]) by postgresql.org (Postfix) with ESMTP id A204A9FA02C for ; Mon, 18 Dec 2006 11:36:59 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by fr112.ext.magwien.gv.at (Postfix) with ESMTP id 063686EF4B; Mon, 18 Dec 2006 16:36:58 +0100 (CET) Received: from exrat0.host.magwien.gv.at (exrat0.host.magwien.gv.at [10.136.132.84]) by fr112.ext.magwien.gv.at (Postfix) with ESMTP id C42126EF42; Mon, 18 Dec 2006 16:36:57 +0100 (CET) Received: from EXADV1.host.magwien.gv.at ([10.152.101.185]) by exrat0.host.magwien.gv.at with Microsoft SMTPSVC(6.0.3790.0); Mon, 18 Dec 2006 16:36:12 +0100 X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 16:36:11 +0100 Message-ID: <52EF20B2E3209443BC37736D00C3C1380BF08124@EXADV1.host.magwien.gv.at> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [JDBC] Efficient Insert statement for one record into multiple Thread-Index: AcciuWgw6akTyyAZTI6nY4+Lz86h9gAAJkwg From: "Albe Laurenz" To: "Dave Cramer *EXTERN*" , "Peter Neu" Cc: X-OriginalArrivalTime: 18 Dec 2006 15:36:12.0978 (UTC) FILETIME=[3F9BB920:01C722BA] X-Virus-Scanned: by amavisd-new using Sophie and ClamAV at magwien.gv.at X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/89 X-Sequence-Number: 16321 >> If I got you right I cannot link this two insert statements together. >> So I need two times to open a prepared statement and execute it. >> Would I need to do this in a transaction or is it safe to do this >> without it? > > It's safe to do it without a transaction, what you have to make sure =20 > is that you do it with the same connection object. Safe as far as currval() is concerned, yes, but then the second INSERT could fail while the first INSERT is committed, right? Yours, Laurenz Albe From roland.walter.rwa@gmx.net Mon Dec 18 16:22:44 2006 Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 8B1379F9FE1 for ; Mon, 18 Dec 2006 12:22:44 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 05155-01 for ; Mon, 18 Dec 2006 12:22:39 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id B29959F9FB2 for ; Mon, 18 Dec 2006 12:22:38 -0400 (AST) Received: (qmail invoked by alias); 18 Dec 2006 16:22:37 -0000 Received: from xdsl-213-196-246-72.netcologne.de (EHLO [192.168.3.10]) [213.196.246.72] by mail.gmx.net (mp045) with SMTP; 18 Dec 2006 17:22:37 +0100 X-Authenticated: #2870507 Message-ID: <4586C04D.5070008@gmx.net> Date: Mon, 18 Dec 2006 17:22:37 +0100 From: Roland Walter User-Agent: Thunderbird 1.5.0.8 (X11/20060911) MIME-Version: 1.0 To: Albe Laurenz CC: Dave Cramer *EXTERN* , Peter Neu , pgsql-jdbc@postgresql.org Subject: Re: Efficient Insert statement for one record into multiple References: <52EF20B2E3209443BC37736D00C3C1380BF08124@EXADV1.host.magwien.gv.at> In-Reply-To: <52EF20B2E3209443BC37736D00C3C1380BF08124@EXADV1.host.magwien.gv.at> X-Enigmail-Version: 0.94.1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/90 X-Sequence-Number: 16322 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Albe Laurenz wrote: >>> If I got you right I cannot link this two insert statements together. >>> So I need two times to open a prepared statement and execute it. >>> Would I need to do this in a transaction or is it safe to do this >>> without it? >> It's safe to do it without a transaction, what you have to make sure >> is that you do it with the same connection object. > > Safe as far as currval() is concerned, yes, but then the second > INSERT could fail while the first INSERT is committed, right? Yes, but as I said, it depends of the application if you do the two insertions in one transaction. Regards, Roland. - -- Dipl.-Phys. Roland Walter mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFFhsBMxoOEAkary1ERAlp2AJ97Sw/pKIPr7Lm4QPKBYg1wD4oQZwCdG/0r xmvivi1ApSuI8viTpcKbIS0= =A8ZT -----END PGP SIGNATURE----- From peter.neu@gmx.net Tue Dec 19 10:19:39 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 218719F946C for ; Tue, 19 Dec 2006 06:19:39 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 16840-05 for ; Tue, 19 Dec 2006 06:19:20 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id 28D2E9F9357 for ; Tue, 19 Dec 2006 06:19:22 -0400 (AST) Received: (qmail invoked by alias); 19 Dec 2006 10:19:21 -0000 Received: from ktbl.heagmedianet.de (EHLO PCMELZER) [213.188.107.46] by mail.gmx.net (mp047) with SMTP; 19 Dec 2006 11:19:21 +0100 X-Authenticated: #17979613 From: "Peter Neu" To: Cc: "'Roland Walter'" Subject: Re: Efficient Insert statement for one record into multiple Date: Tue, 19 Dec 2006 11:18:36 +0100 Message-ID: <00f201c72357$0ba220d0$bcc8a8c0@PCMELZER> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <4586C04D.5070008@gmx.net> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 Thread-Index: AcciwMqDZQE5Npm4QdSmojZYce60+AAlaOxg X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_PASS X-Spam-Level: X-Archive-Number: 200612/92 X-Sequence-Number: 16324 Hello, the insertion within the same connection objects works just fine. ;o) One thing which still keeps me thinking is how should I handle duplicate key exceptions when it happens in the first insert? Should I just close the connection object in the catch clause when this happens in the first statement so the second statement hits an AlreadyClosedConnectionException? Cheers, Pete From schabi@logix-tt.com Tue Dec 19 11:04:09 2006 Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id D05439F9F46 for ; Tue, 19 Dec 2006 07:04:09 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 09290-07 for ; Tue, 19 Dec 2006 07:03:58 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.logix-tt.com (mail.logix-tt.com [212.211.145.186]) by postgresql.org (Postfix) with ESMTP id 552A09F9386 for ; Tue, 19 Dec 2006 07:04:02 -0400 (AST) Received: from kingfisher.intern.logix-tt.com (84-72-188-217.dclient.hispeed.ch [84.72.188.217]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.logix-tt.com (Postfix) with ESMTP id 813F764BFA for ; Tue, 19 Dec 2006 12:06:52 +0100 (CET) Received: from kingfisher.sec.intern.logix-tt.com (localhost [127.0.0.1]) by kingfisher.intern.logix-tt.com (Postfix) with ESMTP id DD2B4895C0 for ; Tue, 19 Dec 2006 12:03:52 +0100 (CET) Date: Tue, 19 Dec 2006 12:03:52 +0100 From: Markus Schaber To: pgsql-jdbc@postgresql.org Subject: Re: Efficient Insert statement for one record into multiple Message-ID: <20061219120352.7ebf0ffc@kingfisher.sec.intern.logix-tt.com> In-Reply-To: <00f201c72357$0ba220d0$bcc8a8c0@PCMELZER> References: <4586C04D.5070008@gmx.net> <00f201c72357$0ba220d0$bcc8a8c0@PCMELZER> Organization: www.logix-tt.com X-Mailer: Sylpheed-Claws 2.6.0 (GTK+ 2.8.20; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/93 X-Sequence-Number: 16325 Hi, Peter, "Peter Neu" wrote: > the insertion within the same connection objects works just fine. ;o) > One thing which still keeps me thinking is how should I handle duplicate > key exceptions when it happens in the first insert? Should I just close > the connection object in the catch clause when this happens in the first > statement so the second statement hits an AlreadyClosedConnectionException? Why execute the second statement at all? Maybe you should rework the control flow of your application. Regards, Markus -- Markus Schaber | Logical Tracking&Tracing International AG Dipl. Inf. | Software Development GIS Fight against software patents in Europe! www.ffii.org www.nosoftwarepatents.org From pg@fastcrypt.com Tue Dec 19 11:09:39 2006 Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id DB9E09F9F46 for ; Tue, 19 Dec 2006 07:09:39 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 33206-04-2 for ; Tue, 19 Dec 2006 07:09:16 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 6EF079F9FB2 for ; Tue, 19 Dec 2006 07:09:20 -0400 (AST) Received: (qmail 9117 invoked from network); 19 Dec 2006 11:09:19 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 19 Dec 2006 11:09:19 -0000 In-Reply-To: <00f201c72357$0ba220d0$bcc8a8c0@PCMELZER> References: <00f201c72357$0ba220d0$bcc8a8c0@PCMELZER> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <30DA21B9-0758-4A54-BC35-D955A2D95B83@fastcrypt.com> Cc: , "'Roland Walter'" Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Efficient Insert statement for one record into multiple Date: Tue, 19 Dec 2006 06:09:47 -0500 To: Peter Neu X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/94 X-Sequence-Number: 16326 If you create the key using nextval you will never get duplicate key exceptions. However, you should never close connections in the catch clause. Always close connections in a finally clause. Dave On 19-Dec-06, at 5:18 AM, Peter Neu wrote: > Hello, > > the insertion within the same connection objects works just fine. ;o) > One thing which still keeps me thinking is how should I handle > duplicate > key exceptions when it happens in the first insert? Should I just > close > the connection object in the catch clause when this happens in the > first > statement so the second statement hits an > AlreadyClosedConnectionException? > > Cheers, > Pete > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that > your > message can get through to the mailing list cleanly >