Re: Function returns composite type

From: Teodor Sigaev <teodor(at)sigaev(dot)ru>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Function returns composite type
Date: 2003-06-10 14:57:05
Message-ID: 3EE5F1C1.5070509@sigaev.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)sigaev(dot)ru

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message scott.marlowe 2003-06-10 15:00:07 Re: [HACKERS] SAP and MySQL ... [and Benchmark]
Previous Message J.R. Nield 2003-06-10 14:49:45 Returning to the List