Reading timestamp values from Datums gives garbage values

From: Sushrut Shivaswamy <sushrut(dot)shivaswamy(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Reading timestamp values from Datums gives garbage values
Date: 2024-05-20 14:37:13
Message-ID: CAH5mb99QvpQsbR0CbF=9gObbU-C8D2-8TYWKUw389Q2J3qLiqA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hey,

I'm trying to read a timestamp column as EPOCH.
My query is as follows.
```
SELECT EXTRACT(EPOCH FROM timestamp_column) FROM table;

column
----------

1716213097.86486
```
When running in the console this query gives valid epoch output which
appears to be of type double.

When trying to read the query response from the Datum, I get garbage values.
I've tried various types and none of them read the correct value.
```

Datum current_timestamp = SPI_getbinval(SPI_tuptable->vals[i],
SPI_tuptable->tupdesc, 5, &isnull);

double current_time = DatumGetFloat8(current_timestamp); // prints 0

int64 time = DatumGetUint64(current_timestamp); // prints 5293917674
```

Can you help me out with the correct way to read EPOCH values from datums?

Thanks,
Sushrut

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2024-05-20 15:12:34 Re: Shared detoast Datum proposal
Previous Message Robert Haas 2024-05-20 14:18:57 Re: commitfest.postgresql.org is no longer fit for purpose