Skip to content

Fix 'too many LWLocks taken' error on partitioned tables#135

Merged
tjgreen42 merged 1 commit intomainfrom
fix-lwlock-partitioned-tables
Jan 12, 2026
Merged

Fix 'too many LWLocks taken' error on partitioned tables#135
tjgreen42 merged 1 commit intomainfrom
fix-lwlock-partitioned-tables

Conversation

@tjgreen42
Copy link
Copy Markdown
Collaborator

@tjgreen42 tjgreen42 commented Jan 12, 2026

Summary

  • Fixes "too many LWLocks taken" error when creating BM25 indexes on partitioned tables with many partitions (>140)
  • Uses fixed LWLock tranche IDs instead of dynamically allocating new IDs per index
  • Releases per-index locks at the end of each partition index build

Fixes #134

Problem

When creating a BM25 index on a partitioned table, Postgres creates an index on each partition within a single transaction. The extension was:

  1. Allocating new LWLock tranche IDs for each index (exhausting available IDs)
  2. Holding per-index LWLocks until transaction end (exceeding MAX_SIMUL_LWLOCKS ~200)

This caused failures at around partition 142.

Solution

  • Define fixed tranche IDs (1001-1008) in constants.h for all extension LWLocks
  • Release the per-index lock at the end of tp_build() so locks can be reused

Testing

  • Tested with 600 partitions - all indexes built successfully
  • All 36 regression tests pass
  • All shell-based tests pass

@tjgreen42 tjgreen42 force-pushed the fix-lwlock-partitioned-tables branch 2 times, most recently from 0e71d61 to 7c65a0f Compare January 12, 2026 19:45
When creating a BM25 index on a partitioned table with many partitions
(>140), the index creation would fail with "too many LWLocks taken"
because:

1. Each partition index build acquires a per-index LWLock during
   document processing but doesn't release it until transaction end
2. Dynamic LWLock tranche IDs were allocated for each index, exhausting
   the available tranche IDs

This fix:
- Uses fixed tranche IDs (1001-1008) for all extension LWLocks instead
  of dynamically allocating new tranche IDs per index
- Releases the per-index lock at the end of each index build, allowing
  locks to be reused between partition index builds within the same
  transaction

Tested with 600 partitions successfully where previously it failed at
partition ~142.
@tjgreen42 tjgreen42 force-pushed the fix-lwlock-partitioned-tables branch from 7c65a0f to c36edaf Compare January 12, 2026 20:30
@tjgreen42 tjgreen42 merged commit b7f6539 into main Jan 12, 2026
12 checks passed
@tjgreen42 tjgreen42 deleted the fix-lwlock-partitioned-tables branch January 12, 2026 21:11
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.

Unable to create an index on table with large number of partitions

1 participant