pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: lossyrob (@lossyrob) <noreply+lossyrob@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #933: Add per-index default search parameters (default_probes, default_ef_search)
Date: Fri, 05 Dec 2025 16:20:23 +0000
Message-ID: <gh-pgvector-pgvector-933@github.com> (raw)

# Search Defaults Index Option

## Summary

This PR adds per-index default search parameters (`default_probes` for IVFFlat and `default_ef_search` for HNSW) that automatically configure search behavior without requiring session-level `SET` commands. Index defaults take effect when no explicit session setting is active, while still respecting user overrides.

### Problem Solved

Before this feature, configuring search parameters like the number of IVFFlat probes or HNSW ef_search required:
- Setting session-level GUC variables before each query
- Wrapping queries in transaction blocks with `SET LOCAL` statements
- Using database-wide defaults that apply to all indexes

This created complexity when managing multiple tables with different accuracy/performance tradeoffs, partitioned tables with per-partition indexes, or applications sharing database connections.

### Solution

Per-index defaults allow administrators to specify optimal search parameters at index creation time. Queries automatically use the appropriate settings based on which index is selected.

## Related Issues

- Closes #235

## Artifacts

- [Specification](https://github.com/lossyrob/pgvector/blob/2985e84e396db4d6bbbd8a0214d74a69845df69b/.paw/work/235-sea...)
- [Spec Research](https://github.com/lossyrob/pgvector/blob/2985e84e396db4d6bbbd8a0214d74a69845df69b/.paw/work/235-sea...)
- [Code Research](https://github.com/lossyrob/pgvector/blob/2985e84e396db4d6bbbd8a0214d74a69845df69b/.paw/work/235-sea...)
- [Implementation Plan](https://github.com/lossyrob/pgvector/blob/2985e84e396db4d6bbbd8a0214d74a69845df69b/.paw/work/235-sea...)
- [Documentation](https://github.com/lossyrob/pgvector/blob/2985e84e396db4d6bbbd8a0214d74a69845df69b/.paw/work/235-sea...)

## Changes Summary

### Key Changes

- **New IVFFlat option**: `default_probes` - specifies default number of probes when no session SET is active
- **New HNSW option**: `default_ef_search` - specifies default ef_search value when no session SET is active
- **Precedence rules**: Explicit `SET` > Index default > GUC default
- **Cost estimation**: Query planner considers per-index defaults for accurate cost estimates
- **ALTER INDEX support**: Modify defaults after creation without index rebuild

### Files Modified

- `src/ivfflat.h`, `src/ivfflat.c`, `src/ivfutils.c`, `src/ivfscan.c` - IVFFlat support
- `src/hnsw.h`, `src/hnsw.c`, `src/hnswutils.c`, `src/hnswscan.c` - HNSW support
- `test/sql/ivfflat_vector.sql`, `test/expected/ivfflat_vector.out` - IVFFlat tests
- `test/sql/hnsw_vector.sql`, `test/expected/hnsw_vector.out` - HNSW tests
- `README.md` - User documentation
- `CHANGELOG.md` - Release notes

## Usage Examples

### IVFFlat

```sql
-- Create index with default probes
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) 
    WITH (lists = 100, default_probes = 10);

-- Queries automatically use 10 probes without SET command
SELECT * FROM items ORDER BY embedding <-> '[1,2,3]' LIMIT 10;

-- Override with session SET when needed
SET ivfflat.probes = 20;
SELECT * FROM items ORDER BY embedding <-> '[1,2,3]' LIMIT 10;
```

### HNSW

```sql
-- Create index with default ef_search
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops) 
    WITH (default_ef_search = 100);

-- Queries automatically use ef_search=100
SELECT * FROM items ORDER BY embedding <-> '[1,2,3]' LIMIT 10;
```

### Modify After Creation

```sql
ALTER INDEX idx_items SET (default_probes = 15);
ALTER INDEX idx_items RESET (default_probes);  -- Remove default
```

## Testing

All 14 regression tests pass:

```
ok 1  - bit                                        43 ms
ok 2  - btree                                     113 ms
ok 3  - cast                                       69 ms
ok 4  - copy                                      125 ms
ok 5  - halfvec                                    92 ms
ok 6  - hnsw_bit                                   93 ms
ok 7  - hnsw_halfvec                              360 ms
ok 8  - hnsw_sparsevec                            299 ms
ok 9  - hnsw_vector                               360 ms
ok 10 - ivfflat_bit                                76 ms
ok 11 - ivfflat_halfvec                           262 ms
ok 12 - ivfflat_vector                            312 ms
ok 13 - sparsevec                                  52 ms
ok 14 - vector_type                                68 ms
# All 14 tests passed.
```

### Test Coverage

- Index default used when GUC not explicitly SET
- Explicit SET overrides index default
- RESET returns to using index default
- ALTER INDEX can modify defaults without rebuild
- Sentinel value (0) acts as unset
- Invalid values rejected

## Acceptance Criteria

- [x] IVFFlat index with `default_probes = N` uses N probes during search
- [x] HNSW index with `default_ef_search = N` uses ef_search of N during search
- [x] Explicit `SET` in session overrides any index default
- [x] Indexes without new options behave identically to current behavior
- [x] Query planner cost estimates reflect per-index defaults
- [x] ALTER INDEX can add, modify, or remove default search parameters
- [x] Index default value of 0 treated as "unset"
- [x] SET LOCAL within a transaction overrides index defaults

## Deployment Considerations

- **No SQL migration required** - Index options are automatically available after C code registration
- **Backward compatible** - Existing indexes without new options behave identically to current behavior
- **No data migration needed** - Options stored in index metadata automatically

## Breaking Changes

None. This is a purely additive feature that preserves full backward compatibility.

inline patch 1: CHANGELOG.md, README.md, src/hnsw.c and 11 more (+318/-8 lines) download inline-1.patchdiff --git a/CHANGELOG.md b/CHANGELOG.md index 8f74ea89f..870c8e260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.8.2 (unreleased) +- Added `default_probes` and `default_ef_search` index options for per-index search defaults - Improved `install` target on Windows - Fixed `Index Searches` in `EXPLAIN` output for Postgres 18 diff --git a/README.md b/README.md index a1085204f..76b5d0f7f 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,7 @@ Specify HNSW parameters - `m` - the max number of connections per layer (16 by default) - `ef_construction` - the size of the dynamic candidate list for constructing the graph (64 by default) +- `default_ef_search` - the default ef_search for queries using this index (unset by default) ```sql CREATE INDEX ON items USING hnsw (embedding vector_l2_ops) WITH (m = 16, ef_construction = 64); @@ -265,6 +266,12 @@ CREATE INDEX ON items USING hnsw (embedding vector_l2_ops) WITH (m = 16, ef_cons A higher value of `ef_construction` provides better recall at the cost of index build time / insert speed. +Use `default_ef_search` to set a per-index search default (overridden by explicit `SET hnsw.ef_search`) + +```sql +CREATE INDEX ON items USING hnsw (embedding vector_l2_ops) WITH (default_ef_search = 100); +``` + ### Query Options Specify the size of the dynamic candidate list for search (40 by default) @@ -371,6 +378,19 @@ Supported types are: - `halfvec` - up to 4,000 dimensions - `bit` - up to 64,000 dimensions +### Index Options + +Specify IVFFlat parameters + +- `lists` - the number of inverted lists (required) +- `default_probes` - the default number of probes for queries using this index (unset by default) + +Use `default_probes` to set a per-index search default (overridden by explicit `SET ivfflat.probes`) + +```sql +CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100, default_probes = 10); +``` + ### Query Options Specify the number of probes (1 by default) diff --git a/src/hnsw.c b/src/hnsw.c index 1d56ef6ea..8d5d29825 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -82,6 +82,8 @@ HnswInit(void) HNSW_DEFAULT_M, HNSW_MIN_M, HNSW_MAX_M, AccessExclusiveLock); add_int_reloption(hnsw_relopt_kind, "ef_construction", "Size of the dynamic candidate list for construction", HNSW_DEFAULT_EF_CONSTRUCTION, HNSW_MIN_EF_CONSTRUCTION, HNSW_MAX_EF_CONSTRUCTION, AccessExclusiveLock); + add_int_reloption(hnsw_relopt_kind, "default_ef_search", "Default ef_search for searches", + 0, 0, HNSW_MAX_EF_SEARCH, AccessExclusiveLock); DefineCustomIntVariable("hnsw.ef_search", "Sets the size of the dynamic candidate list for search", "Valid range is 1..1000.", &hnsw_ef_search, @@ -158,7 +160,6 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, index = index_open(path->indexinfo->indexoid, NoLock); HnswGetMetaPageInfo(index, &m, NULL); - index_close(index, NoLock); /* * HNSW cost estimation follows a formula that accounts for the total @@ -190,9 +191,10 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, if (path->indexinfo->tuples > 0) { double scalingFactor = 0.55; + int effectiveEfSearch = HnswGetEffectiveEfSearch(index); int entryLevel = (int) (log(path->indexinfo->tuples) * HnswGetMl(m)); - int layer0TuplesMax = HnswGetLayerM(m, 0) * hnsw_ef_search; - double layer0Selectivity = scalingFactor * log(path->indexinfo->tuples) / (log(m) * (1 + log(hnsw_ef_search))); + int layer0TuplesMax = HnswGetLayerM(m, 0) * effectiveEfSearch; + double layer0Selectivity = scalingFactor * log(path->indexinfo->tuples) / (log(m) * (1 + log(effectiveEfSearch))); ratio = (entryLevel * m + layer0TuplesMax * layer0Selectivity) / path->indexinfo->tuples; @@ -202,6 +204,8 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, else ratio = 1; + index_close(index, NoLock); + get_tablespace_page_costs(path->indexinfo->reltablespace, NULL, &spc_seq_page_cost); /* Startup cost is cost before returning the first row */ @@ -234,6 +238,7 @@ hnswoptions(Datum reloptions, bool validate) static const relopt_parse_elt tab[] = { {"m", RELOPT_TYPE_INT, offsetof(HnswOptions, m)}, {"ef_construction", RELOPT_TYPE_INT, offsetof(HnswOptions, efConstruction)}, + {"default_ef_search", RELOPT_TYPE_INT, offsetof(HnswOptions, defaultEfSearch)}, }; return (bytea *) build_reloptions(reloptions, validate, diff --git a/src/hnsw.h b/src/hnsw.h index 5102bfb5a..e0dad49ed 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -183,6 +183,7 @@ typedef struct HnswOptions int32 vl_len_; /* varlena header (do not touch directly!) */ int m; /* number of connections */ int efConstruction; /* size of dynamic candidate list */ + int defaultEfSearch; /* default ef_search for searches (0 = unset) */ } HnswOptions; typedef struct HnswGraph @@ -410,6 +411,8 @@ typedef struct HnswVacuumState /* Methods */ int HnswGetM(Relation index); int HnswGetEfConstruction(Relation index); +int HnswGetDefaultEfSearch(Relation index); +int HnswGetEffectiveEfSearch(Relation index); FmgrInfo *HnswOptionalProcInfo(Relation index, uint16 procnum); void HnswInitSupport(HnswSupport * support, Relation index); Datum HnswNormValue(const HnswTypeInfo * typeInfo, Oid collation, Datum value); diff --git a/src/hnswscan.c b/src/hnswscan.c index 5c526f4a6..b4c778bc5 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -41,7 +41,7 @@ GetScanItems(IndexScanDesc scan, Datum value) ep = w; } - return HnswSearchLayer(base, q, ep, hnsw_ef_search, 0, index, support, m, false, NULL, &so->v, hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF ? &so->discarded : NULL, true, &so->tuples); + return HnswSearchLayer(base, q, ep, HnswGetEffectiveEfSearch(index), 0, index, support, m, false, NULL, &so->v, hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF ? &so->discarded : NULL, true, &so->tuples); } /* @@ -54,7 +54,7 @@ ResumeScanItems(IndexScanDesc scan) Relation index = scan->indexRelation; List *ep = NIL; char *base = NULL; - int batch_size = hnsw_ef_search; + int batch_size = HnswGetEffectiveEfSearch(index); if (pairingheap_is_empty(so->discarded)) return NIL; diff --git a/src/hnswutils.c b/src/hnswutils.c index 8e2a42c1d..96f3bd8fc 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -12,6 +12,7 @@ #include "sparsevec.h" #include "storage/bufmgr.h" #include "utils/datum.h" +#include "utils/guc_tables.h" #include "utils/memdebug.h" #include "utils/rel.h" @@ -132,6 +133,46 @@ HnswGetEfConstruction(Relation index) return HNSW_DEFAULT_EF_CONSTRUCTION; } +/* + * Get the default ef_search for the index + */ +int +HnswGetDefaultEfSearch(Relation index) +{ + HnswOptions *opts = (HnswOptions *) index->rd_options; + + if (opts) + return opts->defaultEfSearch; + + return 0; +} + +/* + * Get effective ef_search for the index, implementing precedence: + * 1. Explicit SET hnsw.ef_search takes precedence + * 2. Index default_ef_search if set (> 0) + * 3. GUC default value + */ +int +HnswGetEffectiveEfSearch(Relation index) +{ + struct config_generic *record; + int defaultEfSearch; + + /* Check if GUC was explicitly set in session */ + record = find_option("hnsw.ef_search", false, true, ERROR); + if (record != NULL && record->source == PGC_S_SESSION) + return hnsw_ef_search; + + /* Check for index default */ + defaultEfSearch = HnswGetDefaultEfSearch(index); + if (defaultEfSearch > 0) + return defaultEfSearch; + + /* Fall back to GUC default */ + return hnsw_ef_search; +} + /* * Get proc */ diff --git a/src/ivfflat.c b/src/ivfflat.c index 31c2f7d5e..6c80fcbe5 100644 --- a/src/ivfflat.c +++ b/src/ivfflat.c @@ -36,6 +36,8 @@ IvfflatInit(void) ivfflat_relopt_kind = add_reloption_kind(); add_int_reloption(ivfflat_relopt_kind, "lists", "Number of inverted lists", IVFFLAT_DEFAULT_LISTS, IVFFLAT_MIN_LISTS, IVFFLAT_MAX_LISTS, AccessExclusiveLock); + add_int_reloption(ivfflat_relopt_kind, "default_probes", "Default number of probes for searches", + 0, 0, IVFFLAT_MAX_LISTS, AccessExclusiveLock); DefineCustomIntVariable("ivfflat.probes", "Sets the number of probes", "Valid range is 1..lists.", &ivfflat_probes, @@ -112,10 +114,11 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, index = index_open(path->indexinfo->indexoid, NoLock); IvfflatGetMetaPageInfo(index, &lists, NULL); - index_close(index, NoLock); /* Get the ratio of lists that we need to visit */ - ratio = ((double) ivfflat_probes) / lists; + ratio = ((double) IvfflatGetEffectiveProbes(index)) / lists; + + index_close(index, NoLock); if (ratio > 1.0) ratio = 1.0; @@ -153,6 +156,7 @@ ivfflatoptions(Datum reloptions, bool validate) { static const relopt_parse_elt tab[] = { {"lists", RELOPT_TYPE_INT, offsetof(IvfflatOptions, lists)}, + {"default_probes", RELOPT_TYPE_INT, offsetof(IvfflatOptions, defaultProbes)}, }; return (bytea *) build_reloptions(reloptions, validate, diff --git a/src/ivfflat.h b/src/ivfflat.h index c4e64e1dc..4d45ea6c3 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -117,6 +117,7 @@ typedef struct IvfflatOptions { int32 vl_len_; /* varlena header (do not touch directly!) */ int lists; /* number of lists */ + int defaultProbes; /* default probes for searches (0 = unset) */ } IvfflatOptions; typedef struct IvfflatSpool @@ -314,6 +315,8 @@ FmgrInfo *IvfflatOptionalProcInfo(Relation index, uint16 procnum); Datum IvfflatNormValue(const IvfflatTypeInfo * typeInfo, Oid collation, Datum value); bool IvfflatCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value); int IvfflatGetLists(Relation index); +int IvfflatGetDefaultProbes(Relation index); +int IvfflatGetEffectiveProbes(Relation index); void IvfflatGetMetaPageInfo(Relation index, int *lists, int *dimensions); void IvfflatUpdateList(Relation index, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum); void IvfflatCommitBuffer(Buffer buf, GenericXLogState *state); diff --git a/src/ivfscan.c b/src/ivfscan.c index b42ada15d..f14c62722 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -245,7 +245,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) IvfflatScanOpaque so; int lists; int dimensions; - int probes = ivfflat_probes; + int probes = IvfflatGetEffectiveProbes(index); int maxProbes; MemoryContext oldCtx; diff --git a/src/ivfutils.c b/src/ivfutils.c index 9596391be..b2e809812 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -8,6 +8,7 @@ #include "halfvec.h" #include "ivfflat.h" #include "storage/bufmgr.h" +#include "utils/guc_tables.h" /* * Allocate a vector array @@ -52,6 +53,46 @@ IvfflatGetLists(Relation index) return IVFFLAT_DEFAULT_LISTS; } +/* + * Get the default probes for the index + */ +int +IvfflatGetDefaultProbes(Relation index) +{ + IvfflatOptions *opts = (IvfflatOptions *) index->rd_options; + + if (opts) + return opts->defaultProbes; + + return 0; +} + +/* + * Get effective probes for the index, implementing precedence: + * 1. Explicit SET ivfflat.probes takes precedence + * 2. Index default_probes if set (> 0) + * 3. GUC default value + */ +int +IvfflatGetEffectiveProbes(Relation index) +{ + struct config_generic *record; + int defaultProbes; + + /* Check if GUC was explicitly set in session */ + record = find_option("ivfflat.probes", false, true, ERROR); + if (record != NULL && record->source == PGC_S_SESSION) + return ivfflat_probes; + + /* Check for index default */ + defaultProbes = IvfflatGetDefaultProbes(index); + if (defaultProbes > 0) + return defaultProbes; + + /* Fall back to GUC default */ + return ivfflat_probes; +} + /* * Get proc */ diff --git a/test/expected/hnsw_vector.out b/test/expected/hnsw_vector.out index cb4d8b97b..456ea579f 100644 --- a/test/expected/hnsw_vector.out +++ b/test/expected/hnsw_vector.out @@ -199,3 +199,62 @@ ERROR: 0 is outside the valid range for parameter "hnsw.scan_mem_multiplier" (1 SET hnsw.scan_mem_multiplier = 1001; ERROR: 1001 is outside the valid range for parameter "hnsw.scan_mem_multiplier" (1 .. 1000) DROP TABLE t; +-- default_ef_search option tests +-- Test: Create index with default_ef_search +CREATE TABLE t_des (val vector(3)); +INSERT INTO t_des (val) SELECT ARRAY[random(), random(), random()]::vector FROM generate_series(1, 100); +CREATE INDEX idx_des ON t_des USING hnsw (val vector_l2_ops) WITH (default_ef_search = 100); +-- Test: Query uses index default (100 ef_search) +SET enable_seqscan = off; +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: Explicit SET overrides index default +SET hnsw.ef_search = 50; +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: RESET returns to index default +RESET hnsw.ef_search; +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: ALTER INDEX changes default +ALTER INDEX idx_des SET (default_ef_search = 200); +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: ALTER INDEX RESET removes default (falls back to GUC default) +ALTER INDEX idx_des RESET (default_ef_search); +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: default_ef_search = 0 acts as unset (use GUC default) +CREATE INDEX idx_des_zero ON t_des USING hnsw (val vector_l2_ops) WITH (default_ef_search = 0); +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +DROP INDEX idx_des_zero; +-- Test: Invalid values rejected +CREATE INDEX ON t_des USING hnsw (val vector_l2_ops) WITH (default_ef_search = -1); +ERROR: value -1 out of bounds for option "default_ef_search" +DETAIL: Valid values are between "0" and "1000". +DROP TABLE t_des; diff --git a/test/expected/ivfflat_vector.out b/test/expected/ivfflat_vector.out index aad8226c1..ef05dfb19 100644 --- a/test/expected/ivfflat_vector.out +++ b/test/expected/ivfflat_vector.out @@ -173,3 +173,62 @@ ERROR: 0 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32 SET ivfflat.max_probes = 32769; ERROR: 32769 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768) DROP TABLE t; +-- default_probes option tests +-- Test: Create index with default_probes +CREATE TABLE t_dp (val vector(3)); +INSERT INTO t_dp (val) SELECT ARRAY[random(), random(), random()]::vector FROM generate_series(1, 100); +CREATE INDEX idx_dp ON t_dp USING ivfflat (val vector_l2_ops) WITH (lists = 10, default_probes = 5); +-- Test: Query uses index default (5 probes) +SET enable_seqscan = off; +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: Explicit SET overrides index default +SET ivfflat.probes = 3; +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: RESET returns to index default +RESET ivfflat.probes; +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: ALTER INDEX changes default +ALTER INDEX idx_dp SET (default_probes = 8); +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: ALTER INDEX RESET removes default (falls back to GUC default) +ALTER INDEX idx_dp RESET (default_probes); +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +-- Test: default_probes = 0 acts as unset (use GUC default) +CREATE INDEX idx_dp_zero ON t_dp USING ivfflat (val vector_l2_ops) WITH (lists = 10, default_probes = 0); +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + count +------- + 5 +(1 row) + +DROP INDEX idx_dp_zero; +-- Test: Invalid values rejected +CREATE INDEX ON t_dp USING ivfflat (val vector_l2_ops) WITH (default_probes = -1); +ERROR: value -1 out of bounds for option "default_probes" +DETAIL: Valid values are between "0" and "32768". +DROP TABLE t_dp; diff --git a/test/sql/hnsw_vector.sql b/test/sql/hnsw_vector.sql index a928ba44e..e0c1d3b60 100644 --- a/test/sql/hnsw_vector.sql +++ b/test/sql/hnsw_vector.sql @@ -115,3 +115,40 @@ SET hnsw.scan_mem_multiplier = 0; SET hnsw.scan_mem_multiplier = 1001; DROP TABLE t; + +-- default_ef_search option tests + +-- Test: Create index with default_ef_search +CREATE TABLE t_des (val vector(3)); +INSERT INTO t_des (val) SELECT ARRAY[random(), random(), random()]::vector FROM generate_series(1, 100); +CREATE INDEX idx_des ON t_des USING hnsw (val vector_l2_ops) WITH (default_ef_search = 100); + +-- Test: Query uses index default (100 ef_search) +SET enable_seqscan = off; +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: Explicit SET overrides index default +SET hnsw.ef_search = 50; +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: RESET returns to index default +RESET hnsw.ef_search; +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: ALTER INDEX changes default +ALTER INDEX idx_des SET (default_ef_search = 200); +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: ALTER INDEX RESET removes default (falls back to GUC default) +ALTER INDEX idx_des RESET (default_ef_search); +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: default_ef_search = 0 acts as unset (use GUC default) +CREATE INDEX idx_des_zero ON t_des USING hnsw (val vector_l2_ops) WITH (default_ef_search = 0); +SELECT COUNT(*) FROM (SELECT * FROM t_des ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; +DROP INDEX idx_des_zero; + +-- Test: Invalid values rejected +CREATE INDEX ON t_des USING hnsw (val vector_l2_ops) WITH (default_ef_search = -1); + +DROP TABLE t_des; diff --git a/test/sql/ivfflat_vector.sql b/test/sql/ivfflat_vector.sql index 4c20d2faf..6af099e61 100644 --- a/test/sql/ivfflat_vector.sql +++ b/test/sql/ivfflat_vector.sql @@ -97,3 +97,40 @@ SET ivfflat.max_probes = 0; SET ivfflat.max_probes = 32769; DROP TABLE t; + +-- default_probes option tests + +-- Test: Create index with default_probes +CREATE TABLE t_dp (val vector(3)); +INSERT INTO t_dp (val) SELECT ARRAY[random(), random(), random()]::vector FROM generate_series(1, 100); +CREATE INDEX idx_dp ON t_dp USING ivfflat (val vector_l2_ops) WITH (lists = 10, default_probes = 5); + +-- Test: Query uses index default (5 probes) +SET enable_seqscan = off; +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: Explicit SET overrides index default +SET ivfflat.probes = 3; +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: RESET returns to index default +RESET ivfflat.probes; +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: ALTER INDEX changes default +ALTER INDEX idx_dp SET (default_probes = 8); +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: ALTER INDEX RESET removes default (falls back to GUC default) +ALTER INDEX idx_dp RESET (default_probes); +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; + +-- Test: default_probes = 0 acts as unset (use GUC default) +CREATE INDEX idx_dp_zero ON t_dp USING ivfflat (val vector_l2_ops) WITH (lists = 10, default_probes = 0); +SELECT COUNT(*) FROM (SELECT * FROM t_dp ORDER BY val <-> '[0.5, 0.5, 0.5]' LIMIT 5) t; +DROP INDEX idx_dp_zero; + +-- Test: Invalid values rejected +CREATE INDEX ON t_dp USING ivfflat (val vector_l2_ops) WITH (default_probes = -1); + +DROP TABLE t_dp;

view thread (2+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-933@github.com>
Permalink:  ../gh-pgvector-pgvector-933@github.com/

 ·  · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox