2017-08-04 77 views
1

基於PostgreSQL 9.6文檔,我試圖執行一個返回一組記錄的函數。按照C中的代碼:返回一組記錄C

#include <funcapi.h> 
..... 

FuncCallContext *funcctx; /*help control to save and remember state */ 
    MemoryContext oldcontext, newcontext; 
    Datum result; 
    uint32 ct_call; 
    TupleDesc tupdesc; 
    AttInMetadata *attinmeta; 

    // struct line_reverse_tuple_args *args; /*aux struct for save state*/ 

    // struct geo_linestring *line1; 
    // struct geo_linestring *line2; 

    elog(NOTICE, "is being called called"); 

    /* Determine if the function is being called for the first time.*/ 
    if(SRF_IS_FIRSTCALL()) 
    { 
    elog(NOTICE, "geo_linestring_intersection_points_v1 first called"); 

    /* initialize the FuncCallContext */ 
    funcctx = SRF_FIRSTCALL_INIT(); 

    /* switch to memory context appropriate for multiple function calls */ 
    newcontext = funcctx->multi_call_memory_ctx; 
     oldcontext = MemoryContextSwitchTo(newcontext); 

    // line1 = PG_GETARG_GEOLINESTRING_TYPE_P(0); 
    // line2 = PG_GETARG_GEOLINESTRING_TYPE_P(1); 

    /* allocate and zero-fill struct for persisting extracted arguments*/ 
    //args = palloc0(sizeof(struct line_reverse_tuple_args)); 

    //funcctx->user_fctx = args; 

    /* total number of tuples to be returned */ 
    funcctx->max_calls = 3; 

     /* Build a tuple descriptor for our result type */ 
    if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) 
     ereport(ERROR, 
       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), 
       errmsg("function returning record called in context " 
         "that cannot accept type record"))); 



    attinmeta = TupleDescGetAttInMetadata(tupdesc); 
    funcctx->attinmeta = attinmeta; 


    /* restore memory context */ 
    MemoryContextSwitchTo(oldcontext); 

    elog(NOTICE, "geo_linestring_intersection_points_v1 end first call"); 

    } 

    funcctx = SRF_PERCALL_SETUP(); 


    ct_call = funcctx->call_cntr; 
    attinmeta = funcctx->attinmeta; 

    elog(NOTICE, "funcapi others call"); 

    if (ct_call < funcctx->max_calls) 
    { 
    HeapTuple tuple; 
    char  **values; 

    /* 
    * Prepare a values array for building the returned tuple. 
    * This should be an array of C strings which will 
    * be processed later by the type input functions. 
    */ 
    values = (char **) palloc(3 * sizeof(char *)); 
    values[0] = (char *) palloc(16 * sizeof(char)); 
    values[1] = (char *) palloc(16 * sizeof(char)); 
    values[2] = (char *) palloc(16 * sizeof(char)); 

    snprintf(values[0], 16, "%d", 1 * PG_GETARG_INT32(1)); 
    snprintf(values[1], 16, "%d", 2 * PG_GETARG_INT32(1)); 
    snprintf(values[2], 16, "%d", 3 * PG_GETARG_INT32(1)); 

    /* build a tuple */ 
    tuple = BuildTupleFromCStrings(attinmeta, values); 

    /* make the tuple into a datum */ 
    result = HeapTupleGetDatum(tuple); 

    /* clean up (this is not really necessary) */ 
    pfree(values[0]); 
    pfree(values[1]); 
    pfree(values[2]); 
    pfree(values); 

    SRF_RETURN_NEXT(funcctx, result); 
    } 

    else 
    { 
    SRF_RETURN_DONE(funcctx); 
    } 

在sql中創建函數在文檔中是相同的。 但是,當我運行這樣的SQL:

SELECT * FROM build_rows_funcapi(1,48); 

服務器崩潰。出現以下消息:

服務器意外地關閉了連接。

基於日誌管理系統,它是可以觀察到的功能,直到第一個被執行的:如果(SRF_IS_FIRSTCALL())

我是不是實現了錯事,或忘了點什麼?

+0

常見問題是對返回的數據使用錯誤的內存上下文。 –

+0

嘗試[獲取堆棧跟蹤](https://wiki.postgresql.org/wiki/Generating_a_stack_trace_of_a_PostgreSQL_backend),這將幫助您找出哪裏出了問題以及在哪裏。 –

+0

請發佈完整的C文件。 –

回答

0

請確保您具有與服務器相同的postgre SQL版本。如果沒有,請下載正確的版本。如果這不是問題,請檢查pg_hba.conf文件中的IP配置是否設置正確。

+0

服務器和PostgreSQL具有相同的版本:9.6。並且pg_hba.conf中的IP配置正確。謝謝,但問題仍然存在。 'SRF_IS_FIRSTCALL()'函數會崩潰服務器。 –