Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMZph-0000Zu-H7 for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 13:33:53 +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 1eMZpg-0001kw-Pt for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 13:33:52 +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 1eMZpg-0001km-Dr for pgsql-bugs@lists.postgresql.org; Wed, 06 Dec 2017 13:33:52 +0000 Received: from mail-wr0-x231.google.com ([2a00:1450:400c:c0c::231]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eMZpY-0006pH-K4 for pgsql-bugs@postgresql.org; Wed, 06 Dec 2017 13:33:50 +0000 Received: by mail-wr0-x231.google.com with SMTP id v22so3940569wrb.0 for ; Wed, 06 Dec 2017 05:33:43 -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=WRAYtEyu7n24590bH0qjDS4tTyA0ZuSo62v4LJ0vJOY=; b=UNRi1jMIS7hvm2h3i6M5N/t4muLc0AJ60LfVrIVy8YkucvhPMJP5RUQHn8CV4S2SDB kSGDjysByg2eUBeOb/S+uIKCysNsxYpU399Iu4rZ3/h1Jd6nksy+nrQm4S6Petpy3/bY ydw2EKB+ROEkqpDaTOBGOy7oQdNpRZ7yUfnEA5aM3EvAw3oTDmBRbAFYAnjbjfsFn09R mHZcHkP209p5TYKF8CzCyQIc0O3JgVKrBX9sI6z9I7KNjD1A3hnGCmz5GTpUtiJt2YnY bRrJjTVIkO+Ybf9lSSu/AFhdIFd6CtnSfwKI+51kGC0RGZUVMfUNmHoSIYbOxdPbSR+B ItkQ== 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=WRAYtEyu7n24590bH0qjDS4tTyA0ZuSo62v4LJ0vJOY=; b=RXkACDcVWPNSUrTkCtZCbG8Yb/HLShi0nFpYdD6vAxe5uYKQFAl6brCCVdLyXoPl6L wJ+Uz4t/6fgrmgqQJvDUYpOEV3uaaVrIP2+1/ACxV+mSx2g1MQequeOWa0sYIWYX/4TA IBBFhC2HVRV3IHMjstpOCrfqJlkylfIQYRA36dGX0OKVYzVxQd772l5wAkGeUfytzvzC LWPxa9YJFMhyQXS3npHKlaZ3dekVBXkJSIpRdlsYInHWRFgwsevoKn1wL719HRnNrQEr 3xLYZ1RIuTLYjJRnf3Nu0O8PlmPdBaypl+NDCgG1sw1lAFJaQYnMhFBwoECYPCVtg7M4 +ydw== X-Gm-Message-State: AJaThX4heX4SiaomGrmfkNI46IdI+2UM18SEcYylsYuPu44TXOauYqHX LlbHQgC6IXmRABZD2JpxcMgJ78CFo83mlBRlYaoCt3xxkncQABTLtnK0fatGe9mJIYKF2wdffmh myCb6Tk6VFUH5wlAcpPEvH50rbYszQiNj4G+aXOA4qRHjznT3sAXxBTXQ/h/L1BFuoJfeF+GALM qMV0Wg X-Google-Smtp-Source: AGs4zMbUiSkMrcatMQoCNBH6r58iB9QtK3ko8PiXIUCJgqQXmFCXb78wNiwYzIT+42d5mgNaP1Ye1A== X-Received: by 10.223.182.7 with SMTP id f7mr18850749wre.71.1512567220067; Wed, 06 Dec 2017 05:33:40 -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 128sm3127481wmi.28.2017.12.06.05.33.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 05:33:38 -0800 (PST) Subject: Re: BUG #14932: SELECT DISTINCT val FROM table gets stuck in an infinite loop To: "Todd A. Cook" , Tom Lane , Thomas Munro Cc: Andres Freund , 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> <219b37b2-d740-d04a-203a-49ae422028f7@blackducksoftware.com> From: Tomas Vondra Message-ID: Date: Wed, 6 Dec 2017 14:33:36 +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: <219b37b2-d740-d04a-203a-49ae422028f7@blackducksoftware.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/05/2017 11:33 PM, Todd A. Cook wrote: > On 12/05/17 17:01, Tomas Vondra wrote: >> >> >> On 12/05/2017 10:23 PM, Todd A. Cook wrote: >>> On 11/27/17 23:03, Tom Lane wrote: >>>> Thomas Munro writes: >>>>> When SH_INSERT tries to insert that final extra value, insertdist >>>>> keeps exceeding SH_GROW_MAX_DIB (25) no matter how many times we >>>>> double the size (at least until my computer gives up, somewhere around >>>>> 11 doublings and 75GB of virtual memory).  If you set SH_GROW_MAX_DIB >>>>> to 26 then it succeeds, but I guess some other attack could be crafted >>>>> for that.  What is the theory behind this parameter? >>>> >>>> You beat me to it --- after looking at simplehash.h I'd guessed that >>>> either the SH_GROW_MAX_DIB or SH_GROW_MAX_MOVE code path was causing >>>> an infinite loop, but I'd not gotten to determining which one yet. >>>> >>>> I'd ask what's the theory behind SH_GROW_MAX_MOVE, as well.  Neither >>>> of them are obviously loop-proof. >>>> >>>> Note that the sample data has a lot of collisions: >>>> >>>> regression=# select hashint8(val), count(*) from reproducer group by 1 >>>> order by 2 desc; >>>>     hashint8   | count >>>> -------------+------- >>>>      441526644 |  2337 >>>>    -1117776826 |  1221 >>>>    -1202007016 |   935 >>>>    -2068831050 |   620 >>>>     1156644653 |   538 >>>>      553783815 |   510 >>>>      259780770 |   444 >>>>      371047036 |   394 >>>>      915722575 |   359 >>>>    ... etc etc ... >>>> >>>> It's evidently more complicated than just that the code fails with >>>> more than SH_GROW_MAX_DIB duplicate hashcodes, but I suspect not >>>> by a lot.  There needs to be a safety valve that prevents letting >>>> the hash fill factor approach zero, which is what's happening in >>>> this test case. >>> >>> FWIW, I can also reproduce the infinite loop with 167834 unique values. >>> >> >> Unique values or unique *hash* values? > > Unique values. > > >> Can you share the data, so that whoever fixes the bug can verify it also >> fixes your example? > > Sure.  It's attached. > Seems the dataset has pretty much the same issue as the one reported before, that is select hashint8(val), count(distinct val), count(*) from temp_f_03 group by 1 order by 2 desc; hashint8 | count | count -------------+-------+------- -1971396144 | 45 | 45 2035896403 | 42 | 42 -1633843397 | 30 | 30 1425704662 | 29 | 29 -455482779 | 22 | 22 -300163565 | 17 | 17 -1803963420 | 17 | 17 -537082846 | 14 | 14 603707034 | 13 | 13 -176369887 | 12 | 12 1274957136 | 11 | 11 1465522632 | 11 | 11 -1589862230 | 10 | 10 -1145403239 | 10 | 10 i.e. there are many hash collisions (more than in the other data set). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services