Returning Vector of Pairs with a PostgreSQL C Extension Function - Mailing list pgsql-general

From TalGloz
Subject Returning Vector of Pairs with a PostgreSQL C Extension Function
Date
Msg-id 1535201678396-0.post@n3.nabble.com
Whole thread Raw
Responses Re: Returning Vector of Pairs with a PostgreSQL C Extension Function
List pgsql-general
Hello,

I want to return an vector of pairs using a C extension. This is a simple
code I have:

extern "C" {
Datum pair(PG_FUNCTION_ARGS){

        // For text aka. character varying parameter
        text *t1 = PG_GETARG_TEXT_PP(0);
        text *t2 = PG_GETARG_TEXT_PP(1);
        std::string localT1 = text_to_cstring(t1);
        std::string localT2 = text_to_cstring(t2);

        /* Construct the return value of the C extention to PostgreSQl */
        // Returns Text ponter of casted cstrings to text
        //PG_RETURN_TEXT_P(cstring_to_text_with_len(localT1.c_str(),
localT1.size()));
        //PG_RETURN_TEXT_P(cstring_to_text_with_len(encodedLocalT1.c_str(),
encodedLocalT1.size()));
        //PG_RETURN_TEXT_P(cstring_to_text_with_len(outputParam.c_str(),
outputParam.size()));

        // Return vector of pairs
        std::vector<std::pair<std::string, std::string>> ret;
        ret.emplace_back(encodedLocalT1, encodedLocalT2);
        PG_RETURN_ARRAYTYPE_P(ret);

};
PG_FUNCTION_INFO_V1(pair);
}

But it doesn't work like it. Even using this doesn't work:
ArrayType   *array;
std::vector<std::pair<std::string, std::string>> ret;
ret.emplace_back(encodedLocalT1, encodedLocalT2);
array = ret;
PG_RETURN_POINTER(array);

Is it possible to return a vector of pairs? If not then how can I return 2
strings in an Array?

Best regards,
Tal



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html


pgsql-general by date:

Previous
From: Naveen Dabas
Date:
Subject: Re: pg_sample
Next
From: Tom Lane
Date:
Subject: Re: Returning Vector of Pairs with a PostgreSQL C Extension Function