Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-24 Thread Francisco Olarte
Hi Anil: On Wed, Jul 23, 2014 at 11:34 PM, Anil Menon gakme...@gmail.com wrote: ... . It's that itch to drink deep from the fountain of knowledge. Beware of hyponatremia, and keep in mind it can change in the future. But you'll surely learn a lot. I really do like Laurenz Albe's advice of

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-23 Thread Francisco Olarte
Hi Anil: On Tue, Jul 22, 2014 at 6:46 PM, Anil Menon gakme...@gmail.com wrote: Am a bit confused -which one comes first? 1) the 'data'||currval('id01_col1_seq') is parsed first : which means it or 1) an insert is attempted which causes a sequence.nextval to be performed ... I observe the

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-23 Thread Anil Menon
Thanks Olarte, Exactly following your advice...this being the beauty of open source -you can read the source code ​. It's that itch to drink deep from the fountain of knowledge.​ I really do like ​ ​ ​Laurenz Albe's advice of using WITH() AS which seems to be the best practice I can ask the

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread rob stone
On Mon, 2014-07-21 at 10:00 +0800, Anil Menon wrote: Hi, I have a question on the right/correct practice on using the serial col's sequence for insert. Best way of explanation is by an example: create table id01 (col1 serial, col2 varchar(10)); insert into id01(col2) values

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread Albe Laurenz
rob stone wrote: I have a question on the right/correct practice on using the serial col's sequence for insert. Best way of explanation is by an example: create table id01 (col1 serial, col2 varchar(10)); insert into id01(col2) values ( 'data'|| currval('id01_col1_seq')::varchar); while

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread David G Johnston
Albe Laurenz *EXTERN* wrote Also, I think that your method is vulnerable to race conditions: If somebody else increments the sequence between the INSERT and SELECT lastval() you'd get a wrong value. Uh, no. It returns that last value issued in the same session - which is race-proof.

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread rob stone
On Tue, 2014-07-22 at 13:32 +, Albe Laurenz wrote: rob stone wrote: I have a question on the right/correct practice on using the serial col's sequence for insert. Best way of explanation is by an example: create table id01 (col1 serial, col2 varchar(10)); insert into

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread David G Johnston
On Tuesday, July 22, 2014, rob stone-2 [via PostgreSQL] ml-node+s1045698n5812384...@n5.nabble.com wrote: On Tue, 2014-07-22 at 13:32 +, Albe Laurenz wrote: rob stone wrote: I have a question on the right/correct practice on using the serial col's sequence for insert. Best

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread Albe Laurenz
David G Johnston wrote: Also, I think that your method is vulnerable to race conditions: If somebody else increments the sequence between the INSERT and SELECT lastval() you'd get a wrong value. Uh, no. It returns that last value issued in the same session - which is race-proof.

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread David Johnston
On Tue, Jul 22, 2014 at 9:46 AM, Anil Menon gakme...@gmail.com wrote: Am a bit confused -which one comes first? 1) the 'data'||currval('id01_col1_seq') is parsed first : which means it takes the current session's currval 2) then the insert is attempted which causes a sequence.nextval to be

Re: [GENERAL] Referencing serial col's sequence for insert

2014-07-22 Thread Anil Menon
Am a bit confused -which one comes first? 1) the 'data'||currval('id01_col1_seq') is parsed first : which means it takes the current session's currval 2) then the insert is attempted which causes a sequence.nextval to be performed which means that 'data'||currval('id01_col1_seq')will be different

[GENERAL] Referencing serial col's sequence for insert

2014-07-21 Thread Anil Menon
Hi, I have a question on the right/correct practice on using the serial col's sequence for insert. Best way of explanation is by an example: create table id01 (col1 serial, col2 varchar(10)); insert into id01(col2) values ( 'data'||currval('id01_col1_seq')::varchar); while I do get what I