Skip to content

Introduce the ability to set index visibility using ALTER INDEX #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shayonj
Copy link
Owner

@shayonj shayonj commented Sep 21, 2024

This patch introduces index visibility control using ALTER INDEX and CREATE INDEX
commands.

Original motivation for the problem and proposal for a patch
can be found at [1].

This patch passes all the existing specs and the newly added regression tests. The patch
is ready for review and test. It compiles, so the patch can be applied for testing as well.

Note: The patch has gone through a few iterations. Earlier versions of the patch had the ENABLE/DISABLE grammar.
The current version has the VISIBLE/INVISIBLE grammar. So, you will the local variable names using the new grammar accordingly.

Implementation details:

  • New Grammar:

    • ALTER INDEX ... VISIBLE/INVISIBLE
    • CREATE INDEX ... INVISIBLE
  • Default state is visible. Indexes are only invisible when explicitly
    instructed via CREATE INDEX ... INVISIBLE or ALTER INDEX ... INVISIBLE.

  • Primary Key and Unique constraint indexes are always visible. The
    VISIBLE/INVISIBLE grammar is supported for these types of indexes and they can
    be made invisible via ALTER INDEX ... INVISIBLE.

  • ALTER INDEX ... VISIBLE/INVISIBLE performs update on the relevant row in pg_index
    catalog

  • pg_get_indexdef() supports the new functionality and grammar. This change is
    reflected in \d output for tables and pg_dump. We show the INVISIBLE syntax accordingly.

  • Added force_invisible_index GUC parameter that forces the planner to use invisible
    indexes. This is useful for testing and validating index behavior without changing
    their visibility state. Based on feedback from Sami S [2]

  • Updated create_index.sql regression test to cover the new grammar and verify
    that invisible indexes are not used in queries. The test covers:

    • Basic single-column and multi-column indexes
    • Partial indexes
    • Expression indexes
    • Join indexes
    • GIN and GiST indexes
    • Covering indexes
    • Range indexes
    • Unique indexes and constraints
  • Adds a new indisvisible attribute to the IndexOptInfo structure.

  • Modifies get_relation_info in plancat.c to skip invisible indexes entirely, thus reducing the number of places we need to check if an index is invisible or not. Inspired by the conversations start at [3].

    • I chose to modify the logic within get_relation_info as compared to, say, reducing the cost to make the planner not consider an index during planning, mostly to keep the number of changes being introduced to a minimum and also the logic itself being self-contained and easier to understand perhaps (?).
  • No changes are made to stop the index from getting maintained. This way we ensure no
    data loss or corruption when index is made visible again.

  • TOAST indexes are supported and visible by default as well.

  • REINDEX CONCURRENTLY is supported as well and existing state of pg_index.indisvisible
    is carried over accordingly.

  • See the changes in create_index.sql to get an idea of the grammar and sql statements.

  • See the changes in create_index.out to get an idea of the catalogue states and EXPLAIN
    output to see when an index is getting used or isn't (when invisible).

  • Incorporated DavidR's feedback from [4] around documentation and also you will see that by skipping invisible indexes entirely from get_relation_info in plancat.c (as mentioned above), we address the other mentioned issues as well.

  • Lastly, protects against the case where indcheckxmin is true by raising ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE.

  • Supports index visibility status on \d (h/t to Benoit Lobréau for the patch)

Looking forward to any and all feedback on this patch, including but not limited to code quality, tests, fundamental logic.

[1] https://www.postgresql.org/message-id/CANqtF-oXKe0M%3D0QOih6H%2BsZRjE2BWAbkW_1%2B9nMEAMLxUJg5jA%40mail.gmail.com
[2] https://www.postgresql.org/message-id/CAA5RZ0udzydObMDi65C59-oq54B9ZmjSZ1wVH3h%2Bv4XiVm6QDA%40mail.gmail.com
[3] https://www.postgresql.org/message-id/3465209.1727202064%40sss.pgh.pa.us
[4] https://www.postgresql.org/message-id/CAApHDvpUNu%3DiVcdJ74sypvgeaCF%2Btfpyb8VRhZaF7DTd1xVr7g%40mail.gmail.com

@shayonj shayonj force-pushed the s/enable-disable-index branch 2 times, most recently from 76503e7 to 498d2ec Compare September 22, 2024 14:16
@shayonj shayonj changed the title WIP Introduce the ability to enable/disable indexes Sep 22, 2024
@shayonj shayonj force-pushed the s/enable-disable-index branch 3 times, most recently from 4e9c6f2 to 15da0f3 Compare September 22, 2024 16:13
@shayonj shayonj force-pushed the s/enable-disable-index branch 4 times, most recently from 24c3439 to 848b143 Compare September 22, 2024 18:01
@shayonj shayonj force-pushed the s/enable-disable-index branch 5 times, most recently from a036bf8 to 4e7be6e Compare October 16, 2024 15:10
@shayonj shayonj changed the title Introduce the ability to enable/disable indexes Introduce the ability to enable/disable indexes using ALTER INDEX Oct 16, 2024
@shayonj shayonj force-pushed the s/enable-disable-index branch 13 times, most recently from 3199d30 to 0a096b3 Compare October 16, 2024 20:28
@shayonj shayonj force-pushed the s/enable-disable-index branch from e5d2fa2 to e64b19d Compare December 6, 2024 16:03
@shayonj shayonj force-pushed the s/enable-disable-index branch 6 times, most recently from 348c1f2 to 99ab5b6 Compare December 31, 2024 16:51
@shayonj shayonj force-pushed the s/enable-disable-index branch from 99ab5b6 to 28dabf9 Compare January 11, 2025 21:08
@shayonj shayonj changed the title Introduce the ability to enable/disable indexes using ALTER INDEX Introduce the ability to set index visibility using ALTER INDEX Jan 12, 2025
@shayonj shayonj force-pushed the s/enable-disable-index branch 5 times, most recently from 52dac01 to 6445a66 Compare January 12, 2025 19:57
@shayonj shayonj force-pushed the s/enable-disable-index branch 4 times, most recently from a0669d9 to 70b1b61 Compare February 4, 2025 09:42
@shayonj shayonj force-pushed the s/enable-disable-index branch 3 times, most recently from f59758a to 3792fbd Compare March 2, 2025 09:53
@shayonj shayonj force-pushed the s/enable-disable-index branch from 3792fbd to 13b1752 Compare March 7, 2025 07:20
@shayonj shayonj force-pushed the s/enable-disable-index branch 5 times, most recently from b241e6a to b183375 Compare April 7, 2025 20:39
@shayonj shayonj force-pushed the s/enable-disable-index branch from b183375 to 73422a3 Compare April 27, 2025 20:38
This patch introduces the ability to make an index INVISIBLE ( or VISIBLE )
to the planner, making the index not eligible for planning but will continue
to be maintained when the underlying data changes. This behavior is accomplished
by introducing new grammar ALTER INDEX ... VISIBLE|INVISIBLE and
CREATE INDEX ... VISIBLE|INVISIBLE.

Additionally, there is also support for force_invisible_index GUC parameter that forces
the planner to use invisible indexes. This is useful for testing and validating index behavior
without changing their visibility state.

Discussion: https://www.postgresql.org/message-id/flat/EF2313B8-A017-4869-9B7F-A24EDD8795DE%40gmail.com#dbe65017ffa7b65a4f3f29e64ed2fce5
@shayonj shayonj force-pushed the s/enable-disable-index branch from 73422a3 to ee130c6 Compare April 27, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant