Re: INS/UPD/DEL RETURNING for 8.2

Lists: pgsql-patches
From: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-02 22:51:20
Message-ID: 36e682920603021451n1b086c6fu636b43faef70f7b4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Patch for core and PL/pgSQL to support the INSERT/UPDATE/DELETE RETURNING
syntax in 8.2

INSERT/UPDATE/DELETE seem to work fine in normal operation but there is an
error with DELETE RETURNING when used through PL/pgSQL.

Here's an example PL/pgSQL test:

CREATE SEQUENCE test_id_seq START 1 INCREMENT 1;

CREATE TABLE test_tbl (
test_id BIGINT NOT NULL
DEFAULT nextval('test_id_seq'),
test_name VARCHAR(64) NOT NULL,
PRIMARY KEY (test_id));

CREATE OR REPLACE FUNCTION test_func (test_nm VARCHAR)
RETURNS VOID AS $$
DECLARE
current_rec RECORD;
BEGIN
-- Test INSERT RETURNING
INSERT INTO test_tbl (test_name) VALUES (test_nm)
RETURNING * INTO current_rec;

RAISE NOTICE 'test_id is %', current_rec.test_id;
RAISE NOTICE 'test_name is %', current_rec.test_name;

-- Test UPDATE RETURNING
UPDATE test_tbl SET test_name = 'Uncle Bob'
WHERE test_id = current_rec.test_id
RETURNING * INTO current_rec;

RAISE NOTICE 'test_id is %', current_rec.test_id;
RAISE NOTICE 'test_name is %', current_rec.test_name;

-- Test DELETE RETURNING
DELETE FROM test_tbl WHERE test_id = current_rec.test_id
RETURNING * INTO current_rec;

-- This DOES NOT WORK
RAISE NOTICE 'test_id is %', current_rec.test_id;
RAISE NOTICE 'test_name is %', current_rec.test_name;

RETURN;
END;
$$ LANGUAGE plpgsql;

--
Jonah H. Harris, Database Internals Architect
EnterpriseDB Corporation
732.331.1324

Attachment Content-Type Size
pg82-iudret.patch text/x-patch 37.2 KB

From: Neil Conway <neilc(at)samurai(dot)com>
To: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-02 23:45:38
Message-ID: 1141343138.24513.13.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Thu, 2006-03-02 at 17:51 -0500, Jonah H. Harris wrote:
> Patch for core and PL/pgSQL to support the INSERT/UPDATE/DELETE
> RETURNING syntax in 8.2

I wonder if we should try to consistently treat an INSERT/UPDATE/DELETE
with a RETURNING clause like a SELECT with an equivalent target list.
For example, should it be possible to write:

FOR x in DELETE FROM t1 WHERE ... RETURNING t1.x LOOP
...
END LOOP;

Or open a cursor for a data-modifying statement with a RETURNING clause,
etc.

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>, pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-03 01:21:01
Message-ID: 16614.1141348861@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Neil Conway <neilc(at)samurai(dot)com> writes:
> I wonder if we should try to consistently treat an INSERT/UPDATE/DELETE
> with a RETURNING clause like a SELECT with an equivalent target list.
> For example, should it be possible to write:

> FOR x in DELETE FROM t1 WHERE ... RETURNING t1.x LOOP

Seems like you'd want to get there eventually, if not in the first cut.

This might tie into something that was bothering me about Jonah's
first-cut patch, which was that he was introducing special cases into
places where it didn't seem real appropriate (like printtup.c). I
wonder if we should rejigger the representation of Query so that a
FOO-RETURNING command actually *is* a SELECT in some sense, so that
there's no need for special cases.

I'm a bit fuzzy about how this would work exactly --- you still need to
keep track of two targetlists it seems --- but it's worth thinking
about. I've had a bee in my bonnet for literally years about the fact
that INSERT/SELECT really needs two levels of targetlist, as does UNION.
Maybe if we thought a little bit larger we could clean up all of that
messiness at one stroke.

BTW, looking at the patch's test output reminds me that the appearance
of a resultset causes psql to suppress showing the command status.
I think this is reasonable and expected for SELECT, but I wonder whether
we shouldn't force it to appear when it's something else.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-03 01:33:42
Message-ID: 16710.1141349622@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

"Jonah H. Harris" <jonah(dot)harris(at)gmail(dot)com> writes:
> INSERT/UPDATE/DELETE seem to work fine in normal operation but there is an
> error with DELETE RETURNING when used through PL/pgSQL.

Probably other places too. I don't see any provision here for ensuring
that the variables used in the RETURNING list are actually computed by
the plan. This would be masked in the INSERT and non-join UPDATE cases
by the fact that the plan has to compute all columns of the target table
anyway ... but in a DELETE it'd be an issue.

I think set-returning functions in the RETURNING list might give you
some fits too ...

regards, tom lane


From: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Neil Conway" <neilc(at)samurai(dot)com>, pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-03 01:47:03
Message-ID: 36e682920603021747l3fea5d9ctf1012b80b6a4bae1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On 3/2/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> This might tie into something that was bothering me about Jonah's
> first-cut patch, which was that he was introducing special cases into
> places where it didn't seem real appropriate (like printtup.c). I
> wonder if we should rejigger the representation of Query so that a
> FOO-RETURNING command actually *is* a SELECT in some sense, so that
> there's no need for special cases.

I was thinking along the same lines. This is Omar's patch updated to
8.2but as I get to looking through it, there are a couple things that
could be
cleaned up. I paced around a bit today trying to theorize how this could be
done without a lot of changes and retaining the speed increase gained by not
performing two separate operations.

I'm a bit fuzzy about how this would work exactly --- you still need to
> keep track of two targetlists it seems --- but it's worth thinking
> about. I've had a bee in my bonnet for literally years about the fact
> that INSERT/SELECT really needs two levels of targetlist, as does UNION.
> Maybe if we thought a little bit larger we could clean up all of that
> messiness at one stroke.

I'm definitely open to looking into it. Any suggestions are always
welcome.

--
Jonah H. Harris, Database Internals Architect
EnterpriseDB Corporation
732.331.1324


From: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-03 01:48:23
Message-ID: 36e682920603021748x5a597ch5f2ee0d756fc2bc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On 3/2/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> "Jonah H. Harris" <jonah(dot)harris(at)gmail(dot)com> writes:
> > INSERT/UPDATE/DELETE seem to work fine in normal operation but there is
> an
> > error with DELETE RETURNING when used through PL/pgSQL.
>
> Probably other places too. I don't see any provision here for ensuring
> that the variables used in the RETURNING list are actually computed by
> the plan. This would be masked in the INSERT and non-join UPDATE cases
> by the fact that the plan has to compute all columns of the target table
> anyway ... but in a DELETE it'd be an issue.
>
> I think set-returning functions in the RETURNING list might give you
> some fits too ...

Yeah, I got to looking into the special tuple handling code in execUtils for
retrieving the old (deleted) tuple and there's something definitely getting
lost along the way in some cases.

--
Jonah H. Harris, Database Internals Architect
EnterpriseDB Corporation
732.331.1324


From: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-03 20:01:50
Message-ID: 36e682920603031201y3bf90989n6f3543ab5ffa363a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On 3/2/06, Jonah H. Harris <jonah(dot)harris(at)gmail(dot)com> wrote:
>
> On 3/2/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> > "Jonah H. Harris" <jonah(dot)harris(at)gmail(dot)com> writes:
> > > INSERT/UPDATE/DELETE seem to work fine in normal operation but there
> > is an
> > > error with DELETE RETURNING when used through PL/pgSQL.
> >
> > Probably other places too. I don't see any provision here for ensuring
> > that the variables used in the RETURNING list are actually computed by
> > the plan. This would be masked in the INSERT and non-join UPDATE cases
> > by the fact that the plan has to compute all columns of the target table
> > anyway ... but in a DELETE it'd be an issue.
> >
> > I think set-returning functions in the RETURNING list might give you
> > some fits too ...
>
>
> Yeah, I got to looking into the special tuple handling code in execUtils
> for retrieving the old (deleted) tuple and there's something definitely
> getting lost along the way in some cases.
>

I've been stewing on this and haven't yet come up with anything. Have you
any thoughts on how we can accomplish this better?

--
Jonah H. Harris, Database Internals Architect
EnterpriseDB Corporation
732.331.1324


From: "Pavel Stehule" <pavel(dot)stehule(at)hotmail(dot)com>
To: neilc(at)samurai(dot)com, jonah(dot)harris(at)gmail(dot)com
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-03-05 08:25:49
Message-ID: BAY20-F4F89076E252EDFEB05EF6F9E80@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

>
>I wonder if we should try to consistently treat an INSERT/UPDATE/DELETE
>with a RETURNING clause like a SELECT with an equivalent target list.
>For example, should it be possible to write:
>
>FOR x in DELETE FROM t1 WHERE ... RETURNING t1.x LOOP
> ...
>END LOOP;
>
>Or open a cursor for a data-modifying statement with a RETURNING clause,
>etc.
>
>-Neil

good idea
Pavel

_________________________________________________________________
Citite se osamele? Poznejte nekoho vyjmecneho diky Match.com.
http://www.msn.cz/


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-04-27 02:45:38
Message-ID: 200604270245.k3R2jcn00847@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches


Jonah, where are we on this patch? Do you have a version ready for
review?

---------------------------------------------------------------------------

Jonah H. Harris wrote:
> On 3/2/06, Jonah H. Harris <jonah(dot)harris(at)gmail(dot)com> wrote:
> >
> > On 3/2/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >
> > > "Jonah H. Harris" <jonah(dot)harris(at)gmail(dot)com> writes:
> > > > INSERT/UPDATE/DELETE seem to work fine in normal operation but there
> > > is an
> > > > error with DELETE RETURNING when used through PL/pgSQL.
> > >
> > > Probably other places too. I don't see any provision here for ensuring
> > > that the variables used in the RETURNING list are actually computed by
> > > the plan. This would be masked in the INSERT and non-join UPDATE cases
> > > by the fact that the plan has to compute all columns of the target table
> > > anyway ... but in a DELETE it'd be an issue.
> > >
> > > I think set-returning functions in the RETURNING list might give you
> > > some fits too ...
> >
> >
> > Yeah, I got to looking into the special tuple handling code in execUtils
> > for retrieving the old (deleted) tuple and there's something definitely
> > getting lost along the way in some cases.
> >
>
> I've been stewing on this and haven't yet come up with anything. Have you
> any thoughts on how we can accomplish this better?
>
>
> --
> Jonah H. Harris, Database Internals Architect
> EnterpriseDB Corporation
> 732.331.1324

--
Bruce Momjian http://candle.pha.pa.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Neil Conway" <neilc(at)samurai(dot)com>, pgsql-patches(at)postgresql(dot)org
Subject: Re: INS/UPD/DEL RETURNING for 8.2
Date: 2006-05-04 16:23:37
Message-ID: 36e682920605040923s3772a1a7x292c5b759f62586a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On 3/2/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > For example, should it be possible to write:
> > FOR x in DELETE FROM t1 WHERE ... RETURNING t1.x LOOP
>
> Seems like you'd want to get there eventually, if not in the first cut.

I'd like to get this into the first release of RETURNING for 8.2.

> I wonder if we should rejigger the representation of Query so that a
> FOO-RETURNING command actually *is* a SELECT in some sense, so that
> there's no need for special cases.

I want to get rid of all the special case code and move in this
direction, that way we can make better use of code that already exists
and is well-tested.

> I'm a bit fuzzy about how this would work exactly --- you still need to
> keep track of two targetlists it seems --- but it's worth thinking
> about. I've had a bee in my bonnet for literally years about the fact
> that INSERT/SELECT really needs two levels of targetlist, as does UNION.
> Maybe if we thought a little bit larger we could clean up all of that
> messiness at one stroke.

Have you had any ideas on how to best accomplish this?

--
Jonah H. Harris, Database Internals Architect
EnterpriseDB Corporation
732.331.1324