if exists, select; if not, create then select... ?? - Mailing list pgsql-general

From will trillich
Subject if exists, select; if not, create then select... ??
Date
Msg-id 20010515014632.A18580@serensoft.com
Whole thread Raw
List pgsql-general
okay. i seem to recall some discussion on pl/pgsql wherein a
function called via SELECT can't do an INSERT.

is there a way to do something like

    select get_or_create(...);

which runs some pl/pgsql such as

    select * from tbl where...;
    if not found
        insert into tbl values (...);
        select * from tbl where...;
    end if;
    return found stuff, even if we had to create it...

or not?

i'm looking for results similar to perl code like

    $something ||= &make_new();
    # which translates to (more or less, for this application)
    if ( ! defined( $something ) ) {
        $something = &make_new();
    }
    return $something;

i've tried conceiving of a rule that might intervene properly,
but i'm stymied...

--
What do I need manners for? I already got me a wife.
-- Adam Pontipee, "Seven Brides for Seven Brothers"

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

pgsql-general by date:

Previous
From: Justin Clift
Date:
Subject: Re: PostgreSQL in Comparison to mySQL
Next
From: Lincoln Yeoh
Date:
Subject: index ops for _int4 and trees?