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 ...
随机推荐
- java读取照片信息 获取照片拍摄时的经纬度
项目结构 源码:ImageInfo.zip 第一步:添加需要的架包metadate-extractor.jar 架包下载地址:https://code.google.com/p/metadata-ex ...
- Delphi打开窗体时报"Corrupt Portfolio Stream"
今天在打开一个Delphi窗体时报了这么一个错误: Corrupt Portfolio Stream 查了一下,主要是由于Delphi窗体的*.ddp文件损坏引起的. 解决方法: 删除.ddp 文 ...
- MySQL基础之第2章 Windows平台下安装与配置MySQL
2.1.msi安装包 2.1.1.安装 特别要注意的是,安装前要删除原来的my.ini和原来的data目录,改名也行,不然在最后一步会“apply security settings”报个1045错误 ...
- VS2010使用EventHandler发邮件
转:http://blog.csdn.net/alfred_72/article/details/9980279 因为不知道VS2010 Sharepoint 有EventReciver这个添加项,走 ...
- OracleBulkCopy的批量数据导入
private void button1_Click(object sender, EventArgs e) { OpenFileDialog afd = new OpenFileDialog(); ...
- C# new用法总结
有一道这样的题:写出c#中new关键字的三种用法,思前想后挖空心思也只想出了两种用法,回来查了下msdn,还真是有第三种用法: 用于在泛型声明中约束可能用作类型参数的参数的类型,这是在Framewor ...
- HDU 5122 K.Bro Sorting
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- linux命令——磁盘管理cd
Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. cd指令可让用户在不同的目录间切换,但该用户必须拥有足够的权限进入目的目录. 1 ...
- Allegro从.brd文件中导出器件封装
打开.brd文件,File→Export→Libraries,除了No libraries dependencies之外,所有选项都勾选上,设定好存放路径之后,Export. 注意事项: 1. 一般的 ...
- offsetWidth、offsetleft 等图文详解
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...