Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 58f0fe3

Browse files
committed
Add length to compression acknolegment message
1 parent c4570bf commit 58f0fe3

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

doc/src/sgml/protocol.sgml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
</para>
9494

9595
<para>
96-
Is is possible to compress protocol data to reduce traffic and speed-up client-server interaction.
97-
Compression is especialy useful for importing/exprorting data to/from database using COPY command
98-
and for replication (oth physical and logical). Also compression can reduce server response time
99-
in case of queries, requestion larger amount of data (for example returning JSON, BLOBs, text,...)
96+
It is possible to compress protocol data to reduce traffic and speed-up client-server interaction.
97+
Compression is especial useful for importing/exporting data to/from database using COPY command
98+
and for replication (both physical and logical). Also compression can reduce server response time
99+
in case of queries returning large amount of data (for example returning JSON, BLOBs, text,...)
100100
Right now compression algorithm is hardcoded: is it is either zlib (default), either zstd (if Postgres was
101101
configured with --with-zstd option). In both cases streaming mode is used.
102102
</para>
@@ -3437,7 +3437,16 @@ CompressionOk (B)
34373437
and exchange compressed messages.
34383438
</para>
34393439
</listitem>
3440-
3440+
</varlistentry>
3441+
<varlistentry>
3442+
<term>
3443+
Int32
3444+
</term>
3445+
<listitem>
3446+
<para>
3447+
Length of message contents in bytes, including self.
3448+
</para>
3449+
</listitem>
34413450
</varlistentry>
34423451
<varlistentry>
34433452
<term>

src/backend/libpq/pqcomm.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,14 @@ pq_configure(Port* port)
201201
{
202202
if (port->use_compression)
203203
{
204-
char compression[2];
204+
char compression[6] = {'z',0,0,0,5,0}; /* message length = 5 */
205205
int rc;
206-
compression[0] = 'z'; /* Request compression message */
207-
compression[1] = zpq_algorithm();
206+
compression[5] = zpq_algorithm();
208207
/* Switch on compression at client side */
209208
socket_set_nonblocking(false);
210-
while ((rc = secure_write(MyProcPort, &compression, sizeof compression)) < 0
209+
while ((rc = secure_write(MyProcPort, compression, sizeof(compression))) < 0
211210
&& errno == EINTR);
212-
if (rc != 2)
211+
if ((size_t)rc != sizeof(compression))
213212
return -1;
214213

215214
/* initialize compression */

src/interfaces/libpq/fe-connect.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2880,13 +2880,27 @@ PQconnectPoll(PGconn *conn)
28802880
if (beresp == 'z') /* Switch on compression */
28812881
{
28822882
char algorithm;
2883+
/* Read message length word */
2884+
if (pqGetInt(&msgLength, 4, conn))
2885+
{
2886+
/* We'll come back when there is more data */
2887+
return PGRES_POLLING_READING;
2888+
}
2889+
if (msgLength != 5)
2890+
{
2891+
appendPQExpBuffer(&conn->errorMessage,
2892+
libpq_gettext(
2893+
"expected compression algorithm specification message length is 5 bytes, but %d is recevied\n"),
2894+
msgLength);
2895+
goto error_return;
2896+
}
28832897
pqGetc(&algorithm, conn);
28842898
if (zpq_algorithm() != algorithm)
28852899
{
28862900
appendPQExpBuffer(&conn->errorMessage,
28872901
libpq_gettext(
28882902
"server and client were configured with different libpq compression algorithms: %c vs. %c\n"),
2889-
algorithm, zpq_algorithm());
2903+
algorithm, zpq_algorithm());
28902904
goto error_return;
28912905
}
28922906
/* mark byte consumed */

src/interfaces/libpq/fe-misc.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
6161
time_t end_time);
6262
static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
6363

64+
65+
#define pq_read_conn(conn,processed) \
66+
(conn->zstream \
67+
? zpq_read(conn->zstream, conn->inBuffer + conn->inEnd, \
68+
conn->inBufSize - conn->inEnd, &processed) \
69+
: pqsecure_read(conn, conn->inBuffer + conn->inEnd, \
70+
conn->inBufSize - conn->inEnd))
71+
72+
6473
/*
6574
* PQlibVersion: return the libpq version number
6675
*/
@@ -681,11 +690,7 @@ pqReadData(PGconn *conn)
681690
/* OK, try to read some data */
682691
retry3:
683692
processed = 0;
684-
nread = conn->zstream
685-
? zpq_read(conn->zstream, conn->inBuffer + conn->inEnd,
686-
conn->inBufSize - conn->inEnd, &processed)
687-
: pqsecure_read(conn, conn->inBuffer + conn->inEnd,
688-
conn->inBufSize - conn->inEnd);
693+
nread = pq_read_conn(conn,processed);
689694
conn->inEnd += processed;
690695
if (nread < 0)
691696
{
@@ -784,11 +789,7 @@ pqReadData(PGconn *conn)
784789
*/
785790
retry4:
786791
processed = 0;
787-
nread = conn->zstream
788-
? zpq_read(conn->zstream, conn->inBuffer + conn->inEnd,
789-
conn->inBufSize - conn->inEnd, &processed)
790-
: pqsecure_read(conn, conn->inBuffer + conn->inEnd,
791-
conn->inBufSize - conn->inEnd);
792+
nread = pq_read_conn(conn,processed);
792793
conn->inEnd += processed;
793794

794795
if (nread < 0)

0 commit comments

Comments
 (0)