Re: Problem with background worker

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Marc Cousin <cousinmarc(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Problem with background worker
Date: 2013-03-20 15:33:54
Message-ID: 20130320153354.GA3688@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Marc Cousin escribió:
> 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 log VALUES (now(),statement_timestamp(),clock_timestamp())", false, 0);
> SPI_finish();
> PopActiveSnapshot();
> CommitTransactionCommand();
> }

Ah. The reason for this problem is that the statement start time (which
also sets the transaction start time, when it's the first statement) is
set by postgres.c, not the transaction-control functions in xact.c. So
you'd need to add a SetCurrentStatementStartTimestamp() call somewhere
in your loop.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Josh Kupershmidt 2013-03-20 15:38:58 Re: Ignore invalid indexes in pg_dump
Previous Message Tom Lane 2013-03-20 14:43:57 Re: postgres_fdw vs data formatting GUCs (was Re: [v9.3] writable foreign tables)