Millisecond-precision connect_timeout for libpq

Lists: pgsql-hackers
From: ivan babrou <ibobrik(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 17:28:59
Message-ID: CANWdNRA3wgpiU_nxQis7ZBNk87uWUFkMLb+hA6C-dkfjO4h0yg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi, guys! I made a quick patch to support floating number in
connect_timeout param for libpq. This will treat floating number as
seconds so this is backwards-compatible. I don't usually write in C,
so there may be mistakes. Could you review it and give me some
feedback?

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou

Attachment Content-Type Size
connect_timeout_in_ms.patch application/octet-stream 5.1 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 18:20:53
Message-ID: 23774.1373048453@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

ivan babrou <ibobrik(at)gmail(dot)com> writes:
> Hi, guys! I made a quick patch to support floating number in
> connect_timeout param for libpq.

What exactly is the use case for that? It seems like extra complication
for something with little if any real-world usefulness.

regards, tom lane


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 18:31:12
Message-ID: CANWdNRAByb7Hvi_wCrdT4ny-5UQGh4r8JFXBm_ojOjSA72_QWg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

If you can figure out that postgresql is overloaded then you may
decide what to do faster. In our app we have very strict limit for
connect time to mysql, redis and other services, but postgresql has
minimum of 2 seconds. When processing time for request is under 100ms
on average sub-second timeouts matter.

On 5 July 2013 22:20, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> ivan babrou <ibobrik(at)gmail(dot)com> writes:
>> Hi, guys! I made a quick patch to support floating number in
>> connect_timeout param for libpq.
>
> What exactly is the use case for that? It seems like extra complication
> for something with little if any real-world usefulness.
>
> regards, tom lane

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 19:26:30
Message-ID: 25212.1373052390@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

ivan babrou <ibobrik(at)gmail(dot)com> writes:
> If you can figure out that postgresql is overloaded then you may
> decide what to do faster. In our app we have very strict limit for
> connect time to mysql, redis and other services, but postgresql has
> minimum of 2 seconds. When processing time for request is under 100ms
> on average sub-second timeouts matter.

If you are issuing a fresh connection for each sub-100ms query, you're
doing it wrong anyway ...

regards, tom lane


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 19:47:16
Message-ID: CANWdNRBE9wS2Re6pzkStqOhxNyU+4VSAZUatC1vq+HzTe8grQQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 5 July 2013 23:26, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> ivan babrou <ibobrik(at)gmail(dot)com> writes:
>> If you can figure out that postgresql is overloaded then you may
>> decide what to do faster. In our app we have very strict limit for
>> connect time to mysql, redis and other services, but postgresql has
>> minimum of 2 seconds. When processing time for request is under 100ms
>> on average sub-second timeouts matter.
>
> If you are issuing a fresh connection for each sub-100ms query, you're
> doing it wrong anyway ...
>
> regards, tom lane

In php you cannot persist connection between requests without worrying
about transaction state. We don't use postgresql for every sub-100ms
query because it can block the whole request for 2 seconds. Usually it
takes 1.5ms to connect, btw.

Can you tell me why having ability to specify more accurate connect
timeout is a bad idea?

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: ivan babrou <ibobrik(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 20:01:54
Message-ID: 51D72632.2090604@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 07/05/2013 12:26 PM, Tom Lane wrote:
> ivan babrou <ibobrik(at)gmail(dot)com> writes:
>> If you can figure out that postgresql is overloaded then you may
>> decide what to do faster. In our app we have very strict limit for
>> connect time to mysql, redis and other services, but postgresql has
>> minimum of 2 seconds. When processing time for request is under 100ms
>> on average sub-second timeouts matter.
>
> If you are issuing a fresh connection for each sub-100ms query, you're
> doing it wrong anyway ...

It's fairly common with certain kinds of apps, including Rails and PHP.
This is one of the reasons why we've discussed having a kind of
stripped-down version of pgbouncer built into Postgres as a connection
manager. If it weren't valuable to be able to relocate pgbouncer to
other hosts, I'd still say that was a good idea.

Ivan would really strongly benefit from running pgbouncer on his
appservers instead of connecting directly to Postgres.

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, ivan babrou <ibobrik(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 20:09:49
Message-ID: 51D7280D.2050705@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 7/5/2013 1:01 PM, Josh Berkus wrote:
> If you are issuing a fresh connection for each sub-100ms query, you're
> doing it wrong anyway ...
> It's fairly common with certain kinds of apps, including Rails and PHP.
> This is one of the reasons why we've discussed having a kind of
> stripped-down version of pgbouncer built into Postgres as a connection
> manager. If it weren't valuable to be able to relocate pgbouncer to
> other hosts, I'd still say that was a good idea.

No kidding. I think a lot of -hackers forget that the web rules here and
the web is stateless, which means a huge performance loss for postgresql
unless we add yet another piece of software. Pre-forking here would
really help us.

JD


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, ivan babrou <ibobrik(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-05 20:24:58
Message-ID: 26529.1373055898@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Joshua D. Drake" <jd(at)commandprompt(dot)com> writes:
> On 7/5/2013 1:01 PM, Josh Berkus wrote:
>> This is one of the reasons why we've discussed having a kind of
>> stripped-down version of pgbouncer built into Postgres as a connection
>> manager. If it weren't valuable to be able to relocate pgbouncer to
>> other hosts, I'd still say that was a good idea.

> No kidding. I think a lot of -hackers forget that the web rules here and
> the web is stateless, which means a huge performance loss for postgresql
> unless we add yet another piece of software. Pre-forking here would
> really help us.

Pre-forking, per se, wouldn't be that much help IMO. You really want to
connect to a backend that's already loaded its catalog caches etc. So a
connection pooler is the right solution, not least because you can get
it today. Whether we should integrate a pooler into core is more of a
project management issue than a technical one, I think.

regards, tom lane


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-08 05:44:32
Message-ID: CANWdNRDaiNo7vgfQFkrMdC4kvmzSdyoBG=Qaj0S4gm_sZeZa2g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 5 July 2013 23:47, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> On 5 July 2013 23:26, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> ivan babrou <ibobrik(at)gmail(dot)com> writes:
>>> If you can figure out that postgresql is overloaded then you may
>>> decide what to do faster. In our app we have very strict limit for
>>> connect time to mysql, redis and other services, but postgresql has
>>> minimum of 2 seconds. When processing time for request is under 100ms
>>> on average sub-second timeouts matter.
>>
>> If you are issuing a fresh connection for each sub-100ms query, you're
>> doing it wrong anyway ...
>>
>> regards, tom lane
>
> In php you cannot persist connection between requests without worrying
> about transaction state. We don't use postgresql for every sub-100ms
> query because it can block the whole request for 2 seconds. Usually it
> takes 1.5ms to connect, btw.
>
> Can you tell me why having ability to specify more accurate connect
> timeout is a bad idea?
>
> --
> Regards, Ian Babrou
> http://bobrik.name http://twitter.com/ibobrik skype:i.babrou

Nobody answered my question yet.

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-08 16:40:29
Message-ID: 0D6497CD-0E5B-4C96-8D77-2DE623E40941@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 8, 2013, at 7:44 AM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:

>> Can you tell me why having ability to specify more accurate connect
>> timeout is a bad idea?
>
> Nobody answered my question yet.

From an earlier post by Tom:

> What exactly is the use case for that? It seems like extra complication
> for something with little if any real-world usefulness.

So the answer is: extra complication.

Best,

David


From: Cédric Villemain <cedric(at)2ndquadrant(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, ivan babrou <ibobrik(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-08 17:13:38
Message-ID: 201307081913.39070.cedric@2ndquadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Le lundi 8 juillet 2013 18:40:29, David E. Wheeler a écrit :
> On Jul 8, 2013, at 7:44 AM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> >> Can you tell me why having ability to specify more accurate connect
> >> timeout is a bad idea?
> >
> > Nobody answered my question yet.
>
> From an earlier post by Tom:
> > What exactly is the use case for that? It seems like extra complication
> > for something with little if any real-world usefulness.
>
> So the answer is: extra complication.

for something that must go through a pooler anyway.
You can have a look at pgbouncer: query_wait_timeout parameter for example.

--
Cédric Villemain +33 (0)6 20 30 22 52
http://2ndQuadrant.fr/
PostgreSQL: Support 24x7 - Développement, Expertise et Formation


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-08 18:31:23
Message-ID: CANWdNRBP7GXHCZxO3gXd9kM_iphLqoc3gzO7cSKKi9n62n3SVg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 8 July 2013 20:40, David E. Wheeler <david(at)justatheory(dot)com> wrote:
> On Jul 8, 2013, at 7:44 AM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
>
>>> Can you tell me why having ability to specify more accurate connect
>>> timeout is a bad idea?
>>
>> Nobody answered my question yet.
>
> From an earlier post by Tom:
>
>> What exactly is the use case for that? It seems like extra complication
>> for something with little if any real-world usefulness.
>
> So the answer is: extra complication.
>
> Best,
>
> David
>

I don't see any extra complication in backwards-compatible patch that
removes more lines that adds. Can you tell me, what exactly is extra
complicated?

About pooling connections: we have 150 applications servers and 10
postgresql servers. Each app connects to each server -> 150
connections per server if I run pooler on each application server.
That's more than default setting and now we usually have not more than
10 connections per server. What would happen if we have 300 app
servers? I thought connections consume some memory. Running pooler not
on every app server gives no advantage — I still may get network
blackhole and 2 seconds delay. Moreover, now I can guess that
postgresql is overloaded if it does not accept connections, with
pooler I can simply blow up disks with heavy io.

Seriously, I don't get why running 150 poolers is easier. And my
problem is still here: server (pooler is this case) is down — 2
seconds delay. 2000% slower.

Where am I wrong?

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 02:44:12
Message-ID: 12157C19-315E-4392-9D21-840D6555273B@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 8, 2013, at 1:31 PM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> On 8 July 2013 20:40, David E. Wheeler <david(at)justatheory(dot)com> wrote:
>> On Jul 8, 2013, at 7:44 AM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
>>
>>>> Can you tell me why having ability to specify more accurate connect
>>>> timeout is a bad idea?
>>>
>>> Nobody answered my question yet.
>>
>> From an earlier post by Tom:
>>
>>> What exactly is the use case for that? It seems like extra complication
>>> for something with little if any real-world usefulness.
>>
>> So the answer is: extra complication.
>>
>> Best,
>>
>> David
>
> I don't see any extra complication in backwards-compatible patch that
> removes more lines that adds. Can you tell me, what exactly is extra
> complicated?
>
> About pooling connections: we have 150 applications servers and 10
> postgresql servers. Each app connects to each server -> 150
> connections per server if I run pooler on each application server.
> That's more than default setting and now we usually have not more than
> 10 connections per server. What would happen if we have 300 app
> servers? I thought connections consume some memory. Running pooler not
> on every app server gives no advantage — I still may get network
> blackhole and 2 seconds delay. Moreover, now I can guess that
> postgresql is overloaded if it does not accept connections, with
> pooler I can simply blow up disks with heavy io.
>
> Seriously, I don't get why running 150 poolers is easier. And my
> problem is still here: server (pooler is this case) is down — 2
> seconds delay. 2000% slower.
>
> Where am I wrong?

I agree with you.

...Robert


From: Markus Wanner <markus(at)bluegap(dot)ch>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 07:05:54
Message-ID: 51DBB652.3070805@bluegap.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 07/08/2013 08:31 PM, ivan babrou wrote:
> Seriously, I don't get why running 150 poolers is easier.

Did you consider running pgbouncer on the database servers?

Regards

Markus Wanner


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Markus Wanner <markus(at)bluegap(dot)ch>
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 07:15:17
Message-ID: CANWdNRCsWfLhcukiqa9ZrvTOR-J2AX-ARzAJ2XRG3d6Hjn1M2Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 9 July 2013 11:05, Markus Wanner <markus(at)bluegap(dot)ch> wrote:
> On 07/08/2013 08:31 PM, ivan babrou wrote:
>> Seriously, I don't get why running 150 poolers is easier.
>
> Did you consider running pgbouncer on the database servers?
>
> Regards
>
> Markus Wanner

Database server lost network — boom, 2 seconds delay. What's the point then?

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Markus Wanner <markus(at)bluegap(dot)ch>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 08:20:45
Message-ID: 51DBC7DD.5000806@bluegap.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 07/09/2013 09:15 AM, ivan babrou wrote:
> Database server lost network — boom, 2 seconds delay. What's the point then?

Oh, I see. Good point. It could still improve connection time during
normal operation, though.

None the less, I now agree with you: we recommend a pooler, which may be
capable of millisecond timeouts, but arguably is vastly more complex
than the proposed patch. And it even brings its own set of gotchas (lots
of connections). I guess I don't quite buy the complexity argument, yet.

Sure, gettimeofday() is subject to clock adjustments. But so is time().
And if you're setting timeouts that low, you probably know what you're
doing (or at least care about latency a lot). Or is gettimeofday() still
considerably slower on certain architectures or in certain scenarios?
Where's the complexity?

Regards

Markus Wanner


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Markus Wanner <markus(at)bluegap(dot)ch>
Cc: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 12:25:50
Message-ID: CANWdNRA5wsmvBC=-6BeZnt-zOhHrogdpM97L136omDfWK45o5w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 9 July 2013 12:20, Markus Wanner <markus(at)bluegap(dot)ch> wrote:
> On 07/09/2013 09:15 AM, ivan babrou wrote:
>> Database server lost network — boom, 2 seconds delay. What's the point then?
>
> Oh, I see. Good point. It could still improve connection time during
> normal operation, though.

Connection time during normal operation is 1.5ms which is fast enough for now.

> None the less, I now agree with you: we recommend a pooler, which may be
> capable of millisecond timeouts, but arguably is vastly more complex
> than the proposed patch. And it even brings its own set of gotchas (lots
> of connections). I guess I don't quite buy the complexity argument, yet.

Pooler isn't capable of millisecond timeouts. At least I don't see how
could I understand that pooler is dead in 50ms.

> Sure, gettimeofday() is subject to clock adjustments. But so is time().
> And if you're setting timeouts that low, you probably know what you're
> doing (or at least care about latency a lot). Or is gettimeofday() still
> considerably slower on certain architectures or in certain scenarios?
> Where's the complexity?

There's no complexity here :)

> Regards
>
> Markus Wanner

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 13:53:12
Message-ID: CAHyXU0weaox7pXxf13FZFBXUvDkgDmYHk3nUqYjn2Uvq-AdV3w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 5, 2013 at 12:28 PM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> Hi, guys! I made a quick patch to support floating number in
> connect_timeout param for libpq. This will treat floating number as
> seconds so this is backwards-compatible. I don't usually write in C,
> so there may be mistakes. Could you review it and give me some
> feedback?

First thing that jumps into my head: why not use asynchronous
connection (PQconnectStart, etc) and code the timeout on top of that?

merlin


From: Markus Wanner <markus(at)bluegap(dot)ch>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 13:59:43
Message-ID: 51DC174F.7000204@bluegap.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Ian,

On 07/05/2013 07:28 PM, ivan babrou wrote:
> - /*
> - * Rounding could cause connection to fail; need at least 2 secs
> - */

You removed this above comment... please check why it's there. The
relevant revision seems to be:

###
commit 2908a838ac2cf8cdccaa115249f8399eef8a731e
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Date: Thu Oct 24 23:35:55 2002 +0000

Code review for connection timeout patch. Avoid unportable assumption
that tv_sec is signed; return a useful error message on timeout failure;
honor PGCONNECT_TIMEOUT environment variable in PQsetdbLogin; make code
obey documentation statement that timeout=0 means no timeout.
###

> - if (timeout < 2)
> - timeout = 2;
> - /* calculate the finish time based on start + timeout */
> - finish_time = time(NULL) + timeout;
> + gettimeofday(&finish_time, NULL);
> + finish_time.tv_usec += (int) timeout_usec;

I vaguely recall tv_usec only being required to hold values up to
1000000 by some standard. A signed 32 bit value would qualify, but only
hold up to a good half hour worth of microseconds. That doesn't quite
seem enough to calculate finish_time the way you are proposing to do it.

> + finish_time.tv_sec += finish_time.tv_usec / 1000000;
> + finish_time.tv_usec = finish_time.tv_usec % 1000000;
> }
> }
>
> @@ -1073,15 +1074,15 @@ pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
> input_fd.events |= POLLOUT;
>
> /* Compute appropriate timeout interval */
> - if (end_time == ((time_t) -1))
> + if (end_time == NULL)
> timeout_ms = -1;
> else
> {
> - time_t now = time(NULL);
> + struct timeval now;
> + gettimeofday(&now, NULL);
>
> - if (end_time > now)
> - timeout_ms = (end_time - now) * 1000;
> - else
> + timeout_ms = (end_time->tv_sec - now.tv_sec) * 1000 + (end_time->tv_usec - now.tv_usec) / 1000;

I think that's incorrect on a platform where tv_sec and/or tv_usec is
unsigned. (And the cited commit above indicates there are such platforms.)

On 07/09/2013 02:25 PM, ivan babrou wrote:
> There's no complexity here :)

Not so fast, cowboy... :-)

Regards

Markus Wanner


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Markus Wanner <markus(at)bluegap(dot)ch>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 14:27:09
Message-ID: CANWdNRDH3wejovjnzEh9X7NtrnNK3JtGmqQw_rpwk8YAnd7gPA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 9 July 2013 17:59, Markus Wanner <markus(at)bluegap(dot)ch> wrote:
> Ian,
>
> On 07/05/2013 07:28 PM, ivan babrou wrote:
>> - /*
>> - * Rounding could cause connection to fail; need at least 2 secs
>> - */
>
> You removed this above comment... please check why it's there. The
> relevant revision seems to be:
>
> ###
> commit 2908a838ac2cf8cdccaa115249f8399eef8a731e
> Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
> Date: Thu Oct 24 23:35:55 2002 +0000

That's not correct, facb72007 is the relevant revision. It seems that
it's only applicable for small timeouts in seconds, but it you request
connect timeout in 1 ms you should be ready to fail. I may be wrong
about this, Bruce Momjian introduced that change in 2002.

> Code review for connection timeout patch. Avoid unportable assumption
> that tv_sec is signed; return a useful error message on timeout failure;
> honor PGCONNECT_TIMEOUT environment variable in PQsetdbLogin; make code
> obey documentation statement that timeout=0 means no timeout.
> ###
>
>> - if (timeout < 2)
>> - timeout = 2;
>> - /* calculate the finish time based on start + timeout */
>> - finish_time = time(NULL) + timeout;
>> + gettimeofday(&finish_time, NULL);
>> + finish_time.tv_usec += (int) timeout_usec;
>
> I vaguely recall tv_usec only being required to hold values up to
> 1000000 by some standard. A signed 32 bit value would qualify, but only
> hold up to a good half hour worth of microseconds. That doesn't quite
> seem enough to calculate finish_time the way you are proposing to do it.

Agree, this should be fixed.

>> + finish_time.tv_sec += finish_time.tv_usec / 1000000;
>> + finish_time.tv_usec = finish_time.tv_usec % 1000000;
>> }
>> }
>>
>> @@ -1073,15 +1074,15 @@ pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
>> input_fd.events |= POLLOUT;
>>
>> /* Compute appropriate timeout interval */
>> - if (end_time == ((time_t) -1))
>> + if (end_time == NULL)
>> timeout_ms = -1;
>> else
>> {
>> - time_t now = time(NULL);
>> + struct timeval now;
>> + gettimeofday(&now, NULL);
>>
>> - if (end_time > now)
>> - timeout_ms = (end_time - now) * 1000;
>> - else
>> + timeout_ms = (end_time->tv_sec - now.tv_sec) * 1000 + (end_time->tv_usec - now.tv_usec) / 1000;
>
> I think that's incorrect on a platform where tv_sec and/or tv_usec is
> unsigned. (And the cited commit above indicates there are such platforms.)

I don't get it. timeout_ms is signed, and can hold unsigned -
unsigned. Is it about anything else?

> On 07/09/2013 02:25 PM, ivan babrou wrote:
>> There's no complexity here :)
>
> Not so fast, cowboy... :-)
>
> Regards
>
> Markus Wanner

Is there anything else I should fix?

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 14:43:19
Message-ID: 20130709144319.GA27898@alap2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2013-07-05 21:28:59 +0400, ivan babrou wrote:
> Hi, guys! I made a quick patch to support floating number in
> connect_timeout param for libpq. This will treat floating number as
> seconds so this is backwards-compatible. I don't usually write in C,
> so there may be mistakes. Could you review it and give me some
> feedback?
>
> --
> Regards, Ian Babrou
> http://bobrik.name http://twitter.com/ibobrik skype:i.babrou

> diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
> index 18fcb0c..58c1a35 100644
> --- a/src/interfaces/libpq/fe-connect.c
> +++ b/src/interfaces/libpq/fe-connect.c
> @@ -1452,7 +1452,7 @@ static int
> connectDBComplete(PGconn *conn)
> {
> PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
> - time_t finish_time = ((time_t) -1);
> + struct timeval finish_time;
>
> if (conn == NULL || conn->status == CONNECTION_BAD)
> return 0;
> @@ -1462,17 +1462,14 @@ connectDBComplete(PGconn *conn)
> */
> if (conn->connect_timeout != NULL)
> {
> - int timeout = atoi(conn->connect_timeout);
> + int timeout_usec = (int) (atof(conn->connect_timeout) * 1000000);

I'd rather not use a plain int for storing usecs. An overflow is rather
unlikely, but still. Also, I'd rather use something like USECS_PER_SEC
instead of a plain 1000000 in multiple places.

>
> - if (timeout > 0)
> + if (timeout_usec > 0)
> {
> - /*
> - * Rounding could cause connection to fail; need at least 2 secs
> - */
> - if (timeout < 2)
> - timeout = 2;
> - /* calculate the finish time based on start + timeout */
> - finish_time = time(NULL) + timeout;
> + gettimeofday(&finish_time, NULL);
> + finish_time.tv_usec += (int) timeout_usec;

Accordingly adjust this.

Looks like a sensible thing to me.

*Independent* from this patch, you might want look into server-side
connection pooling using transaction mode. If that's applicable for
your application it might reduce latency noticeably.

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: ivan babrou <ibobrik(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 15:17:34
Message-ID: CAAfz9KNUhnn8m8aE3u+Sv6==B70kv4916mqXda6F1Ej2rP9aTQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2013/7/9 Merlin Moncure <mmoncure(at)gmail(dot)com>

> On Fri, Jul 5, 2013 at 12:28 PM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> > Hi, guys! I made a quick patch to support floating number in
> > connect_timeout param for libpq. This will treat floating number as
> > seconds so this is backwards-compatible. I don't usually write in C,
> > so there may be mistakes. Could you review it and give me some
> > feedback?
>
> First thing that jumps into my head: why not use asynchronous
> connection (PQconnectStart, etc) and code the timeout on top of that?
>
+1.

>
> merlin
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>

--
// Dmitriy.


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, ivan babrou <ibobrik(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-09 20:18:15
Message-ID: CAHyXU0xyj53agUgYUV2WUR9O7wLcnMg+RHZM-x39rcW+Z-vgbA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Jul 5, 2013 at 3:01 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
> On 07/05/2013 12:26 PM, Tom Lane wrote:
>> ivan babrou <ibobrik(at)gmail(dot)com> writes:
>>> If you can figure out that postgresql is overloaded then you may
>>> decide what to do faster. In our app we have very strict limit for
>>> connect time to mysql, redis and other services, but postgresql has
>>> minimum of 2 seconds. When processing time for request is under 100ms
>>> on average sub-second timeouts matter.
>>
>> If you are issuing a fresh connection for each sub-100ms query, you're
>> doing it wrong anyway ...
>
> It's fairly common with certain kinds of apps, including Rails and PHP.
> This is one of the reasons why we've discussed having a kind of
> stripped-down version of pgbouncer built into Postgres as a connection
> manager. If it weren't valuable to be able to relocate pgbouncer to
> other hosts, I'd still say that was a good idea.

for the record, I think this is a great idea.

merlin


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-10 07:49:31
Message-ID: CANWdNRAkt48p8uqtANBRPhW-hzeT5XJayAgzz4-5fEs70ia0-A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 9 July 2013 18:43, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> On 2013-07-05 21:28:59 +0400, ivan babrou wrote:
>> Hi, guys! I made a quick patch to support floating number in
>> connect_timeout param for libpq. This will treat floating number as
>> seconds so this is backwards-compatible. I don't usually write in C,
>> so there may be mistakes. Could you review it and give me some
>> feedback?
>>
>> --
>> Regards, Ian Babrou
>> http://bobrik.name http://twitter.com/ibobrik skype:i.babrou
>
>> diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
>> index 18fcb0c..58c1a35 100644
>> --- a/src/interfaces/libpq/fe-connect.c
>> +++ b/src/interfaces/libpq/fe-connect.c
>> @@ -1452,7 +1452,7 @@ static int
>> connectDBComplete(PGconn *conn)
>> {
>> PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
>> - time_t finish_time = ((time_t) -1);
>> + struct timeval finish_time;
>>
>> if (conn == NULL || conn->status == CONNECTION_BAD)
>> return 0;
>> @@ -1462,17 +1462,14 @@ connectDBComplete(PGconn *conn)
>> */
>> if (conn->connect_timeout != NULL)
>> {
>> - int timeout = atoi(conn->connect_timeout);
>> + int timeout_usec = (int) (atof(conn->connect_timeout) * 1000000);
>
> I'd rather not use a plain int for storing usecs. An overflow is rather
> unlikely, but still. Also, I'd rather use something like USECS_PER_SEC
> instead of a plain 1000000 in multiple places.
>
>>
>> - if (timeout > 0)
>> + if (timeout_usec > 0)
>> {
>> - /*
>> - * Rounding could cause connection to fail; need at least 2 secs
>> - */
>> - if (timeout < 2)
>> - timeout = 2;
>> - /* calculate the finish time based on start + timeout */
>> - finish_time = time(NULL) + timeout;
>> + gettimeofday(&finish_time, NULL);
>> + finish_time.tv_usec += (int) timeout_usec;
>
> Accordingly adjust this.
>
> Looks like a sensible thing to me.
>
> *Independent* from this patch, you might want look into server-side
> connection pooling using transaction mode. If that's applicable for
> your application it might reduce latency noticeably.
>
> Greetings,
>
> Andres Freund
>
> --
> Andres Freund http://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Training & Services

I tried to make it more safe. Still not sure about constants, I
haven't found any good examples in libpq.

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou

Attachment Content-Type Size
connect_timeout_in_ms.patch application/octet-stream 5.2 KB

From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-10 07:54:29
Message-ID: CANWdNRCNWQ68XrXXmgC-4SbjL6PaXxrhq6wc2VbpM1pWpYJGcQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 9 July 2013 19:17, Dmitriy Igrishin <dmitigr(at)gmail(dot)com> wrote:
>
>
>
> 2013/7/9 Merlin Moncure <mmoncure(at)gmail(dot)com>
>>
>> On Fri, Jul 5, 2013 at 12:28 PM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
>> > Hi, guys! I made a quick patch to support floating number in
>> > connect_timeout param for libpq. This will treat floating number as
>> > seconds so this is backwards-compatible. I don't usually write in C,
>> > so there may be mistakes. Could you review it and give me some
>> > feedback?
>>
>> First thing that jumps into my head: why not use asynchronous
>> connection (PQconnectStart, etc) and code the timeout on top of that?
>
> +1.
>>
>>
>> merlin
>>
>>
>> --
>> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-hackers
>
>
>
>
> --
> // Dmitriy.
>

Doesn't look like straightforward solution for me. In my case existing
drivers will benefit from my patch, in async case they should be
rewritten. We don't use libpq directly, we use native pgsql module
from php.

Even with that, kernel can wait for milliseconds — why should we limit
precision 1000x times and reinvent milliseconds again in userspace?

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-10 14:55:54
Message-ID: CAHyXU0zuv4JOtE4T4x136XwwMVsDEBevqHc7uyNuL+xLnNinnA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jul 10, 2013 at 2:54 AM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> On 9 July 2013 19:17, Dmitriy Igrishin <dmitigr(at)gmail(dot)com> wrote:
>>
>>
>>
>> 2013/7/9 Merlin Moncure <mmoncure(at)gmail(dot)com>
>>>
>>> On Fri, Jul 5, 2013 at 12:28 PM, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
>>> > Hi, guys! I made a quick patch to support floating number in
>>> > connect_timeout param for libpq. This will treat floating number as
>>> > seconds so this is backwards-compatible. I don't usually write in C,
>>> > so there may be mistakes. Could you review it and give me some
>>> > feedback?
>>>
>>> First thing that jumps into my head: why not use asynchronous
>>> connection (PQconnectStart, etc) and code the timeout on top of that?
>>
>> +1.
>>>
>>>
>>> merlin
>>>
>>>
>>> --
>>> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-hackers
>>
>>
>>
>>
>> --
>> // Dmitriy.
>>
>
> Doesn't look like straightforward solution for me. In my case existing
> drivers will benefit from my patch, in async case they should be
> rewritten. We don't use libpq directly, we use native pgsql module
> from php.
>
> Even with that, kernel can wait for milliseconds — why should we limit
> precision 1000x times and reinvent milliseconds again in userspace?

Fair point. Although I still agree with Tom in the sense that if I
were in your shoes I would be reconsidering certain parts of the
connection stack since you have such demanding requirements; even with
this solved I think other issues are lurking right around the corner.
That said, I did scan your patch briefly and noted it only changed
internal API functions and seems pretty straightforward. I withdraw my
objection.

merlin


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, ivan babrou <ibobrik(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-10 17:46:48
Message-ID: 51DD9E08.9050602@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 07/09/2013 01:18 PM, Merlin Moncure wrote:
> On Fri, Jul 5, 2013 at 3:01 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
>> It's fairly common with certain kinds of apps, including Rails and PHP.
>> This is one of the reasons why we've discussed having a kind of
>> stripped-down version of pgbouncer built into Postgres as a connection
>> manager. If it weren't valuable to be able to relocate pgbouncer to
>> other hosts, I'd still say that was a good idea.
>
> for the record, I think this is a great idea.

Well, we discussed this a bit last year. I'd personally love to have an
event-based connection manager tied to local Postgres, which could use
all of PostgreSQL's various authentication methods. For the simple
case, where a user has a rails/mod_python/PHP app which just spawns lots
of connections, this could make the whole "too many connections"
headache go away as a concern for first-time users.

We'd still need pgbouncer for the serious scalability cases (so that we
could put it on other machines), but I am a bit tired of explaining how
to manage max_connections to people.

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: ivan babrou <ibobrik(at)gmail(dot)com>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-15 12:34:07
Message-ID: CANWdNRAeAZ-d6+srqoBce=Eu0i+EcPh=kLadXFKh2EiywAYMFA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Is there any hope to see it in libpq? If so, can anyone review latest
version of my patch?

On 10 July 2013 11:49, ivan babrou <ibobrik(at)gmail(dot)com> wrote:
> On 9 July 2013 18:43, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
>> On 2013-07-05 21:28:59 +0400, ivan babrou wrote:
>>> Hi, guys! I made a quick patch to support floating number in
>>> connect_timeout param for libpq. This will treat floating number as
>>> seconds so this is backwards-compatible. I don't usually write in C,
>>> so there may be mistakes. Could you review it and give me some
>>> feedback?
>>>
>>> --
>>> Regards, Ian Babrou
>>> http://bobrik.name http://twitter.com/ibobrik skype:i.babrou
>>
>>> diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
>>> index 18fcb0c..58c1a35 100644
>>> --- a/src/interfaces/libpq/fe-connect.c
>>> +++ b/src/interfaces/libpq/fe-connect.c
>>> @@ -1452,7 +1452,7 @@ static int
>>> connectDBComplete(PGconn *conn)
>>> {
>>> PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
>>> - time_t finish_time = ((time_t) -1);
>>> + struct timeval finish_time;
>>>
>>> if (conn == NULL || conn->status == CONNECTION_BAD)
>>> return 0;
>>> @@ -1462,17 +1462,14 @@ connectDBComplete(PGconn *conn)
>>> */
>>> if (conn->connect_timeout != NULL)
>>> {
>>> - int timeout = atoi(conn->connect_timeout);
>>> + int timeout_usec = (int) (atof(conn->connect_timeout) * 1000000);
>>
>> I'd rather not use a plain int for storing usecs. An overflow is rather
>> unlikely, but still. Also, I'd rather use something like USECS_PER_SEC
>> instead of a plain 1000000 in multiple places.
>>
>>>
>>> - if (timeout > 0)
>>> + if (timeout_usec > 0)
>>> {
>>> - /*
>>> - * Rounding could cause connection to fail; need at least 2 secs
>>> - */
>>> - if (timeout < 2)
>>> - timeout = 2;
>>> - /* calculate the finish time based on start + timeout */
>>> - finish_time = time(NULL) + timeout;
>>> + gettimeofday(&finish_time, NULL);
>>> + finish_time.tv_usec += (int) timeout_usec;
>>
>> Accordingly adjust this.
>>
>> Looks like a sensible thing to me.
>>
>> *Independent* from this patch, you might want look into server-side
>> connection pooling using transaction mode. If that's applicable for
>> your application it might reduce latency noticeably.
>>
>> Greetings,
>>
>> Andres Freund
>>
>> --
>> Andres Freund http://www.2ndQuadrant.com/
>> PostgreSQL Development, 24x7 Support, Training & Services
>
> I tried to make it more safe. Still not sure about constants, I
> haven't found any good examples in libpq.
>
> --
> Regards, Ian Babrou
> http://bobrik.name http://twitter.com/ibobrik skype:i.babrou

--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou


From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: ivan babrou <ibobrik(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Millisecond-precision connect_timeout for libpq
Date: 2013-07-15 12:43:02
Message-ID: 20130715124301.GC8203@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2013-07-15 16:34:07 +0400, ivan babrou wrote:
> Is there any hope to see it in libpq? If so, can anyone review latest
> version of my patch?

A littlebit of patience please. There are heaps of other patches still
being reviewed that have been posted earlier than yours ;). I know it
can be frustrating, but that doesn't make reviewer time grow on trees...

http://wiki.postgresql.org/wiki/CommitFest

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services