使用 palloc 分配

Allocating with palloc

这是 postgres 扩展的一部分,我试图理解为什么下面的代码行给我这个错误:

ERROR:  invalid memory alloc request size 1073741824

This code is a odbc fdw for postgres; I get that error when I use the extension to create a sql server foreign table; the problem begins when a sql server column is declared as nvarchar(max), according to this nvarchar(max) 的最大大小是 1073741823。代码基本上是这样做的:

char * buf;

//varsize contains the max size of nvarchar(max)

buf = (char *)palloc(sizeof(char) * (varsize + 1));

palloc 究竟在做什么,所以您不能在指针中分配超过 1073741823 的值?它不像大小超过整数范围

您可以阅读 this comment 中的详细信息,但 TL;DR 是 TOAST 无法存储大于 1GB-1B 的内容,因此尝试 palloc 是毫无意义的。有几个地方使用了 MemoryContextAllocHuge(),但听起来对您没有帮助。