• Indexes in PostgreSQL — 4 (Btree)

    • Translation
    We've already discussed PostgreSQL indexing engine and interface of access methods, as well as hash index, one of access methods. We will now consider B-tree, the most traditional and widely used index. This article is large, so be patient.

    Btree


    Structure


    B-tree index type, implemented as «btree» access method, is suitable for data that can be sorted. In other words, «greater», «greater or equal», «less», «less or equal», and «equal» operators must be defined for the data type. Note that the same data can sometimes be sorted differently, which takes us back to the concept of operator family.
    Read more →
    • +5
    • 10.4k
    • 5
  • Indexes in PostgreSQL — 3 (Hash)

    • Translation
    The first article described PostgreSQL indexing engine, the second one dealt with the interface of access methods, and now we are ready to discuss specific types of indexes. Let's start with hash index.

    Hash


    Structure


    General theory


    Plenty of modern programming languages include hash tables as the base data type. On the outside, a hash table looks like a regular array that is indexed with any data type (for example, string) rather than with an integer number. Hash index in PostgreSQL is structured in a similar way. How does this work?

    As a rule, data types have very large ranges of permissible values: how many different strings can we potentially envisage in a column of type «text»? At the same time, how many different values are actually stored in a text column of some table? Usually, not so many of them.

    The idea of hashing is to associate a small number (from 0 to N−1, N values in total) with a value of any data type. Association like this is called a hash function. The number obtained can be used as an index of a regular array where references to table rows (TIDs) will be stored. Elements of this array are called hash table buckets — one bucket can store several TIDs if the same indexed value appears in different rows.

    The more uniformly a hash function distributes source values by buckets, the better it is. But even a good hash function will sometimes produce equal results for different source values — this is called a collision. So, one bucket can store TIDs corresponding to different keys, and therefore, TIDs obtained from the index need to be rechecked.
    Read more →
  • Indexes in PostgreSQL — 2

    • Translation

    Interface


    In the first article, we've mentioned that an access method must provide information about itself. Let's look into the structure of the access method interface.

    Properties


    All properties of access methods are stored in the «pg_am» table («am» stands for access method). We can also get a list of available methods from this same table:

    postgres=# select amname from pg_am;
    
     amname
    --------
     btree
     hash
     gist
     gin
     spgist
     brin
    (6 rows)
    

    Although sequential scan can rightfully be referred to access methods, it is not on this list for historical reasons.

    In PostgreSQL versions 9.5 and lower, each property was represented with a separate field of the «pg_am» table. Starting with version 9.6, properties are queried with special functions and are separated into several layers:

    • Access method properties — «pg_indexam_has_property»
    • Properties of a specific index — «pg_index_has_property»
    • Properties of individual columns of the index — «pg_index_column_has_property»

    The access method layer and index layer are separated with an eye towards the future: as of now, all indexes based on one access method will always have the same properties.
    Read more →
  • Indexes in PostgreSQL — 1

    • Translation

    Introduction


    This series of articles is largely concerned with indexes in PostgreSQL.

    Any subject can be considered from different perspectives. We will discuss matters that should interest an application developer who uses DBMS: what indexes are available, why there are so many different types of them, and how to use them to speed up queries. The topic can probably be covered in fewer words, but in secrecy we hope for a curious developer, who is also interested in details of the internals, especially since understanding of such details allows you to not only defer to other's judgement, but also make conclusions of your own.

    Development of new types of indexes is outside the scope. This requires knowledge of the C programming language and pertains to the expertise of a system programmer rather than an application developer. For the same reason we almost won't discuss programming interfaces, but will focus only on what matters for working with ready-to-use indexes.

    In this article we will discuss the distribution of responsibilities between the general indexing engine related to the DBMS core and individual index access methods, which PostgreSQL enables us to add as extensions. In the next article we will discuss the interface of the access method and critical concepts such as classes and operator families. After that long but necessary introduction we will consider details of the structure and application of different types of indexes: Hash, B-tree, GiST, SP-GiST, GIN and RUM, BRIN, and Bloom.

    Before we start, I would like to thank Elena Indrupskaya for translating the articles to English.
    Things have changed a bit since the original publication. My comments on the current state of affairs are indicated like this.
    Read more →
    • +21
    • 12.7k
    • 8