Re: Custom column ordering - Mailing list pgsql-general

From Emre Hasegeli
Subject Re: Custom column ordering
Date
Msg-id CAE2gYzzn1Hsk+s=1B3A6_LOxXdHUjrfs_U6+dJr__7wjXB+kXg@mail.gmail.com
Whole thread Raw
In response to Custom column ordering  (Steven Xu <stevenx@yorku.ca>)
Responses Re: Custom column ordering
List 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.053rows=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.


pgsql-general by date:

Previous
From: Rémi Cura
Date:
Subject: Re: PLPythonu for production server
Next
From: Konstantin Izmailov
Date:
Subject: Re: arrays returned in text format