Postgres C 扩展中的环境变量

Nik*_*ola 2 postgresql environment-variables postgresql-extensions c

我无法在 PostgreSQL C 扩展代码中获取环境变量。

例如,此函数始终返回111

#include "postgres.h"
#include "fmgr.h"
#include <stdlib.h>

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(myinc);
Datum
myinc(PG_FUNCTION_ARGS)
{
  int32 arg = PG_GETARG_INT32(0);
  char* envar = getenv("MYINC");
  if (envar) {
    PG_RETURN_INT32(arg + atoi(envar));
  } else {
    PG_RETURN_INT32(111);
  }
}
Run Code Online (Sandbox Code Playgroud)

虽然此 C 程序按预期工作,但它会打印以下内容MYINC

#include <stdio.h>
#include <stdlib.h>

int main()
{
  printf("MYINC: %s", getenv("MYINC"));
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Lau*_*lbe 5

那应该工作得很好。但请注意,它将从 PostgreSQL 服务器进程的环境中获取变量,而不是当前的客户端进程。