Thread: Postgre Protocol

Postgre Protocol

From
Greg
Date:
Hi all, I have a question (or more of a guidence request) in regards to PostgreSQL v8.4 Protocol v3 and accessing the database via TCP.

Background: part of the summer project that Im involved in I have to create fast & light (open connection, send query, read results) .Net 4 (C#) provider, similar to npgsql but should only use streams. So far I managed to get socket connection to database and authenticate (recived AuthenticationOk response code from database), right now Im not shure what to do. Documentation says that I need to wait for other messages from the server, such as "BackendKeyData" or "ReadyForQuery" but the database is not writing anything into a buffer so when I try to read from it, my application just waits.

So question is after I recive AuthenticationOk, do I need to wait for ReadyForQuery message from the database or I can go ahead and start senting queries to the database?

Database: PostgreSQL v8.4
Aplication Platform: .Net 4.0
Protol Documentation: http://www.postgresql.org/docs/8.4/interactive/protocol.html

Thanks.

PS: A have been looking at the source code for Npgsql, but I can't make sence of whats going on.

Re: Postgre Protocol

From
Tom Lane
Date:
Greg <grigorey@yahoo.co.uk> writes:
> So question is after I recive AuthenticationOk, do I need to wait for
> ReadyForQuery message from the database

Yes.  You should have gotten it --- the backend is designed to coalesce
those messages into a single network packet, so as to avoid extra
network traffic.  I wonder whether your code is failing to notice
additional logical messages arriving in the same bufferload.

            regards, tom lane

Re: Postgre Protocol

From
Greg
Date:
Hi Tom, thanks for the quick reply. I've looked at the break points and you are right there are 7 more bytes with data send with AuthenticationOk message, so I think thats what I need to look at :) (Im sure this is just a confirmation that everything is ok).

May I ask, sequence of bytes after the AuthenticationOk messages is: 0080003; Is this sequence represent BackendKeyData or values for Authentication*** status codes, or Im completely worng and it is realy nothing but confirmation for a frontend how the database applied options passed with start-up packet?

Thanks!
Greg.




--- On Mon, 28/6/10, Tom Lane <tgl@sss.pgh.pa.us> wrote:

From: Tom Lane <tgl@sss.pgh.pa.us>
Subject: Re: [NOVICE] Postgre Protocol
To: "Greg" <grigorey@yahoo.co.uk>
Cc: pgsql-novice@postgresql.org
Date: Monday, 28 June, 2010, 19:08

Greg <grigorey@yahoo.co.uk> writes:
> So question is after I recive AuthenticationOk, do I need to wait for
> ReadyForQuery message from the database

Yes.  You should have gotten it --- the backend is designed to coalesce
those messages into a single network packet, so as to avoid extra
network traffic.  I wonder whether your code is failing to notice
additional logical messages arriving in the same bufferload.

            regards, tom lane

Re: Postgre Protocol

From
Tom Lane
Date:
Greg <grigorey@yahoo.co.uk> writes:
> Hi Tom, thanks for the quick reply. I've looked at the break points and you are right there are 7 more bytes with
datasend with AuthenticationOk message, so I think thats what I need to look at :) (Im sure this is just a confirmation
thateverything is ok).  
> May I ask, sequence of bytes after the AuthenticationOk messages is: 0080003; Is this sequence represent
BackendKeyDataor values for Authentication*** status codes, or Im completely worng and it is realy nothing but
confirmationfor a frontend how the database applied options passed with start-up packet? 

That doesn't look right at all.  Per the start-up portion of the Message
Flow section:

    After having received AuthenticationOk, the frontend must wait for further messages from the server. In this phase
abackend process is being started, and the frontend is just an interested bystander. It is still possible for the
startupattempt to fail (ErrorResponse), but in the normal case the backend will send some ParameterStatus messages,
BackendKeyData,and finally ReadyForQuery. 

I'd expect ten or so ParameterStatus messages (depending on which PG
version you are working with), so there's easily going to be 100+ bytes
between the AuthenticationOk and ReadyForQuery messages.  As I said
earlier, the backend will not flush this data out until it's ready for a
query, so you should expect to see it all arriving in one network
packet.

[ eyeballs that data some more... ]  Are you *sure* you've collected an
AuthenticationOk message?  The various authentication response messages
all have the same type code ('R'), and 0000000800000003 would be what
follows that for AuthenticationCleartextPassword.  Maybe the backend is
really challenging you for a password.  Have you checked what ought to
happen according to pg_hba.conf?

            regards, tom lane

Re: Postgre Protocol

From
Greg
Date:
Ok, after some though I got the error: reading integer from stream was done in a wrong way. So with fixed stream reader and after sending start-up packet, server response with AuthenticationClearTextPassword (as Tom said), so now its sending the password to the database and see how this turn out.

Thanks Tom!



--- On Tue, 29/6/10, Tom Lane <tgl@sss.pgh.pa.us> wrote:

From: Tom Lane <tgl@sss.pgh.pa.us>
Subject: Re: [NOVICE] Postgre Protocol
To: "Greg" <grigorey@yahoo.co.uk>
Cc: pgsql-novice@postgresql.org
Date: Tuesday, 29 June, 2010, 0:10

Greg <grigorey@yahoo.co.uk> writes:
> Hi Tom, thanks for the quick reply. I've looked at the break points and you are right there are 7 more bytes with data send with AuthenticationOk message, so I think thats what I need to look at :) (Im sure this is just a confirmation that everything is ok).
> May I ask, sequence of bytes after the AuthenticationOk messages is: 0080003; Is this sequence represent BackendKeyData or values for Authentication*** status codes, or Im completely worng and it is realy nothing but confirmation for a frontend how the database applied options passed with start-up packet?

That doesn't look right at all.  Per the start-up portion of the Message
Flow section:

    After having received AuthenticationOk, the frontend must wait for further messages from the server. In this phase a backend process is being started, and the frontend is just an interested bystander. It is still possible for the startup attempt to fail (ErrorResponse), but in the normal case the backend will send some ParameterStatus messages, BackendKeyData, and finally ReadyForQuery.

I'd expect ten or so ParameterStatus messages (depending on which PG
version you are working with), so there's easily going to be 100+ bytes
between the AuthenticationOk and ReadyForQuery messages.  As I said
earlier, the backend will not flush this data out until it's ready for a
query, so you should expect to see it all arriving in one network
packet.

[ eyeballs that data some more... ]  Are you *sure* you've collected an
AuthenticationOk message?  The various authentication response messages
all have the same type code ('R'), and 0000000800000003 would be what
follows that for AuthenticationCleartextPassword.  Maybe the backend is
really challenging you for a password.  Have you checked what ought to
happen according to pg_hba.conf?

            regards, tom lane