Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bKWTa-0000W5-Ia for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Jul 2016 19:57:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bKWN3-0001oT-DP for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Jul 2016 19:51:01 +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 1bKWLc-0000F1-OR for pgsql-hackers@postgresql.org; Tue, 05 Jul 2016 19:49:33 +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 1bKWKV-0005kD-On for pgsql-hackers@postgresql.org; Tue, 05 Jul 2016 19:48:25 +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 u65JmKxu028550; Tue, 5 Jul 2016 15:48:20 -0400 From: Tom Lane To: Robert Haas cc: Greg Stark , PostgreSQL-development Subject: Re: can we optimize STACK_DEPTH_SLOP In-reply-to: References: <32729.1467734050@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Tue, 05 Jul 2016 15:09:42 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <28201.1467747532.0@sss.pgh.pa.us> Date: Tue, 05 Jul 2016 15:48:20 -0400 Message-ID: <28549.1467748100@sss.pgh.pa.us> X-Pg-Spam-Score: -3.2 (---) 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: <28201.1467747532.1@sss.pgh.pa.us> Robert Haas writes: > On Tue, Jul 5, 2016 at 11:54 AM, Tom Lane wrote: >> I'm pretty nervous about reducing that materially without any >> investigation into how much of the slop we actually use. > To me it seems like using anything based on stack_rlimit/2 is pretty > risky for the reason that you state, but I also think that not being > able to start the database at all on some platforms with small stacks > is bad. My point was that this is something we should investigate, not just guess about. I did some experimentation using the attached quick-kluge patch, which (1) causes each exiting server process to report its actual ending stack size, and (2) hacks the STACK_DEPTH_SLOP test so that you can set max_stack_depth considerably higher than what rlimit(2) claims. Unfortunately the way I did (1) only works on systems with pmap; I'm not sure how to make it more portable. My results on an x86_64 RHEL6 system were pretty interesting: 1. All but two of the regression test scripts have ending stack sizes of 188K to 196K. There is one outlier at 296K (most likely the regex test, though I did not stop to confirm that) and then there's the errors.sql test, which intentionally provokes a "stack too deep" failure and will therefore consume approximately max_stack_depth stack if it can. 2. With the RHEL6 default "ulimit -s" setting of 10240kB, you actually have to increase max_stack_depth to 12275kB before you get a crash in errors.sql. At the highest passing value, 12274kB, pmap says we end with 1 00007ffc51f6e000 12284K rw--- [ stack ] which is just shy of 2MB more than the alleged limit. I conclude that at least in this kernel version, the kernel doesn't complain until your stack would be 2MB *more* than the ulimit -s value. That result also says that at least for that particular test, the value of STACK_DEPTH_SLOP could be as little as 10K without a crash, even without this surprising kernel forgiveness. But of course that test isn't really pushing the slop factor, since it's only compiling a trivial expression at each recursion depth. Given these results I definitely wouldn't have a problem with reducing STACK_DEPTH_SLOP to 200K, and you could possibly talk me down to less. On x86_64. Other architectures might be more stack-hungry, though. I'm particularly worried about IA64 --- I wonder if anyone can perform these same experiments on that? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="stack-depth-hacks.patch"; charset="us-ascii" Content-ID: <28201.1467747532.2@sss.pgh.pa.us> Content-Description: stack-depth-hacks.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/ipc/ipc.c b/src/backend/storage/ipc/ipc.c index cc36b80..7740120 100644 *** a/src/backend/storage/ipc/ipc.c --- b/src/backend/storage/ipc/ipc.c *************** static int on_proc_exit_index, *** 98,106 **** --- 98,113 ---- void proc_exit(int code) { + char sysbuf[256]; +=20 /* Clean up everything that must be cleaned up */ proc_exit_prepare(code); =20=20 + /* report stack size to stderr */ + snprintf(sysbuf, sizeof(sysbuf), "pmap %d | grep stack 1>&2", + (int) getpid()); + system(sysbuf); +=20 #ifdef PROFILE_PID_DIR { /* diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h index 7254355..009bec2 100644 *** a/src/include/tcop/tcopprot.h --- b/src/include/tcop/tcopprot.h *************** *** 27,33 **** =20=20 =20=20 /* Required daylight between max_stack_depth and the kernel limit, in byt= es */ ! #define STACK_DEPTH_SLOP (512 * 1024L) =20=20 extern CommandDest whereToSendOutput; extern PGDLLIMPORT const char *debug_query_string; --- 27,33 ---- =20=20 =20=20 /* Required daylight between max_stack_depth and the kernel limit, in byt= es */ ! #define STACK_DEPTH_SLOP (-100 * 1024L * 1024L) =20=20 extern CommandDest whereToSendOutput; extern PGDLLIMPORT const char *debug_query_string; ------- =_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--