Le Droid
Le Droid

Reputation: 4774

Postgresql 9.2 timestamp sorting on indexed field doesn't sort on the hours part within a day

I can't believe what I see with a simple select with an ORDER BY clause. Here is my query and the faulty result:

SELECT date_valid, id_variable FROM myTable
   WHERE id_stn='78224' AND date_valid BETWEEN '2014-07-03 09:00:00'
         AND '2014-07-03 21:00:00' AND id_variable IN (11012,12004)
   ORDER BY date_valid ;

     date_valid      | id_variable 
---------------------+-------------
 2014-07-03 09:00:00 |       11012
 2014-07-03 15:00:00 |       11012
 2014-07-03 21:00:00 |       11012
 2014-07-03 09:00:00 |       12004
 2014-07-03 15:00:00 |       12004
 2014-07-03 21:00:00 |       12004

As you see, the sorting seems to be done on id_variable instead of date_valid. To get the expected result, I have to create a new field that Postgresql can't optimize or to give a timestamp range of more than 1 day:

SELECT date_valid,id_variable FROM myTable
       WHERE id_stn='78224' AND date_valid BETWEEN '2014-07-03 09:00:00'
             AND '2014-07-03 21:00:00' AND id_variable IN (11012,12004)
       ORDER BY date_valid + '0 hours'::INTERVAL;

     date_valid      | id_variable
---------------------+-------------
 2014-07-03 09:00:00 |       11012
 2014-07-03 09:00:00 |       12004
 2014-07-03 15:00:00 |       11012
 2014-07-03 15:00:00 |       12004
 2014-07-03 21:00:00 |       11012
 2014-07-03 21:00:00 |       12004

Here is a partial table definition, it's partitionned on date_valid for each month:

    Column     |            Type
---------------+-----------------------------
 id_obs        | bigint                      
 date_valid    | timestamp without time zone
 id_variable   | integer
 id_stn        | character varying(50)
Indexes:
    "myTable_pkey" PRIMARY KEY, btree (id_obs)
    "myTable_ukey" UNIQUE CONSTRAINT, btree (date_valid, id_variable, lat, lon)
Check constraints:
    "myTable_date_valid_check" CHECK (date_valid >= '2014-07-01 00:00:00'::timestamp without time zone AND date_valid < '2014-08-01 00:00:00'::timestamp without time zone)
Triggers:
    myTable_before_update BEFORE UPDATE ON myTable_201407 FOR EACH ROW EXECUTE PROCEDURE obs_update()
Inherits: myTable_parent
Has OIDs: no

It seems like a bug that Postgresql doesn't sort on hours if the result is on the same day. It must be an optimizer issue as I don't have this problem if I sort on another timestamp field which is not indexed. The result is the same (unsorted) if I specify ::TIMESTAMP after each date string, or if I enclose the select in another on with: SELECT * FROM (SELECT ...) x ORDER BY DATE_VALID. I have the same problem with other tables with similar structure.

This is the EXPLAIN result with Postgresql 9.2.8:

 Result  (cost=0.02..62864.86 rows=10 width=87)
   ->  Merge Append  (cost=0.02..62864.86 rows=10 width=87)
         Sort Key: myTable.date_valid
         ->  Sort  (cost=0.01..0.02 rows=1 width=220)
               Sort Key: myTable.date_valid
               ->  Seq Scan on myTable  (cost=0.00..0.00 rows=1 width=220)
                     Filter: ((date_valid >= '2014-07-03 09:00:00'::timestamp without time zone) AND (date_valid <= '2014-07-03 21:00:00'::timestamp without time zone) AND (id_variable = ANY ('{11012,12004}'::integer[])) AND ((id
_stn)::text = '78224'::text))
         ->  Index Scan using myTable_201407_ukey on myTable_201407 myTable  (cost=0.00..62864.71 rows=9 width=72)
               Index Cond: ((date_valid >= '2014-07-03 09:00:00'::timestamp without time zone) AND (date_valid <= '2014-07-03 21:00:00'::timestamp without time zone) AND (id_variable = ANY ('{11012,12004}'::integer[])))
               Filter: ((id_stn)::text = '78224'::text)

Upvotes: 1

Views: 204

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125214

Probably this error fixed in 9.2.1

Fix possible incorrect sorting of output from queries involving WHERE indexed_column IN (list_of_values)

http://www.postgresql.org/docs/9.2/static/release-9-2-1.html

9.2 is already at 9.2.8

Upvotes: 1

Related Questions