Re: PL/pgSQL 1.2

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Joel Jacobson <joel(at)trustly(dot)com>
Cc: Marko Tiikkaja <marko(at)joh(dot)to>, Hannu Krosing <hannu(at)2ndquadrant(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Josh Berkus <josh(at)agliodbs(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/pgSQL 1.2
Date: 2014-09-06 13:24:30
Message-ID: CAFj8pRAkCyO85e_1jYM5RGM6eU7M4F1GiWZafukyB2Pg=Hwnfg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

2014-09-06 15:12 GMT+02:00 Joel Jacobson <joel(at)trustly(dot)com>:

> On Sat, Sep 6, 2014 at 6:59 AM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
> wrote:
> > People can prepare a simple functions like you did:
> >
> > ...
> >
> > CREATE OR REPLACE FUNCTION user_list ()
> > RETURNS SETOF id AS $$
> > BEGIN
> > RETURN QUERY SELECT id FROM user WHERE .. some = $1
> > END;
> > $$ LANGUAGE plpgsql;
> >
> > CREATE OR REPLACE FUNCTION update_user(int)
> > RETURNS void AS $$
> > BEGIN
> > UPDATE user SET .. WHERE id = $1
> > END;
> > $$ LANGUAGE;
> >
> > And then use it in mass operations:
> >
> > BEGIN
> > FOR company IN SELECT * FROM company_list()
> > LOOP
> > FOR id IN SELECT * FROM user_list(company)
> > LOOP
> > update_user(id);
> > END LOOP;
> >
> > Or use it in application same style.
> >
> > It is safe .. sure, and I accept it. But It is terrible slow.
>
> The above is horrible and ugly. That's not how I write code.
> Only for top-level functions, i.e. API-functions, is it motivated to
> encapsulate even simple queries like that, but *never* in other
> PL-functions, as that doesn't fulfil any purpose, putting simple
> queries inside functions only make it less obvious what the code does
> where you have a function call instead of a SQL-query.
>

It is ugly, but I meet it. Its nothing special.

Pavel

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2014-09-06 13:30:06 Re: pg_upgrade and epoch
Previous Message Joel Jacobson 2014-09-06 13:12:43 Re: PL/pgSQL 1.2