2017-04-05 46 views
0

我正在使用C SPI擴展Postgresql函數。該函數需要能夠接收Postgres N-Dim數組並從中獲取數據。我能夠從一維數組中獲取數據,但在嘗試訪問N-Dim數組時遇到段錯誤。將多維數組作爲輸入構建Postgresql C函數

我第一次嘗試只訪問一個元素只是

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

您可以嘗試使用deconstruct_array(..)函數來提取值。這個函數會給你一個Datum類型的指針。書的

實施例:

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]);