Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b21e665

Browse files
committed
Disable transforms that replaced AT TIME ZONE with RelabelType.
These resulted in wrong answers if the relabeled argument could be matched to an index column, as shown in bug #14504 from Evgeniy Kozlov. We might be able to resurrect these optimizations by adjusting the planner's treatment of RelabelType, or by adjusting btree's rules for selecting comparison functions, but either solution will take careful analysis and does not sound like a fit candidate for backpatching. I left the catalog infrastructure in place and just reduced the transform functions to always-return-NULL. This would be necessary anyway in the back branches, and it doesn't seem important to be more invasive in HEAD. Bug introduced by commit b8a18ad. Back-patch to 9.5 where that came in. Report: https://postgr.es/m/20170118144828.1432.52823@wrigleys.postgresql.org Discussion: https://postgr.es/m/18771.1484759439@sss.pgh.pa.us
1 parent 60a8b63 commit b21e665

File tree

3 files changed

+38
-113
lines changed

3 files changed

+38
-113
lines changed

src/backend/utils/adt/timestamp.c

+10-113
Original file line numberDiff line numberDiff line change
@@ -5165,84 +5165,15 @@ interval_part(PG_FUNCTION_ARGS)
51655165

51665166

51675167
/* timestamp_zone_transform()
5168-
* If the zone argument of a timestamp_zone() or timestamptz_zone() call is a
5169-
* plan-time constant denoting a zone equivalent to UTC, the call will always
5170-
* return its second argument unchanged. Simplify the expression tree
5171-
* accordingly. Civil time zones almost never qualify, because jurisdictions
5172-
* that follow UTC today have not done so continuously.
5168+
* The original optimization here caused problems by relabeling Vars that
5169+
* could be matched to index entries. It might be possible to resurrect it
5170+
* at some point by teaching the planner to be less cavalier with RelabelType
5171+
* nodes, but that will take careful analysis.
51735172
*/
51745173
Datum
51755174
timestamp_zone_transform(PG_FUNCTION_ARGS)
51765175
{
5177-
Node *func_node = (Node *) PG_GETARG_POINTER(0);
5178-
FuncExpr *expr = (FuncExpr *) func_node;
5179-
Node *ret = NULL;
5180-
Node *zone_node;
5181-
5182-
Assert(IsA(expr, FuncExpr));
5183-
Assert(list_length(expr->args) == 2);
5184-
5185-
zone_node = (Node *) linitial(expr->args);
5186-
5187-
if (IsA(zone_node, Const) &&!((Const *) zone_node)->constisnull)
5188-
{
5189-
text *zone = DatumGetTextPP(((Const *) zone_node)->constvalue);
5190-
char tzname[TZ_STRLEN_MAX + 1];
5191-
char *lowzone;
5192-
int type,
5193-
abbrev_offset;
5194-
pg_tz *tzp;
5195-
bool noop = false;
5196-
5197-
/*
5198-
* If the timezone is forever UTC+0, the FuncExpr function call is a
5199-
* no-op for all possible timestamps. This passage mirrors code in
5200-
* timestamp_zone().
5201-
*/
5202-
text_to_cstring_buffer(zone, tzname, sizeof(tzname));
5203-
lowzone = downcase_truncate_identifier(tzname,
5204-
strlen(tzname),
5205-
false);
5206-
type = DecodeTimezoneAbbrev(0, lowzone, &abbrev_offset, &tzp);
5207-
if (type == TZ || type == DTZ)
5208-
noop = (abbrev_offset == 0);
5209-
else if (type == DYNTZ)
5210-
{
5211-
/*
5212-
* An abbreviation of a single-offset timezone ought not to be
5213-
* configured as a DYNTZ, so don't bother checking.
5214-
*/
5215-
}
5216-
else
5217-
{
5218-
long tzname_offset;
5219-
5220-
tzp = pg_tzset(tzname);
5221-
if (tzp && pg_get_timezone_offset(tzp, &tzname_offset))
5222-
noop = (tzname_offset == 0);
5223-
}
5224-
5225-
if (noop)
5226-
{
5227-
Node *timestamp = (Node *) lsecond(expr->args);
5228-
5229-
/* Strip any existing RelabelType node(s) */
5230-
while (timestamp && IsA(timestamp, RelabelType))
5231-
timestamp = (Node *) ((RelabelType *) timestamp)->arg;
5232-
5233-
/*
5234-
* Replace the FuncExpr with its timestamp argument, relabeled as
5235-
* though the function call had computed it.
5236-
*/
5237-
ret = (Node *) makeRelabelType((Expr *) timestamp,
5238-
exprType(func_node),
5239-
exprTypmod(func_node),
5240-
exprCollation(func_node),
5241-
COERCE_EXPLICIT_CAST);
5242-
}
5243-
}
5244-
5245-
PG_RETURN_POINTER(ret);
5176+
PG_RETURN_POINTER(NULL);
52465177
}
52475178

52485179
/* timestamp_zone()
@@ -5339,49 +5270,15 @@ timestamp_zone(PG_FUNCTION_ARGS)
53395270
}
53405271

53415272
/* timestamp_izone_transform()
5342-
* If we deduce at plan time that a particular timestamp_izone() or
5343-
* timestamptz_izone() call can only compute tz=0, the call will always return
5344-
* its second argument unchanged. Simplify the expression tree accordingly.
5273+
* The original optimization here caused problems by relabeling Vars that
5274+
* could be matched to index entries. It might be possible to resurrect it
5275+
* at some point by teaching the planner to be less cavalier with RelabelType
5276+
* nodes, but that will take careful analysis.
53455277
*/
53465278
Datum
53475279
timestamp_izone_transform(PG_FUNCTION_ARGS)
53485280
{
5349-
Node *func_node = (Node *) PG_GETARG_POINTER(0);
5350-
FuncExpr *expr = (FuncExpr *) func_node;
5351-
Node *ret = NULL;
5352-
Node *zone_node;
5353-
5354-
Assert(IsA(expr, FuncExpr));
5355-
Assert(list_length(expr->args) == 2);
5356-
5357-
zone_node = (Node *) linitial(expr->args);
5358-
5359-
if (IsA(zone_node, Const) &&!((Const *) zone_node)->constisnull)
5360-
{
5361-
Interval *zone;
5362-
5363-
zone = DatumGetIntervalP(((Const *) zone_node)->constvalue);
5364-
if (zone->month == 0 && zone->day == 0 && zone->time == 0)
5365-
{
5366-
Node *timestamp = (Node *) lsecond(expr->args);
5367-
5368-
/* Strip any existing RelabelType node(s) */
5369-
while (timestamp && IsA(timestamp, RelabelType))
5370-
timestamp = (Node *) ((RelabelType *) timestamp)->arg;
5371-
5372-
/*
5373-
* Replace the FuncExpr with its timestamp argument, relabeled as
5374-
* though the function call had computed it.
5375-
*/
5376-
ret = (Node *) makeRelabelType((Expr *) timestamp,
5377-
exprType(func_node),
5378-
exprTypmod(func_node),
5379-
exprCollation(func_node),
5380-
COERCE_EXPLICIT_CAST);
5381-
}
5382-
}
5383-
5384-
PG_RETURN_POINTER(ret);
5281+
PG_RETURN_POINTER(NULL);
53855282
}
53865283

53875284
/* timestamp_izone()

src/test/regress/expected/timestamptz.out

+19
Original file line numberDiff line numberDiff line change
@@ -2638,3 +2638,22 @@ select count(distinct utc_offset) >= 24 as ok from pg_timezone_abbrevs;
26382638
t
26392639
(1 row)
26402640

2641+
--
2642+
-- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504)
2643+
--
2644+
create temp table tmptz (f1 timestamptz primary key);
2645+
insert into tmptz values ('2017-01-18 00:00+00');
2646+
explain (costs off)
2647+
select * from tmptz where f1 at time zone 'utc' = '2017-01-18 00:00';
2648+
QUERY PLAN
2649+
-------------------------------------------------------------------------------------------------
2650+
Seq Scan on tmptz
2651+
Filter: (timezone('utc'::text, f1) = 'Wed Jan 18 00:00:00 2017'::timestamp without time zone)
2652+
(2 rows)
2653+
2654+
select * from tmptz where f1 at time zone 'utc' = '2017-01-18 00:00';
2655+
f1
2656+
------------------------------
2657+
Tue Jan 17 16:00:00 2017 PST
2658+
(1 row)
2659+

src/test/regress/sql/timestamptz.sql

+9
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,12 @@ set timezone_abbreviations = 'Australia';
484484
select count(distinct utc_offset) >= 24 as ok from pg_timezone_abbrevs;
485485
set timezone_abbreviations = 'India';
486486
select count(distinct utc_offset) >= 24 as ok from pg_timezone_abbrevs;
487+
488+
--
489+
-- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504)
490+
--
491+
create temp table tmptz (f1 timestamptz primary key);
492+
insert into tmptz values ('2017-01-18 00:00+00');
493+
explain (costs off)
494+
select * from tmptz where f1 at time zone 'utc' = '2017-01-18 00:00';
495+
select * from tmptz where f1 at time zone 'utc' = '2017-01-18 00:00';

0 commit comments

Comments
 (0)