Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hrL6M-0001zH-N7 for pgsql-general@arkaria.postgresql.org; Sat, 27 Jul 2019 11:43:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hrL6J-0000Dc-PX for pgsql-general@arkaria.postgresql.org; Sat, 27 Jul 2019 11:42:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hrL6J-0000DU-DT for pgsql-general@lists.postgresql.org; Sat, 27 Jul 2019 11:42:59 +0000 Received: from forward101o.mail.yandex.net ([2a02:6b8:0:1a2d::601]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hrL6F-0001DS-Tv for pgsql-general@postgresql.org; Sat, 27 Jul 2019 11:42:58 +0000 Received: from mxback2g.mail.yandex.net (mxback2g.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b7:163]) by forward101o.mail.yandex.net (Yandex) with ESMTP id CCBFB3C0255F for ; Sat, 27 Jul 2019 14:42:50 +0300 (MSK) Received: from smtp1o.mail.yandex.net (smtp1o.mail.yandex.net [2a02:6b8:0:1a2d::25]) by mxback2g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id QDbcc1f9F8-goJmc9qT; Sat, 27 Jul 2019 14:42:50 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ya.ru; s=mail; t=1564227770; bh=DJqKIZ6u4BZ+sU9phk/+PAxql5lLJ4Ayvv1avea5Np0=; h=Subject:From:To:Date:Message-ID; b=cYvxW7WNmxGeK+I8IxguqsuXrRXVW2aLhUtxL2NFWxD0h4v5Z0ItBRuIzNwzO2/5P IkZfpwIGeBjOUw/J9mv3q2F3oEwva5Gka6ahjufDHubp+wnB6MRPrxF6HnsL41/a2C wO2rKvH2+CY0G6V6AxqTgSQHtNCqXl78IKwjPvgQ= Authentication-Results: mxback2g.mail.yandex.net; dkim=pass header.i=@ya.ru Received: by smtp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id NYAHEdP11o-goTOuZ7g; Sat, 27 Jul 2019 14:42:50 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) To: pgsql-general@postgresql.org From: Artemiy Ryabinkov Subject: Why does backend send buffer size hardcoded at 8KB? Message-ID: <0cdc5485-cb3c-5e16-4a46-e3b2f7a41322@ya.ru> Date: Sat, 27 Jul 2019 14:43:54 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Why backend send buffer use exactly 8KB? (https://github.com/postgres/postgres/blob/249d64999615802752940e017ee5166e726bc7cd/src/backend/libpq/pqcomm.c#L134) I had this question when I try to measure the speed of reading data. The bottleneck was a read syscall. With strace I found that in most cases read returns 8192 bytes (https://pastebin.com/LU10BdBJ). With tcpdump we can confirm, that network packets have size 8192 (https://pastebin.com/FD8abbiA) So, with well-tuned networking stack, the limit is 8KB. The reason is the hardcoded size of Postgres write buffer. I found discussion, where Tom Lane says that the reason of this limit is the size of pipe buffers in Unix machines: https://www.postgresql.org/message-id/9426.1388761242%40sss.pgh.pa.us > Traditionally, at least, that was the size of pipe buffers in Unix machines, so in principle this is the most optimal chunk size for sending data across a Unix socket. I have no idea though if that's still true in kernels in common use today. For TCP communication it might be marginally better to find out the MTU size and use that; but it's unclear that it's worth the trouble, or indeed that we can know the end-to-end MTU size with any reliability. Does it make sense to make this parameter configurable? -- Artemiy Ryabinkov getlag(at)ya(dot)ru