Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dAPrk-0004uO-NT for pgsql-hackers@arkaria.postgresql.org; Mon, 15 May 2017 23:57:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dAPrj-0005fo-RD for pgsql-hackers@arkaria.postgresql.org; Mon, 15 May 2017 23:57:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dAPrh-0005dQ-Lf for pgsql-hackers@postgreSQL.org; Mon, 15 May 2017 23:57:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dAPre-00013Y-VT for pgsql-hackers@postgreSQL.org; Mon, 15 May 2017 23:57:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v4FNvLNR027404 for ; Mon, 15 May 2017 19:57:21 -0400 From: Tom Lane To: pgsql-hackers@postgreSQL.org Subject: Re: Receive buffer size for the statistics socket In-reply-to: <22173.1494788088@sss.pgh.pa.us> References: <22173.1494788088@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 14 May 2017 14:54:48 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <27359.1494892564.0@sss.pgh.pa.us> Date: Mon, 15 May 2017 19:57:21 -0400 Message-ID: <27403.1494892641@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27359.1494892564.1@sss.pgh.pa.us> I wrote: > I propose that it'd be a good idea to try to set the stats socket's > receive buffer size to be a minimum of, say, 100K on all platforms. > Code for this would be analogous to what we already have in pqcomm.c > (circa line 760) for forcing up the send buffer size, but SO_RCVBUF > not SO_SNDBUF. I experimented with the attached patch. Modern platforms such as recent Linux and macOS seem to have default receive buffer sizes in the 100K-200K range. The default is less in older systems, but even my very oldest dinosaurs will let you set it to at least 256K. (Don't have Windows to try, though.) I propose committing this (less the debug logging part) to HEAD once the beta is out, and then back-patching if it doesn't break anything and seems to improve matters on frogmouth. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="set-minimum-receive-bufsize-for-pgstat.patch"; charset="us-ascii" Content-ID: <27359.1494892564.2@sss.pgh.pa.us> Content-Description: set-minimum-receive-bufsize-for-pgstat.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgsta= t.c index d4feed1..d868976 100644 *** a/src/backend/postmaster/pgstat.c --- b/src/backend/postmaster/pgstat.c *************** *** 93,98 **** --- 93,101 ---- #define PGSTAT_POLL_LOOP_COUNT (PGSTAT_MAX_WAIT_TIME / PGSTAT_RETRY_DELAY) #define PGSTAT_INQ_LOOP_COUNT (PGSTAT_INQ_INTERVAL / PGSTAT_RETRY_DELAY) =20=20 + /* Minimum receive buffer size for the collector's socket. */ + #define PGSTAT_MIN_RCVBUF (100 * 1024) +=20 =20=20 /* ---------- * The initial size hints for the hash tables used in the collector. *************** retry2: *** 574,579 **** --- 577,620 ---- goto startup_failed; } =20=20 + /* + * Try to ensure that the socket's receive buffer is at least + * PGSTAT_MIN_RCVBUF bytes, so that it won't easily overflow and lose + * data. Use of UDP protocol means that we are willing to lose data und= er + * heavy load, but we don't want it to happen just because of ridiculous= ly + * small default buffer sizes (such as 8KB on older Windows versions). + */ + { + int old_rcvbuf; + int new_rcvbuf; + ACCEPT_TYPE_ARG3 rcvbufsize =3D sizeof(old_rcvbuf); +=20 + if (getsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF, + (char *) &old_rcvbuf, &rcvbufsize) < 0) + { + elog(LOG, "getsockopt(SO_RCVBUF) failed: %m"); + /* if we can't get existing size, always try to set it */ + old_rcvbuf =3D 0; + } +=20 + new_rcvbuf =3D PGSTAT_MIN_RCVBUF; + if (old_rcvbuf < new_rcvbuf) + { + if (setsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF, + (char *) &new_rcvbuf, sizeof(new_rcvbuf)) < 0) + elog(LOG, "setsockopt(SO_RCVBUF) failed: %m"); + } +=20 + /* this part is just for debugging, not needed at commit: */ + rcvbufsize =3D sizeof(new_rcvbuf); + if (getsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF, + (char *) &new_rcvbuf, &rcvbufsize) < 0) + elog(LOG, "getsockopt(SO_RCVBUF) failed: %m"); + else + elog(LOG, "getsockopt(SO_RCVBUF) was %d, now %d", + old_rcvbuf, new_rcvbuf); + } +=20 pg_freeaddrinfo_all(hints.ai_family, addrs); =20=20 return; ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--