openvibers/openvibe.events Commits
A claim stops at the first pending delivery instead of sorting them all (#216)
Changes
nnew file mode 100644@@@ -0,0 +1,7 @@++-- phase: expand++-- The claim walks a subscription's pending deliveries in (priority, seq) order and takes the first claimable one++-- (store.js claimDeliveries / next). With only (status, next_attempt_at) and (subscription_id, status, seq) to use, it++-- read every pending row of the subscription and sorted them: 265 ms a claim with 74,000 pending for Search on++-- 2026-11-02 (one PostgreSQL core at 100%, ~190 deliveries a minute). This index lets the claim stop at the first++-- row: 0.4 ms. Created CONCURRENTLY on production the same day; IF NOT EXISTS makes this a no-op there.++CREATE INDEX IF NOT EXISTS idx_deliveries_claim ON deliveries (subscription_id, priority, seq) WHERE status IN ('pending', 'failed');nnew file mode 100644@@@ -0,0 +1,23 @@++'use strict';++/**++ * The claim walks a subscription's pending deliveries in (priority, seq) order and stops at the first claimable one++ * (migration 0011, idx_deliveries_claim). Without it, a claim read and sorted every pending row of the subscription:++ * 265 ms each with 74,000 pending for Search on 2026-11-02.++ *++ * node test/claim-index.test.js++ */++const assert = require('assert');++const { testDb } = require('./db');++++(async () => {++ const d = await testDb();++ try {++ const idx = await d.db.prepare("SELECT indexdef FROM pg_indexes WHERE tablename = 'deliveries' AND indexname = 'idx_deliveries_claim'").get();++ assert.ok(idx, 'the claim index exists after the migrations');++ assert.match(idx.indexdef, /\(subscription_id, priority, seq\)/);++ assert.match(idx.indexdef, /WHERE \(status = ANY \(ARRAY\['pending'::text, 'failed'::text\]\)\)/);++ } finally {++ await d.close();++ }++ console.log('claim index: all checks passed');++})().catch((err) => { console.error(err); process.exit(1); });
new file mode 100644
new file mode 100644
@@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
+-- phase: expand
+-- The claim walks a subscription's pending deliveries in (priority, seq) order and takes the first claimable one
+-- (store.js claimDeliveries / next). With only (status, next_attempt_at) and (subscription_id, status, seq) to use, it
+-- read every pending row of the subscription and sorted them: 265 ms a claim with 74,000 pending for Search on
+-- 2026-11-02 (one PostgreSQL core at 100%, ~190 deliveries a minute). This index lets the claim stop at the first
+-- row: 0.4 ms. Created CONCURRENTLY on production the same day; IF NOT EXISTS makes this a no-op there.
+CREATE INDEX IF NOT EXISTS idx_deliveries_claim ON deliveries (subscription_id, priority, seq) WHERE status IN ('pending', 'failed');
new file mode 100644
new file mode 100644
@@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
+'use strict';
+/**
+ * The claim walks a subscription's pending deliveries in (priority, seq) order and stops at the first claimable one
+ * (migration 0011, idx_deliveries_claim). Without it, a claim read and sorted every pending row of the subscription:
+ * 265 ms each with 74,000 pending for Search on 2026-11-02.
+ *
+ * node test/claim-index.test.js
+ */
+const assert = require('assert');
+const { testDb } = require('./db');
+
+(async () => {
+ const d = await testDb();
+ try {
+ const idx = await d.db.prepare("SELECT indexdef FROM pg_indexes WHERE tablename = 'deliveries' AND indexname = 'idx_deliveries_claim'").get();
+ assert.ok(idx, 'the claim index exists after the migrations');
+ assert.match(idx.indexdef, /\(subscription_id, priority, seq\)/);
+ assert.match(idx.indexdef, /WHERE \(status = ANY \(ARRAY\['pending'::text, 'failed'::text\]\)\)/);
+ } finally {
+ await d.close();
+ }
+ console.log('claim index: all checks passed');
+})().catch((err) => { console.error(err); process.exit(1); });