Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 03518B5DC09 for ; Thu, 22 Sep 2011 01:24:19 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 53696-03 for ; Thu, 22 Sep 2011 04:24:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 4EB72B5DBD1 for ; Thu, 22 Sep 2011 01:24:07 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p8M4O4D0008756; Thu, 22 Sep 2011 00:24:04 -0400 (EDT) To: Robert Haas cc: Kyotaro HORIGUCHI , pgsql-hackers@postgresql.org Subject: Re: [v9.2] make_greater_string() does not return a string in some cases In-reply-to: References: <20110711.155531.15100243.horiguchi.kyotaro@oss.ntt.co.jp> <20110712.161231.138791411.horiguchi.kyotaro@oss.ntt.co.jp> <20110914.111320.18404009.horiguchi.kyotaro@oss.ntt.co.jp> <6425.1316656167@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Wed, 21 Sep 2011 23:04:02 -0400" Date: Thu, 22 Sep 2011 00:24:04 -0400 Message-ID: <8755.1316665444@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/1108 X-Sequence-Number: 195297 Robert Haas writes: > I'm a bit perplexed as to why we can't find a non-stochastic way of doing this. Because the behavior of common collation algorithms is so wacky as to approach stochasticity. In particular, as soon as your string contains a mix of letter and non-letter characters, "dictionary" algorithms tend to behave really strangely. We went around on this quite a few times before settling on the current approach; we kept thinking exactly what you're thinking, namely that we ought to be able to predict more than nothing about what the collation algorithm would consider larger. An example of the weirdness is that in en_US collation, we have postgres=# select '/root/' < '/root0'; ?column? ---------- t (1 row) postgres=# select '/root/t' < '/root0'; ?column? ---------- f (1 row) It was cases like this that forced us to give up on using non-C-locale indexes for LIKE optimization, because the derived greater-than and less-than conditions have to be 100% correct for that. What we're still using make_greater_string for in non-C locales is to generate estimates about the selectivity of LIKE prefix conditions; for that, some amount of error is tolerable and so we just live with effects like the above. (We have to deal with the locale's sort order, whatever the heck it is, because that's what the pg_statistic histogram is computed in.) Now, having said that, I'm starting to wonder again why it's worth our trouble to fool with encoding-specific incrementers. The exactness of the estimates seems unlikely to be improved very much by doing this. >>> One random idea I have is - instead of generating > and < clauses, >>> could we define a "prefix match" operator - i.e. a ### b iff substr(a, >>> 1, length(b)) = b?  We'd need to do something about the selectivity, >>> but I don't see why that would be a problem. >> The problem is that you'd need to make that a btree-indexable operator. > Well, right. Without that, there's not much point. But do you think > that's prohibitively difficult? The problem is that you'd just be shifting all these same issues into the btree index machinery, which is not any better equipped to cope with them, and would not be a good place to be adding overhead. regards, tom lane