Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ipuRF-0007Z5-1V for pgsql-performance@arkaria.postgresql.org; Fri, 10 Jan 2020 13:34:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ipuRD-00024E-R4 for pgsql-performance@arkaria.postgresql.org; Fri, 10 Jan 2020 13:34:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ipuRD-000247-9u for pgsql-performance@lists.postgresql.org; Fri, 10 Jan 2020 13:34:55 +0000 Received: from mail-yb1-xb43.google.com ([2607:f8b0:4864:20::b43]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ipuRA-0001wv-0n for pgsql-performance@lists.postgresql.org; Fri, 10 Jan 2020 13:34:54 +0000 Received: by mail-yb1-xb43.google.com with SMTP id z15so670586ybm.8 for ; Fri, 10 Jan 2020 05:34:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=vyv1WmXowlxWlW5cZpSJM8mBwfbdPZH8kWdKNn0ondo=; b=lLrJO788r5HAbg8Lv4YxnAWAGyvwLfYaNqhaRq0HeXgUg6NPkHlOEGLEm9580UZX6w +2b+gtMD+cvdw7b1Wtdbsyb6xcpW8kooNkxcP9jVqS+x3O3cpcB7Wpx1ulc59E4yU92u sqKr6XJUIm0hlptfdjBJCPP5HzBEqrn/XOR+Vlfe2l78I+gZ22JOmTIZejJLo3XZa5L1 fiWt37+aSp/06kS9g76TDn5KneFE1ewI7/JTgEGp09utqjWzwGAk5RRTc9lMsQw9nuEt Zc5CwsRyXRI7kWq8shS3+GfTfxQ8EkDo1gU/edD4SFHdHyLtWG6Vktp9VZxAdneayO1a 33Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=vyv1WmXowlxWlW5cZpSJM8mBwfbdPZH8kWdKNn0ondo=; b=N36Li5YVk7URsYFweF8kQLQ602VzvO+GvfPeXNbsS9BT7a+ttZ7vBATSdujdpPOXsD /grYxFjSevNmP84cPpUX+rPt6D1iVTkzW4ZhwLf/QU6sdy61gKZMQgUHTimjmVPiwTZz yfhorGe+yUGV00NgwaOYGM+uKLCxMFypS2nMY5UvpFxgwTLdKSfGlcydqnW1TY1gFZ0s Mt6W3p0Q5pPdGCMfikMUBTdXIKUiKGsJyR7NYDxya5+bcLfkbA0fM2vyjIXFtHcYzrXp 5rwpe9BuUFF+MFDiebkQNlVLhL79r8fpx4ve2pErHU85udaIFDvA5NE+zSb6Nmx8p5I4 WRqA== X-Gm-Message-State: APjAAAXeZSy96vV6NpqipWCzMqyqy5tOeVjVDC1iruyvkOb2OFj/YN/o aSpiS/kaGElMyk5hU8nxORROLg== X-Google-Smtp-Source: APXvYqx55eRXZT6oBF29eWcNB6OgxB1PzsIY8d00wXz8E+CjfepmLPXPXla/sz3Jz7fDLegIjAQFrw== X-Received: by 2002:a25:d6d5:: with SMTP id n204mr2338759ybg.326.1578663291259; Fri, 10 Jan 2020 05:34:51 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id u130sm990248ywc.10.2020.01.10.05.34.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Jan 2020 05:34:48 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 448778009F4; Fri, 10 Jan 2020 07:34:47 -0600 (CST) Date: Fri, 10 Jan 2020 07:34:47 -0600 From: Justin Pryzby To: Marco Colli Cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad query plan when you add many OR conditions Message-ID: <20200110133447.GI12066@telsasoft.com> References: <20200110030601.GH12066@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jan 10, 2020 at 12:03:39PM +0100, Marco Colli wrote: > I have added this index which would allow an index only scan: > "index_subscriptions_on_project_id_and_created_at_and_tags" btree > (project_id, created_at DESC, tags) WHERE trashed_at IS NULL Are those the only columns in subscriptions ? > But Postgresql continues to use this index (which has less information and > then requires slow access to disk): > "index_subscriptions_on_project_id_and_created_at" btree (project_id, > created_at DESC) Did you vacuum the table ? Did you try to "explain" the query after dropping the 1st index (like: begin; DROP INDEX..; explain analyze..; rollback). Also, is the first (other) index btree_gin (you can \dx to show extensions) ? I think it needs to be a gin index to search tags ? On Fri, Jan 10, 2020 at 01:42:24PM +0100, Marco Colli wrote: > I would like to try your solution but I read that ALTER TABLE... SET > STATISTICS locks the table... Since it is just an experiment and we don't > know if it actually works it would be greate to avoid locking a large table > (50M) in production. I suggest to CREATE TABLE test_subscriptions (LIKE subscriptions INCLUDING ALL); INSERT INTO test_subscriptions SELECT * FROM subscriptions; ANALYZE test_subscriptions; Anyway, ALTER..SET STATS requires a strong lock but for only a brief moment (assuming it doesn't have to wait). Possibly you'd be ok doing SET statement_timeout='1s'; ALTER TABLE.... > Does CREATE STATISTICS lock the table too? You can check by SET client_min_messages=debug; SET lock_timeout=333; SET log_lock_waits=on; Looks like it needs ShareUpdateExclusiveLock. > Does statistics work on an array field like tags? (I can't find any > information) It think it'd be data type agnostic. And seems to work with arrays. On Fri, Jan 10, 2020 at 02:30:27PM +0100, Marco Colli wrote: > @Justin Pryzby I have tried this as you suggested: > > CREATE STATISTICS statistics_on_subscriptions_project_id_and_tags ON > project_id, tags FROM subscriptions; > VACUUM ANALYZE subscriptions; > > Unfortunately nothing changes and Postgresql continues to use the wrong > plan (maybe stats don't work well on array fields like tags??). It'd help to see SELECT stxddependencies FROM pg_statistic_ext WHERE stxoid='subscriptions'::regclass