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 1j2wx1-0001hm-Pw for pgsql-performance@arkaria.postgresql.org; Sat, 15 Feb 2020 12:53:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j2wx0-0004OL-DI for pgsql-performance@arkaria.postgresql.org; Sat, 15 Feb 2020 12:53:38 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1j2wx0-0004Kc-0L for pgsql-performance@lists.postgresql.org; Sat, 15 Feb 2020 12:53:38 +0000 Received: from mail-yb1-xb41.google.com ([2607:f8b0:4864:20::b41]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j2www-0004aP-SW for pgsql-performance@lists.postgresql.org; Sat, 15 Feb 2020 12:53:37 +0000 Received: by mail-yb1-xb41.google.com with SMTP id p123so6242951ybp.2 for ; Sat, 15 Feb 2020 04:53:34 -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=ELgQm9snnkViuXj4QuFfxqxdhO8kutLX7CSHXlGRulk=; b=YreZ29uyiURX2xCRDQU7acOg5ifEJ4FAD0M7WN4HBsM4FfDJk4oy1vIQJDgpO8gCd6 QERbM9OFqv6fHswhCZT8MYYNiUYO28MTMTpZTWFPRVnb0/+1nriaPH19VxRBnYdZwW3X 9qSJgWM3WCsq50w++bs42n9hurgxJUfKVJcuNf9l9Ixuuv0niekSc1GN1uLUmXbj92dz k41x7IVkRUtdROf8d8e/J5KCXL+sXsevpVfgG/4xVtkKuH/1mNWMQuXFPBgxMYZYce3+ pnXB7tOrzHbxGzOl2cUepHxDQxue8X8Nr3uTJICv5QQN2fewvOsFbuIkE4i8mGeDXm6y m6Kg== 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=ELgQm9snnkViuXj4QuFfxqxdhO8kutLX7CSHXlGRulk=; b=LG8bpGird1wNESv8408nGf7yT6xWz5Cjx+YCKetMCvLpgaXnJrak2Fk6K/oSDg+Q/k dgb5sSmySXQjGc8k2E4TC97Z86hhL7mcY05+xAjkrCZqWx7q40VxWIX5Ks/OrkWtHuAv 3s0WOZ9lhRg277baZcFUUDnh0N4wBMy3lmTaAxap01n/piNVjjcEkJKVecqHvC7uYZDk maDZP+Vb3c/fxpxA0AyKsSrafTE2PJFeF+RHronzGTiK5/X+om+tji4cIXlcTItXBThw EwKWh6XgoqY/BU4hoUjZOuk6GQJ1/K45f0R7pOrONeksM7ak7L5K/tlTULwNRHDDG+aI nIVA== X-Gm-Message-State: APjAAAVqhqbytK8NVGJ1hNv5eJBsXbqE2TsUT7DnvHua5qvytOXrToql Ga7gfgEzra3JAb1HsR57HcI6ag== X-Google-Smtp-Source: APXvYqyUWX40A88BRHXXh8t+Ph5MgeYelVsnhmmq2UBwxZcBg4ZrT2tR1e6xu2YpX+j7ayFOmY28bQ== X-Received: by 2002:a25:2f84:: with SMTP id v126mr7485157ybv.67.1581771212805; Sat, 15 Feb 2020 04:53:32 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id t5sm3784710ywc.3.2020.02.15.04.53.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Feb 2020 04:53:32 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id DC9E5800926; Sat, 15 Feb 2020 06:53:30 -0600 (CST) Date: Sat, 15 Feb 2020 06:53:30 -0600 From: Justin Pryzby To: MingJu Wu Cc: pgsql-performance@lists.postgresql.org Subject: Re: Partial index creation always scans the entire table Message-ID: <20200215125330.GE31889@telsasoft.com> References: 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 Sat, Feb 15, 2020 at 07:04:48PM +0800, MingJu Wu wrote: > Hello, > > When creating partial indexes, can postgres utilize another index for > figuring which rows should be included in the partial index, without > performing a full table scan? > > My scenario is that I have a table with 50M rows that are categorized into > 10K categories. I need to create a partial index for each category. I have > created a index on the category column, hoping that postgres can use this > information when creating the partial indexes. However, postgres always > performs full table scan. > > I've tested with PostgreSQL 12.2. Below is an example setup showing the I don't think it's possible, and an index scan wouldn't necessarily be faster, anyway, since the reads might be unordered rather than sequantial, and might hit large fractions of the table even though only returning a fraction of its tuples. But have you thought about partitioning on category rather than partial indexes? Possibly hash partition of (category). If your queries usually include category_id=X, that might be a win for performance anyway, since tables can now be read sequentially rather than scannned by index (again, probably out of order). -- Justin