Re: plpgsql return select from multiple tables

From: "Artis Caune" <artis(dot)caune(at)gmail(dot)com>
To: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: plpgsql return select from multiple tables
Date: 2008-09-11 08:19:24
Message-ID: 9e20d71e0809110119p3f29ea62s6a10b6c3d9fe88c8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Sep 10, 2008 at 5:43 PM, Filip Rembiałkowski
<plk(dot)zuber(at)gmail(dot)com> wrote:
> nothing wrong here but this can also be rewritten to pure SQL function
> (can be few percent faster and optimizable by planner)
>
> CREATE OR REPLACE FUNCTION get_user_data( INT )
> RETURNS SETOF RECORD AS $$
> SELECT
> (SELECT email FROM emails WHERE id = $1) as email,
> (SELECT backend FROM backends WHERE id = $1) as backend
> $$ LANGUAGE 'sql' STABLE STRICT SECURITY DEFINER;

We need some logic in selects (because applications like postfix can
do just simple queries):
- select email
- if not found then return empty
- if email.type is 1 then return foo
- if email.type is 2 then return bar

'IF FOUND THEN' or 'IF variable = X THEN' features are only in plpgsql, i think.
Didn't know that sql functions also can be definied with "SECURITY
DEFINER". We use them, so query_user can only select from function and
can not see the whole table/tables. I'll check sql functions.

> one question, why SETOF? this is supposed to always return one row
> always, right?
> you could create a TYPE and return this. queries would be a bit simpler:
>
> SELECT * FROM get_user_data('${id}');

Yes, it should return only one row. I can not use "return query" without SETOF.
Or should I create my_type, select into my_type_variable and return
my_type_variable?

--
regards,
Artis Caune

<----. CCNA
<----|====================
<----' didii FreeBSD

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Artis Caune 2008-09-11 08:30:03 Re: plpgsql return select from multiple tables
Previous Message Pavel Stehule 2008-09-11 08:01:23 Re: declare column update expression