<!-- doc/src/sgml/protocol.sgml -->
<chapter id="protocol">
<title>Frontend/Backend Protocol</title>
<indexterm zone="protocol">
<primary>protocol</primary>
<secondary>frontend-backend</secondary>
</indexterm>
<para>
<productname>PostgreSQL</productname> uses a message-based protocol
for communication between frontends and backends (clients and servers).
The protocol is supported over <acronym>TCP/IP</acronym> and also over
Unix-domain sockets. Port number 5432 has been registered with IANA as
the customary TCP port number for servers supporting this protocol, but
in practice any non-privileged port number can be used.
</para>
<para>
This document describes version 3.2 of the protocol, introduced in
<productname>PostgreSQL</productname> version 18. The server and the libpq
client library are backwards compatible with protocol version 3.0,
implemented in <productname>PostgreSQL</productname> 7.4 and later.
</para>
<para>
In order to serve multiple clients efficiently, the server launches
a new <quote>backend</quote> process for each client.
In the current implementation, a new child
process is created immediately after an incoming connection is detected.
This is transparent to the protocol, however. For purposes of the
protocol, the terms <quote>backend</quote> and <quote>server</quote> are
interchangeable; likewise <quote>frontend</quote> and <quote>client</quote>
are interchangeable.
</para>
<sect1 id="protocol-overview">
<title>Overview</title>
<para>
The protocol has separate phases for startup and normal operation.
In the startup phase, the frontend opens a connection to the server
and authenticates itself to the satisfaction of the server. (This might
involve a single message, or multiple messages depending on the
authentication method being used.) If all goes well, the server then sends
status information to the frontend, and finally enters normal operation.
Except for the initial startup-request message, this part of the
protocol is driven by the server.
</para>
<para>
During normal operation, the frontend sends queries and
other commands to the backend, and the backend sends back query results
and other responses. There are a few cases (such as <command>NOTIFY</command>)
wherein the
backend will send unsolicited messages, but for the most part this portion
of a session is driven by frontend requests.
</para>
<para>
Termination of the session is normally by frontend choice, but can be
forced by the backend in certain cases. In any case, when the backend
closes the connection, it will roll back any open (incomplete) transaction
before exiting.
</para>
<para>
Within normal operation, SQL commands can be executed through either of
two sub-protocols. In the <quote>simple query</quote> protocol, the frontend
just sends a textual query string, which is parsed and immediately
executed by the backend. In the <quote>extended query</quote> protocol,
processing of queries is separated into multiple steps: parsing,
binding of parameter values, and execution. This offers flexibility
and performance benefits, at the cost of extra complexity.
</para>
<para>
Normal operation has additional sub-protocols for special operations
such as <command>COPY</command>.
</para>
<sect2 id="protocol-message-concepts">
<title>Messaging Overview</title>
<para>
All communication is through a stream of messages. The first byte of a
message identifies the message type, and the next four bytes specify the
length of the rest of the message (this length count includes itself, but
not the message-type byte). The remaining contents of the message are
determined by the message type. For historical reasons, the very first
message sent by the client (the startup message) has no initial
message-type byte.
</para>
<para>
To avoid losing synchronization