PostgreSQL的 initdb 源代码分析之十一
继续分析:
/* Top level PG_VERSION is checked by bootstrapper, so make it first */
write_version_file(NULL);
就是建立了一个 PG_VERSION的文件
在我系统里,可以看到:
[pgsql@localhost DemoDir]$ cat PG_VERSION
9.1
[pgsql@localhost DemoDir]$
接下来:
我先看看 set_null_conf 函数
/* Select suitable configuration settings */
set_null_conf();
test_config_settings();
展开 set_null_conf 函数:就是生成了一个 空的 postgresql.conf文件。
/*
* set up an empty config file so we can check config settings by launching
* a test backend
*/
static void
set_null_conf(void)
{
FILE *conf_file;
char *path; path = pg_malloc(strlen(pg_data) + );
sprintf(path, "%s/postgresql.conf", pg_data);
conf_file = fopen(path, PG_BINARY_W);
if (conf_file == NULL)
{
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
if (fclose(conf_file))
{
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
free(path);
}
再看 test_config_settings() 完成了什么?
我得到的cmd的值是:
对于确定 max_connections :
"/home/pgsql/project/bin/postgres" --boot -x0 -F -c max_connections=100 -c shared_buffers=1000 < "/dev/null" > "/dev/null" 2>&1
对于确定 shared_buffers :
"/home/pgsql/project/bin/postgres" --boot -x0 -F -c max_connections=100 -c shared_buffers=4096 < "/dev/null" > "/dev/null" 2>&1
/*
* Determine platform-specific config settings
*
* Use reasonable values if kernel will let us, else scale back. Probe
* for max_connections first since it is subject to more constraints than
* shared_buffers.
*/
static void
test_config_settings(void)
{
/*
* This macro defines the minimum shared_buffers we want for a given
* max_connections value. The arrays show the settings to try.
*/
#define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10) static const int trial_conns[] = {
, , , , ,
};
static const int trial_bufs[] = {
, , , , , ,
, , , , , ,
, , , ,
}; char cmd[MAXPGPATH];
const int connslen = sizeof(trial_conns) / sizeof(int);
const int bufslen = sizeof(trial_bufs) / sizeof(int);
int i,
status,
test_conns,
test_buffs,
ok_buffers = ; printf(_("selecting default max_connections ... "));
fflush(stdout); for (i = ; i < connslen; i++)
{
test_conns = trial_conns[i];
test_buffs = MIN_BUFS_FOR_CONNS(test_conns); snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
"-c max_connections=%d "
"-c shared_buffers=%d "
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
backend_exec, boot_options,
test_conns, test_buffs,
DEVNULL, DEVNULL);
status = system(cmd);
if (status == )
{
ok_buffers = test_buffs;
break;
}
}
if (i >= connslen)
i = connslen - ;
n_connections = trial_conns[i]; printf("%d\n", n_connections); printf(_("selecting default shared_buffers ... "));
fflush(stdout); for (i = ; i < bufslen; i++)
{
/* Use same amount of memory, independent of BLCKSZ */
test_buffs = (trial_bufs[i] * ) / BLCKSZ;
if (test_buffs <= ok_buffers)
{
test_buffs = ok_buffers;
break;
} snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
"-c max_connections=%d "
"-c shared_buffers=%d "
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
backend_exec, boot_options,
n_connections, test_buffs,
DEVNULL, DEVNULL);
status = system(cmd);
if (status == )
break;
}
n_buffers = test_buffs; if ((n_buffers * (BLCKSZ / )) % == )
printf("%dMB\n", (n_buffers * (BLCKSZ / )) / );
else
printf("%dkB\n", n_buffers * (BLCKSZ / ));
}
关键是看 system函数的内容:
int
system(const char *command)
{
pid_t pid;
int pstat;
struct sigaction ign,
intact,
quitact;
sigset_t newsigblock,
oldsigblock; if (!command) /* just checking... */
return (); /*
* Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save existing
* signal dispositions.
*/
ign.sa_handler = SIG_IGN;
(void) sigemptyset(&ign.sa_mask);
ign.sa_flags = ;
(void) sigaction(SIGINT, &ign, &intact);
(void) sigaction(SIGQUIT, &ign, &quitact);
(void) sigemptyset(&newsigblock);
(void) sigaddset(&newsigblock, SIGCHLD);
(void) sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
switch (pid = fork())
{
case -: /* error */
break;
case : /* child */ /*
* Restore original signal dispositions and exec the command.
*/
(void) sigaction(SIGINT, &intact, NULL);
(void) sigaction(SIGQUIT, &quitact, NULL);
(void) sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
_exit();
default: /* parent */
do
{
pid = wait4(pid, &pstat, , (struct rusage *) );
} while (pid == - && errno == EINTR);
break;
}
(void) sigaction(SIGINT, &intact, NULL);
(void) sigaction(SIGQUIT, &quitact, NULL);
(void) sigprocmask(SIG_SETMASK, &oldsigblock, NULL); return (pid == - ? - : pstat);
}
结合上述 test_config_settings 函数 和 system函数,
可以知道,是给出 max_connections 参数和 shared_buffers参数,带给postgres,让它执行。
如果可以正常返回,说明可以工作,于是就用这个参数。也就是试探出最大允许的max_connections 和 shared_buffers参数。
我的运行结果是:
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
PostgreSQL的 initdb 源代码分析之十一的更多相关文章
- PostgreSQL的 initdb 源代码分析之二十一
继续分析: setup_schema(); 展开: 实质就是创建info_schema. cmd 是: "/home/pgsql/project/bin/postgres" --s ...
- PostgreSQL的initdb 源代码分析之六
继续分析 下面的是获取运行此程序的用户名称,主要还是为了防止在linux下用root来运行的情形. effective_user = get_id(); ) username = effective_ ...
- PostgreSQL的 initdb 源代码分析之二
继续分析 下面这一段,当 initdb --version 或者 initdb --help 才有意义. ) { ], || strcmp(argv[], ) { usage(progname); ...
- PostgreSQL的 initdb 源代码分析之二十四
继续分析: make_template0(); 展开: 无需再作解释,就是创建template0数据库 /* * copy template1 to template0 */ static void ...
- PostgreSQL的 initdb 源代码分析之十五
继续分析: if (pwprompt || pwfilename) get_set_pwd(); 由于我启动initdb的时候,没有设置口令相关的选项,故此略过. 接下来: setup_depend( ...
- PostgreSQL的 initdb 源代码分析之十三
继续分析: /* Bootstrap template1 */ bootstrap_template1(); 展开: 我这里读入的文件是:/home/pgsql/project/share/postg ...
- PostgreSQL的 initdb 源代码分析之十二
继续分析 /* Now create all the text config files */ setup_config(); 将其展开: 实质就是,确定各种参数,分别写入 postgresql.co ...
- PostgreSQL的 initdb 源代码分析之七
继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8 printf(_("The files belonging ...
- PostgreSQL的initdb 源代码分析之五
接前面,继续分析: putenv("TZ=GMT") 设置了时区信息. find_other_exec(argv[0], "postgres", PG_BACK ...
随机推荐
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- eclipse设置自定义快捷键
eclipse有很多强大且人性化的功能,而各项功能有时又隐藏得比较深(需要点击数次菜单才能找到),而系统提供的快捷键有时比较难记住甚至根本没有提供快捷键时,就需要自己手动设置快捷键了.设置方法有两种, ...
- VS2010使用EventHandler发邮件
转:http://blog.csdn.net/alfred_72/article/details/9980279 因为不知道VS2010 Sharepoint 有EventReciver这个添加项,走 ...
- 【c++内存分布系列】单继承
父类包括成员函数.静态函数.静态方法,子类包括成员函数.静态函数.静态方法的情况与一个类时完全一致,这里就不做分析了.子类单独包含虚函数时继承无关,也不做分析了. 一.父类子类都为空 #include ...
- Java SE 6 新特性: Java DB 和 JDBC 4.0
http://www.ibm.com/developerworks/cn/java/j-lo-jse65/index.html 长久以来,由于大量(甚至几乎所有)的 Java 应用都依赖于数据库,如何 ...
- CXF之五 拦截器Interceptor
拦截器(Interceptor)是CXF功能最主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加很多功能.拦截器和JAX-WS Handler.Filter的功能类似,当服务被调用时,就会创建 ...
- A标记点击后去掉虚线
没加时,谷歌无虚线,但ie有虚线. 加上这句,所有的a标签点击都没有虚线了. a {outline: none;} a:active {star:expression_r(this.onFocus=t ...
- bjfu1211 推公式,筛素数
题目是求fun(n)的值 fun(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])C[n][k] ...
- Unity中Instantiate一个prefab时需要注意的问题
在调用Instantiate()方法使用prefab创建对象时,接收Instantiate()方法返回值的变量类型必须和声明prefab变量的类型一致,否则接收变量的值会为null. 比如说,我在 ...
- [CODEVS1697]⑨要写信
题目描述 Description 琪露诺(冰之妖精)有操控冷气的能力.能瞬间冻结小东西,比普通的妖精更危险.一直在释放冷气的她周围总是非常寒冷. 由于以下三点原因…… 琪露诺的符卡 冰符“Icicle ...