Getting char * from timestamp in a composite type

Started by Michael Akindeover 18 years ago5 messagesgeneral
Jump to latest
#1Michael Akinde
michael.akinde@met.no

Hi,

I need to extract a text string representation of a timestamp from a
user-defined row type; e.g., from the composite type ('1980-01-01
12:00:00', 'Random Comment'), I'd like to extract the C string
1980-01-01 12:00:00 in my user-defined C function.

As I understand it, I should be able to do something like (assuming
"time" is the attribute name):

bool isNull;
HeapTupleHeader t = DatumGetHeapTupleHeader(row);
Datum var = GetAttributeByName( row, "time", & isNull );
// Check for null
char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

However, the code above (and every other variant I've tried) invariable
segmentation faults the postmaster. so obviously I am doing something
wrong. Can someone explain (or point to an example that works) of how to
do this?

Regards,

Michael A.

#2Martijn van Oosterhout
kleptog@svana.org
In reply to: Michael Akinde (#1)
Re: Getting char * from timestamp in a composite type

On Wed, Dec 19, 2007 at 04:40:38PM +0100, Michael Akinde wrote:

As I understand it, I should be able to do something like (assuming
"time" is the attribute name):

bool isNull;
HeapTupleHeader t = DatumGetHeapTupleHeader(row);
Datum var = GetAttributeByName( row, "time", & isNull );
// Check for null
char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

That's not going to work. textout wants a text datum. Try calling
timestamp_out instead...

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

Those who make peaceful revolution impossible will make violent revolution inevitable.
-- John F Kennedy

#3Michael Akinde
michael.akinde@met.no
In reply to: Martijn van Oosterhout (#2)
Re: Getting char * from timestamp in a composite type

Martijn van Oosterhout wrote:

On Wed, Dec 19, 2007 at 04:40:38PM +0100, Michael Akinde wrote:

As I understand it, I should be able to do something like (assuming
"time" is the attribute name):

bool isNull;
HeapTupleHeader t = DatumGetHeapTupleHeader(row);
Datum var = GetAttributeByName( row, "time", & isNull );
// Check for null
char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

That's not going to work. textout wants a text datum. Try calling
timestamp_out instead..

Sigh... it's always the idiot bugs that are the hardest to see. Thanks a
lot.

Regards,

Michael A.

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Akinde (#3)
Re: Getting char * from timestamp in a composite type

Michael Akinde <michael.akinde@met.no> writes:

bool isNull;
HeapTupleHeader t = DatumGetHeapTupleHeader(row);
Datum var = GetAttributeByName( row, "time", & isNull );
// Check for null
char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

That's not going to work. textout wants a text datum. Try calling
timestamp_out instead..

Sigh... it's always the idiot bugs that are the hardest to see. Thanks a
lot.

The other problem is that you're passing row not t to
GetAttributeByName. If you don't have your compiler configured to bleat
about type mismatches like that, your days of C programming will be
bleak and painful.

regards, tom lane

#5Michael Akinde
michael.akinde@met.no
In reply to: Tom Lane (#4)
Re: Getting char * from timestamp in a composite type

Thanks, though that was an error caused by my attempt to copy-paste some
readable example of code from the source file.

Matching the correct _out functions in the DirectFunctionCall fixed the
segmentation fault problems I was seeing.

Regards,

Michael A.

Tom Lane wrote:

Show quoted text

Michael Akinde <michael.akinde@met.no> writes:

bool isNull;
HeapTupleHeader t = DatumGetHeapTupleHeader(row);
Datum var = GetAttributeByName( row, "time", & isNull );
// Check for null
char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

That's not going to work. textout wants a text datum. Try calling
timestamp_out instead..

Sigh... it's always the idiot bugs that are the hardest to see. Thanks a
lot.

The other problem is that you're passing row not t to
GetAttributeByName. If you don't have your compiler configured to bleat
about type mismatches like that, your days of C programming will be
bleak and painful.

regards, tom lane