# What Access Methods Does pgrust Support? (B‑tree, Hash, GiST, GIN, SP‑GiST, and BRIN)

> Explore pgrust's support for PostgreSQL index access methods including B-tree, Hash, GiST, GIN, SP-GiST, and BRIN. Optimize your PostgreSQL performance with efficient indexing.

- Repository: [Michael Malis/pgrust](https://github.com/malisper/pgrust)
- Tags: deep-dive
- Published: 2026-07-13

---

**pgrust supports six built‑in PostgreSQL index access methods: B‑tree, Hash, GiST, GIN, SP‑GiST, and BRIN, with hard‑coded OIDs defined in [`opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/opfamily_operator.rs) to optimize cache lookups.**

The pgrust project implements PostgreSQL’s core index infrastructure, mirroring the built‑in access methods that power PostgreSQL’s query optimization. These access methods are identified by constant **Object Identifiers (OIDs)** hard‑coded in the source to avoid expensive system catalog lookups.

## Supported Access Methods in pgrust

According to the source code in [`crates/backend/utils/cache/lsyscache/src/opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/crates/backend/utils/cache/lsyscache/src/opfamily_operator.rs), pgrust recognizes the following six access methods by their PostgreSQL-compatible OIDs:

### B‑tree (OID 403)

The **B‑tree** access method (`BTREE_AM_OID = 403`) provides the classic ordered tree structure used for primary keys, unique constraints, and range queries. This is the default access method when you create an index without specifying a type.

### Hash (OID 405)

The **Hash** access method (`HASH_AM_OID = 405`) enables hash‑based indexing optimized for equality comparisons. Unlike B‑tree, hash indexes do not support ordering or range scans.

### GiST (OID 783)

The **GiST** (Generalized Search Tree) access method (`GIST_AM_OID = 783`) offers a flexible framework for building custom indexing schemes. It supports geometric data types, nearest‑neighbor searches, and user‑defined operators.

### GIN (OID 2742)

The **GIN** (Generalized Inverted Index) access method (`GIN_AM_OID = 2742`) is optimized for indexing multi‑valued data such as arrays, JSON documents, and full‑text search vectors.

### SP‑GiST (OID 4000)

The **SP‑GiST** (Space‑Partitioned GiST) access method (`SPGIST_AM_OID = 4000`) extends the GiST framework with support for space‑partitioned data structures like quadtrees and k‑d trees.

### BRIN (OID 3580)

The **BRIN** (Block Range INdex) access method (`BRIN_AM_OID = 3580`) provides a lightweight indexing strategy that stores summary information about ranges of table blocks, ideal for very large tables with naturally ordered data.

## How pgrust Implements Access Method OIDs

In [`opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/opfamily_operator.rs), pgrust defines these OIDs as constants to eliminate runtime catalog lookups for the most common cases:

```rust
// From crates/backend/utils/cache/lsyscache/src/opfamily_operator.rs
const BTREE_AM_OID: Oid = 403;
const HASH_AM_OID: Oid = 405;
const GIST_AM_OID: Oid = 783;
const GIN_AM_OID: Oid = 2742;
const SPGIST_AM_OID: Oid = 4000;
const BRIN_AM_OID: Oid = 3580;

```

These constants are used throughout the backend to determine index behavior without querying the system cache.

## Checking Access Method Capabilities

The function `get_opmethod_canorder` in [`opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/opfamily_operator.rs) determines whether an access method can provide ordering semantics. Only **B‑tree** returns `true`; all other access methods (Hash, GiST, GIN, SP‑GiST, and BRIN) are treated as non‑ordering, matching PostgreSQL’s semantics.

```rust
use backend::utils::cache::lsyscache::opfamily_operator::get_opmethod_canorder;
use types_core::Oid;

/// Returns true only for B‑tree (the only ordering‑capable AM).
fn can_order(am_oid: Oid) -> bool {
    get_opmethod_canorder(am_oid).unwrap_or(false)
}

```

## Working with Access Methods in Code

When interacting with pgrust’s access method system, you can reference these OIDs directly or query their capabilities:

```rust
use types_core::Oid;

/// Retrieve the OID for a given built‑in access method.
fn get_btree_oid() -> Oid { 403 }  // BTREE_AM_OID
fn get_hash_oid() -> Oid { 405 }   // HASH_AM_OID
fn get_gist_oid() -> Oid { 783 }   // GIST_AM_OID

```

The `CreateAccessMethod` command in [`crates/backend/commands/amcmds/src/lib.rs`](https://github.com/malisper/pgrust/blob/main/crates/backend/commands/amcmds/src/lib.rs) allows registration of new access methods using the same underlying infrastructure:

```rust
use backend::commands::amcmds::CreateAccessMethod;
use backend::nodes::parsenodes::CreateAmStmt;
use mcx::Mcx;

/// Example: create a GIN access method named "my_gin".
fn create_gin(mcx: &mut Mcx<'_>) -> Result<(), PgError> {
    let stmt = CreateAmStmt {
        amname: "my_gin".into(),
        amtype: "gin".into(),
        amhandler: None,
        amoptions: None,
    };
    CreateAccessMethod(mcx, &stmt)?;
    Ok(())
}

```

## Summary

- pgrust supports **six built‑in access methods**: B‑tree (403), Hash (405), GiST (783), GIN (2742), SP‑GiST (4000), and BRIN (3580).
- These OIDs are hard‑coded in [`opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/opfamily_operator.rs) to avoid costly cache lookups.
- Only **B‑tree** supports ordering semantics; all other access methods are treated as non‑ordering.
- The `get_opmethod_canorder` function provides a fast path to check ordering capabilities.
- Source files including [`amcmds.rs`](https://github.com/malisper/pgrust/blob/main/amcmds.rs) and [`relation.rs`](https://github.com/malisper/pgrust/blob/main/relation.rs) use these constants to drive index creation and relation cache initialization.

## Frequently Asked Questions

### What access methods does pgrust support?

pgrust supports B‑tree, Hash, GiST, GIN, SP‑GiST, and BRIN access methods, matching PostgreSQL’s built‑in index types. These are defined by hard‑coded OIDs in the [`opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/opfamily_operator.rs) source file.

### Where are access method OIDs defined in pgrust?

Access method OIDs are defined as constants in [`crates/backend/utils/cache/lsyscache/src/opfamily_operator.rs`](https://github.com/malisper/pgrust/blob/main/crates/backend/utils/cache/lsyscache/src/opfamily_operator.rs). This file contains `BTREE_AM_OID` (403), `HASH_AM_OID` (405), `GIST_AM_OID` (783), `GIN_AM_OID` (2742), `SPGIST_AM_OID` (4000), and `BRIN_AM_OID` (3580).

### Does pgrust support custom access methods?

Yes, pgrust supports custom access methods through the `CreateAccessMethod` command implemented in [`crates/backend/commands/amcmds/src/lib.rs`](https://github.com/malisper/pgrust/blob/main/crates/backend/commands/amcmds/src/lib.rs), allowing users to register new access methods with custom handlers while using the same OID infrastructure.

### Which access method provides ordering semantics in pgrust?

Only the **B‑tree** access method provides ordering semantics. The `get_opmethod_canorder` function returns `true` only for `BTREE_AM_OID` (403), while Hash, GiST, GIN, SP‑GiST, and BRIN are treated as non‑ordering according to PostgreSQL semantics.