Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMh8F-0000E3-Mk for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 21:21:31 +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 1eMh8E-0006q1-Q9 for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Dec 2017 21:21:30 +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 1eMh8E-0006pq-HH for pgsql-bugs@lists.postgresql.org; Wed, 06 Dec 2017 21:21:30 +0000 Received: from mail-wr0-x235.google.com ([2a00:1450:400c:c0c::235]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eMh87-0008Uz-C0 for pgsql-bugs@postgresql.org; Wed, 06 Dec 2017 21:21:29 +0000 Received: by mail-wr0-x235.google.com with SMTP id q9so5351706wre.7 for ; Wed, 06 Dec 2017 13:21:22 -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=0u4u0CR1fWP2ssXYqugWQm+W9eg86pIhKbQIvYWjpdU=; b=Wt/zh4B1YhmEPBZsiadIGTafw8G79kyNflLCCffVImHHDoi3CP/e53P72BJ37Ouout vy7q4nD+Z7PpNA3+fJP0eQNPHN+QkSGQsqPSrPbabgQruMRhpaLLLYPaXvajw+/qDbkH Prg/l3MP+/poa7CWFD6VjO+2aUkyZ5Em3mL/gStSVz35JPOaskP8TGxwkqTggPPJL8oN kOWWmzy4xBDyhcP6ODrzztY4gjrUXmL++yxSBWMcSWzsdH+TKc1zXYg0hyiyN7X4fYJ9 Ftidqs3Gz5oUEj6pd1q4eOD29kguJasNUYUYYd8KnABCfLWFKNgd7un/tBCBnMrt7Gi1 Q6tQ== 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=0u4u0CR1fWP2ssXYqugWQm+W9eg86pIhKbQIvYWjpdU=; b=cWO7r8WE2D5+yL7+FReB1ejQX3ZEmQVxZ5/Vz/e3FgZISskHDzHxc61C5MTjTyXJ3t 8vPwrL9zqrmToZ7zgFRBn9jTe3UnBUGnzE2LitxtMwvhAL9yLbqZ5JnanGDR3nxhTYJC /vwLY9PkHRFRxb+pYLVcS4/Q0l/V553ObjQNLwyD1jHAF1qO289sCBoetALD1f5z6l8T n6bSouqeqz30EunVadLW+YdgWBfpG0kpaQwgqnmqi3wjSWBc6r0bZotehdv0LdzOxsee m925ksCLdV7kiOKjBVMkC760AYmYkaTLuB1cZC/ZQslGhIxErRXB1PLB4e46CeWKjzsy EuBw== X-Gm-Message-State: AJaThX6L0lmFYdjEhNjJMhNtaAuPd7We1u1lMJZsDqxZYusdpVPyGmQe KjcVbvaJu0klJKBUuKhNxjCqXr2JdSWygToN+Q+1AYAyooULwCCHFJn4TA8bYllmK9Cw1rTGR9I UhLtAt+hm9hoXbMvBOgCv/wAyy0e8hINuxwqTC5rwvoyoFVdyk8jyYbhU6hSKIB/E2kfAUwDkyA xsX9Oe X-Google-Smtp-Source: AGs4zMa56Bdh1xDEFzRieOALTQxWGGy3mUVRyYn3sr3vzwUD3Y4aS3vqAghK35Fbk+KnKxQsyIL9uA== X-Received: by 10.223.199.82 with SMTP id b18mr20036966wrh.78.1512595281044; Wed, 06 Dec 2017 13:21:21 -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 d9sm3732299wrf.45.2017.12.06.13.21.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 13:21:20 -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: <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> <790BC80E-D8E5-499B-A1E3-8EE0752B53ED@anarazel.de> <06523583-b9c5-1424-ef0a-1fc8063e9ed7@2ndquadrant.com> <20171206204616.wcect35fuj5gjde2@alap3.anarazel.de> <67e5af9b-f509-7b9e-f7a1-e01112f6c18d@2ndquadrant.com> <20171206211225.zcp2crfr3bssn3xr@alap3.anarazel.de> From: Tomas Vondra Message-ID: <0c3d7ca9-1619-8b78-0a33-277a08db335a@2ndquadrant.com> Date: Wed, 6 Dec 2017 22:21:17 +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: <20171206211225.zcp2crfr3bssn3xr@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 10:12 PM, Andres Freund wrote: > On 2017-12-06 22:05:22 +0100, Tomas Vondra wrote: >> On 12/06/2017 09:46 PM, Andres Freund wrote: >>> On 2017-12-06 21:38:42 +0100, Tomas Vondra wrote: >>>> It's one thing when the hash table takes longer to lookup something or >>> >>> longer aka "forever". >>> >> >> Not necessarily. > >> The datasets I shared are somewhat extreme in the sense that there are >> many contiguous sequences of hash values, but it only takes one such >> sequence with at least SH_GROW_MAX_MOVE values to trigger the issue. So >> the hash table may still be perfectly fine for most keys, and only >> slightly slower for the keys in the sequence. > > Meh, we're talking about adversarial attacks here. > > >>>> when it consumes a bit more memory. Say, ~2x more than needed, give or >>>> take. I'm perfectly fine with that, particularly when it's a worst-case >>>> evil data set like this one. >>> >>> I think the way to prevent that kind of attack is to add randomization. >>> >> >> By randomization you mean universal hashing [1], or something else? > > No, adding in a random seed to the hash. It'd be a lot better if we > had a way to provide internal state to the hashfunction, but even > just using hash_combine()ing a random number into a hash would be a > lot better than what we're doing now. > The universal hashing (of "our" hash value) would have the benefit that we could change it when growing the hash table. I.e. do this h(x) = ((A * hash_any(x) + B) mod P)) mod M and generate new A, B, P whenever we grow the hashtable. That would further reduce the probability of either of the failures described in this thread (I mean, hitting it again after growing the table). I don't know if it's worth the additional CPU cycles, of course. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services