我正在使用C SPI扩展Postgresql函数。该函数需要能够接收Postgres N-Dim数组并从中获取数据。我能够从一维数组中获取数据,但在尝试访问N-Dim数组时遇到了段错误。
我第一次尝试访问元素只是
PG_FUNCTION_INFO_V1(sum_elements);
Datum
matmul(PG_FUNCTION_ARGS)
{
ArrayType *anyArray = PG_GETARG_ARRAYTYPE_P(0);
int32 **a = (int32 **) ARR_DATA_PTR(anyArray);
PG_RETURN_INT64(a[1][1]);
}
我也试过将矩阵展平为一维数组但是出来的值只是垃圾。
感谢您的帮助!
答案 0 :(得分:0)
您可以尝试使用deconstruct_array(..)函数来提取值。此函数将为您提供基准类型指针。
这本书的例子:
deconstruct_array(input_array, //one-dimensional array
INT4OID, //of integers
4,//size of integer in bytes
true,//int4 is pass-by value
'i',//alignment type is 'i'
&datums, &nulls, &count); // result here
“基准指针将设置为指向填充实际元素的数组。”
您可以使用以下方式访问这些值:
DatumGetInt32(datums[i]);