Skip site navigation (1) Skip section navigation (2)

Re: Add dblink function to check if a named connection exists

From: Tommy Gildseth <tommy(dot)gildseth(at)usit(dot)uio(dot)no>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Joe Conway <mail(at)joeconway(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Add dblink function to check if a named connection exists
Date: 2008-06-03 08:54:05
Message-ID: 484506AD.7050102@usit.uio.no (view raw or flat)
Thread:
Lists: pgsql-hackers
Joe Conway wrote:
> Tom Lane wrote:
>> Tommy Gildseth <tommy(dot)gildseth(at)usit(dot)uio(dot)no> writes:
>>> One obvious disadvantage of this approach, is that I need to connect 
>>> and disconnect in every function. A possible solution to this, would 
>>> be having a function f.ex dblink_exists('connection_name') that 
>>> returns true/false depending on whether the  connection already exists.
>>
>> Can't you do this already?
>>
>>     SELECT 'myconn' = ANY (dblink_get_connections());
>>
>> A dedicated function might be a tad faster, but it probably isn't going
>> to matter compared to the overhead of sending a remote query.
> 
> I agree. The above is about as simple as
>   SELECT dblink_exists('dtest1');
> and probably not measurably slower. If you still think a dedicated 
> function is needed, please send the output of some performance testing 
> to justify it.
> 
> If you really want the notational simplicity, you could use an SQL 
> function to wrap it:
> 
> CREATE OR REPLACE FUNCTION dblink_exists(text)
> RETURNS bool AS $$
>   SELECT $1 = ANY (dblink_get_connections())
> $$ LANGUAGE sql;



dblink_get_connections() returns null if there are no connections 
though, so the above will fail if you haven't already established a 
connection, unless you also check for null, and not just false.

I guess you could rewrite the above function to something like:

CREATE OR REPLACE FUNCTION dblink_exists(text)
RETURNS bool AS $$
   SELECT COALESCE($1 = ANY (dblink_get_connections()), false)
$$ LANGUAGE sql;



-- 
Tommy Gildseth


In response to

pgsql-hackers by date

Next:From: Zdenek KotalaDate: 2008-06-03 09:27:29
Subject: Re: Case-Insensitve Text Comparison
Previous:From: Hannu KrosingDate: 2008-06-03 07:19:59
Subject: Re: Core team statement on replication in PostgreSQL

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group