pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PL/pgsql EXECUTE 'SELECT INTO ...'
8+ messages / 6 participants
[nested] [flat]

* PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 03:15  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 4 replies; 8+ messages in thread

From: Tom Lane @ 2001-02-08 03:15 UTC (permalink / raw)
  To: Jan Wieck <JanWieck@Yahoo.com>; +Cc: pgsql-hackers@postgreSQL.org; pgsql-sql

I have looked a little bit at what it'd take to make SELECT INTO inside
an EXECUTE work the same as it does in plain plpgsql --- that is, the
INTO should reference plpgsql variables, not a destination table.
It looks to me like this is possible but would require some nontrivial
re-engineering inside plpgsql.  What I'm visualizing is that EXECUTE
should read its string argument not just as an SPI_exec() string, but
as an arbitrary plpgsql proc_stmt.  This would offer some interesting
capabilities, like building a whole FOR-loop for dynamic execution.
But there are a number of problems to be surmounted, notably arranging
for the parsetree built by the plpgsql compiler not to be irretrievably
memory-leaked.  (That ties into something I'd wanted to do anyway,
which is to have the plpgsql compiler build its trees in a memory
context associated with the function, not via malloc().)

This does not look like something to be tackling when we're already
in late beta, unfortunately.  So we have to decide what to do for 7.1.
If we do nothing now, and then implement this feature in 7.2, we will
have a backwards compatibility problem: EXECUTE 'SELECT INTO ...'
will completely change in meaning.

I am inclined to keep our options open by forbidding EXECUTE 'SELECT
INTO ...' for now.  That's more than a tad annoying, because that leaves
no useful way to do a dynamically-built SELECT, but if we don't forbid
it I think we'll regret it later.

Comments?

			regards, tom lane



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

* Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 04:49  Josh Berkus <josh@agliodbs.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  3 siblings, 0 replies; 8+ messages in thread

From: Josh Berkus @ 2001-02-08 04:49 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; Jan Wieck <JanWieck@Yahoo.com>; +Cc: pgsql-sql

Tom,

> I have looked a little bit at what it'd take to make
> SELECT INTO inside
> an EXECUTE work the same as it does in plain plpgsql ---
> that is, the
> INTO should reference plpgsql variables, not a
> destination table.
> It looks to me like this is possible but would require
> some nontrivial
> re-engineering inside plpgsql.  What I'm visualizing is
<snip>
>  (That ties into something I'd wanted to
> do anyway,
> which is to have the plpgsql compiler build its trees in
> a memory
> context associated with the function, not via malloc().)

All of this sounds good, but as a *heavy* PL/pgSQL user,
it's still going off on somewhat of a tangent.  As far as
I'm concerned, the EXECUTE method was just a workaround for
the lack "object" variables.  What I always would rather
have had is simply being able to drop a variable ... or an
OID ... into a SELECT statement and not bothering with
EXECUTE at all.

> This does not look like something to be tackling when
> we're already
> in late beta, unfortunately.

I'd agree with that.  :-)

> I am inclined to keep our options open by forbidding
> EXECUTE 'SELECT
> INTO ...' for now.  That's more than a tad annoying,
> because that leaves
> no useful way to do a dynamically-built SELECT, but if we
> don't forbid
> it I think we'll regret it later.

Unfortunately, I have already used EXECUTE in several
functions ... my search routines will be hard to run without
it.  Perhaps you could turn off EXECUTE by default, but
allow it as a compile-time option for those of us wise
enough to understand the dangers?

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



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

* Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 05:33  Nathan Myers <ncm@zembu.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  3 siblings, 0 replies; 8+ messages in thread

From: ncm@zembu.com @ 2001-02-08 05:33 UTC (permalink / raw)
  To: pgsql-hackers@postgreSQL.org

On Wed, Feb 07, 2001 at 10:15:02PM -0500, Tom Lane wrote:
> I have looked a little bit at what it'd take to make SELECT INTO inside
> an EXECUTE work the same as it does in plain plpgsql  ...
> If we do nothing now, and then implement this feature in 7.2, we will
> have a backwards compatibility problem: EXECUTE 'SELECT INTO ...'
> will completely change in meaning.
> 
> I am inclined to keep our options open by forbidding EXECUTE 'SELECT
> INTO ...' for now. ... if [not] I think we'll regret it later.

I agree, disable it.  But put a backpatch into contrib along with 
a reference to this last e-mail.  Anybody who cares enough can
apply the patch, and will be prepared for the incompatibility.

Nathan Myers
ncm@zembu.com



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

* Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 13:29  Jan Wieck <janwieck@Yahoo.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  3 siblings, 1 reply; 8+ messages in thread

From: Jan Wieck @ 2001-02-08 13:29 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Jan Wieck <JanWieck@Yahoo.com>; pgsql-hackers@postgreSQL.org; pgsql-sql

Tom Lane wrote:
> I have looked a little bit at what it'd take to make SELECT INTO inside
> an EXECUTE work the same as it does in plain plpgsql --- that is, the
> INTO should reference plpgsql variables, not a destination table.
> It looks to me like this is possible but would require some nontrivial
> re-engineering inside plpgsql.  What I'm visualizing is that EXECUTE
> should read its string argument not just as an SPI_exec() string, but
> as an arbitrary plpgsql proc_stmt.  This would offer some interesting
> capabilities, like building a whole FOR-loop for dynamic execution.
> But there are a number of problems to be surmounted, notably arranging
> for the parsetree built by the plpgsql compiler not to be irretrievably
> memory-leaked.  (That ties into something I'd wanted to do anyway,
> which is to have the plpgsql compiler build its trees in a memory
> context associated with the function, not via malloc().)
>
> This does not look like something to be tackling when we're already
> in late beta, unfortunately.  So we have to decide what to do for 7.1.
> If we do nothing now, and then implement this feature in 7.2, we will
> have a backwards compatibility problem: EXECUTE 'SELECT INTO ...'
> will completely change in meaning.
>
> I am inclined to keep our options open by forbidding EXECUTE 'SELECT
> INTO ...' for now.  That's more than a tad annoying, because that leaves
> no useful way to do a dynamically-built SELECT, but if we don't forbid
> it I think we'll regret it later.

    You can do something like

        FOR record_var IN EXECUTE <string-expr> LOOP
            ...
        END LOOP;

    In this case, the <string-expr> executed over SPI_exec() must
    return tuples (0-n). Otherwise you'll get a runtime error.

    Inside the loop you have access to the tuples via the record.
    Is  that  the dynamically-built SELECT capability you've been
    missing?

    There's not that much need for mucking with  temp  tables  in
    EXECUTE as all this discussion looks to me.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




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

* RE: [SQL] Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 13:35  Michael Ansley <Michael.Ansley@intec-telecom-systems.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Michael Ansley @ 2001-02-08 13:35 UTC (permalink / raw)
  To: 'Jan Wieck' <janwieck@Yahoo.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers@postgreSQL.org; pgsql-sql

What I wrote wasn't about temp tables, it was about selecting into plpgsql
variables.  It would appear that Jan's syntax gets around this problem.

MikeA


-----Original Message-----
From: Jan Wieck [mailto:janwieck@Yahoo.com]
Sent: 08 February 2001 13:30
To: Tom Lane
Cc: Jan Wieck; pgsql-hackers@postgreSQL.org; pgsql-sql@postgreSQL.org
Subject: [SQL] Re: PL/pgsql EXECUTE 'SELECT INTO ...'


Tom Lane wrote:
> I have looked a little bit at what it'd take to make SELECT INTO inside
> an EXECUTE work the same as it does in plain plpgsql --- that is, the
> INTO should reference plpgsql variables, not a destination table.
> It looks to me like this is possible but would require some nontrivial
> re-engineering inside plpgsql.  What I'm visualizing is that EXECUTE
> should read its string argument not just as an SPI_exec() string, but
> as an arbitrary plpgsql proc_stmt.  This would offer some interesting
> capabilities, like building a whole FOR-loop for dynamic execution.
> But there are a number of problems to be surmounted, notably arranging
> for the parsetree built by the plpgsql compiler not to be irretrievably
> memory-leaked.  (That ties into something I'd wanted to do anyway,
> which is to have the plpgsql compiler build its trees in a memory
> context associated with the function, not via malloc().)
>
> This does not look like something to be tackling when we're already
> in late beta, unfortunately.  So we have to decide what to do for 7.1.
> If we do nothing now, and then implement this feature in 7.2, we will
> have a backwards compatibility problem: EXECUTE 'SELECT INTO ...'
> will completely change in meaning.
>
> I am inclined to keep our options open by forbidding EXECUTE 'SELECT
> INTO ...' for now.  That's more than a tad annoying, because that leaves
> no useful way to do a dynamically-built SELECT, but if we don't forbid
> it I think we'll regret it later.

    You can do something like

        FOR record_var IN EXECUTE <string-expr> LOOP
            ...
        END LOOP;

    In this case, the <string-expr> executed over SPI_exec() must
    return tuples (0-n). Otherwise you'll get a runtime error.

    Inside the loop you have access to the tuples via the record.
    Is  that  the dynamically-built SELECT capability you've been
    missing?

    There's not that much need for mucking with  temp  tables  in
    EXECUTE as all this discussion looks to me.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
Nick West - Global Infrastructure Manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

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

* Re: [SQL] Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 15:03  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jan Wieck <janwieck@Yahoo.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Tom Lane @ 2001-02-08 15:03 UTC (permalink / raw)
  To: Jan Wieck <janwieck@Yahoo.com>; +Cc: pgsql-hackers@postgresql.org; pgsql-sql

Jan Wieck <janwieck@Yahoo.com> writes:
>> I am inclined to keep our options open by forbidding EXECUTE 'SELECT
>> INTO ...' for now.  That's more than a tad annoying, because that leaves
>> no useful way to do a dynamically-built SELECT, but if we don't forbid
>> it I think we'll regret it later.

>     You can do something like
>         FOR record_var IN EXECUTE <string-expr> LOOP
>             ...
>         END LOOP;

Okay, that solves the concern I had about not being able to get the
result of an EXECUTEd select at all.  I'll go ahead and forbid
EXECUTE 'SELECT INTO' for the time being, and we can talk about
improving plpgsql later.

			regards, tom lane



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

* Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 19:37  Peter Eisentraut <peter_e@gmx.net>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  3 siblings, 1 reply; 8+ messages in thread

From: Peter Eisentraut @ 2001-02-08 19:37 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Jan Wieck <JanWieck@Yahoo.com>; pgsql-hackers@postgreSQL.org; pgsql-sql

Tom Lane writes:

> I am inclined to keep our options open by forbidding EXECUTE 'SELECT
> INTO ...' for now.  That's more than a tad annoying, because that leaves
> no useful way to do a dynamically-built SELECT, but if we don't forbid
> it I think we'll regret it later.

You can always use CREATE TABLE AS.

-- 
Peter Eisentraut      peter_e@gmx.net       http://yi.org/peter-e/




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

* Re: PL/pgsql EXECUTE 'SELECT INTO ...'
@ 2001-02-08 19:49  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Peter Eisentraut <peter_e@gmx.net>
  0 siblings, 0 replies; 8+ messages in thread

From: Tom Lane @ 2001-02-08 19:49 UTC (permalink / raw)
  To: Peter Eisentraut <peter_e@gmx.net>; +Cc: Jan Wieck <JanWieck@Yahoo.com>; pgsql-hackers@postgreSQL.org; pgsql-sql

Peter Eisentraut <peter_e@gmx.net> writes:
> Tom Lane writes:
>> I am inclined to keep our options open by forbidding EXECUTE 'SELECT
>> INTO ...' for now.  That's more than a tad annoying, because that leaves
>> no useful way to do a dynamically-built SELECT, but if we don't forbid
>> it I think we'll regret it later.

> You can always use CREATE TABLE AS.

Does SPI_exec() support that?  (Tries it ... seems to work ...)
Cool.  OK, we have the bases covered then; there's no need to allow
SELECT INTO inside EXECUTE until we can make it work as expected.

			regards, tom lane



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


end of thread, other threads:[~2001-02-08 19:49 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-08 03:15 PL/pgsql EXECUTE 'SELECT INTO ...' Tom Lane <tgl@sss.pgh.pa.us>
2001-02-08 04:49 ` Josh Berkus <josh@agliodbs.com>
2001-02-08 05:33 ` Nathan Myers <ncm@zembu.com>
2001-02-08 13:29 ` Jan Wieck <janwieck@Yahoo.com>
2001-02-08 15:03   ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-08 19:37 ` Peter Eisentraut <peter_e@gmx.net>
2001-02-08 19:49   ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-08 13:35 RE: [SQL] Re: PL/pgsql EXECUTE 'SELECT INTO ...' Michael Ansley <Michael.Ansley@intec-telecom-systems.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