pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Replacing IDENTITY with lastval()
5+ messages / 4 participants
[nested] [flat]

* Replacing IDENTITY with lastval()
@ 2007-05-04 11:42 Dmitry Samokhin <sdld@mail.ru>
  2007-05-05 15:56 ` Re: Replacing IDENTITY with lastval() Hiroshi Inoue <inoue@tpf.co.jp>
  2007-05-05 16:47 ` Re: Replacing IDENTITY with lastval() Andreas <maps.on@gmx.net>
  0 siblings, 2 replies; 5+ messages in thread

From: Dmitry Samokhin @ 2007-05-04 11:42 UTC (permalink / raw)
  To: pgsql-odbc

Accordind to the release notes, "Use lastval() function to replace IDENTITY on 8.1 or later servers" was introduced in release 8.2.0205. This may cause incorrect results: "<...> assumes that your database does not expect any triggers to fire when the INSERT is executed. If a trigger does fire and if that trigger adds another row to a table, the @@IDENTITY global variable would be set to point to that new Identity value—not the one your INSERT generated. <...> work for simple situations, but not when your database gets more sophisticated".
See "Managing an @@IDENTITY Crisis" on MSDN (http://msdn2.microsoft.com/en-us/library/ms971502.aspx) for more details.

The PostgreSQL documentation states that the lastval() function returns the value most recently returned by nextval in the current session. It works the same way as the @@IDENTITY variable in MSSQL. The currval(...) function returns a value of the explicitly specified sequence, this is exactly what we need.

Please consider to revert the code.

Regards,
Dmitry.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Replacing IDENTITY with lastval()
  2007-05-04 11:42 Replacing IDENTITY with lastval() Dmitry Samokhin <sdld@mail.ru>
@ 2007-05-05 15:56 ` Hiroshi Inoue <inoue@tpf.co.jp>
  1 sibling, 0 replies; 5+ messages in thread

From: Hiroshi Inoue @ 2007-05-05 15:56 UTC (permalink / raw)
  To: Dmitry Samokhin <sdld@mail.ru>; +Cc: pgsql-odbc

Dmitry Samokhin wrote:
> Accordind to the release notes, "Use lastval() function to replace IDENTITY on 8.1 or later servers" was introduced in release 8.2.0205. This may cause incorrect results: "<...> assumes that your database does not expect any triggers to fire when the INSERT is executed. If a trigger does fire and if that trigger adds another row to a table, the @@IDENTITY global variable would be set to point to that new Identity value—not the one your INSERT generated. <...> work for simple situations, but not when your database gets more sophisticated".
> See "Managing an @@IDENTITY Crisis" on MSDN (http://msdn2.microsoft.com/en-us/library/ms971502.aspx) for more details.
> 
> The PostgreSQL documentation states that the lastval() function returns the value most recently returned by nextval in the current session. It works the same way as the @@IDENTITY variable in MSSQL. The currval(...) function returns a value of the explicitly specified sequence, this is exactly what we need.
> 
> Please consider to revert the code.

OK I would take care of it in 8.2.0402.

regards,
Hiroshi Inoue



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Replacing IDENTITY with lastval()
  2007-05-04 11:42 Replacing IDENTITY with lastval() Dmitry Samokhin <sdld@mail.ru>
@ 2007-05-05 16:47 ` Andreas <maps.on@gmx.net>
  2007-06-01 15:00   ` Re: Replacing IDENTITY with lastval() Ben Trewern <ben.trewern@mowlem.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Andreas @ 2007-05-05 16:47 UTC (permalink / raw)
  To: pgsql-odbc


So - generally speaking - it is not advised to use lastval() at all?
At least not, if one is not 110% sure what happens in the application 
now and on later modifications of the client application.

Thats a pitty, as I have only known lastval() for about 15 minutes when 
I looked in the documentation for the sequence stuff.

On the other hand I ran today in troubles with sequences when I was 
doing a bit coding on a migration from MS-Access DAO + JET to DAO, ODBC 
+ PG.
With pure Access + DAO one can fetch the newly assigned AUTOVALUE by 
simply reading it out of the newly created record right after AddNew and 
before Update.

With ODBC + PG. as backend the SERIAL is undefined between AddNew and 
Update.
And the recordset points not to the new record after Update but to the 
first record in the recordset.
I had to use PG's CurrVal respectivly LastVal which would be easier 
without having to know the name of the sequence.

I'm happy that I read your post before using LastVal all over the 
place.   :)


Dmitry Samokhin schrieb:
> Accordind to the release notes, "Use lastval() function to replace IDENTITY on 8.1 or later servers" was introduced in release 8.2.0205. This may cause incorrect results: "<...> assumes that your database does not expect any triggers to fire when the INSERT is executed. If a trigger does fire and if that trigger adds another row to a table, the @@IDENTITY global variable would be set to point to that new Identity value—not the one your INSERT generated. <...> work for simple situations, but not when your database gets more sophisticated".
> See "Managing an @@IDENTITY Crisis" on MSDN (http://msdn2.microsoft.com/en-us/library/ms971502.aspx) for more details.
>
> The PostgreSQL documentation states that the lastval() function returns the value most recently returned by nextval in the current session. It works the same way as the @@IDENTITY variable in MSSQL. The currval(...) function returns a value of the explicitly specified sequence, this is exactly what we need.
>
> Please consider to revert the code.
>
> Regards,
> Dmitry.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>
>   




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Replacing IDENTITY with lastval()
  2007-05-04 11:42 Replacing IDENTITY with lastval() Dmitry Samokhin <sdld@mail.ru>
  2007-05-05 16:47 ` Re: Replacing IDENTITY with lastval() Andreas <maps.on@gmx.net>
@ 2007-06-01 15:00   ` Ben Trewern <ben.trewern@mowlem.com>
  2007-06-01 15:10     ` Re: Replacing IDENTITY with lastval() Ben Trewern <ben.trewern@mowlem.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Ben Trewern @ 2007-06-01 15:00 UTC (permalink / raw)
  To: pgsql-odbc

Shouldn't you be using INSERT ... RETURNING .. ;  for this?  Seems like the 
perfect useage.

Regards,

Ben

"Andreas" <maps.on@gmx.net> wrote in message news:463CB520.90208@gmx.net...
>
> So - generally speaking - it is not advised to use lastval() at all?
> At least not, if one is not 110% sure what happens in the application now 
> and on later modifications of the client application.
>
> Thats a pitty, as I have only known lastval() for about 15 minutes when I 
> looked in the documentation for the sequence stuff.
>
> On the other hand I ran today in troubles with sequences when I was doing 
> a bit coding on a migration from MS-Access DAO + JET to DAO, ODBC + PG.
> With pure Access + DAO one can fetch the newly assigned AUTOVALUE by 
> simply reading it out of the newly created record right after AddNew and 
> before Update.
>
> With ODBC + PG. as backend the SERIAL is undefined between AddNew and 
> Update.
> And the recordset points not to the new record after Update but to the 
> first record in the recordset.
> I had to use PG's CurrVal respectivly LastVal which would be easier 
> without having to know the name of the sequence.
>
> I'm happy that I read your post before using LastVal all over the place. 
> :)
>
>
> Dmitry Samokhin schrieb:
>> Accordind to the release notes, "Use lastval() function to replace 
>> IDENTITY on 8.1 or later servers" was introduced in release 8.2.0205. 
>> This may cause incorrect results: "<...> assumes that your database does 
>> not expect any triggers to fire when the INSERT is executed. If a trigger 
>> does fire and if that trigger adds another row to a table, the @@IDENTITY 
>> global variable would be set to point to that new Identity value—not the 
>> one your INSERT generated. <...> work for simple situations, but not when 
>> your database gets more sophisticated".
>> See "Managing an @@IDENTITY Crisis" on MSDN 
>> (http://msdn2.microsoft.com/en-us/library/ms971502.aspx) for more 
>> details.
>>
>> The PostgreSQL documentation states that the lastval() function returns 
>> the value most recently returned by nextval in the current session. It 
>> works the same way as the @@IDENTITY variable in MSSQL. The currval(...) 
>> function returns a value of the explicitly specified sequence, this is 
>> exactly what we need.
>>
>> Please consider to revert the code.
>>
>> Regards,
>> Dmitry.
>>
>> ---------------------------(end of broadcast)---------------------------
>> TIP 2: Don't 'kill -9' the postmaster
>>
>>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: You can help support the PostgreSQL project by donating at
>
>                http://www.postgresql.org/about/donate
> 





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Replacing IDENTITY with lastval()
  2007-05-04 11:42 Replacing IDENTITY with lastval() Dmitry Samokhin <sdld@mail.ru>
  2007-05-05 16:47 ` Re: Replacing IDENTITY with lastval() Andreas <maps.on@gmx.net>
  2007-06-01 15:00   ` Re: Replacing IDENTITY with lastval() Ben Trewern <ben.trewern@mowlem.com>
@ 2007-06-01 15:10     ` Ben Trewern <ben.trewern@mowlem.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Ben Trewern @ 2007-06-01 15:10 UTC (permalink / raw)
  To: pgsql-odbc

> Shouldn't you be using INSERT ... RETURNING .. ;  for this?  Seems like 
> the perfect useage.
>

Sorry to reply to myself but I just looked this up and it is only supported 
from 8.2 onwards. :-(

Ben 





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2007-06-01 15:10 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-05-04 11:42 Replacing IDENTITY with lastval() Dmitry Samokhin <sdld@mail.ru>
2007-05-05 15:56 ` Hiroshi Inoue <inoue@tpf.co.jp>
2007-05-05 16:47 ` Andreas <maps.on@gmx.net>
2007-06-01 15:00   ` Ben Trewern <ben.trewern@mowlem.com>
2007-06-01 15:10     ` Ben Trewern <ben.trewern@mowlem.com>

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