Unix Technical Forum

Checking if Aggregate exists

This is a discussion on Checking if Aggregate exists within the Pgsql General forums, part of the PostgreSQL category; --> Hi, I have an upgrade script that is supposed to install items into a postgresql database if they don't ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 12:22 AM
Josh Trutwin
 
Posts: n/a
Default Checking if Aggregate exists

Hi,

I have an upgrade script that is supposed to install items into a
postgresql database if they don't already exist. One of the items I'm
having a hard time with is aggregates. I want to check if aggregate
foo doesn't exist, then run an SQL command to generate it.

if (check_aggregate_exists('foo') === false) {
$db->execute("CREATE AGGREGATE foo ....");
}

My code to check if an aggregate exists runs this query:

SELECT * FROM pg_catalog.pg_aggretate WHERE aggfnoid = 'foo'::REGPROC;

That works great IF foo exists, problem is if foo doesn't exist when
this query runs I get an error:

ERROR: function "foo" does not exist

The error causes my transaction to abort and rollback all the changes
I had already made in my update script.

Is there a better way to do this? Maybe a stored proc that eats the
error message?

Thanks,

Josh

-
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 12:22 AM
Martijn van Oosterhout
 
Posts: n/a
Default Re: Checking if Aggregate exists

On Mon, Mar 24, 2008 at 12:47:43PM -0500, Josh Trutwin wrote:
> My code to check if an aggregate exists runs this query:
>
> SELECT * FROM pg_catalog.pg_aggretate WHERE aggfnoid = 'foo'::REGPROC;


Seems to me you'd rather want the proisagg column in pg_proc and forget
about pg_aggregate altogether...

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFH5+5uIB7bNG8LQkwRAuGXAJ9zwN2cNOl0zadEJrKhvP bz+FmXDACfS+PN
C2Ryfr8Hvu0RFitnW5v0qvE=
=IXxM
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 12:22 AM
Alvaro Herrera
 
Posts: n/a
Default Re: Checking if Aggregate exists

Josh Trutwin wrote:

> Is there a better way to do this? Maybe a stored proc that eats the
> error message?


Well, you can use a plpgsql function with a BEGIN/EXCEPTION/END block to
"eat" the error message. However I'm wondering whether you should be
checking the aggregate argument type(s) as well.

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

-
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 12:22 AM
Erik Jones
 
Posts: n/a
Default Re: Checking if Aggregate exists


On Mar 24, 2008, at 1:09 PM, Martijn van Oosterhout wrote:
> On Mon, Mar 24, 2008 at 12:47:43PM -0500, Josh Trutwin wrote:
>> My code to check if an aggregate exists runs this query:
>>
>> SELECT * FROM pg_catalog.pg_aggretate WHERE aggfnoid =
>> 'foo'::REGPROC;

>
> Seems to me you'd rather want the proisagg column in pg_proc and
> forget
> about pg_aggregate altogether...


Also, the idiom for checking if something is present is normally:

SELECT 1 FROM some_table WHERE ...;

This way you aren't dealing with errors, if it doesn't exist the query
simply doesn't return any results.

Erik Jones

DBA | EmmaŽ
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com




-
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 12:22 AM
Josh Trutwin
 
Posts: n/a
Default Re: Checking if Aggregate exists

On Mon, 24 Mar 2008 14:02:02 -0500
Erik Jones <erik@myemma.com> wrote:

>
> On Mar 24, 2008, at 1:09 PM, Martijn van Oosterhout wrote:
> > On Mon, Mar 24, 2008 at 12:47:43PM -0500, Josh Trutwin wrote:
> >> My code to check if an aggregate exists runs this query:
> >>
> >> SELECT * FROM pg_catalog.pg_aggretate WHERE aggfnoid =
> >> 'foo'::REGPROC;

> >
> > Seems to me you'd rather want the proisagg column in pg_proc and
> > forget
> > about pg_aggregate altogether...

>
> Also, the idiom for checking if something is present is normally:
>
> SELECT 1 FROM some_table WHERE ...;
>
> This way you aren't dealing with errors, if it doesn't exist the
> query simply doesn't return any results.


This one still does return an error though I think because of the
cast:

select 1 from pg_catalog.pg_aggregate where aggfnoid =
'foo'::regproc;

ERROR: function "foo" does not exist

Thanks,

Josh

-
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-10-2008, 12:22 AM
Erik Jones
 
Posts: n/a
Default Re: Checking if Aggregate exists


On Mar 24, 2008, at 2:18 PM, Josh Trutwin wrote:
> On Mon, 24 Mar 2008 14:02:02 -0500
> Erik Jones <erik@myemma.com> wrote:
>
>>
>> On Mar 24, 2008, at 1:09 PM, Martijn van Oosterhout wrote:
>>> On Mon, Mar 24, 2008 at 12:47:43PM -0500, Josh Trutwin wrote:
>>>> My code to check if an aggregate exists runs this query:
>>>>
>>>> SELECT * FROM pg_catalog.pg_aggretate WHERE aggfnoid =
>>>> 'foo'::REGPROC;
>>>
>>> Seems to me you'd rather want the proisagg column in pg_proc and
>>> forget
>>> about pg_aggregate altogether...

>>
>> Also, the idiom for checking if something is present is normally:
>>
>> SELECT 1 FROM some_table WHERE ...;
>>
>> This way you aren't dealing with errors, if it doesn't exist the
>> query simply doesn't return any results.

>
> This one still does return an error though I think because of the
> cast:
>
> select 1 from pg_catalog.pg_aggregate where aggfnoid =
> 'foo'::regproc;
>
> ERROR: function "foo" does not exist


As Martijn pointed out, use pg_proc instead of pg_aggregate:

SELECT 1 from pg_proc WHERE proname='foo' AND proisagg IS TRUE;

And, as Alvarro pointed out in another reply, you'll probably want to
include conditions in your where clause for the argument types.

Erik Jones

DBA | EmmaŽ
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com




-
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 12:22 AM
Shane Ambler
 
Posts: n/a
Default Re: Checking if Aggregate exists

Erik Jones wrote:
>
> On Mar 24, 2008, at 2:18 PM, Josh Trutwin wrote:
>> On Mon, 24 Mar 2008 14:02:02 -0500
>> Erik Jones <erik@myemma.com> wrote:
>>
>>>
>>> On Mar 24, 2008, at 1:09 PM, Martijn van Oosterhout wrote:
>>>> On Mon, Mar 24, 2008 at 12:47:43PM -0500, Josh Trutwin wrote:
>>>>> My code to check if an aggregate exists runs this query:
>>>>>
>>>>> SELECT * FROM pg_catalog.pg_aggretate WHERE aggfnoid =
>>>>> 'foo'::REGPROC;
>>>>
>>>> Seems to me you'd rather want the proisagg column in pg_proc and
>>>> forget
>>>> about pg_aggregate altogether...
>>>
>>> Also, the idiom for checking if something is present is normally:
>>>
>>> SELECT 1 FROM some_table WHERE ...;
>>>
>>> This way you aren't dealing with errors, if it doesn't exist the
>>> query simply doesn't return any results.

>>
>> This one still does return an error though I think because of the
>> cast:
>>
>> select 1 from pg_catalog.pg_aggregate where aggfnoid =
>> 'foo'::regproc;
>>
>> ERROR: function "foo" does not exist

>
> As Martijn pointed out, use pg_proc instead of pg_aggregate:
>
> SELECT 1 from pg_proc WHERE proname='foo' AND proisagg IS TRUE;
>
> And, as Alvarro pointed out in another reply, you'll probably want to
> include conditions in your where clause for the argument types.
>


Now I'd go the other way and SELECT count(*) FROM....

If it ain't there you get 0 returned - no errors.
In your function you can test >0 and return true else false or you can
return the count and test numerically against the return.



--

Shane Ambler
pgSQL (at) Sheeky (dot) Biz

Get Sheeky @ http://Sheeky.Biz

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com