<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
tt
        {mso-style-priority:99;
        font-family:"Courier New";}
span.EmailStyle19
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Or I forgot to mention, I think the old hack of OFFSET like you have in 3 still works and for backward compatibility would be preferable over using the MATERIALIZED keyword, but less clear in intent<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>So something<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p><tt><span style='font-size:10.0pt'>WITH foo AS (SELECT ST_GeometryType(geom) gtype, geom FROM tmp_test_geoms WHERE ST_GeometryType(geom) = 'ST_Point' OFFSET 0) SELECT * FROM foo WHERE ST_X(geom) > 1;</span></tt><o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'> Regina Obe [mailto:lr@pcorp.us] <br><b>Sent:</b> Saturday, September 5, 2020 2:12 PM<br><b>To:</b> 'PostGIS Users Discussion' <postgis-users@lists.osgeo.org><br><b>Subject:</b> RE: [postgis-users] Bizarre behavior with postgis function as part of where clause!<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Have you tried adding a MATERIALIZED to your WITH?  In PostgreSQL 12 and above, CTEs are no longer always materialized.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>e.g.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><tt><span style='font-size:10.0pt'>WITH foo AS MATERIALIZED (SELECT ST_GeometryType(geom) gtype, geom FROM tmp_test_geoms WHERE ST_GeometryType(geom) = 'ST_Point') SELECT * FROM foo WHERE ST_X(geom) > 1;<o:p></o:p></span></tt></p><p class=MsoNormal><tt><span style='font-size:10.0pt'><o:p> </o:p></span></tt></p><p class=MsoNormal><tt><span style='font-size:10.0pt'>This article describes it a bit<o:p></o:p></span></tt></p><p class=MsoNormal><tt><span style='font-size:10.0pt'><o:p> </o:p></span></tt></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><a href="https://paquier.xyz/postgresql-2/postgres-12-with-materialize/">https://paquier.xyz/postgresql-2/postgres-12-with-materialize/</a><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'> postgis-users [<a href="mailto:postgis-users-bounces@lists.osgeo.org">mailto:postgis-users-bounces@lists.osgeo.org</a>] <b>On Behalf Of </b>Bo Guo<br><b>Sent:</b> Friday, September 4, 2020 11:28 PM<br><b>To:</b> <a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br><b>Subject:</b> [postgis-users] Bizarre behavior with postgis function as part of where clause!<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p>Hi List,<o:p></o:p></p><p>I just ran into a pretty bizarre query behavior involving simple queries involving PostGIS functions as part of where clause condition.  <o:p></o:p></p><p>I have two geometries in table tmp_test_geoms , one point and a line: <o:p></o:p></p><p class=MsoNormal><tt><span style='font-size:10.0pt'>    ST_Point    "0101000000806FE29BC53B2241800FDDE3E9C53341"</span></tt><span style='font-size:10.0pt;font-family:"Courier New"'><br><tt>    ST_LineString    "010200000005000000E072B95C215A22412033C40C65963341401C0E074D5A22412097CB056E963341809821E6235E22412033C48C6D963341A027E909BA5F22410087C3E16E963341C0B2AEACD95F224100FCFDFE71963341"</tt></span><o:p></o:p></p><p>Both Query 1 and Query 2 fails w/ message "Argument to ST_X() must have type POINT," Query 3 works however<o:p></o:p></p><p><tt><span style='font-size:10.0pt'>    1. SELECT ST_GeometryType(geom) gtype, geom FROM tmp_test_geoms WHERE ST_GeometryType(geom) = 'ST_Point' AND ST_X(geom) > 1;</span></tt><span style='font-size:10.0pt;font-family:"Courier New"'><br><br><tt>    2. WITH foo AS (SELECT ST_GeometryType(geom) gtype, geom FROM tmp_test_geoms WHERE ST_GeometryType(geom) = 'ST_Point') SELECT * FROM foo WHERE ST_X(geom) > 1;</tt><br><tt>    3. WITH foo AS (SELECT ST_GeometryType(geom) gtype, geom FROM tmp_test_geoms WHERE ST_GeometryType(geom) = 'ST_Point' LIMIT 10) SELECT * FROM foo WHERE ST_X(geom) > 1;</tt></span><o:p></o:p></p><p>It seems that the query engine evaluates ST_X first before applying the GeometryType filter, even when the filter is in a CTE.  <o:p></o:p></p><p>It further seems that by adding LIMIT with an arbitrary value at the end of the CTE, the filter is applied first!<o:p></o:p></p><p>Wonder if there is a way to give some type of query hint to force the evaluation sequences.<o:p></o:p></p><p>Thanks for any suggestions!<o:p></o:p></p><p>Bo<o:p></o:p></p></div></div></div></body></html>