Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMfpP-0003p8-92 for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 19:57:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMfpN-0006WX-Ga for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 19:57:57 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eMfpN-0006WM-7q for pgsql-bugs@lists.postgresql.org; Wed, 06 Dec 2017 19:57:57 +0000 Received: from mail-wm0-x231.google.com ([2a00:1450:400c:c09::231]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eMfpG-0006jU-5e for pgsql-bugs@postgresql.org; Wed, 06 Dec 2017 19:57:55 +0000 Received: by mail-wm0-x231.google.com with SMTP id f206so9106789wmf.5 for ; Wed, 06 Dec 2017 11:57:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=k+HhOmxOmnD7dWCmKfk7+oXoCGxSUqY0oTi/24wb2Fg=; b=RH98wp6EVI20XR7jd1MFszyCS0Espn811j8s4YZmp9ftBcQnZiYL9FYcGbxke0SRbX OTIT68o+4b6FlvN6CXejElG9HLJ8FVRgjEPCJApoKmIF34XtHatPcD8NDUMt5XJLxo7f GlOvfqxF9qAPXMVnbHy4monQzxGK11EdkW+cM4qe4IsqKbGPBXS2WFFAkp8wAEqvCb4Z Fj3bDsvwCpaM7L/m3kIJl4L+qvZdj6JUuu3lAyii+dr3eRjU0aKC72UwH7qV6WcWsb9J p3Nyq3TBdNjfCpbq5S/V6EyGiSRAz+S8n2laIiwjgB5QQmNlYfj1kJRZHGT8jH0ffFgB t+Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=k+HhOmxOmnD7dWCmKfk7+oXoCGxSUqY0oTi/24wb2Fg=; b=WvoWAHhYu6rXuki1XgogWjRiUQ0J04ePiyEy+RN1BPbhMHPKhFpQaWqEC7f1aK6na1 kWWpf/pbSju9gqrB7UGjZZg/eOwQXFT0ApHspjzSfqD0VWDTKiLipo+wMriIZ47lTplS EopIyJS02ARctkzFrVGv0G/ji57fLYjhTZAcz5zhgOmRXrr+feA139ulFcW5hZG5u5pM d1Pa74VFv395q+PNpcTDOb5TYyhZFIVsjXqctY2xB8tzv5NXfIrLARGb7k5FPPluXjmK Igkd94/YWb7bDQYSlmWQ8bc4Z1dGQPdkH1zIW2trYO6Ewev+lDb/hXe+2VHyDE7I0tvx eQnA== X-Gm-Message-State: AKGB3mJi++fEAEjohZRxtxANHp/ia8FNUHubq+7gjlutOLjIpSDBe4B8 iO7INnw3/9yrAeeGFVoCwtJp9IfKSq/Uq6NFQYZdwV49VFYBvlhItHm76RsqLOQ3+BaE5eGJxig LjeVfYttEz677yTbK8kETdfqKDcouyp25OeHghvDzYnIp2I5z9BYl5ibLATh5gwcOE7EQ7lezEh +mCDDc X-Google-Smtp-Source: AGs4zMYS2E/snhnYybegbPPwilWyEXlM2qYDj1dmPqFMPFG+GqFMmGCfmb4R24iIq030Buno5sz9Hg== X-Received: by 10.28.54.3 with SMTP id d3mr15386607wma.79.1512590267744; Wed, 06 Dec 2017 11:57:47 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id o135sm3952354wmg.1.2017.12.06.11.57.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 11:57:47 -0800 (PST) Subject: Re: BUG #14932: SELECT DISTINCT val FROM table gets stuck in an infinite loop To: Andres Freund Cc: "Todd A. Cook" , Tom Lane , Thomas Munro , PostgreSQL Bugs References: <20171127185700.1470.20362@wrigleys.postgresql.org> <7a22f496-92b5-d199-984e-183965918304@blackducksoftware.com> <20171127221041.mezzysc4zcfsqkyc@alap3.anarazel.de> <17357.1511841783@sss.pgh.pa.us> <9cd29b81-7ba1-91a2-f491-9014f4ac87e6@blackducksoftware.com> <2a76784c-06cb-46ec-35be-9895d144b839@2ndquadrant.com> <263b03b1-3e1c-49ca-165a-8ac6751419c4@2ndquadrant.com> <20171206193545.3imvbfwjgrmuho3s@alap3.anarazel.de> From: Tomas Vondra Message-ID: Date: Wed, 6 Dec 2017 20:57:44 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <20171206193545.3imvbfwjgrmuho3s@alap3.anarazel.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/06/2017 08:35 PM, Andres Freund wrote: > On 2017-12-06 20:32:24 +0100, Tomas Vondra wrote: >> On 12/05/2017 11:01 PM, Tomas Vondra wrote: >>> >>> Based on the initial discussion, the problem here is twofold. >>> >>> Firstly, the code assumes that if it gets certain number of bucket >>> collisions (essentially, the initial bucket and certain number of >>> neighboring buckets already being full), making the table larger is >>> guaranteed to reduce the number of collisions. >>> >>> Which is obviously untrue for duplicate hash values, but it may also >>> happen for distinct hash values that form a chain (think a sequence of >>> hash values 1,2,3,4,5,6,7,...,K - that is never gonna get fixed). > >> I've managed to construct (by sheer brute force) an example that breaks >> simplehash in this way. It triggers the growth by having a sequence of >> distinct but consecutive hash values, e.g. 1, 2, 3, 4, ..., 1000, and >> then trying to insert a value with duplicate hash (say, 4). > > I fail to be excited by this. That's possible for any sort of hashtable > in some way. You might hit issues due to resizing or due to lookup > performance, but you'll hit problem. That's a fairly fundamental issue > of pure hashtables. > Eh? I think you misunderstood the issue this triggers. I'm perfectly fine with poor lookup performance - that's expected and reasonable. But it actually triggers the same behavior as the already presented example, i.e. the hash table grows indefinitely (so eventually gets OOM). I really doubt allocating 3GB of memory (which is when my laptop gives up and kills the query) to do distinct on 50MB table is a sane behavior. Even if the table is constructed in so intentionally evil way. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services