PostgreSQL程序版本号的作用

网友投稿 876 2022-10-26

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

PostgreSQL程序版本号的作用

很多人查看过程序的版本号:

quanzl-mac:bin quanzl$ ./postgres -Vpostgres (PostgreSQL) 11beta2quanzl-mac:bin quanzl$ ./initdb -Vinitdb (PostgreSQL) 11beta2quanzl-mac:bin quanzl$

似乎这些也就是给我们看看,没有其他用处,其实并非如此。

1、把initdb 放到其他版本bin目录下试试看(别忘记备份,尝试完之后别忘记恢复原状):

quanzl-mac:bin quanzl$ ./initdb -D ../dataThe 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版本不一致。

2、如何根据错误提示定位代码位置

遇到错误信息时,如果对代码不够熟悉,怎么知道哪段代码在报错呢?就像上边的例子,可以在代码中搜索“but was not the same version”,或者 “The program \"postgres\" was found by”,可以很快定位。如果不明白后者为什么这么搜,需要补一下C语言基础,这种格式的字符串如果搜不到那么应该试试搜索 “The program \"%s\" was found by”。

搜后者可以看到还有其他几个程序在做同样的检查:pg_ctl、pg_dumpall、pg_rewind。

3、initdb

来看initdb的报错部分

if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,   backend_exec)) < 0){

当返回值小于0时进入错误处理。

find_other_exec 的几个参数:变量 argv0 的值是 "initdb"、常量 "postgres"、常量 PG_BACKEND_VERSIONSTR定义是:

#define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"

而最后一个参数 backend_exec 将在 find_other_exec 函数内赋值。

4、函数 find_other_exec

intfind_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;

5、其他程序

像 pg_ctl,过程类似,有兴趣自己读一读。

如果想学习怎样改PG代码,不妨动手试一下,只看别人怎么改是很难有提高的。

欢迎关注

上一篇:不忘初心,恪守信条:微盟经历的从删库到跑路的数据灾难
下一篇:从流量思维到产品思维,有鱼猫粮一直在路上
相关文章

 发表评论

暂时没有评论,来抢沙发吧~