Per-query local timezone

Started by Mark Morgan Lloydalmost 15 years ago8 messagesgeneral
Jump to latest
#1Mark Morgan Lloyd
markMLl.pgsql-general@telemetry.co.uk

Is it possible to incorporate SET TIMEZONE into a query, so that
to_char(...'TZ') etc. is appropriately localised?

The development environment I'm working with uses short-lifetime
sessions, and it's proving difficult to get a set command and a query
associated with the same handle.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

#2Karsten Hilbert
Karsten.Hilbert@gmx.net
In reply to: Mark Morgan Lloyd (#1)
Re: Per-query local timezone

On Tue, Jun 14, 2011 at 09:40:20AM +0000, Mark Morgan Lloyd wrote:

Is it possible to incorporate SET TIMEZONE into a query, so that
to_char(...'TZ') etc. is appropriately localised?

You seem to want "AT TIME ZONE".

Karsten
--
GPG key ID E4071346 @ gpg-keyserver.de
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

#3John R Pierce
pierce@hogranch.com
In reply to: Mark Morgan Lloyd (#1)
Re: Per-query local timezone

On 06/14/11 2:40 AM, Mark Morgan Lloyd wrote:

The development environment I'm working with uses short-lifetime
sessions, and it's proving difficult to get a set command and a query
associated with the same handle.

this environment doesn't support even a transaction?

--
john r pierce N 37, W 122
santa cruz ca mid-left coast

#4Mark Morgan Lloyd
markMLl.pgsql-general@telemetry.co.uk
In reply to: Karsten Hilbert (#2)
Re: Per-query local timezone

Karsten Hilbert wrote:

On Tue, Jun 14, 2011 at 09:40:20AM +0000, Mark Morgan Lloyd wrote:

Is it possible to incorporate SET TIMEZONE into a query, so that
to_char(...'TZ') etc. is appropriately localised?

You seem to want "AT TIME ZONE".

Thanks for that. How can I do /this/

select to_char(now() at time zone 'GMT0BST', 'TZ');

It appears to return '', while if I used a separate SET TIMEZONE I'd
expect 'BST'.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: John R Pierce (#3)
Re: Per-query local timezone

John R Pierce <pierce@hogranch.com> writes:

On 06/14/11 2:40 AM, Mark Morgan Lloyd wrote:

The development environment I'm working with uses short-lifetime
sessions, and it's proving difficult to get a set command and a query
associated with the same handle.

this environment doesn't support even a transaction?

Sounds kinda broken :-( ... but maybe Mark could wrap the operations
he needs into custom functions.

regards, tom lane

#6Steve Crawford
scrawford@pinpointresearch.com
In reply to: Mark Morgan Lloyd (#4)
Re: Per-query local timezone

On 06/14/2011 05:13 AM, Mark Morgan Lloyd wrote:

Karsten Hilbert wrote:

On Tue, Jun 14, 2011 at 09:40:20AM +0000, Mark Morgan Lloyd wrote:

Is it possible to incorporate SET TIMEZONE into a query, so that
to_char(...'TZ') etc. is appropriately localised?

You seem to want "AT TIME ZONE".

Thanks for that. How can I do /this/

select to_char(now() at time zone 'GMT0BST', 'TZ');

It appears to return '', while if I used a separate SET TIMEZONE I'd
expect 'BST'.

The "now()" function returns a timestamp with time zone (aka a point in
time). When you ask for a timestamp with time zone at a specific time
zone, you get a timestamp *without* time zone (you provided and
therefore know the desired time zone and PostgreSQL returned the
timestamp in that zone).

I'm a bit concerned with your initial statement that "The development
environment I'm working with uses short-lifetime sessions, and it's
proving difficult to get a set command and a query associated with the
same handle.". Do I take this to mean that connections are going through
some sort of pooler that is allocating connections on as short as a
per-statement basis so you might end up with a different connection
between the "set time zone.." statement and the query? If so, you may
start to find all sorts of other issues.

It's a bit convoluted, but you could get the zone from a subquery and
select the timestamp converted to that zone along with the zone itself
from the outer query:

select now() at time zone foo.tz, foo.tz from (select 'est5edt'::text as
tz) as foo;

Cheers,
Steve

#7Mark Morgan Lloyd
markMLl.pgsql-general@telemetry.co.uk
In reply to: Tom Lane (#5)
Re: Per-query local timezone

Tom Lane wrote:

John R Pierce <pierce@hogranch.com> writes:

On 06/14/11 2:40 AM, Mark Morgan Lloyd wrote:

The development environment I'm working with uses short-lifetime
sessions, and it's proving difficult to get a set command and a query
associated with the same handle.

this environment doesn't support even a transaction?

Sounds kinda broken :-( ... but maybe Mark could wrap the operations
he needs into custom functions.

Is always a possibility. The problem is that particular component I'm
using conflates the open and issue-query operations and has an implicit
transaction, the developers are aware that this has undesirable
implications.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

#8Mark Morgan Lloyd
markMLl.pgsql-general@telemetry.co.uk
In reply to: Steve Crawford (#6)
Re: Per-query local timezone

Steve Crawford wrote:

On 06/14/2011 05:13 AM, Mark Morgan Lloyd wrote:

Karsten Hilbert wrote:

On Tue, Jun 14, 2011 at 09:40:20AM +0000, Mark Morgan Lloyd wrote:

Is it possible to incorporate SET TIMEZONE into a query, so that
to_char(...'TZ') etc. is appropriately localised?

You seem to want "AT TIME ZONE".

Thanks for that. How can I do /this/

select to_char(now() at time zone 'GMT0BST', 'TZ');

It appears to return '', while if I used a separate SET TIMEZONE I'd
expect 'BST'.

The "now()" function returns a timestamp with time zone (aka a point in
time). When you ask for a timestamp with time zone at a specific time
zone, you get a timestamp *without* time zone (you provided and
therefore know the desired time zone and PostgreSQL returned the
timestamp in that zone).

I'm a bit concerned with your initial statement that "The development
environment I'm working with uses short-lifetime sessions, and it's
proving difficult to get a set command and a query associated with the
same handle.". Do I take this to mean that connections are going through
some sort of pooler that is allocating connections on as short as a
per-statement basis so you might end up with a different connection
between the "set time zone.." statement and the query? If so, you may
start to find all sorts of other issues.

It's a bit convoluted, but you could get the zone from a subquery and
select the timestamp converted to that zone along with the zone itself
from the outer query:

select now() at time zone foo.tz, foo.tz from (select 'est5edt'::text as
tz) as foo;

Looking back through the mailing list, the issue appears to be the way
that AT TIME ZONE is parsed into a function which returns a string. I
think the easiest way round most of this is going to be to use the PGTZ
shell variable, otherwise I think I can pull the info I need out of
pg_timezone_names subject to using the correct zone name.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]