Problem with background worker - Mailing list pgsql-hackers

From Marc Cousin
Subject Problem with background worker
Date
Msg-id 5149C9EC.3000803@gmail.com
Whole thread Raw
Responses Re: Problem with background worker  (Alvaro Herrera <alvherre@2ndquadrant.com>)
List pgsql-hackers
Hi,

I'm trying to write a background writer, and I'm facing a problem with
timestamps. The following code is where I'm having a problem (it's just a demo for
the problem):

BackgroundWorkerInitializeConnection("test", NULL);
while (!got_sigterm)
{int ret;/* Wait 1s */ret = WaitLatch(&MyProc->procLatch,WL_LATCH_SET | WL_TIMEOUT |
WL_POSTMASTER_DEATH,1000L);ResetLatch(&MyProc->procLatch);/*Insert dummy for now
*/StartTransactionCommand();SPI_connect();PushActiveSnapshot(GetTransactionSnapshot());ret= SPI_execute("INSERT INTO
logVALUES (now(),statement_timestamp(),clock_timestamp())", false,
0);SPI_finish();PopActiveSnapshot();CommitTransactionCommand();
}

\d log
      Column        |           Type           | Modifiers
---------------------+--------------------------+---------------now                 | timestamp with time zone |
statement_timestamp| timestamp with time zone | clock_timestamp     | timestamp with time zone |
 

Here is its content. Only the clock_timestamp is right. There are missing records at the
beginning because i truncated the table for this example.
             now              |      statement_timestamp      |        clock_timestamp        
-------------------------------+-------------------------------+-------------------------------2013-03-20
15:01:44.618623+01| 2013-03-20 15:01:44.618623+01 | 2013-03-20 15:01:52.77683+012013-03-20 15:01:44.618623+01 |
2013-03-2015:01:44.618623+01 | 2013-03-20 15:01:53.784301+012013-03-20 15:01:44.618623+01 | 2013-03-20
15:01:44.618623+01| 2013-03-20 15:01:54.834212+012013-03-20 15:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 |
2013-03-2015:01:55.848497+012013-03-20 15:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20
15:01:56.872671+012013-03-2015:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20
15:01:57.888461+012013-03-2015:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20
15:01:58.912448+012013-03-2015:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20
15:01:59.936335+012013-03-2015:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20
15:02:00.951247+012013-03-2015:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20
15:02:01.967937+012013-03-2015:01:44.618623+01 | 2013-03-20 15:01:44.618623+01 | 2013-03-20 15:02:02.983613+01
 


Most of the code is copy/paste from worker_spi (I don't really understand the 
PushActiveSnapshot(GetTransactionSnapshot()) and PopActiveSnapshot() but the 
behaviour is the same with or without them, and they were in worker_spi).

I guess I'm doing something wrong, but I really dont't know what it is.

Should I attach the whole code ?

Regards,

Marc



pgsql-hackers by date:

Previous
From: Magnus Hagander
Date:
Subject: Materialized views vs event triggers missing docs?
Next
From: Tom Lane
Date:
Subject: Re: postgres_fdw vs data formatting GUCs (was Re: [v9.3] writable foreign tables)