B-tree
btree 索引访问方法。
当前阅读 PG 19·选择有来源记录的版本
能力 · PostgreSQL 19
以下结论来自同版本文档。条件性能力取决于运算符类、索引类型或查询,不代表普遍保证。
| 能力 | 支持 | 条件与证据 |
|---|---|---|
| Sorted output | Yes | Ordinary sorted output is distinct from ordering by an operator, such as nearest-neighbor distance. 同版本文档
|
| Unique keys | Yes | This means a unique index. Exclusion constraints use a different contract. |
| Multiple key columns | Yes | Multiple search keys are distinct from non-key INCLUDE payload columns. 同版本文档
|
| INCLUDE columns | Yes | Payload columns do not become search keys. Wide payloads can exceed the index tuple-size limit. 同版本文档
|
| Index-only scans | Yes | The query must use covered values; visibility-map state determines whether heap visits can be avoided. 同版本文档
|
| Distance ordering | Not established | Availability depends on the chosen operator class and ordering operator. 所采集来源不足以确认此能力。 |
| Parallel index scan | Yes | A cooperating scan of one index is distinct from a parallel bitmap heap scan or separate serial scans under Parallel Append. 同版本文档
|
| Parallel index build | Yes | Parallel construction is separate from parallel scans. Worker availability, settings and build phases also matter. 同版本文档
|
存储选项
fillfactor
Controls how full the index method will try to pack index pages. For B-trees, leaf pages are filled to this percentage during initial index builds, and also when extending the index at the right (adding new largest key values). If pages subsequently become completely full, they will be split, leading to fragmentation of the on-disk index structure. B-trees use a default fillfactor of 90, but any integer value from 10 to 100 can be selected.
B-tree indexes on tables where many inserts and/or updates are anticipated can benefit from lower fillfactor settings at CREATE INDEX time (following bulk loading into the table). Values in the range of 50 - 90 can usefully “ smooth out ” the rate of page splits during the early life of the B-tree index (lowering fillfactor like this may even lower the absolute number of page splits, though this effect is highly workload dependent). The B-tree bottom-up index deletion technique described in Section 65.1.4.2 is dependent on having some “ extra ” space on pages to store “ extra ” tuple versions, and so can be affected by fillfactor (though the effect is usually not significant).
In other specific cases it might be useful to increase fillfactor to 100 at CREATE INDEX time as a way of maximizing space utilization. You should only consider this when you are completely sure that the table is static (i.e. that it will never be affected by either inserts or updates). A fillfactor setting of 100 otherwise risks harming performance: even a few updates or inserts will cause a sudden flood of page splits.
The other index methods use fillfactor in different but roughly analogous ways; the default fillfactor varies between methods.
deduplicate_items
Controls usage of the B-tree deduplication technique described in Section 65.1.4.3 . Set to ON or OFF to enable or disable the optimization. (Alternative spellings of ON and OFF are allowed as described in Section 19.1 .) The default is ON .
Turning deduplicate_items off via ALTER INDEX prevents future insertions from triggering deduplication, but does not in itself make existing posting list tuples use the standard tuple representation.
- 有序输出
- 支持
- 唯一键
- 支持
- 多键列
- 支持
- INCLUDE 附加列
- 支持
- 仅索引扫描
- 支持
- 按距离排序
- 未确定
- 并行索引扫描
- 支持
- 并行构建索引
- 支持
- evidence kind
- documentation
- runtime verified
- false
- signature
- CREATE INDEX name ON table_name USING btree (column_name);
版本定义 PG 19
btree 索引访问方法。
- 有序输出
是- 唯一键
是- 多键列
是- INCLUDE 列
是- 仅索引扫描
是- 距离排序
未确定- 并行索引扫描
是- 并行索引构建
是
同版本中文手册节选
B-树能够处理可以按某种顺序排序的数据上的等值查询和范围查询。特别是,只要已索引列参与了下列任一操作符的比较,PostgreSQL 查询规划器就会考虑使用 B-树索引:
与这些操作符组合等价的构造,例如 BETWEEN 和 IN,也可以通过 B-树索引搜索来实现。此外,索引列上的 IS NULL 或 IS NOT
NULL 条件也可以配合 B-树索引使用。
如果模式是常量,并且锚定在字符串起始位置,优化器也可以对涉及模式匹配操作符 LIKE 和~的查询使用 B-树索引,例如 col LIKE
'foo%' 或 col ~ '^foo',但不能用于 col LIKE '%bar'。不过,如果你的数据库没有使用 C 区域设置,就需要以一个特殊操作符类来创建该索引,才能支持模式匹配查询的索引化,详见下文第 11.10 节。B-树索引也可以用于 ILIKE 和~*,但前提是模式以非字母字符开头,也就是不会受到大小写转换影响的字符。
B-树索引还可以用于按排序顺序取回数据。这并不总是比简单扫描再排序更快,但通常会很有帮助。
比较版本
完整来源事实
capabilities
map[evidence:[map[anchor:INDEXES-ORDERING file:indexes-ordering.html label:PostgreSQL 19beta4 · indexes-ordering quote:In addition to simply finding the rows to be returned by a query, an index may be able to deliver them in a specific sorted order. This allows a query's ORDER BY specification to be honored without a separate sorting step. Of the index types currently supported by PostgreSQL , only B-tree can produce sorted output — the other index types return matching rows in an unspecified, implementation-dependent order. sha256:d832a6c5a9f9a0a2b933bc14a235b15b50fee8b3e6ba8e4e0ccc5c37d0f217f4 url:/docs/19/indexes-ordering.html#INDEXES-ORDERING]] key:ordered label:Sorted output note:Ordinary sorted output is distinct from ordering by an operator, such as nearest-neighbor distance. state:yes value:Yes], map[evidence:[map[anchor:INDEXES-UNIQUE file:indexes-unique.html label:PostgreSQL 19beta4 · indexes-unique quote:Currently, only B-tree indexes can be declared unique. sha256:bdc4a94b059d5e024e25a6483b04a06881b9e1ea6d3c4dfe26c7e9e53b5fa0cb url:/docs/19/indexes-unique.html#INDEXES-UNIQUE]] key:unique label:Unique keys note:This means a unique index. Exclusion constraints use a different contract. state:yes value:Yes], map[evidence:[map[anchor:INDEXES-MULTICOLUMN file:indexes-multicolumn.html label:PostgreSQL 19beta4 · indexes-multicolumn quote:Currently, only the B-tree, GiST, GIN, and BRIN index types support multiple-key-column indexes. Whether there can be multiple key columns is independent of whether INCLUDE columns can be added to the index. Indexes can have up to 32 columns, including INCLUDE columns. (This limit can be altered when building PostgreSQL ; see the file pg_config_manual.h .) sha256:d88b436c251cab8655ae9ae0ae0e10a90e16c8ae32b95e116735970f22b27c62 url:/docs/19/indexes-multicolumn.html#INDEXES-MULTICOLUMN]] key:multicolumn label:Multiple key columns note:Multiple search keys are distinct from non-key INCLUDE payload columns. state:yes value:Yes], map[evidence:[map[anchor:SQL-CREATEINDEX file:sql-createindex.html label:PostgreSQL 19beta4 · sql-createindex quote:Currently, the B-tree, GiST and SP-GiST index access methods support this feature. In these indexes, the values of columns listed in the INCLUDE clause are included in leaf tuples which correspond to heap tuples, but are not included in upper-level index entries used for tree navigation. sha256:e90c2051c335f6636fb68f3b03f98c3282445c537c99fd7d2e74e4943662e224 url:/docs/19/sql-createindex.html#SQL-CREATEINDEX]] key:include label:INCLUDE columns note:Payload columns do not become search keys. Wide payloads can exceed the index tuple-size limit. state:yes value:Yes], map[evidence:[map[anchor:INDEXES-INDEX-ONLY-SCANS file:indexes-index-only-scans.html label:PostgreSQL 19beta4 · indexes-index-only-scans quote:The index type must support index-only scans. B-tree indexes always do. GiST and SP-GiST indexes support index-only scans for some operator classes but not others. Other index types have no support. The underlying requirement is that the index must physically store, or else be able to reconstruct, the original data value for each index entry. As a counterexample, GIN indexes cannot support index-only scans because each index entry typically holds only part of the original data value. sha256:52fbd3d2f64bb4634c09c57a769c5b231744e4557bb95584f3c8f4ac6886a698 url:/docs/19/indexes-index-only-scans.html#INDEXES-INDEX-ONLY-SCANS]] key:index_only label:Index-only scans note:The query must use covered values; visibility-map state determines whether heap visits can be avoided. state:yes value:Yes], map[evidence:[] key:distance label:Distance ordering note:Availability depends on the chosen operator class and ordering operator. state:unknown value:Not established], map[evidence:[map[anchor:PARALLEL-SCANS file:parallel-plans.html label:PostgreSQL 19beta4 · parallel-plans quote:In a parallel index scan or parallel index-only scan , the cooperating processes take turns reading data from the index. Currently, parallel index scans are supported only for btree indexes. Each process will claim a single index block and will scan and return all tuples referenced by that block; other processes can at the same time be returning tuples from a different index block. The results of a parallel btree scan are returned in sorted order within each worker process. sha256:f99fee3456a48a5ce2c399d6b35ab93183c2c9012e8ed60c53c0c253f79c3756 url:/docs/19/parallel-plans.html#PARALLEL-SCANS]] key:parallel_scan label:Parallel index scan note:A cooperating scan of one index is distinct from a parallel bitmap heap scan or separate serial scans under Parallel Append. state:yes value:Yes], map[evidence:[map[anchor:SQL-CREATEINDEX file:sql-createindex.html label:PostgreSQL 19beta4 · sql-createindex quote:PostgreSQL can build indexes while leveraging multiple CPUs in order to process the table rows faster. This feature is known as parallel index build . For index methods that support building indexes in parallel (currently, B-tree, GIN, and BRIN), maintenance_work_mem specifies the maximum amount of memory that can be used by each index build operation as a whole, regardless of how many worker processes were started. Generally, a cost model automatically determines how many worker processes should be requested, if any. sha256:e90c2051c335f6636fb68f3b03f98c3282445c537c99fd7d2e74e4943662e224 url:/docs/19/sql-createindex.html#SQL-CREATEINDEX]] key:parallel_build label:Parallel index build note:Parallel construction is separate from parallel scans. Worker availability, settings and build phases also matter. state:yes value:Yes]
storage options
map[description:[Controls how full the index method will try to pack index pages. For B-trees, leaf pages are filled to this percentage during initial index builds, and also when extending the index at the right (adding new largest key values). If pages subsequently become completely full, they will be split, leading to fragmentation of the on-disk index structure. B-trees use a default fillfactor of 90, but any integer value from 10 to 100 can be selected. B-tree indexes on tables where many inserts and/or updates are anticipated can benefit from lower fillfactor settings at CREATE INDEX time (following bulk loading into the table). Values in the range of 50 - 90 can usefully “ smooth out ” the rate of page splits during the early life of the B-tree index (lowering fillfactor like this may even lower the absolute number of page splits, though this effect is highly workload dependent). The B-tree bottom-up index deletion technique described in Section 65.1.4.2 is dependent on having some “ extra ” space on pages to store “ extra ” tuple versions, and so can be affected by fillfactor (though the effect is usually not significant). In other specific cases it might be useful to increase fillfactor to 100 at CREATE INDEX time as a way of maximizing space utilization. You should only consider this when you are completely sure that the table is static (i.e. that it will never be affected by either inserts or updates). A fillfactor setting of 100 otherwise risks harming performance: even a few updates or inserts will cause a sudden flood of page splits. The other index methods use fillfactor in different but roughly analogous ways; the default fillfactor varies between methods.] name:fillfactor source:map[anchor:INDEX-RELOPTION-FILLFACTOR file:sql-createindex.html label:PostgreSQL 19beta4 · sql-createindex sha256:e90c2051c335f6636fb68f3b03f98c3282445c537c99fd7d2e74e4943662e224 url:/docs/19/sql-createindex.html#INDEX-RELOPTION-FILLFACTOR] url:/wiki/relopts/btree-fillfactor/?v=19], map[description:[Controls usage of the B-tree deduplication technique described in Section 65.1.4.3 . Set to ON or OFF to enable or disable the optimization. (Alternative spellings of ON and OFF are allowed as described in Section 19.1 .) The default is ON . Turning deduplicate_items off via ALTER INDEX prevents future insertions from triggering deduplication, but does not in itself make existing posting list tuples use the standard tuple representation.] name:deduplicate_items source:map[anchor:INDEX-RELOPTION-DEDUPLICATE-ITEMS file:sql-createindex.html label:PostgreSQL 19beta4 · sql-createindex sha256:e90c2051c335f6636fb68f3b03f98c3282445c537c99fd7d2e74e4943662e224 url:/docs/19/sql-createindex.html#INDEX-RELOPTION-DEDUPLICATE-ITEMS] url:/wiki/relopts/btree-deduplicate-items/?v=19]
来源引用
- PostgreSQL 19rc1: B-tree
- PostgreSQL 19rc1 · catalog-pg-am
- PostgreSQL 19rc1 · hash-index
- PostgreSQL 19rc1 · indexam
- PostgreSQL 19rc1 · indexes-index-only-scans
- PostgreSQL 19rc1 · indexes-multicolumn
- PostgreSQL 19rc1 · indexes-opclass
- PostgreSQL 19rc1 · indexes-ordering
- PostgreSQL 19rc1 · indexes-unique
- PostgreSQL 19rc1 · parallel-plans
- PostgreSQL 19rc1 · sql-createindex
定义来源
center · PostgreSQL 19 · 7a2cc9bea1204b31b9707bccdd13a423c966454d88c1fab7530d499d817c76f7
正文语言: zh-Hans · 7a2cc9bea1204b31b9707bccdd13a423c966454d88c1fab7530d499d817c76f7