Re: Proposal: plpgsql - "for in array" statement

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Proposal: plpgsql - "for in array" statement
Date: 2010-09-28 19:41:16
Message-ID: AANLkTi=18UivzKezbi+Eax0xx3LeHGr_CfK24z6xrMp1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

2010/9/28 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>> I looked on some constructs that helps with iteration over array in
>> plpgsql. I propose a following syntax:
>
>> FOR var IN [array variable | array expression]
>> LOOP
>
> I don't have any opinion about whether the functionality proposed here
> is worth the trouble, but I do have an opinion about that syntax: it's
> an awful choice.  plpgsql has enough trouble already distinguishing
> between integer for-loops and query for-loops, not to mention trouble
> in producing a helpful error message when somebody gets either of those
> constructs slightly wrong.  Providing a variant where a single
> expression can follow IN will make both of those problems an order of
> magnitude worse.  As an example, is this a for-in-query or a
> for-in-array?
>
>        FOR v IN (SELECT arraycol FROM tab) LOOP ...
>

This is a subquery - so it is a for-in-array - should return one row
with one column. Similar construct is in SQL/PSM

where you can to write SET var = (SELECT ...)

You cannot to write just (SELECT ...) anywhere

> Either answer is plausible depending on whether you assume the
> parentheses make it a subquery.
>
> Pick something less easily confusable with the existing constructs.

It's not simple - FOR i IN array is natural - Original ADA use a very
similar construct.

FOR i IN ARRAY has problem with constant array - FOR i IN ARRAY ARRAY[1,2,3,]
and FOREACH is used in Oracle for absolutely different task.

>

we have now a for-in-cursor, so there is a precedent.

regards

Pavel

>                        regards, tom lane
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2010-09-28 19:48:36 Re: Proposal: plpgsql - "for in array" statement
Previous Message Heikki Linnakangas 2010-09-28 19:14:22 Re: patch: SQL/MED(FDW) DDL