文章目录
#函数原型
/*
* PortalSetResultFormat
* Select the format codes for a portal's output.
*
* This must be run after PortalStart for a portal that will be read by
* a DestRemote or DestRemoteExecute destination.
* It is not presently needed for other destination types.
*
* formats[] is the client format request, as per Bind message conventions.
*/
void
PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
{
int natts;
int i;
/*如果没有tuple返回的话,直接返回*/
if (portal->tupDesc == NULL)
return;
/*natts代表一个表中的属性值个数,也就是列的个数*/
natts = portal->tupDesc->natts;
/*在指定的内存块中分配内存*/
portal->formats = (int16 *)
MemoryContextAlloc(PortalGetHeapMemory(portal),
natts * sizeof(int16));
/*判断nFormats的个数,由调用函数传入*/
if (nFormats > 1)
{
/* format specified for each column */
if (nFormats != natts)
ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("bind message has %d result formats but query has %d columns",
nFormats, natts)));
/*将formats带进来的格式拷贝到结构体portal中的formats指向的内存块中
*也就是上面分配的内存块(format为1的时候代表是text,为0时代表二进制)
*/
memcpy(portal->formats, formats, natts * sizeof(int16));
}
else if (nFormats > 0)
{
/* single format specified, use for all columns */
int16 format1 = formats[0];
for (i = 0; i < natts; i++)
portal->formats[i] = format1;
}
else
{
/* use default format for all columns */
for (i = 0; i < natts; i++)
portal->formats[i] = 0;
}
}
综上所述,次函数的作用就是将参数formats数组的值,拷贝到portal内部formats指向的内存块中。
#解释
DestRemote : 结果发送到前端程序
DestRemoteExecute:执行命令的结果发送到前端
两者可以认为没什么区别