Windows下PostgreSQL C扩展中子进程无法解析localhost的问题求助
Windows下PostgreSQL C扩展中子进程无法解析localhost的问题求助
大家好,我最近碰到一个棘手的问题,想请各位帮忙分析下:
我用C开发了一个PostgreSQL扩展,核心功能是调用外部可执行文件FastTransfer.exe完成数据迁移任务。这个扩展在Linux上运行完全正常,但在Windows环境下,当传入包含localhost(比如--sourceserver "localhost")或者127.0.0.1的连接参数时,子进程总是无法解析主机,报错提示找不到对应的主机。
具体错误日志
2025-06-19T17:06:54.264 +02:00 -|- -|- 654825c2-f4a9-4f21-8a30-03408ba03eb0 -|- ERROR -|- postgres.public.orders -|- Source : System.Net.NameResolution 2025-06-19T17:06:54.264 +02:00 -|- -|- 654825c2-f4a9-4f21-8a30-03408ba03eb0 -|- ERROR -|- postgres.public.orders -|- Message : No such host is known.
我强烈怀疑问题出在C扩展的进程启动逻辑,或者PostgreSQL作为Windows服务运行时的环境限制上——毕竟直接在命令行里执行生成好的命令是完全正常的。
扩展中Windows平台的进程启动代码
// Build command snprintf(command, sizeof(command), "\"%s\"", binary_path); for (i = 0; i < 33; i++) { if (PG_ARGISNULL(i)) continue; is_bool = false; for (b = 0; b < sizeof(bool_params) / sizeof(char *); b++) { if (strcmp(arg_names[i], bool_params[b]) == 0) { is_bool = true; break; } } if (is_bool) { if (PG_GETARG_BOOL(i)) { strncat(command, " ", sizeof(command) - strlen(command) - 1); strncat(command, arg_names[i], sizeof(command) - strlen(command) - 1); } continue; } is_int = false; for (x = 0; x < sizeof(int_params) / sizeof(char *); x++) { if (strcmp(arg_names[i], int_params[x]) == 0) { snprintf(numbuf, sizeof(numbuf), "%d", PG_GETARG_INT32(i)); val = numbuf; is_int = true; break; } } if (!is_int) { val = text_to_cstring(PG_GETARG_TEXT_PP(i)); } if (val && strlen(val) > 0) { strncat(command, " ", sizeof(command) - strlen(command) - 1); strncat(command, arg_names[i], sizeof(command) - strlen(command) - 1); strncat(command, " \"", sizeof(command) - strlen(command) - 1); strncat(command, val, sizeof(command) - strlen(command) - 1); strncat(command, "\"", sizeof(command) - strlen(command) - 1); } } strncat(command, " 2>&1", sizeof(command) - strlen(command) - 1); // Execute command fp = popen(command, "r"); if (!fp) { strncat(result_buffer, "Error: unable to execute FastTransfer.\n", sizeof(result_buffer) - strlen(result_buffer) - 1); exit_code = -1; } else { size_t result_len = strlen(result_buffer); while (fgets(buffer, sizeof(buffer), fp) != NULL && result_len < sizeof(result_buffer) - 1) { size_t buf_len = strlen(buffer); if (result_len + buf_len < sizeof(result_buffer) - 1) { strcat(result_buffer, buffer); result_len += buf_len; } } status = pclose(fp); }
已尝试的排查步骤
- 确认生成的命令行在PostgreSQL外部执行完全正常,参数解析无问题
- 将PostgreSQL服务的运行账户从LocalSystem切换为普通本地用户,但问题依然存在
有没有朋友遇到过类似的情况?或者能看出我的代码里有什么可能导致这个问题的地方吗?
内容来源于stack exchange
相关产品推荐
相关产品推荐

