Re: Update more than one table

From: Roman Neuhauser <neuhauser(at)sigpipe(dot)cz>
To: Bruno Wolff III <bruno(at)wolff(dot)to>, David Pratt <fairwinds(at)eastlink(dot)ca>, pgsql-general(at)postgresql(dot)org
Subject: Re: Update more than one table
Date: 2005-07-12 19:46:08
Message-ID: 20050712194608.GA9090@isis.sigpipe.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

# bruno(at)wolff(dot)to / 2005-07-12 12:11:45 -0500:
> On Tue, Jul 12, 2005 at 17:35:35 +0200,
> Roman Neuhauser <neuhauser(at)sigpipe(dot)cz> wrote:
> > # bruno(at)wolff(dot)to / 2005-07-12 10:08:37 -0500:
> > > On Sun, Jul 10, 2005 at 15:05:30 -0300,
> > > David Pratt <fairwinds(at)eastlink(dot)ca> wrote:
> > > > I was thinking the only way to solve was a function that
> > > > performed an update and returned the nextval at the same time so
> > > > that I could use that value to perform the update on next
> > > > table,etc.
> > >
> > > Normally you can just use currval. But in your case you insert insert two
> > > records and currval will only return the value of the second record's key.
> > > Assuming the first record's key is one less than the second's is not a good
> > > idea. With the current version you can probably make this work reliably
> > > by grabbing a block of ids for your session and making sure that the two
> > > records get their keys from the same preallocated block.
> >
> > Notice the pseudo code I posted:
> >
> > INSERT INTO first_table ...; <- insert one row
> > SELECT currval(first_table); <- first currval()
> > INSERT INTO first_table ...; <- insert another row
> > SELECT currval(first_table); <- second currval()
> > INSERT INTO second_table ...; <- this ellipsis hides the two
> > currval() return values
> >
> > See? I didn't assume anything.
>
> I didn't claim that your approach was wrong. The issue is trying to reduce
> the number of round trips by eliminating the two selects.

Sorry, I reread David's email (the part in question quoted above),
and see what you were talking about.

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marc G. Fournier 2005-07-12 20:00:49 Re: gborg borked again?
Previous Message Christopher Browne 2005-07-12 19:14:29 Re: Transaction Handling in pl/pgsql