pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: L J Bayuk <ljb220@mindspring.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: pgin.tcl pg_exec_prepared slow (was: Released...)
Date: Wed, 7 Jul 2004 21:12:44 -0400
Message-ID: <20040708011244.GA331@bxlbisnugqvi.mindspring.com> (raw)
In-Reply-To: <14766.1089081827@sss.pgh.pa.us>
References: <24110.1087945736@sss.pgh.pa.us>
	<Pine.LNX.4.21.0406302310340.12226-100000@ponder.fairway2k.co.uk>
	<20040706010731.GA15183@bxlbisnugqvi.mindspring.com>
	<14766.1089081827@sss.pgh.pa.us>

On Mon, Jul 05, 2004 at 10:43:47PM -0400, Tom Lane wrote:
> L J Bayuk <ljb220@mindspring.com> writes:
> > The delay we are seeing (about 40 ms) is from the time the client sends the
> > Bind message to PostgreSQL until it gets a TCP ACK back. (It should get
> > a BindComplete message back, but the backend doesn't flush after sending
> > this - no pq_flush() at the end of exec_bind_message(). Hello, Tom, is this
> > a bug?
> 
> No.  If you wanted a flush just there, send a Flush message.  But AFAICS
> the only reason why you'd do that is if you needed to look at the
> BindComplete before sending your next message --- which you do not.


I agree, it works OK as is. I only raised the question because of two
things. First, the protocol documentation says:
  "The response (to the Bind message) is either BindComplete
   or ErrorResponse."
From which one might wrongly conclude that you get BindComplete (or
ErrorResponse) back when you send Bind. (Pgin.tcl doesn't assume this.)

Second, this comment in backend/utils/error/elog.c:
   "This flush is normally not necessary, since postgres.c will flush
    out waiting data when control returns to the main loop. But it..."
I could not find the described flush "when control returns to the main
loop". Either I'm missing it, or it isn't there, and the comment implies
that it may have been there at one time.


> > pgin.tcl sets its Tcl connection socket channel to "unbuffered",
> > which means each PostgreSQL message it sends will be go into the TCP
> > buffers immediately, since each message is written in a single "puts".
> 
> This is certainly bogus.  A good implementation would arrange to flush
> the TCP outbound queue immediately after sending either a Flush or a
> Sync message --- and *no place else*.  Those are exactly the points
> where you are starting to wait for a backend reply.  More frequent
> flushing simply results in more separate network packets, which is not
> a win.  Also, if you haven't sent one of these message types, you are
> not entitled to expect any immediate reply (which is essentially what's
> happening with pgin.tcl, evidently).
> ...

No, I didn't explain it well.  Pgin.tcl itself isn't waiting for a reply to
Bind. It sends Bind, DescribePortal, Execute, and Sync without waiting in
between.  Currently it flushes after each, but this is Tcl flushing to TCP,
not to the wire. I'm going to change that, although in Tcl I can't force
TCP to flush its outbound queue.  But for now, I seem to end up with Bind
in one packet, and the other three messages combined into another.  That
delay is pgin.tcl blocked while the TCP stack waits for an ACK to the Bind
message's packet. I don't know why this happens but it seems to be a case
where TCP performs "less than optimally" in deciding when to send what it's
got, when to ACK, and when to wait. I will work around it by exercising
more control on when Tcl flushes the data to TCP.  I just haven't decided
whether to flush before reading, or flush after all messages that need a
response (I think: Startup, PasswordMessage, Query, Sync, CopyDone, and
FunctionCall are the ones I use).



view thread (8+ messages)  latest in thread

Message-ID: <20040708011244.GA331@bxlbisnugqvi.mindspring.com>
Permalink:  ../20040708011244.GA331@bxlbisnugqvi.mindspring.com/
Also on:    postgresql.org/message-id/20040708011244.GA331@bxlbisnugqvi.mindspring.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: ljb220@mindspring.com, tgl@sss.pgh.pa.us
  Subject: Re: pgin.tcl pg_exec_prepared slow (was: Released...)
  In-Reply-To: <20040708011244.GA331@bxlbisnugqvi.mindspring.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox