Skip to content

Commit fde9bd7

Browse files
committed
Making it possible to specify different compresion algorithms in both directions
1 parent 5fe6d5d commit fde9bd7

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
12321232
Request compression of libpq traffic. The client sends a request with a list of compression algorithms.
12331233
Compression can be requested by a client by including the "compression" option in its connection string.
12341234
This can either be a boolean value to enable or disable compression
1235-
("true"/"false", "on"/"off", "yes"/"no", "1"/"0"), "auto", or an explicit list of comma-separated compression algorithms
1235+
("true"/"false", "on"/"off", "yes"/"no", "1"/"0"), "any", or an explicit list of comma-separated compression algorithms
12361236
which can optionally include compression level ("zlib,zstd:5").
12371237
If compression is enabled but an algorithm is not explicitly specified, the client library sends its full list of
12381238
supported algorithms and the server chooses a preferred algorithm.

src/interfaces/libpq/fe-connect.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,6 +3268,14 @@ PQconnectPoll(PGconn *conn)
32683268
conn->compression);
32693269
goto error_return;
32703270
}
3271+
if ((unsigned)index >= conn->n_compressors)
3272+
{
3273+
appendPQExpBuffer(&conn->errorMessage,
3274+
libpq_gettext(
3275+
"server returns incorrect compression aslogirhm index: %d\n"),
3276+
index);
3277+
goto error_return;
3278+
}
32713279
Assert(!conn->zstream);
32723280
conn->zstream = zpq_create(conn->compressors[index].impl,
32733281
conn->compressors[index].level,
@@ -3286,6 +3294,13 @@ PQconnectPoll(PGconn *conn)
32863294
}
32873295
/* reset buffer */
32883296
conn->inStart = conn->inCursor = conn->inEnd = 0;
3297+
}
3298+
else if (conn->n_compressors != 0 && beresp == 'v') /* negotiate protocol version */
3299+
{
3300+
appendPQExpBuffer(&conn->errorMessage,
3301+
libpq_gettext(
3302+
"server is not supporting libpq compression\n"));
3303+
goto error_return;
32893304
} else
32903305
break;
32913306
}

src/interfaces/libpq/fe-protocol3.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,7 @@ build_compressors_list(PGconn *conn, char** client_compressors, bool build_descr
21722172
{
21732173
*client_compressors = NULL; /* no compressors are available */
21742174
conn->compressors = NULL;
2175+
conn->n_compressors = 0;
21752176
return true;
21762177
}
21772178
*client_compressors = p = malloc(total_len);
@@ -2200,6 +2201,7 @@ build_compressors_list(PGconn *conn, char** client_compressors, bool build_descr
22002201
/* Compression is disabled */
22012202
*client_compressors = NULL;
22022203
conn->compressors = NULL;
2204+
conn->n_compressors = 0;
22032205
return true;
22042206
}
22052207
else
@@ -2258,6 +2260,7 @@ build_compressors_list(PGconn *conn, char** client_compressors, bool build_descr
22582260
break;
22592261
}
22602262
free(suggested_algorithms);
2263+
conn->n_compressors = n_suggested_algorithms;
22612264
if (n_suggested_algorithms == 0)
22622265
{
22632266
if (!build_descriptors)
@@ -2268,6 +2271,7 @@ build_compressors_list(PGconn *conn, char** client_compressors, bool build_descr
22682271
{
22692272
free(conn->compressors);
22702273
conn->compressors = NULL;
2274+
conn->n_compressors = 0;
22712275
}
22722276
free(*client_compressors);
22732277
*client_compressors = NULL;

src/interfaces/libpq/libpq-int.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ struct pg_conn
386386
* comma) */
387387
pg_conn_compressor *compressors; /* descriptors of compression
388388
* algorithms chosen by client */
389+
unsigned n_compressors; /* size of compressors array */
389390

390391
/* Type of connection to make. Possible values: any, read-write. */
391392
char *target_session_attrs;

0 commit comments

Comments
 (0)