From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Creager <Robert_Creager@LogicalChaos.org>
Cc: PGPerformance <pgsql-performance@postgresql.org>
Subject: Re: Speed up a function?CREATE TABLE readings ( "when" TIMESTAMP DEFAULT timeofday()::timestamp NOT NULL PRIMARY KEY, "barometer" FLOAT DEFAULT NULL,
Date: Wed, 25 Feb 2004 01:04:48 -0500
Message-ID: <938.1077689088@sss.pgh.pa.us> (raw)
In-Reply-To: <20040224221016.2c35e448.Robert_Creager@LogicalChaos.org>
References: <20040224221016.2c35e448.Robert_Creager@LogicalChaos.org>
Robert Creager <Robert_Creager@LogicalChaos.org> writes:
> I've implemented a couple of functions ala date_trunc (listed at the bottom)
> [ and they're too slow ]
Well, it's hardly surprising that a function that invokes date_trunc and
half a dozen other comparably-expensive operations should be half a
dozen times as expensive as date_trunc. Not to mention that plpgsql is
inherently far slower than C.
Assuming that you don't want to descend to writing C, I'd suggest doing
arithmetic on the Unix-epoch version of the timestamp. Perhaps
something along the lines of
select 'epoch'::timestamptz +
trunc(extract(epoch from now())/(3600*24*7))*(3600*24*7) * '1sec'::interval;
This doesn't have the same roundoff behavior as what you posted, but I
think it could be adjusted to do so with a couple more additions and
subtractions, unless there's some magic I'm not seeing about the year
boundary behavior. Certainly the five-minute-trunc problem could be
done this way.
If you do feel like descending to C, I don't see any fundamental reason
why we accept date_part('week',...) but not date_trunc('week',...).
Feel free to submit a patch.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, Robert_Creager@LogicalChaos.org
Subject: Re: Speed up a function?CREATE TABLE readings ( "when" TIMESTAMP DEFAULT timeofday()::timestamp NOT NULL PRIMARY KEY, "barometer" FLOAT DEFAULT NULL,
In-Reply-To: <938.1077689088@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox