Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x26zF-005ank-1W for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Sep 2026 12:56:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x26zE-000V1x-1Q for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Sep 2026 12:56:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x26zE-000V1o-09 for pgsql-hackers@lists.postgresql.org; Thu, 03 Sep 2026 12:56:12 +0000 Received: from mo4-p00-ob.smtp.rzone.de ([85.215.255.24]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x26zA-00000002ivz-3Pz2 for pgsql-hackers@lists.postgresql.org; Thu, 03 Sep 2026 12:56:11 +0000 ARC-Seal: i=1; a=rsa-sha256; t=1788440166; cv=none; d=strato.com; s=strato-dkim-0002; b=qS3qhaR1Tphw3+AFv9RItCcRVVMKImP5x4VEfWKRtqWG3PSNnyY8fi7IX2wwFNQsRi 0ppC2oaTDZrLeombqdruJi/ixAq/37Mg85Hqndvya2anwTwoYbSG/v4WWR9/m/aBQ68Y UGeH3Ujtl0MjHnQen8bAahch511h3vX2tiMi86N7umrsmQua1mtd/XVJW2dGtp/ty3oa bxLYSQ0LePaSi2/9qOvj+ec/9W5re3yNDf3+zWiSVrya7fF0TNJP+OVgEf6C3WEYNAao dWuHcSP06zfxIBBwo/2SDhMeAokYHliwAIrBLCAYLas9xp21JyddZLcIuUAzx59dUnqo aSVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1788440166; s=strato-dkim-0002; d=strato.com; h=In-Reply-To:From:References:To:Subject:Date:Message-ID:Cc:Date:From: Subject:Sender; bh=3h7PZDnKTmLGzOb33MIHR53P76U5lW1ce5g2t7lt9/E=; b=ozvACODU/hhGtAsP/i6DWHigzUf9ESAMNrNBuChOHLLM/+fjX0o4znVTo7iEIcyPZb a56h9igrefT5jI0TbsgyHppCPN/Z+KQ+MUDvIJhVDi6TdmgD5KZqrqR/7YofbgSa5p/b EmpQv9BfQSF2sfOjaloVS+raPm4mUwlraG1smVrSVYp/ETxDTmYxcfJ1KHsiDx+ZcH7s aw0h1+4wwpC4FEDJiRKS5+d1YUZBLpG2aQ51fqwEi/1HEF/eRXoKz23psTOKw3ePt6GM LIcp/3lORazLGmeSQeSrGvqXT3iLugyLvSYLDA1SnzGGScwR3GhipjxPmFf5HqUYnfOZ 3rrg== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1788440166; s=strato-dkim-0002; d=jbitc.de; h=In-Reply-To:From:References:To:Subject:Date:Message-ID:Cc:Date:From: Subject:Sender; bh=3h7PZDnKTmLGzOb33MIHR53P76U5lW1ce5g2t7lt9/E=; b=oXSn33ltu+8nTCT2bT6k/qPfFBmNAOyuQtf1nnX25pROGW5AJUcOODpRROAG5eLOC4 2ddk2fZRgHvJ0vnw/0BXNVhM2nV+i82FAkJv7yKHyH1saGTjX3tfCsLpAgr8HwIlCCFf IGweQ1SzpNXQIGFbqyEBnzsrvnX04HVTSrWBiyJoqgy10xzfpc6r+TScAf0MmxbDW+CP CBA44a7GgVvSUcEHvefsOdqKi8fVR05ecT+u0i2ptV/c5ExKkajWWi/14UHzNIkoEDJ5 dlB8KO08ezCeNNsG7Rtqw9Va1bBE3Cnokiyp3BHHIOXUAzwznW2+8//TBy2bCkpelca8 TD/Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1788440166; s=strato-dkim-0003; d=jbitc.de; h=In-Reply-To:From:References:To:Subject:Date:Message-ID:Cc:Date:From: Subject:Sender; bh=3h7PZDnKTmLGzOb33MIHR53P76U5lW1ce5g2t7lt9/E=; b=PJFSS1tNIvFQIAe+F2ygJf2dvc59aN89thGf5oQ2/JmQZqyxZ7OjDIGvf/8Hs3mHza rVIqoFE89P9Qt4/MOKAw== X-RZG-AUTH: ":Jm4kf0Khb/yzNwyGkr6R611+JpIbxpU/vUdKufVE/BwSQoubt/s/N4gP" Received: from [11.1.1.169] by smtp.strato.de (RZmta 55.6.2 AUTH) with ESMTPSA id 5bab49283Cu6FPL (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 3 Sep 2026 14:56:06 +0200 (CEST) Content-Type: multipart/mixed; boundary="------------gXvDc0YAa7WvvqlUWABTsKM2" Message-ID: <579c31d4-97e1-4edc-a4e1-1cb7523f3097@jbitc.de> Date: Thu, 3 Sep 2026 14:56:05 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Protocol compression: a fourth design To: Andrey Borodin , pgsql-hackers mailing list References: Content-Language: de-DE, en-US From: Jochen Bandhauer In-Reply-To: Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------gXvDc0YAa7WvvqlUWABTsKM2 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 31.08.2026 14:26, Andrey Borodin wrote: > Hi, > > There have been three substantial attempts to add protocol compression > to PostgreSQL [0][1][2]. They demonstrated useful reductions in network > traffic, but also accumulated questions about negotiation, I/O layering, > codec and direction selection, poolers, and compression before > encryption. Thanks to Konstantin, Daniil, Jacob, and everyone who worked > on and reviewed them. > > I have been experimenting with a different design and decided that it's > time to discuss the topic again. > > The proposal is: > > - Zstandard only. > - An explicitly negotiated _pq_.compression=zstd protocol extension [3]. > - Compression starts only after authentication and the first > ReadyForQuery. > - Server to client: DataRow and CopyData. > - Client to server: CopyData during COPY FROM. > - Query, Parse, Bind, parameters, and all control messages remain > ordinary protocol messages. > - One persistent stream per active direction, carried in bounded > CompressedData messages. > - The backend stream ends before ReadyForQuery. The frontend stream ends > before CopyDone or CopyFail. > > The wrapper contains only a flushed segment of the compressed stream. > The uncompressed bytes from successive wrappers form an ordinary > protocol message stream, so a large DataRow or CopyData message may span > multiple wrappers. The receiver bounds both the wrapper and the output > produced by each segment, and validates the inner message types and > lengths as they become available. > > This is deliberately asymmetric. DataRow and backend CopyData cover > query results, COPY TO, base backup, and WAL streaming. Frontend > CopyData covers COPY FROM and pg_restore. Leaving queries and parameters > visible means that a pooler can still inspect and route queries or > rewrite prepared statement names without decompressing them. > ReadyForQuery also remains an explicit transaction-pooling boundary. A > pooler that passes the negotiation and length-prefixed wrappers through > can relay CompressedData without understanding Zstandard. > > The server has protocol_compression = off | zstd, with off as the > default. libpq has compression=off, prefer, and zstd. > Prefer continues without compression when the server rejects the > extension; zstd requires it. A server build without Zstandard does not > expose the zstd GUC value. > > I also tested the new libpq against an unmodified PostgreSQL 14 > server. prefer fell back and connected normally, while zstd failed with > the expected unsupported-method error. > > The server switch is important because compression happens before SSL > or GSS encryption. Compressed lengths may reveal information when > attacker-controlled and secret values share history. Excluding > authentication, queries, and bind parameters reduces the scope but does > not eliminate that property. The administrator can prohibit the > feature, and it is off by default. > > Compression state is allocated lazily, and small results can remain > uncompressed. > > I deliberately left reporting through pg_stat_* out of the first patch > to keep it small. A follow-up can expose whether a connection uses > compression and its compressed and uncompressed byte counts in each > direction. Other follow-up features I have in mind are additional > codecs and compression levels, and per-direction control. None of these > requires changing the basic CompressedData framing. > > Earlier end-to-end prototype measurements reduced mixed DataRow and > COPY TO workloads to about 10% and 14% of their original wire size. > MD5-heavy DataRow and COPY FROM workloads were reduced to about 51%. > I reran this implementation with the default Zstandard level. In a > realistic pg_restore test, client-to-server traffic fell from 264 MB to > 21 MB; median local restore times were 3.00 seconds without compression > and 3.04 seconds with it. One-row pgbench medians were 7034 and > 7018 TPS. Pseudorandom 32 MB DataRow and COPY TO results were reduced > only to 78% and 81%, and took about 0.5 seconds more CPU time on > loopback. The byte savings on compressible data translated into > corresponding speedups through a bandwidth-limited proxy. > > The memory test also found a useful bound. An earlier prototype with an > 8 MiB Zstandard window retained about 8.6 MiB per active backend. > Limiting history to 64 KiB reduced the additional retained private > anonymous memory to about 1 MiB in a same-backend test, at a cost of > about 1.8% more compressed bytes for COPY TO and no loss in the other > two measured results. > > I would like feedback on three design choices before turning this into a > patch series: > > 1. Message scope. v1 compresses only bulk-data messages with the clearest > benefit. It leaves authentication, query, parameter, and control > messages ordinary where the benefit is less clear or the > confidentiality concern is more direct. Is this the right future-proof > scope? > 2. Stream lifetime. Ending streams before ReadyForQuery and CopyDone or > CopyFail keeps transaction and COPY boundaries explicit, so a pooler > can remap connections without decompressing and recompressing data. Are > these the right boundaries? > 3. Policy. The server GUC uses the familiar off | compression-method > pattern. The client combines that with PostgreSQL's opportunistic or > required connection policy: off | prefer | zstd, where naming the > method requires it. Is this interface sufficient? > > Thank you! > > > Best regards, Andrey Borodin. > > [0] https://www.postgresql.org/message-id/flat/aad16e41-b3f9-e89d-fa57-fb4c694bec25%40postgrespro.ru > [1] https://www.postgresql.org/message-id/flat/ABAA09C6-BB95-47A5-890D-90353533F9AC%40yandex-team.ru > [2] https://www.postgresql.org/message-id/flat/CACzsqT4cJG0kaCbz24Sd%3DGAEgiQDpzU8yuD6vF25zo870%2B3M6g%40mail.gmail.com > [3] https://www.postgresql.org/docs/devel/protocol-flow.html#PROTOCOL-EXTENSIONS Hi, I find this patch interesting and created a script (attached) that tries to measure the effect of the compression.  It uses the ss command and can be run on a linux Postgres server or on a remote linux machine.  It uses psql.  It can use a SELECT statement or \COPY TO or \COPY FROM. Here is some sample output (using SELECT): script started on the PostgreSQL server (RHEL 10 VM): --- without compression ---   psql network data received: 60731.93 KB   psql network data sent:     0.12 KB   runtime:                    18.28 seconds --- with zstd compression ---   psql network data received: 110.19 KB   psql network data sent:     0.15 KB   runtime:                    18.32 seconds script started on another VM on the same host as the PostgreSQL server (RHEL 9): --- without compression ---   psql network data received: 60731.93 KB   psql network data sent:     0.12 KB   runtime:                    20.03 seconds --- with zstd compression ---   psql network data received: 110.19 KB   psql network data sent:     0.15 KB   runtime:                    18.74 seconds script started on another system (laptop) on the same LAN as the PostgreSQL server (Ubuntu 26): --- without compression ---   psql network data received: 60731.93 KB   psql network data sent:     0.12 KB   runtime:                    25.29 seconds --- with zstd compression ---   psql network data received: 110.19 KB   psql network data sent:     0.15 KB   runtime:                    9.78 seconds We can also reduce the network speed of the network adapter by running the following command on the postgres server: tc qdisc replace dev enp0s3 root netem rate 10mbit we then get the following data when we run the script on a remote machine: --- without compression ---   psql network data received: 60731.93 KB   psql network data sent:     0.12 KB   runtime:                    70.98 seconds --- with zstd compression ---   psql network data received: 110.19 KB   psql network data sent:     0.15 KB   runtime:                    18.98 seconds To reset the network config: tc qdisc replace dev enp0s3 root fq_codel Regards Jochen -- Mit freundlichen Grüßen Jochen Bandhauer +49 (0)176 29390012 www.jbitc.de --------------gXvDc0YAa7WvvqlUWABTsKM2 Content-Type: text/plain; charset=UTF-8; name="zstd.sh" Content-Disposition: attachment; filename="zstd.sh" Content-Transfer-Encoding: base64 IyEvdXNyL2Jpbi9iYXNoCiMKIyBWZXJzaW9uIDEuMCAwMy4wOS4yMDI2CiMKc2V0IC1ldW8g cGlwZWZhaWwKCiMgPT09PT0gQ09ORklHID09PT09ClBHSE9TVD0ibGluOCIKUEdQT1JUPSI1 NDMyIgpQR0RBVEFCQVNFPSJteWRiIgpQR1VTRVI9InBvc3RncmVzIgpQR1BBU1NXT1JEPSJj aGFuZ2VtZSIKVEFCTEU9ImNvbXByZXNzaW9uX3Rlc3QiClJPV1M9MTAwMDAKUkVQRUFUX1NU Uj0iQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphY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ZXNfcmVjZWl2ZWQ6LTB9LzEw MjR9IikgS0IiCiAgICBlY2hvICIgIHBzcWwgbmV0d29yayBkYXRhIHNlbnQ6ICAgICAkKGF3 ayAiQkVHSU4ge3ByaW50ZiBcIiUuMmZcIiwgJHtieXRlc19zZW50Oi0wfS8xMDI0fSIpIEtC IgogICAgZWNobyAiICBydW50aW1lOiAgICAgICAgICAgICAgICAgICAgJHtydW50aW1lOi1O L0F9IHNlY29uZHMiCiAgICBlY2hvCgogICAgIyBDbGVhbiB1cAogICAgcm0gLWYgIiR0aW1l X2ZpbGUiICIkc3NfZmlsZSIKfQoKIyA9PT09PSBSdW4gbWVhc3VyZW1lbnRzID09PT09Cm1l YXN1cmUgIndpdGhvdXQgY29tcHJlc3Npb24iICJvZmYiCm1lYXN1cmUgIndpdGggenN0ZCBj b21wcmVzc2lvbiIgInpzdGQiCg== --------------gXvDc0YAa7WvvqlUWABTsKM2 Content-Type: text/vcard; charset=UTF-8; name="jb.vcf" Content-Disposition: attachment; filename="jb.vcf" Content-Transfer-Encoding: base64 QkVHSU46VkNBUkQNClZFUlNJT046NC4wDQpOOkJhbmRoYXVlcjtKb2NoZW47OzsNCkZOOkpv Y2hlbiBCYW5kaGF1ZXINCkVNQUlMO1BSRUY9MTtUWVBFPXdvcms6amJAamJpdGMuZGUNClVS TDpodHRwczovL3d3dy5qYml0Yy5kZQ0KVEVMO1RZUEU9d29yaztWQUxVRT1URVhUOis0OTE3 NjI5MzkwMDEyDQpUWjpFdXJvcGUvQmVybGluDQpFTkQ6VkNBUkQNCg== --------------gXvDc0YAa7WvvqlUWABTsKM2--