| From: | Emre Hasegeli <emre(at)hasegeli(dot)com> | 
|---|---|
| To: | Steven Xu <stevenx(at)yorku(dot)ca> | 
| Cc: | "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org> | 
| Subject: | Re: Custom column ordering | 
| Date: | 2016-03-05 14:23:09 | 
| Message-ID: | CAE2gYzzn1Hsk+s=1B3A6_LOxXdHUjrfs_U6+dJr__7wjXB+kXg@mail.gmail.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
>   - Why is PostgreSQL not using the functional index I created and why is it
> not being ordered correctly?
Your example works for me:
> hasegeli=# CREATE TABLE device_port (port text);
> CREATE TABLE
>
> hasegeli=# CREATE INDEX idx_device_port_port_proper ON device_port (cast_to_port(port) port_ops DESC);
> CREATE INDEX
>
> hasegeli=# INSERT INTO device_port VALUES ('a'), ('b'), ('c');
> INSERT 0 3
>
> hasegeli=# SELECT port FROM device_port ORDER BY port;
> port
> ------
> c
> b
> a
> (3 rows)
>
> hasegeli=# SET enable_seqscan = 0;
> SET
>
> hasegeli=# EXPLAIN ANALYZE SELECT port FROM device_port ORDER BY cast_to_port(port);
>                                                                        QUERY PLAN
> --------------------------------------------------------------------------------------------------------------------------------------------------------
>  Index Scan Backward using idx_device_port_port_proper on device_port  (cost=0.15..408.55 rows=1360 width=32) (actual time=0.042..0.053 rows=3 loops=1)
>  Planning time: 0.079 ms
>  Execution time: 0.079 ms
> (3 rows)
>   - Is creating a separate data type and using a functional index on the
> casts to this data type the right approach to a custom ordering?
You don't need to create a type for this.  You can just create a
non-default operator class and use it with your text type by specify
the operator with ORDER BY ... USING clause.
> Creating the index:
> CREATE INDEX idx_device_port_port_proper on device_port (cast_to_port(port) port_ops desc);
The operator class is not necessary in here as it is the default for
the "port" type.  DESC also wouldn't make any difference.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Konstantin Izmailov | 2016-03-05 16:29:00 | Re: arrays returned in text format | 
| Previous Message | Rémi Cura | 2016-03-05 10:46:47 | Re: PLPythonu for production server |