Re: Function returns composite type - Mailing list pgsql-hackers

From Teodor Sigaev
Subject Re: Function returns composite type
Date
Msg-id 3EE5F1C1.5070509@sigaev.ru
Whole thread Raw
In response to Re: Function returns composite type  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Function returns composite type
List pgsql-hackers
> Perhaps the RECORD stuff would help you?  It's poorly documented, but
> you could look at the code for SRFs to see how to use it.
> 
>             regards, tom lane

Ok, RECORD was a keyword, thank you. But I have questions:
1 What do you mean SRF? Set Return Function?
2 Some example I found in contrib/tablefunc, and so I create test function:
Datum
qqn(PG_FUNCTION_ARGS) {        int attnum = PG_NARGS();        TupleDesc       tupdesc;        char
attname[NAMEDATALEN];       int i;        TupleTableSlot *slot;        AttInMetadata *attinmeta;        Datum
*dvalues,result;        char       *nulls;        HeapTuple       tuple;
 
        /* set tupledesc */        tupdesc = CreateTemplateTupleDesc(attnum, false);        for (i = 0; i < attnum;
i++){                sprintf(attname, "z%d", i+1);                TupleDescInitEntry(tupdesc, i+1, attname, INT4OID,
-1,0, false);        }
 
        slot = TupleDescGetSlot(tupdesc);
        attinmeta = TupleDescGetAttInMetadata(tupdesc);
        dvalues = (Datum *) palloc(attnum * sizeof(Datum));        nulls = (char *) palloc(attnum * sizeof(char));
        for (i = 0; i < attnum; i++) {                nulls[i] = ' ';                dvalues[i] = PG_GETARG_DATUM(i);
    }
 
        /* tupdesc = attinmeta->tupdesc */        tuple = heap_formtuple(tupdesc, dvalues, nulls);
pfree(dvalues);       pfree(nulls);        result = TupleGetDatum(slot, tuple);
 
        PG_RETURN_DATUM(result);
}

create function qqN(int4)
returns record
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

create function qqN(int4,int4)
returns record
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

# select * from qqn(1) as c(qq int4); qq
----  1
(1 row)

# select * from qqn(1,2) as c(qq int4, qq1 int4); qq | qq1
----+-----  1 |   2
(1 row)

It works fine. But is there way not to point 'as c(qq int4, qq1 int4)'? Notice: 
qqn isn't real, it's test only.

Thank you.

-- 
Teodor Sigaev                                  E-mail: teodor@sigaev.ru



pgsql-hackers by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: security flaw
Next
From: Bruce Momjian
Date:
Subject: Re: Proposal to Re-Order Postgresql.Conf, part II