2007-05-08

Windows版本下的参数max_stack_depth(简体)

更新:2007-05-08
對映章節:

內容:
前几天一位朋友跟我讨论max_stack_depth在windows下的调整,调查到一点东西,整理一下共享给大家。

因为着重讲max_stack_depth在windows的问题,所以没有翻译参数说明:
max_stack_depth (integer)
Specifies the maximum safe depth of the server's execution stack. The ideal setting for this parameter is the actual stack size limit enforced by the kernel (as set by ulimit -s or local equivalent), less a safety margin of a megabyte or so. The safety margin is needed because the stack depth is not checked in every routine in the server, but only in key potentially-recursive routines such as expression evaluation. The default setting is two megabytes (2MB), which is conservatively small and unlikely to risk crashes. However, it may be too small to allow execution of complex functions. Only superusers can change this setting.
Setting max_stack_depth higher than the actual kernel limit will mean that a runaway recursive function can crash an individual backend process. On platforms where PostgreSQL can determine the kernel limit, it will not let you set this variable to an unsafe value. However, not all platforms provide the information, so caution is recommended in selecting a value.

它是在8.0中引入的,摘自8.0的release note:
Server configuration parameter max_expr_depth parameter has been replaced with max_stack_depth which measures the physical stack size rather than the expression nesting depth. This helps prevent session termination due to stack overflow caused by recursive functions.

8.2中的调整:
On platforms that have getrlimit(RLIMIT_STACK), use it to ensure that max_stack_depth is not set to an unsafe value. This commit also provides configure-time checking for , and cleans up some perhaps-unportable code associated with use of that include file and getrlimit().

现在遇到的问题是,在8.1 For Windows能够正常运行的程序,在8.2下由于max_stack_depth的限制变得无法正常值运行。
由于这个校验的存在,Windows平台
max_stack_depth的最大值为3584KB,如果超过PostgreSQL 8.2将无法启动,但是这个3584KB却无法满足程序的需求。而早先在8.1下设置为10M是没问题的,程序运行过程中也不会引起错误。我猜想原因是由于架构的不同,在这个方面设置是不一样的,windows允许超过而*nix不允许,这样移植到windows下以后,依然参照*nix的方式进行校验,导致这个问题的出现。windows不同于*nix,内核参数几乎是不能自行改变的,比如这个RLIMIT_STACK在*nix可以通过微调将它设置为更大,而windows下根本就是个常量,没有任何办法改变。好在看起来windows平台并没有像*nix平台一样将max_stack_depth限制在RLIMIT_STACK大小之内。
问题就在这里,似乎只是个移植问题。

解决办法就是修改源代码
postgres.c的函数assign_max_stack_depth中

long stack_rlimit = get_stack_depth_rlimit();
改为:
#if defined(WIN32) || defined(__CYGWIN__)
long stack_rlimit = 10*1024*1024; //10M
#else
long stack_rlimit = get_stack_depth_rlimit();
#endif

然后重新编译即可

编译请参考:
Compiling PostgreSQL On Native Win32 FAQ
Installing PostgreSQL on Windows Using Cygwin FAQ

本方法没有经过测试,仅供参考,对此本人不承担任何责任。

沒有留言:

PostgreSQL & Google-Analytics Running...

::Planet PostgreSQL::

PostgreSQL Information Page

PostgreSQL日記(日本 石井達夫先生Blog)

PostgreSQL News

黑喵的家 - 資料庫相關

Google 網上論壇
PostgreSQL 8 DBA 專業指南中文版
書籍內容討論與更多下載區(造訪此群組)
目錄下載: PostgreSQL_8 _DBA_Index_zh_TW.pdf (更新:2007-05-18)

全球訪客分佈圖(Google)

全球訪客分佈圖(Google)