Re: Using timestamp(tz) in C functions - Mailing list pgsql-general

From Vitaly Burovoy
Subject Re: Using timestamp(tz) in C functions
Date
Msg-id CAKOSWNnnQmB6jYYC37ymFZ1h6Qy8PpqPM1f=QaDONKmwtX+zrg@mail.gmail.com
Whole thread Raw
In response to Re: Using timestamp(tz) in C functions  (Keith Fiske <keith@omniti.com>)
Responses Re: Using timestamp(tz) in C functions
List pgsql-general
On 7/29/16, Keith Fiske <keith@omniti.com> wrote:
> On Fri, Jul 29, 2016 at 12:53 AM, Vitaly Burovoy <vitaly.burovoy@gmail.com>
> wrote:
>
>> On 7/28/16, Keith Fiske <keith@omniti.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://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.
>> >
>> > 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?
>
>
> It returns a single row. Here's an example of the results of the two
> queries that are run that lead to providing the timestamp value
>
> keith=# select partition_tablename from
> partman.show_partitions('partman_test.time_taptest_table', 'DESC') limit
> 1;      partition_tablename
> --------------------------------
>  time_taptest_table_p2016_08_02
> (1 row)
>
> keith=# select child_start_time from
> partman.show_partition_info('partman_test.time_taptest_table_p2016_08_02',
> '1 day', 'partman_test.time_taptest_table');
>     child_start_time
> ------------------------
>  2016-08-02 00:00:00-04
> (1 row)
>
> So there is valid data. As you're pointing out, this may just be a
> misunderstanding of how to actually use the Datum retrieval function and C
> in general. Appreciate the assistance.
>
> Keith
>

Please, add next lines (or debug this values via gdb) and post a result:

ret = SPI_execute(buf.data, true, 1);

ereport(NOTICE, (errmsg("query=%s", buf.data)));
ereport(NOTICE, (errmsg("ret=%i -- rows=%ju", ret, SPI_processed)));

last_partition_timestamp =
DatumGetTimeTzADTP(SPI_getbinval(SPI_tuptable->vals[0],
SPI_tuptable->tupdesc, 1, &isnull));

ereport(NOTICE, (errmsg("SPI_result=%d -- ptr=%p", SPI_result,
(void*)last_partition_timestamp )));

/* elog(LOG, "Just seeing if it's time partitioned: %ld",
last_partition_timestamp->time); --prevent segfaulting */


--
Best regards,
Vitaly Burovoy


pgsql-general by date:

Previous
From: Keith Fiske
Date:
Subject: Re: Using timestamp(tz) in C functions
Next
From: rob stone
Date:
Subject: 9.6beta3