Re: Creating HeapTuple from char and date values

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: Amin <amin(dot)fallahi(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Creating HeapTuple from char and date values
Date: 2022-12-08 13:15:52
Message-ID: CAExHW5sD=JOMcJO7T8Hc+PjPx5ap_j3jHpm4zFXv2kQLjPBPEA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Dec 8, 2022 at 12:56 AM Amin <amin(dot)fallahi(at)gmail(dot)com> wrote:
>
> Hi All,
>
> I am trying to create HeapTuple data structure.
> First, I create a tuple descriptor:
> TupleDesc *td=CreateTemplateTupleDesc(colCount);
> Then, for each variable, I do:
> TupleDescInitEntry(*td,v->varattno,NULL,v->vartype,v->vartypmod,0);
> Then, I assign values:
> if int32: values[v->varattno-1]=Int8GetDatum(myValue);
> Similarly for float.
> Finally, I create the HeapTuple:
> HeapTuple tuple=heap_form_tuple(td,values,isnull);
>
> Everything works fine with int and float. But I don't know how to handle chars.
> Let's say we have a character(10) column. One problem is v->vartypmod will be set to 14. Shouldn't it be 10?

I think the 4 extra bytes is varlena header - not sure. but typmod in
this case indicates the length of binary representation. 14 looks
correct.

> Second, how should I assign values? Is values[v->varattno-1]=CStringGetDatum(myValue); correct? Should I set the last parameter to TupleDescInitEntry? Why am I getting "invalid memory alloc request size" or segfault with different configurations?

is myValue a char *?
I think you need to use CStringGetTextDatum instead of CStringGetDatum.

--
Best Wishes,
Ashutosh Bapat

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Drouvot, Bertrand 2022-12-08 13:34:40 Re: Checksum errors in pg_stat_database
Previous Message Vik Fearing 2022-12-08 12:32:30 Re: ANY_VALUE aggregate