Re:

From: Michal Táborský <michal(at)taborsky(dot)cz>
To: pgsql-general(at)postgresql(dot)org
Cc: Ago <ago(at)nmb(dot)it>
Subject: Re:
Date: 2004-06-25 09:58:50
Message-ID: 40DBF75A.20808@taborsky.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ago wrote:

> I have a PHP script that runs this query on Postgresql 7.2.3. Once it inserts the record in the e_catalog table it takes the id value and then inserts it in the e_catalog_cache table, the two tables must have the same rows and values.
> I thought rhat inside a transaction block the subquery SELECT MAX(id) FROM e_catalog was safe from concurrent same transactions, that is the id value from SELECT MAX(id) FROM e_catalog was exactly that one inserted in the previous statement, but reading some threads in this mailing list I have some doubt now.
> This is the query:
>
> BEGIN WORK;
> INSERT INTO e_catalog(id, name, descr) VALUES (nextval('sequence'), '$Name', '$Descr');
> INSERT INTO e_catalog_cache(id, name, descr) VALUES ((SELECT MAX(id) FROM e_catalog), '$Name', '$Descr');
>
> COMMIT WORK;

You want:
BEGIN WORK;
INSERT INTO e_catalog(id, name, descr) VALUES (nextval('sequence'),
'$Name', '$Descr');
INSERT INTO e_catalog_cache(id, name, descr) VALUES currval('sequence'),
'$Name', '$Descr');
COMMIT WORK;

--
Michal Taborsky
http://www.taborsky.cz

In response to

  • at 2004-06-25 09:41:22 from Ago

Browse pgsql-general by date

  From Date Subject
Next Message NMB Webmaster 2004-06-25 10:27:36 Re: Query inside transaction
Previous Message Ago 2004-06-25 09:41:22