Skip to content

Commit 330dcb3

Browse files
author
Commitfest Bot
committed
[CF 4870] Make query cancellation keys longer
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/4870 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Heikki Linnakangas, Jelte Fennema-Nio
2 parents 764d501 + eb0feda commit 330dcb3

File tree

23 files changed

+786
-161
lines changed

23 files changed

+786
-161
lines changed

configure

+23
Original file line numberDiff line numberDiff line change
@@ -15927,6 +15927,16 @@ fi
1592715927
cat >>confdefs.h <<_ACEOF
1592815928
#define HAVE_DECL_STRSEP $ac_have_decl
1592915929
_ACEOF
15930+
ac_fn_c_check_decl "$LINENO" "timingsafe_bcmp" "ac_cv_have_decl_timingsafe_bcmp" "$ac_includes_default"
15931+
if test "x$ac_cv_have_decl_timingsafe_bcmp" = xyes; then :
15932+
ac_have_decl=1
15933+
else
15934+
ac_have_decl=0
15935+
fi
15936+
15937+
cat >>confdefs.h <<_ACEOF
15938+
#define HAVE_DECL_TIMINGSAFE_BCMP $ac_have_decl
15939+
_ACEOF
1593015940

1593115941

1593215942
# We can't use AC_CHECK_FUNCS to detect these functions, because it
@@ -16087,6 +16097,19 @@ esac
1608716097

1608816098
fi
1608916099

16100+
ac_fn_c_check_func "$LINENO" "timingsafe_bcmp" "ac_cv_func_timingsafe_bcmp"
16101+
if test "x$ac_cv_func_timingsafe_bcmp" = xyes; then :
16102+
$as_echo "#define HAVE_TIMINGSAFE_BCMP 1" >>confdefs.h
16103+
16104+
else
16105+
case " $LIBOBJS " in
16106+
*" timingsafe_bcmp.$ac_objext "* ) ;;
16107+
*) LIBOBJS="$LIBOBJS timingsafe_bcmp.$ac_objext"
16108+
;;
16109+
esac
16110+
16111+
fi
16112+
1609016113

1609116114

1609216115
ac_fn_c_check_func "$LINENO" "pthread_barrier_wait" "ac_cv_func_pthread_barrier_wait"

configure.ac

+2-1
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ AC_CHECK_DECLS(posix_fadvise, [], [], [#include <fcntl.h>])
18061806
]) # fi
18071807

18081808
AC_CHECK_DECLS(fdatasync, [], [], [#include <unistd.h>])
1809-
AC_CHECK_DECLS([strlcat, strlcpy, strnlen, strsep])
1809+
AC_CHECK_DECLS([strlcat, strlcpy, strnlen, strsep, timingsafe_bcmp])
18101810

18111811
# We can't use AC_CHECK_FUNCS to detect these functions, because it
18121812
# won't handle deployment target restrictions on macOS
@@ -1826,6 +1826,7 @@ AC_REPLACE_FUNCS(m4_normalize([
18261826
strlcpy
18271827
strnlen
18281828
strsep
1829+
timingsafe_bcmp
18291830
]))
18301831

18311832
AC_REPLACE_FUNCS(pthread_barrier_wait)

doc/src/sgml/libpq.sgml

+70-1
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,56 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
21442144
</listitem>
21452145
</varlistentry>
21462146

2147+
<varlistentry id="libpq-connect-min-protocol-version" xreflabel="min_protocol_version">
2148+
<term><literal>min_protocol_version</literal></term>
2149+
<listitem>
2150+
<para>
2151+
Specifies the minimum protocol version to allow for the connection.
2152+
The default is to allow any version of the
2153+
<productname>PostgreSQL</productname> protocol supported by libpq,
2154+
which currently means <literal>3.0</literal>. If the server
2155+
does not support at least this protocol version the connection will be
2156+
closed.
2157+
</para>
2158+
2159+
<para>
2160+
The current supported values are
2161+
<literal>3.0</literal>,
2162+
<literal>3.2</literal>,
2163+
and <literal>latest</literal>. The <literal>latest</literal> value is
2164+
equivalent to the latest protocol version that is supported by the used
2165+
libpq version, which currently is <literal>3.2</literal>.
2166+
</para>
2167+
</listitem>
2168+
</varlistentry>
2169+
2170+
<varlistentry id="libpq-connect-max-protocol-version" xreflabel="max_protocol_version">
2171+
<term><literal>max_protocol_version</literal></term>
2172+
<listitem>
2173+
<para>
2174+
Specifies the protocol version to request from the server.
2175+
The default is to use version <literal>3.0</literal> of the
2176+
<productname>PostgreSQL</productname> protocol, unless the connection
2177+
string specifies a feature that relies on a higher protocol version,
2178+
in which case the latest version supported by libpq is used. If the
2179+
server does not support the protocol version requested by the client,
2180+
the connection is automatically downgraded to a lower minor protocol
2181+
version that the server supports. After the connection attempt has
2182+
completed you can use <xref linkend="libpq-PQprotocolVersion"/> to
2183+
find out which exact protocol version was negotiated.
2184+
</para>
2185+
2186+
<para>
2187+
The current supported values are
2188+
<literal>3.0</literal>,
2189+
<literal>3.2</literal>,
2190+
and <literal>latest</literal>. The <literal>latest</literal> value is
2191+
equivalent to the latest protocol version that is supported by the used
2192+
libpq version, which currently is <literal>3.2</literal>.
2193+
</para>
2194+
</listitem>
2195+
</varlistentry>
2196+
21472197
<varlistentry id="libpq-connect-ssl-max-protocol-version" xreflabel="ssl_max_protocol_version">
21482198
<term><literal>ssl_max_protocol_version</literal></term>
21492199
<listitem>
@@ -2482,7 +2532,6 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
24822532
</para>
24832533
</listitem>
24842534
</varlistentry>
2485-
24862535
</variablelist>
24872536
</para>
24882537
</sect2>
@@ -9329,6 +9378,26 @@ myEventProc(PGEventId evtId, void *evtInfo, void *passThrough)
93299378
linkend="libpq-connect-load-balance-hosts"/> connection parameter.
93309379
</para>
93319380
</listitem>
9381+
9382+
<listitem>
9383+
<para>
9384+
<indexterm>
9385+
<primary><envar>PGMINPROTOCOLVERSION</envar></primary>
9386+
</indexterm>
9387+
<envar>PGMINPROTOCOLVERSION</envar> behaves the same as the <xref
9388+
linkend="libpq-connect-min-protocol-version"/> connection parameter.
9389+
</para>
9390+
</listitem>
9391+
9392+
<listitem>
9393+
<para>
9394+
<indexterm>
9395+
<primary><envar>PGMAXPROTOCOLVERSION</envar></primary>
9396+
</indexterm>
9397+
<envar>PGMAXPROTOCOLVERSION</envar> behaves the same as the <xref
9398+
linkend="libpq-connect-max-protocol-version"/> connection parameter.
9399+
</para>
9400+
</listitem>
93329401
</itemizedlist>
93339402
</para>
93349403

doc/src/sgml/protocol.sgml

+108-27
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,10 @@
1818
</para>
1919

2020
<para>
21-
This document describes version 3.0 of the protocol, implemented in
22-
<productname>PostgreSQL</productname> 7.4 and later. For descriptions
23-
of the earlier protocol versions, see previous releases of the
24-
<productname>PostgreSQL</productname> documentation. A single server
25-
can support multiple protocol versions. The initial startup-request
26-
message tells the server which protocol version the client is attempting to
27-
use. If the major version requested by the client is not supported by
28-
the server, the connection will be rejected (for example, this would occur
29-
if the client requested protocol version 4.0, which does not exist as of
30-
this writing). If the minor version requested by the client is not
31-
supported by the server (e.g., the client requests version 3.1, but the
32-
server supports only 3.0), the server may either reject the connection or
33-
may respond with a NegotiateProtocolVersion message containing the highest
34-
minor protocol version which it supports. The client may then choose either
35-
to continue with the connection using the specified protocol version or
36-
to abort the connection.
21+
This document describes version 3.2 of the protocol, introduced in
22+
<productname>PostgreSQL</productname> version 17. The server and the libpq
23+
client library is compatible with protocol version 3.0, implemented in
24+
<productname>PostgreSQL</productname> 7.4 and later.
3725
</para>
3826

3927
<para>
@@ -199,6 +187,85 @@
199187
server versions; the text format is usually the more portable choice.
200188
</para>
201189
</sect2>
190+
191+
<sect2 id="protocol-versions">
192+
<title>Protocol versions</title>
193+
194+
<para>
195+
The current, latest version of the protocol is version 3.2. For backwards
196+
compatibility with old server versions and middleware that don't support
197+
the version negotiation yet, the libpq library still uses protocol version
198+
3.0 by default however.
199+
</para>
200+
201+
<para>
202+
A single server can support multiple protocol versions. The initial
203+
startup-request message tells the server which protocol version the client
204+
is attempting to use. If the major version requested by the client is not
205+
supported by the server, the connection will be rejected (for example,
206+
this would occur if the client requested protocol version 4.0, which does
207+
not exist as of this writing). If the minor version requested by the
208+
client is not supported by the server (e.g., the client requests version
209+
3.2, but the server supports only 3.0), the server may either reject the
210+
connection or may respond with a NegotiateProtocolVersion message
211+
containing the highest minor protocol version which it supports. The
212+
client may then choose either to continue with the connection using the
213+
specified protocol version or to abort the connection.
214+
</para>
215+
216+
<para>
217+
The protocol negotiation was introduced in
218+
<productname>PostgreSQL</productname> version 9.3.21. Earlier versions
219+
would reject the connection if the client requested a minor version that
220+
was not supported by the server.
221+
</para>
222+
223+
<table>
224+
<title>Protocol versions</title>
225+
226+
<tgroup cols="2">
227+
<thead>
228+
<row>
229+
<entry>Version</entry>
230+
<entry>Supported by</entry>
231+
<entry>Description</entry>
232+
</row>
233+
</thead>
234+
235+
<tbody>
236+
<row>
237+
<entry>3.2</entry>
238+
<entry>PostgreSQL 18 and later</entry>
239+
<entry>Current latest version. The secret key used in query
240+
cancellation was enlarged from 4 bytes to a variable length field. The
241+
BackendKeyData message was changed to accomodate that, and the CancelRequest
242+
message was redefined to have a variable length payload.
243+
</entry>
244+
</row>
245+
<row>
246+
<entry>3.1</entry>
247+
<entry>-</entry>
248+
<entry>Reserved. Version 3.1 has not been used by any PostgreSQL
249+
version, but it was skipped because old versions of the popular
250+
pgbouncer application had a bug in the protocol negotiation which made
251+
it incorrectly claim that it supported version 3.1.
252+
</entry>
253+
</row>
254+
<row>
255+
<entry>3.0</entry>
256+
<entry>PostgreSQL 7.4 and later</entry>
257+
</row>
258+
<row>
259+
<entry>2.0</entry>
260+
<entry>up to PostgreSQL 13</entry>
261+
<entry>See previous releases of
262+
the <productname>PostgreSQL</productname> documentation for
263+
details</entry>
264+
</row>
265+
</tbody>
266+
</tgroup>
267+
</table>
268+
</sect2>
202269
</sect1>
203270

204271
<sect1 id="protocol-flow">
@@ -413,8 +480,14 @@
413480
this message indicates the highest supported minor version. This
414481
message will also be sent if the client requested unsupported protocol
415482
options (i.e., beginning with <literal>_pq_.</literal>) in the
416-
startup packet. This message will be followed by an ErrorResponse or
417-
a message indicating the success or failure of authentication.
483+
startup packet.
484+
</para>
485+
<para>
486+
After this message, the authentication will continue using the version
487+
indicated by the server. If the client does not support the older
488+
version, it should immediately close the connection. If the server
489+
does not send this message, it supports the client's requested
490+
protocol version and all the protocol options.
418491
</para>
419492
</listitem>
420493
</varlistentry>
@@ -3624,10 +3697,10 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
36243697
indicate that it can be sent by a frontend (F), a backend (B), or both
36253698
(F &amp; B).
36263699
Notice that although each message includes a byte count at the beginning,
3627-
the message format is defined so that the message end can be found without
3628-
reference to the byte count. This aids validity checking. (The CopyData
3629-
message is an exception, because it forms part of a data stream; the contents
3630-
of any individual CopyData message cannot be interpretable on their own.)
3700+
most messages are defined so that the message end can be found without
3701+
reference to the byte count. This is mostly for historical reasons, as
3702+
the original, now-obsolete protocol version 2 did not have an explicit length
3703+
field, but it also aids validity checking.
36313704
</para>
36323705

36333706
<variablelist>
@@ -4043,7 +4116,7 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
40434116
</varlistentry>
40444117

40454118
<varlistentry>
4046-
<term>Int32(12)</term>
4119+
<term>Int32</term>
40474120
<listitem>
40484121
<para>
40494122
Length of message contents in bytes, including self.
@@ -4061,14 +4134,18 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
40614134
</varlistentry>
40624135

40634136
<varlistentry>
4064-
<term>Int32</term>
4137+
<term>Byte<replaceable>n</replaceable></term>
40654138
<listitem>
40664139
<para>
4067-
The secret key of this backend.
4140+
The secret key of this backend. This field extends to the end of the
4141+
message, indicated by the length field. The maximum key length is 256 bytes.
40684142
</para>
40694143
</listitem>
40704144
</varlistentry>
40714145
</variablelist>
4146+
<para>
4147+
Before protocol version 3.2, the secret key was always 4 bytes long.
4148+
</para>
40724149
</listitem>
40734150
</varlistentry>
40744151

@@ -4274,14 +4351,18 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
42744351
</varlistentry>
42754352

42764353
<varlistentry>
4277-
<term>Int32</term>
4354+
<term>Byte<replaceable>n</replaceable></term>
42784355
<listitem>
42794356
<para>
4280-
The secret key for the target backend.
4357+
The secret key for the target backend. This field extends to the end of the
4358+
message, indicated by the length field. The maximum key length is 256 bytes.
42814359
</para>
42824360
</listitem>
42834361
</varlistentry>
42844362
</variablelist>
4363+
<para>
4364+
Before protocol version 3.2, the secret key was always 4 bytes long.
4365+
</para>
42854366
</listitem>
42864367
</varlistentry>
42874368

meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -2569,6 +2569,7 @@ decl_checks = [
25692569
['strlcpy', 'string.h'],
25702570
['strnlen', 'string.h'],
25712571
['strsep', 'string.h'],
2572+
['timingsafe_bcmp', 'string.h'],
25722573
]
25732574

25742575
# Need to check for function declarations for these functions, because
@@ -2811,6 +2812,7 @@ func_checks = [
28112812
['strsignal'],
28122813
['sync_file_range'],
28132814
['syncfs'],
2815+
['timingsafe_bcmp'],
28142816
['uselocale'],
28152817
['wcstombs_l'],
28162818
]

0 commit comments

Comments
 (0)