Re: Using timestamp(tz) in C functions

From: Vitaly Burovoy <vitaly(dot)burovoy(at)gmail(dot)com>
To: Keith Fiske <keith(at)omniti(dot)com>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using timestamp(tz) in C functions
Date: 2016-07-29 04:53:24
Message-ID: CAKOSWNmt1UkoD+Nst6AExAH84aCYHfF+NYekt5bnvhLdh15CUA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7/28/16, Keith Fiske <keith(at)omniti(dot)com> wrote:
> Working on trying to get a C version of the maintenance function for my
> pg_partman extension working so I can hopefully make it more flexible and
> efficient.
>
> https://github.com/keithf4/pg_partman/blob/master/sql/functions/run_maintenance.sql
>
> Up until this point I've just been using SPI and the Datum conversion
> functions (DatumGetCString, DatumGetInt32) to get string and numeric data
> out of tables and things have been working fine. I'm at the first point in
> that function where I'll need to start dealing with timestamp data.
>
> https://gist.github.com/keithf4/81c32bf8b689c74b20c10ad8c91d45a3#file-pg_partman_bgw-c-L532
>
> There's what I've got working so far and links directly to the area where
> I'm having a problem. I found the DatumGetTimeTzADTP() function and the
> TimeTzADT data type looking through the source and that seems to be exactly
> what I'm looking for. However, when I get to the point of trying to simply
> use the time value in that variable (line 544), Postgres segfaults. So far
> I've just been trying to print the value out to the log to ensure I'm
> pulling it out correctly. The "time" value of the struct appears to be an
> int64, so I thought %ld would be the correct, but even using %d or %s
> fails.
>
> You can see in my original plpgsql function how I eventually intend to use
> the timestamp values. Here's a link directly to the equivalent section
>
> https://github.com/keithf4/pg_partman/blob/master/sql/functions/run_maintenance.sql#L139
>
> So, not sure if I'm even going about this the right manner for the way I
> intend to use the timestamp values. Still fairly new to C and getting use
> to postgres internals. Any help, or even an example of using timstamp data
> pulled from a table in C with SPI, would be great.
>
> Thanks!
>
> --
> Keith Fiske
> Database Administrator
> OmniTI Computer Consulting, Inc.
> http://www.keithf4.com
>

I think it is not about timestamp(tz), but about usage of SPI.
Since DatumGetTimeTzADTP is just a macros implements type conversion
(declared at src/include/utils/date.h:60 (or 75)) you get segfault not
in it but when the code tries to get value by dereference pointer
(last_partition_timestamp->time).

Please, answer questions:
1. How many rows SPI_execute returns (value of "ret" variable)?
2. Is last_partition_timestamp != NULL? Where it points to?
3. Try to check SPI_result just after SPI_getbinval. Has it error code?

--
Best regards,
Vitaly Burovoy

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jan Wieck 2016-07-29 05:05:51 Re: WAL directory size calculation
Previous Message Chris Travers 2016-07-29 03:01:18 Re: Uber migrated from Postgres to MySQL