Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 1771F6340F6 for ; Wed, 13 May 2009 15:50:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 31018-02-5 for ; Wed, 13 May 2009 15:49:57 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from yw-out-2324.google.com (yw-out-2324.google.com [74.125.46.31]) by mail.postgresql.org (Postfix) with ESMTP id B3FC5635910 for ; Wed, 13 May 2009 15:48:46 -0300 (ADT) Received: by yw-out-2324.google.com with SMTP id 3so435641ywj.73 for ; Wed, 13 May 2009 11:48:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.151.69.8 with SMTP id w8mr1726587ybk.16.1242240525070; Wed, 13 May 2009 11:48:45 -0700 (PDT) In-Reply-To: References: <6111.1241879627@sss.pgh.pa.us> Date: Wed, 13 May 2009 14:48:44 -0400 X-Google-Sender-Auth: 28fb38cb8cd77691 Message-ID: Subject: Re: Distinct oddity From: Glenn Maynard To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.174 tagged_above=0 required=5 tests=AWL=0.174 X-Spam-Level: X-Archive-Number: 200905/64 X-Sequence-Number: 32556 For purposes of DISTINCT, I'd expect any sort order should do; all it needs is for equal values to be grouped together. If strcoll() ever fails to do that, I'd call it a critical bug--even throwing total garbage at it should result in a consistent ordering, even if the ordering itself is totally meaningless. Many sort functions depend on this. On Wed, May 13, 2009 at 8:37 AM, Maximilian Tyrtania wrote: > Opened those files (with textwrangler, as I 've never used sed), stripped > off the '$', sorted and looked at the differences (using textwranglers > "compare documents"-feature). Can you narrow down what triggers this? Try copying off the table, and running this: ****** CREATE FUNCTION is_inconsistent() RETURNS BOOLEAN LANGUAGE SQL AS $$ select (select count(distinct(f.bezeichnung)) from firmen_copy f) <> (select count(distinct(f.bezeichnung||'1')) from firmen_copy f) $$; -- If deleting [first,last] leaves the results inconsistent, return true; otherwise -- roll back the deletion and return false. CREATE FUNCTION test_delete_range(first BIGINT, last BIGINT) RETURNS BOOLEAN LANGUAGE plpgsql AS $$ BEGIN DELETE FROM firmen_copy WHERE id BETWEEN first AND last; IF is_inconsistent() THEN RETURN true; END IF; SELECT 1/0; EXCEPTION WHEN division_by_zero THEN RETURN false; END; $$; CREATE FUNCTION test_func() RETURNS INTEGER LANGUAGE plpgsql AS $$ DECLARE total bigint; BEGIN IF NOT is_inconsistent() THEN RETURN -1; END IF; LOOP total := (SELECT MAX(id) FROM firmen_copy); IF test_delete_range(0, total/2) THEN CONTINUE; END IF; IF test_delete_range(total*1/4, total*3/4) THEN CONTINUE; END IF; IF test_delete_range(total/2, total) THEN CONTINUE; END IF; RETURN 0; END LOOP; END; $$; SELECT test_func(); ****** This assumes you have a primary key named "id", and that your IDs start around 0 and are vaguely monotonic (renumber them in the copy if necessary). I can't easily test this code, of course, but it's a simple binary search. Depending on what's triggering this, it may or may not be able to narrow in on a test case. Tangentally, is there a better way of rolling back a function than a dumb hack like "SELECT 1/0"? -- Glenn Maynard