不少人查看过程序的版本号:函数
quanzl-mac:bin quanzl$ ./postgres -V postgres (PostgreSQL) 11beta2 quanzl-mac:bin quanzl$ ./initdb -V initdb (PostgreSQL) 11beta2 quanzl-mac:bin quanzl$
彷佛这些也就是给咱们看看,没有其余用处,其实并不是如此。post
一、把initdb 放到其余版本bin目录下试试看(别忘记备份,尝试完以后别忘记恢复原状):学习
quanzl-mac:bin quanzl$ ./initdb -D ../data The program "postgres" was found by "/Users/quanzl/build/flyingdb-v10bin/bin/initdb" but was not the same version as initdb. Check your installation. quanzl-mac:bin quanzl$
很清楚的错误提示,initdb和postgres版本不一致。ui
二、如何根据错误提示定位代码位置spa
遇到错误信息时,若是对代码不够熟悉,怎么知道哪段代码在报错呢?就像上边的例子,能够在代码中搜索“but was not the same version”,或者 “The program \"postgres\" was found by”,能够很快定位。若是不明白后者为何这么搜,须要补一下C语言基础,这种格式的字符串若是搜不到那么应该试试搜索 “The program \"%s\" was found by”。code
搜后者能够看到还有其余几个程序在作一样的检查:pg_ctl、pg_dumpall、pg_rewind。ip
三、initdb
字符串
来看initdb的报错部分get
if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR, backend_exec)) < 0) {
当返回值小于0时进入错误处理。cmd
find_other_exec 的几个参数:变量 argv0 的值是 "initdb"、常量 "postgres"、常量 PG_BACKEND_VERSIONSTR定义是:
#define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
而最后一个参数 backend_exec
将在 find_other_exec
函数内赋值。
四、函数 find_other_exec
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
在上边的例子里,它接收到的前三个字符串参数分别是:"initdb"、"postgres"、"postgres (PostgreSQL) 11beta2\n"
首先找initdb所在目录,确认 target 也就是 postgres
是否存在,权限够不够,一切正常就执行 postgres[.exe] -V
:
snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath); if (!pipe_read_line(cmd, line, sizeof(line))) return -1;
比较其输出是否是与 "postgres (PostgreSQL) 11beta2\n" 相同:
if (strcmp(line, versionstr) != 0) return -2;
五、其余程序
像 pg_ctl,过程相似,有兴趣本身读一读。
若是想学习怎样改PG代码,不妨动手试一下,只看别人怎么改是很难有提升的。
欢迎关注个人公众号,同步发布