PostgreSQL程序版本号的作用

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

quanzl-mac:bin quanzl$ ./postgres -V
postgres (PostgreSQL) 11beta2
quanzl-mac:bin quanzl$ ./initdb -V
initdb (PostgreSQL) 11beta2
quanzl-mac:bin quanzl$

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

 

1、把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版本不一致。

 

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

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;

 

5、其他程序

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

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

 

4a7ba6e8ac9601eda790b24a20e56130779.jpg

欢迎关注我的公众号,同步发布

 

转载于:https://my.oschina.net/quanzl/blog/3077746

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值