Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WE8o2-0005vz-Go for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Feb 2014 02:47:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WE8o2-0000V9-19 for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Feb 2014 02:47:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WE8o1-0000V1-0M for pgsql-hackers@postgresql.org; Fri, 14 Feb 2014 02:47:09 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WE8nx-0007Fh-8D for pgsql-hackers@postgresql.org; Fri, 14 Feb 2014 02:47:08 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1WE8nt-0000LY-KD; Thu, 13 Feb 2014 21:47:01 -0500 Date: Thu, 13 Feb 2014 21:47:01 -0500 From: Bruce Momjian To: Thomas Fanghaenel Cc: Kevin Grittner , PostgreSQL-development Subject: Re: [SQL] Comparison semantics of CHAR data type Message-ID: <20140214024701.GA3243@momjian.us> References: <20131011194437.GA3614@momjian.us> <1381525130.59803.YahooMailNeo@web162902.mail.bf1.yahoo.com> <20131016181711.GC18048@momjian.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="BOKacYhQ+x31HxR3" Content-Disposition: inline In-Reply-To: <20131016181711.GC18048@momjian.us> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --BOKacYhQ+x31HxR3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Oct 16, 2013 at 02:17:11PM -0400, Bruce Momjian wrote: > > > You can see the UTF8 case is fine because \n is considered greater > > > than space, but in the C locale, where \n is less than space, the > > > false return value shows the problem with > > > internal_bpchar_pattern_compare() trimming the string and first > > > comparing on lengths. This is exactly the problem you outline, where > > > space trimming assumes everything is less than a space. > > > > For collations other than C some of those issues that have to do with > > string comparisons might simply be hidden, depending on how strcoll() > > handles inputs off different lengths: If strcoll() applies implicit > > space padding to the shorter value, there won't be any visible > > difference in ordering between bpchar and varchar values. If strcoll() > > does not apply such space padding, the right-trimming of bpchar values > > causes very similar issues even in a en_US collation. I have added the attached C comment to explain the problem, and added a TODO item to fix it if we ever break binary upgrading. Does anyone think this warrants a doc mention? -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --BOKacYhQ+x31HxR3 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="char.diff" diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c new file mode 100644 index 502ca44..284b5d1 *** a/src/backend/utils/adt/varchar.c --- b/src/backend/utils/adt/varchar.c *************** bpcharcmp(PG_FUNCTION_ARGS) *** 846,851 **** --- 846,863 ---- len2; int cmp; + /* + * Trimming trailing spaces off of both strings can cause a string + * with a character less than a space to compare greater than a + * space-extended string, e.g. this returns false: + * SELECT E'ab\n'::CHAR(10) < E'ab '::CHAR(10); + * even though '\n' is less than the space if CHAR(10) was + * space-extended. The correct solution would be to trim only + * the longer string to be the same length of the shorter, if + * possible, then do the comparison. However, changing this + * might break existing indexes, breaking binary upgrades. + * For details, see http://www.postgresql.org/message-id/CAK+WP1xdmyswEehMuetNztM4H199Z1w9KWRHVMKzyyFM+hV=zA@mail.gmail.com + */ len1 = bcTruelen(arg1); len2 = bcTruelen(arg2); --BOKacYhQ+x31HxR3 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --BOKacYhQ+x31HxR3--