From bbce2912fa899de30783a6a76a6443b5a2aede8d Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 19 Feb 2020 18:52:18 -0500 Subject: [PATCH] Doc: discourage use of partial indexes for poor-man's-partitioning. Creating a bunch of non-overlapping partial indexes is generally a bad idea, so add an example saying not to do that. Back-patch to v10. Before that, the alternative of using (real) partitioning wasn't available, so that the tradeoff isn't quite so clear cut. Discussion: https://postgr.es/m/CAKVFrvFY-f7kgwMRMiPLbPYMmgjc8Y2jjUGK_Y0HVcYAmU6ymg@mail.gmail.com --- doc/src/sgml/indices.sgml | 48 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/indices.sgml b/doc/src/sgml/indices.sgml index 3380eccfe44..9ccee05d671 100644 --- a/doc/src/sgml/indices.sgml +++ b/doc/src/sgml/indices.sgml @@ -975,10 +975,54 @@ CREATE UNIQUE INDEX tests_target_one_null ON tests ((target IS NULL)) WHERE targ know at least as much as the query planner knows, in particular you know when an index might be profitable. Forming this knowledge requires experience and understanding of how indexes in - PostgreSQL work. In most cases, the advantage of a - partial index over a regular index will be minimal. + PostgreSQL work. In most cases, the + advantage of a partial index over a regular index will be minimal. + There are cases where they are quite counterproductive, as in . + + Do Not Use Partial Indexes as a Substitute for Partitioning + + + You might be tempted to create a large set of non-overlapping partial + indexes, for example + + +CREATE INDEX mytable_cat_1 ON mytable (data) WHERE category = 1; +CREATE INDEX mytable_cat_2 ON mytable (data) WHERE category = 2; +CREATE INDEX mytable_cat_3 ON mytable (data) WHERE category = 3; +... +CREATE INDEX mytable_cat_N ON mytable (data) WHERE category = N; + + + This is a bad idea! Almost certainly, you'll be better off with a + single non-partial index, declared like + + +CREATE INDEX mytable_cat_data ON mytable (category, data); + + + (Put the category column first, for the reasons described in + .) While a search in this larger + index might have to descend through a couple more tree levels than a + search in a smaller index, that's almost certainly going to be cheaper + than the planner effort needed to select the appropriate one of the + partial indexes. The core of the problem is that the system does not + understand the relationship among the partial indexes, and will + laboriously test each one to see if it's applicable to the current + query. + + + + If your table is large enough that a single index really is a bad idea, + you should look into using partitioning instead (see + ). With that mechanism, the system + does understand that the tables and indexes are non-overlapping, so + far better performance is possible. + + + More information about partial indexes can be found in , , and