Re: possible connection leak in dblink? - Mailing list pgsql-hackers

From Fujii Masao
Subject Re: possible connection leak in dblink?
Date
Msg-id BANLkTinOnN5JpEXKSEg3XUJj1PeZu-gU-w@mail.gmail.com
Whole thread Raw
In response to possible connection leak in dblink?  (Peter Eisentraut <peter_e@gmx.net>)
Responses Re: possible connection leak in dblink?
Re: possible connection leak in dblink?
Re: possible connection leak in dblink?
List pgsql-hackers
On Wed, Jun 15, 2011 at 5:34 AM, Peter Eisentraut <peter_e@gmx.net> wrote:
> With gcc 4.6, I get this warning:
>
> dblink.c: In function ‘dblink_send_query’:
> dblink.c:620:7: warning: variable ‘freeconn’ set but not used [-Wunused-but-set-variable]
>
> I don't know much about the internals of dblink, but judging from the
> surrounding code, I guess that this fix is necessary:
>
> diff --git i/contrib/dblink/dblink.c w/contrib/dblink/dblink.c
> index 19b98fb..e014c1a 100644
> --- i/contrib/dblink/dblink.c
> +++ w/contrib/dblink/dblink.c
> @@ -634,6 +634,10 @@ dblink_send_query(PG_FUNCTION_ARGS)
>        if (retval != 1)
>                elog(NOTICE, "%s", PQerrorMessage(conn));
>
> +       /* if needed, close the connection to the database and cleanup */
> +       if (freeconn)
> +               PQfinish(conn);
> +
>        PG_RETURN_INT32(retval);
>  }
>
> Otherwise the connection might not get freed.  Could someone verify
> that?

ISTM that the root problem is that dblink_send_query calls DBLINK_GET_CONN
though it doesn't accept the connection string as an argument. Since the first
argument in dblink_send_query must be the connection name, dblink_send_query
should call DBLINK_GET_NAMED_CONN instead. The variable 'freeconn' is used
only when DBLINK_GET_CONN is called. So, if dblink_send_query uses
DBLINK_GET_NAMED_CONN instead, the variable 'freeconn' is no longer necessary.

The similar problem exists in dblink_get_result and dblink_record_internal.
Attached patch fixes those problems.

Regards,

--
Fujii Masao
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center

Attachment

pgsql-hackers by date:

Previous
From: Greg Stark
Date:
Subject: Re: procpid?
Next
From: Andrew Dunstan
Date:
Subject: Re: Why polecat and colugos are failing to build back branches