Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMfQp-0002Lu-6X for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 19:32:35 +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 1eMfQo-0000ev-Mq for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 19:32:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eMfQo-0000el-E5 for pgsql-bugs@lists.postgresql.org; Wed, 06 Dec 2017 19:32:34 +0000 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eMfQk-0005yq-1E for pgsql-bugs@postgresql.org; Wed, 06 Dec 2017 19:32:33 +0000 Received: by mail-wm0-x22b.google.com with SMTP id f206so8999826wmf.5 for ; Wed, 06 Dec 2017 11:32:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=Z83EWayqTj10wqs5fy9uLIZFpHc0qJ4Dga5FDDiy/7k=; b=vlazLRQFb/Chf+XsRVmhSeVl6CmM0VcEW0Vokco4ZkrOJ9XJgF4Wlte5UAv1dVMQOS lU9DPLZHc7hP7RbBi/4tejBmPqzsYIGCCTg8uwvInplCge/VkqBBvts/+rId/HCOXMgy 269oJsYUy2805t4KMKDWlX4l5bMLx3HNcAlYv+5rddOHvzVwoX5YZE9CIfrVS8s7kpdt cEdY3ApLeGIdrO4bTYbYiFO50D556PqkWBoxyoHeyb4tkJ4PE9PP9AWHAjLIbLMonAHY YSwfxReCQaHw8VWiUGMCGsZjIGaFo+wnD4HxJDUcx1Jcq5bhzoUdbR/VXS14eTpDtPyn wFoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Z83EWayqTj10wqs5fy9uLIZFpHc0qJ4Dga5FDDiy/7k=; b=BsmovkjQbeGgaiToBNp2zaBvSxzhJAAqMI8N+FTii3tquGIKsuos+3ohmrGx7LmRNW saqjCh7+D1pZ5+ecUIi0x5385bnrAfsou4JU6jom4lrT1QLf9+1fINUDsS8y4m/v2UqW UM/0zC6ygxa4be6o3R5e5OOysqpi1IRvgLXd+RIQHTjbGplxp4G/K+VCMCvCoQ/FQaW8 icYBYnCassfxkvUvVhRcIUhlj6HAQqMPRPDiTkrjae+KUxivqkNVF+2ZlZkB7/d+1CZp IZoZFo3ZH2gUycaeDQGFtkI4RD8tCUm/wxb57JsArca6YoLuVRu3XyhEYmYyPkvmbUAx UD1g== X-Gm-Message-State: AKGB3mKQFKotXJVhjIVccXmBgAETotC/321z5jFkWbqZpaab7aC7Oqlc lI3q3cZd0469dlbgKzVvzZvNi44oUQTi3UwGs0LwDksZWbggncPrWODU0mdNNnl0UMOcEFnRdVd dZ+KF2wfRcMgVV9sA8iIgy5TJsRX3Mr2vGmvy3AQSDJbbgLkA58zj/yP5aw1WVxcBP0BQh5rcXH Ev2oxy X-Google-Smtp-Source: AGs4zMZ/vog9CjWnycSA4knes5kGMvYwopB19lVg8Rm4yd8OslCtTO+/uXVRwV3jmxFEmOT5+h1ibg== X-Received: by 10.28.157.7 with SMTP id g7mr8726339wme.89.1512588748206; Wed, 06 Dec 2017 11:32:28 -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 p200sm3879335wmd.9.2017.12.06.11.32.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 11:32:27 -0800 (PST) Subject: Re: BUG #14932: SELECT DISTINCT val FROM table gets stuck in an infinite loop From: Tomas Vondra 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> Message-ID: <263b03b1-3e1c-49ca-165a-8ac6751419c4@2ndquadrant.com> Date: Wed, 6 Dec 2017 20:32:24 +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: <2a76784c-06cb-46ec-35be-9895d144b839@2ndquadrant.com> 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/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). Which will trigger the "emptydist" condition (as opposed to "insertdist", as in the other example). This means there are very few collisions (no hash value matching more than 2 distinct values), and all the hash values are "at the beginning of the range" so growing the hash table can't possible break the hash sequences. The generated datasets are quite large (16MB each), so I've placed them here (along with the ugly C code to generate it): https://github.com/tvondra/simplehash-break Each CSV file contains ~1M of strings with distinct hashes between 0 and 1048576 (essentially, covering about 97.5% of the range). There are "chains" of up to ~300 consecutive hash values. This works: CREATE TABLE hash_text (val text); COPY hash_text FROM '/tmp/data1.csv'; SET work_mem = '1GB'; SELECT DISTINCT val FROM hash_text; but this does not: CREATE TABLE hash_text (val text); COPY hash_text FROM '/tmp/data1.csv'; COPY hash_text FROM '/tmp/data2.csv'; SET work_mem = '1GB'; SELECT DISTINCT val FROM hash_text; In fact, only a small subset of the second data set should be needed. FWIW I first tried to do the same thing with bigint values, but no matter what I did I've been unable to generate a dataset covering more than ~60% of the range (essentially hashint8 only ever produces 60% of values from [0,1000000], which likely increases collision rate). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services