Skip to content

Add support for PostgreSQL index access methods (gist, spgist, brin, hash)#1053

Draft
dereuromark wants to merge 3 commits into5.nextfrom
feature/postgres-index-access-methods
Draft

Add support for PostgreSQL index access methods (gist, spgist, brin, hash)#1053
dereuromark wants to merge 3 commits into5.nextfrom
feature/postgres-index-access-methods

Conversation

@dereuromark
Copy link
Copy Markdown
Member

Summary

This PR adds support for additional PostgreSQL index access methods beyond the existing GIN support:

  • GiST (Generalized Search Tree) - for geometric data, range types, full-text search
  • SP-GiST (Space-Partitioned GiST) - for data with natural clustering (IP addresses, phone numbers)
  • BRIN (Block Range Index) - for large, naturally-ordered tables like time-series data
  • Hash - for simple equality comparisons

Additionally, this adds support for specifying operator classes (opclass) on index columns, which is useful for specialized index configurations like trigram similarity searches with gist_trgm_ops.

Changes

  • Add Index::GIN, Index::GIST, Index::SPGIST, Index::BRIN, Index::HASH constants
  • Add opclass option to Index class for specifying operator classes
  • Generalize PostgresAdapter::getIndexSqlDefinition() to support all access methods
  • Add comprehensive tests for each new index type
  • Update documentation with examples for all PostgreSQL index access methods

Usage

// GIN index for JSONB
$table->addIndex('tags', ['type' => 'gin']);

// GiST index with operator class for trigram search
$table->addIndex('name', [
    'type' => 'gist',
    'opclass' => ['name' => 'gist_trgm_ops'],
]);

// BRIN index for time-series data
$table->addIndex('recorded_at', ['type' => 'brin']);

// SP-GiST index for IP addresses
$table->addIndex('client_ip', ['type' => 'spgist']);

// Hash index for equality lookups
$table->addIndex('session_id', ['type' => 'hash']);

Refs cakephp/phinx#1823

@dereuromark dereuromark requested a review from markstory March 15, 2026 01:07
@dereuromark dereuromark changed the base branch from 5.x to 5.next March 17, 2026 14:31
Comment on lines +944 to 949
// Build USING clause for access method types (gin, gist, spgist, brin, hash)
$indexType = $index->getType();
$usingClause = '';
if (in_array($indexType, self::ACCESS_METHOD_TYPES, true)) {
$usingClause = ' USING ' . $indexType;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being able to create these index types is a nice improvement. Should the creation of these index types be done in cakephp/database though so that we can also reflect these index types? Without the reflection aspect snapshot generation will be lossy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With cakephp/cakephp#19369 going into 5.4, do we still already merge this for support now? Until full loss-less support will come in 5.4?

…hash)

- Add Index constants for GIN, GIST, SPGIST, BRIN, and HASH access methods
- Add opclass option for specifying operator classes on index columns
- Generalize PostgresAdapter to support all access method types
- Add tests for each new index type
- Update documentation with examples for all PostgreSQL index types

Refs cakephp/phinx#1823
@dereuromark dereuromark force-pushed the feature/postgres-index-access-methods branch from 40da6d3 to 0228d40 Compare March 27, 2026 18:39
dereuromark added a commit to cakephp/cakephp that referenced this pull request Mar 27, 2026
This adds the ability to reflect PostgreSQL index access methods
(gin, gist, spgist, brin, hash) when describing indexes.

Changes:
- Add `accessMethod` property to Index class with constants for
  PostgreSQL access methods (GIN, GIST, SPGIST, BRIN, HASH)
- Update describeIndexQuery() to join pg_am and fetch amname
- Update describeIndexes() and convertIndexDescription() to include
  accessMethod in result for non-btree indexes
- Update indexSql() to generate USING clause for non-btree indexes

This enables migrations to properly reflect and regenerate indexes
that use specialized PostgreSQL access methods.

Refs cakephp/migrations#1053
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants