Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id A549DB5DC1A for ; Wed, 21 Sep 2011 22:49:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 89186-04 for ; Thu, 22 Sep 2011 01:49:32 +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 AB9C8B5DC64 for ; Wed, 21 Sep 2011 22:49:30 -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 p8M1nR5m006426; Wed, 21 Sep 2011 21:49:27 -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> Comments: In-reply-to Robert Haas message dated "Wed, 21 Sep 2011 20:35:02 -0400" Date: Wed, 21 Sep 2011 21:49:27 -0400 Message-ID: <6425.1316656167@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/1105 X-Sequence-Number: 195294 Robert Haas writes: > As I understand it, the issue here is that when we try to generate > suitable > and < quals for a LIKE expression, we need to find a string > which is greater than the prefix we're searching for, and the existing > algorithm sometimes fails. But there seems to be some dispute over > how likely this is to occur. Tom argues that the case is so rare that > we shouldn't worry about it: > http://archives.postgresql.org/pgsql-bugs/2010-06/msg00336.php Well, I didn't like the amount of overhead that was implied by that proposal. I don't have a great deal of difficulty with the idea of encoding-specific character incrementers, especially since they could presumably be more efficient than the current brute force approach wherein we call pg_verifymbstr on the entire string after mangling just the last byte. The main risk that I foresee with the proposed approach is that if you have, say, a 4-byte final character, you could iterate through a *whole lot* (millions) of larger encoded characters, with absolutely no hope of making a greater string that way when the determining difference occurs at some earlier character. And then when you do run out, you could waste just as much time at the immediately preceding character, etc etc. The existing algorithm is a compromise between thoroughness of investigation of different values of the last character versus speed of falling back to change the preceding character instead. I'd be the first to say that incrementing only the last byte is a very quick-and-dirty heuristic for making that happen. But I think it would be unwise to allow the thing to consider more than a few hundred values for a character position before giving up. Possibly the encoding-specific incrementer could be designed to run through all legal values of the last byte, then start skipping larger and larger ranges --- maybe just move directly to incrementing the first byte. Aside from that issue, the submitted patch seems to need quite a lot of detail work; for instance, I noted an unconstrained memcpy into a 4-byte local buffer, as well as lots and lots of violations of PG house style. That's certainly all fixable but somebody will have to go through it. > 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. regards, tom lane