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 ...
随机推荐
- django - get_or_create() 使用提醒
[omron - debug] user_id建表的时候,不能使用unique,因为一个用户,可能有多个product_id,相对应的是,get_or_create()中的查询参数,如果在建表中有un ...
- django --------------------- [必要操作]
基本models 命令: python manage.py validate (验证模型有效性, 记得配置 settings.py - INSTALLED_APPS) python manage.py ...
- rtp h264注意点(FU-A分包方式说明)
前写过一篇文章,分析了h264使用rtp进行封包的格式介绍:RTP封装h264.但里面好像没有把拆分以及一些需要注意的情况说清楚,因此这里做补充,也作为自己的备忘(自己记性好像不太好). 关于时间戳, ...
- 【JS】<a>标签调用js中函数的几种方法
我们常用的在a标签中有点击事件: a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题,而 ...
- jsoup使用选择器语法来查找元素
问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. 方法 可以使用Element.select(String selector) 和 Elements.select(String sel ...
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- Qt加载网页(加载浏览器插件)和制作托盘后台运行(南信大财务报账看号)
程序模块要添加QNetWork和QWebKit模块: nuistfinancevideo.h文件: #ifndef NUISTFINANCEVIDEO_H #define NUISTFINANCEVI ...
- webstorm下设置sass
关于sass,就不想多说什么了.只要你有css基础,十分钟入门好吗.可以参考下资料:http://www.w3cplus.com/sassguide/ 今天想说的是webStorm下如何实现sass自 ...
- lighttpd mod_status模块
用过nginx的status可以查看服务器的状态,之后就想lighttpd有没有这样的模块呢 之后看下配置文件,真的有,然后就试下 第一步, "mod_auth" 把这个前面的#号 ...
- 快速建立Linux c/c++编译环境
sudo apt-get install build-essential 省时又省心~