Hypothetical Indexes for PostgreSQL
Find a file
2018-04-24 12:41:49 +02:00
expected Fix NULL ordering for SORTBY_DESC case. Thanks to Andrew Kane for the 2017-09-05 06:45:31 +02:00
include Add hypo_find_partition_scheme() instead of hypo_generate_part_scheme() 2018-04-24 09:08:54 +00:00
test/sql Fix NULL ordering for SORTBY_DESC case. Thanks to Andrew Kane for the 2017-09-05 06:45:31 +02:00
.gitignore Add some basic regression test. 2015-07-11 10:28:34 +02:00
CHANGELOG.md Release 1.1.0 2017-10-04 19:33:47 +02:00
CONTRIBUTORS.md Add CONTRIBUTORS.md 2018-02-24 00:32:01 +01:00
hypopg--2.0.0dev.sql Add infrastructure to declare hypothetical partitioning. 2018-03-04 21:44:58 +01:00
hypopg.c Remove a function, hypo_markDummyIfExcluded 2018-04-19 02:25:18 +00:00
hypopg.control Start Working on v2.0.0 2018-03-03 11:40:55 +01:00
hypopg_import.c Fix some code to be compatible with current upstream code. 2018-04-19 09:00:12 +02:00
hypopg_index.c Add a hypopg_reset_index() SQL wrapper. 2018-03-03 14:18:59 +01:00
hypopg_table.c Remove unused var 2018-04-24 12:41:49 +02:00
LICENSE Update year 2018-03-02 21:44:03 +01:00
Makefile Add infrastructure to declare hypothetical partitioning. 2018-03-04 21:44:58 +01:00
META.json Change all references to point to new upstream 2018-02-23 22:00:54 +01:00
README.md Change all references to point to new upstream 2018-02-23 22:00:54 +01:00
TODO.md Add a hypopg_get_indexdef(oid) function 2016-11-16 23:49:27 +01:00
typedefs.list Import and tweak typedefs.list, needed for pgindent. 2015-06-23 23:54:45 +02:00

HypoPG

HypoPG is a PostgreSQL extension adding support for hypothetical indexes.

An hypothetical, or virtual, index is an index that doesn't really exists, and thus doesn't cost CPU, disk or any resource to create. They're useful to know if specific indexes can increase performance for problematic queries, since you can know if PostgreSQL will use these indexes or not without having to spend resources to create them. For more information on this extension usage, you can see this blog post.

Installation

  • Compatible with PostgreSQL 9.2 and above
  • Needs PostgreSQL header files
  • decompress the tarball
  • sudo make install
  • In every needed database: CREATE EXTENSION hypopg;

Usage

NOTE: The hypothetical indexes are contained in a single backend. Therefeore, if you add multiple hypothetical indexes, concurrent connexions doing EXPLAIN won't be bothered by your hypothetical indexes.

Assuming a simple test case:

rjuju=# CREATE TABLE hypo AS SELECT id, 'line ' || id AS val FROM generate_series(1,10000) id;
rjuju=# EXPLAIN SELECT * FROM hypo WHERE id = 1;
                      QUERY PLAN
-------------------------------------------------------
 Seq Scan on hypo  (cost=0.00..180.00 rows=1 width=13)
   Filter: (id = 1)
(2 rows)

The easiest way to create an hypothetical index is to use the hypopg_create_index functions with a regular CREATE INDEX statement as arg.

For instance:

rjuju=# SELECT * FROM hypopg_create_index('CREATE INDEX ON hypo (id)');

NOTE: Some information of the CREATE INDEX statement will be ignored, such as the index name if provided. Some of ignored informations will be handled in future release.

You can check the available hypothetical indexes in your own backend:

rjuju=# SELECT * FROM hypopg_list_indexes();
 indexrelid |                 indexname                 | nspname | relname | amname
 -----------+-------------------------------------------+---------+---------+--------
     205101 | <41072>btree_hypo_id                      | public  | hypo    | btree

If you need more technical informations on the hypothetical indexes, the hypopg() function will return the hypothetical indexes in a similar way as pg_index system catalog.

And now, let's see if your previous EXPLAIN statement would use such an index:

rjuju=# EXPLAIN SELECT * FROM hypo WHERE id = 1;
                                     QUERY PLAN
------------------------------------------------------------------------------------
 Index Scan using <41072>hypo_btree_hypo_id on hypo  (cost=0.29..8.30 rows=1 width=13)
   Index Cond: (id = 1)
(2 rows)

Of course, only EXPLAIN without analyze will use hypothetical indexes:

rjuju=# EXPLAIN ANALYZE SELECT * FROM hypo WHERE id = 1;
                                           QUERY PLAN
-------------------------------------------------------------------------------------------------
 Seq Scan on hypo  (cost=0.00..180.00 rows=1 width=13) (actual time=0.036..6.072 rows=1 loops=1)
   Filter: (id = 1)
   Rows Removed by Filter: 9999
 Planning time: 0.109 ms
 Execution time: 6.113 ms
(5 rows)

To remove your backend's hypothetical indexes, you can use the function hypopg_drop_index(indexrelid) with the OID that hypopg_list_indexes() function returns, call hypopg_reset() to remove all at once or just close your current connection.