Re: Custom column ordering

From: Steven Xu <stevenx(at)yorku(dot)ca>
To: emre(at)hasegeli(dot)com
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Custom column ordering
Date: 2016-03-07 15:30:49
Message-ID: OF378447E6.EE34BDEC-ON85257F6F.00553806-85257F6F.00553808@yorku.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<font face="Default Sans Serif,Verdana,Arial,Helvetica,sans-serif" size="2">Hi Emre,<br><br>Thanks for your tips! I think I'll do as you suggest and keep it simple with a single ordering operator.<br><br>Although, I do find your output very strange. You wrote ORDER BY port, which is a text type. Why does Postgres order using the ordering operators of the "Port" data type rather than the "text" type, even though you haven't performed a cast?<br><br>Steven<br><br><font color="#990099">-----Emre Hasegeli &lt;emre(at)hasegeli(dot)com&gt; wrote: -----</font><div style="padding-left:5px;"><div style="padding-right:0px;padding-left:5px;border-left:solid black 2px;">To: Steven Xu &lt;stevenx(at)yorku(dot)ca&gt;<br>From: Emre Hasegeli &lt;emre(at)hasegeli(dot)com&gt;<br>Date: 03/05/2016 09:30AM<br>Cc: "pgsql-general(at)postgresql(dot)org" &lt;pgsql-general(at)postgresql(dot)org&gt;<br>Subject: Re: [GENERAL] Custom column ordering<br><br><div><font face="Courier New,Courier,monospace" size="2">&gt; &nbsp; - Why is PostgreSQL not using the functional index I created and why is it<br>&gt; not being ordered correctly?<br><br>Your example works for me:<br><br>&gt; hasegeli=# CREATE TABLE device_port (port text);<br>&gt; CREATE TABLE<br>&gt;<br>&gt; hasegeli=# CREATE INDEX idx_device_port_port_proper ON device_port (cast_to_port(port) port_ops DESC);<br>&gt; CREATE INDEX<br>&gt;<br>&gt; hasegeli=# INSERT INTO device_port VALUES ('a'), ('b'), ('c');<br>&gt; INSERT 0 3<br>&gt;<br>&gt; hasegeli=# SELECT port FROM device_port ORDER BY port;<br>&gt; port<br>&gt; ------<br>&gt; c<br>&gt; b<br>&gt; a<br>&gt; (3 rows)<br>&gt;<br>&gt; hasegeli=# SET enable_seqscan = 0;<br>&gt; SET<br>&gt;<br>&gt; hasegeli=# EXPLAIN ANALYZE SELECT port FROM device_port ORDER BY cast_to_port(port);<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QUERY PLAN<br>&gt; --------------------------------------------------------------------------------------------------------------------------------------------------------<br>&gt; &nbsp;Index Scan Backward using idx_device_port_port_proper on device_port &nbsp;(cost=0.15..408.55 rows=1360 width=32) (actual time=0.042..0.053 rows=3 loops=1)<br>&gt; &nbsp;Planning time: 0.079 ms<br>&gt; &nbsp;Execution time: 0.079 ms<br>&gt; (3 rows)<br><br>&gt; &nbsp; - Is creating a separate data type and using a functional index on the<br>&gt; casts to this data type the right approach to a custom ordering?<br><br>You don't need to create a type for this. &nbsp;You can just create a<br>non-default operator class and use it with your text type by specify<br>the operator with ORDER BY ... USING clause.<br><br>&gt; Creating the index:<br>&gt; CREATE INDEX idx_device_port_port_proper on device_port (cast_to_port(port) port_ops desc);<br><br>The operator class is not necessary in here as it is the default for<br>the "port" type. &nbsp;DESC also wouldn't make any difference.<br></font></div></div></div><div></div></font>

Attachment Content-Type Size
unknown_filename text/html 3.1 KB

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2016-03-07 15:35:29 Re: Re: "missing chunk number XX for toast value YY in pg_toast ..." after pg_basebackup.
Previous Message Tom Lane 2016-03-07 14:51:45 Re: index problems (again)