Re: PL/pgSQL 2

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Joel Jacobson <joel(at)trustly(dot)com>
Cc: Kevin Grittner <kgrittn(at)ymail(dot)com>, Marko Tiikkaja <marko(at)joh(dot)to>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/pgSQL 2
Date: 2014-09-02 16:31:43
Message-ID: 5405F0EF.3040006@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 09/02/2014 07:12 PM, Joel Jacobson wrote:
> On Tue, Sep 2, 2014 at 6:03 PM, Heikki Linnakangas
> <hlinnakangas(at)vmware(dot)com> wrote:
>> I think that would actually be a good way to enforce the rule that an UPDATE
>> only updates a single row. Just put a "ASSERT ROW_COUNT=1;" after the
>> update.
>
> So instead of one line of code, I would need to write two lines of
> code at almost *all* places where a currently have an UPDATE. :-(

Right. Doesn't really seem that bad, to be honest. You can put it on the
same line if you wish.

> I think the problem with my perspective is my ambitions. I use
> PL/pgSQL not as a secondary language, but it's my primary language for
> developing applications.

Sure, a lot of people do that.

> For me, updating a row, is like setting a variable in a normal language.
> No normal language would require two rows to set a variable.
> It would be like having to do:
> my $var = 10;
> die unless $var == 10;
> in Perl to set a variable.

I don't think most applications are like that. See Kevin's comments
about doing things in a set-oriented way instead of row-by-row. I know
I've changed several procedures from the row-oriented style, looping
over rows with a FOR loop, updating each one individually, to
set-oriented style with a single UPDATE for a bunch of rows. It makes
for more concise code, and performs better. I'm sure there are
counter-examples, and I've also written many UPDATE statements that are
expected to update exactly one row, but I find an ASSERT would be
adequate for that.

- Heikki

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hannu Krosing 2014-09-02 16:33:57 Re: PL/pgSQL 2
Previous Message Álvaro Hernández Tortosa 2014-09-02 16:31:00 Re: PL/pgSQL 2