Re: Using timestamp(tz) in C functions

From: Keith Fiske <keith(at)omniti(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using timestamp(tz) in C functions
Date: 2016-07-28 21:54:49
Message-ID: CAG1_KcDQkpC0ZdnDiJqNZcsTYyb_=n6X074VkKhROeY=r8UutQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jul 28, 2016 at 5:28 PM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
wrote:

> On 07/28/2016 02:15 PM, Keith Fiske 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.
>>
>
> https://www.postgresql.org/docs/current/static/contrib-spi.html
>
>
> F.34.5. moddatetime — Functions for Tracking Last Modification Time
>
> moddatetime() is a trigger that stores the current time into a timestamp
> field. This can be useful for tracking the last modification time of a
> particular row within a table.
>
> To use, create a BEFORE UPDATE trigger using this function. Specify a
> single trigger argument: the name of the column to be modified. The column
> must be of type timestamp or timestamp with time zone.
>
> There is an example in moddatetime.example.
>
>
> Can be seen here:
>
>
> https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=contrib/spi/moddatetime.c;h=c6d33b73557ff499faa0d475e032fd1f8164afbd;hb=HEAD
>
>
>> Thanks!
>>
>> --
>> Keith Fiske
>> Database Administrator
>> OmniTI Computer Consulting, Inc.
>> http://www.keithf4.com
>>
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com
>

Yeah I did see the SPI contrib module stuff. But none of that seems to
actually be extracting timestamp values from an existing table and using
them. The one you linked just generates the current time.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2016-07-28 22:16:48 Re: Uber migrated from Postgres to MySQL
Previous Message Adrian Klaver 2016-07-28 21:28:09 Re: Using timestamp(tz) in C functions