@ultimat3/db 31.49.0 → 31.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -315,7 +315,7 @@ commit what it writes, ship. The drop is its own non-transactional migration.
315
315
 
316
316
  | API | |
317
317
  |---|---|
318
- | `enableRowSecuritySql(table, column, type)` | `enable` + `force` + one `ultimate_tenant` policy: `using` and `with check` both `column = nullif(current_setting('x.tenant', true), '')::type or current_setting('x.cross', true) = 'on'`. Unset matches nothing |
318
+ | `enableRowSecuritySql(table, column, type)` | `enable` + `force` + one `ultimate_tenant` policy: `using` and `with check` both `case when current_setting('x.cross', true) = 'on' then true else column = nullif(current_setting('x.tenant', true), '')::type end`. Unset matches nothing. A `case` and not an `or` (`As of 2026-11`): the same rows, but the planner's guess for an `or` turned a tenant's keyset page into a bitmap scan and a sort; a `case` keeps the ordered index scan. A table generated before keeps its `or` policy until `drop policy` / `create policy` with this text runs in a migration |
319
319
  | `rowSecureClient(client)` | a statement sent outside a transaction while the provider has a tenant is wrapped: `BEGIN [READ ONLY]`, `set_config`, the statement, `COMMIT` |
320
320
  | `assertRowSecurityBinds(client)` | `X_DB_ROW_SECURITY_BYPASSED` for a superuser, a `bypassrls` role, or the owner of an unforced table — Postgres exempts all three |
321
321
  | `rowSecurityRolesSql()` | `x_app` (`nosuperuser nobypassrls`) and `x_migrator` (`bypassrls`, owns tables), with default privileges from one to the other, as text |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/db",
3
- "version": "31.49.0",
3
+ "version": "31.50.0",
4
4
  "description": "Postgres access, transactions, migrations and drift detection",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -45,7 +45,7 @@
45
45
  "test": "bun test"
46
46
  },
47
47
  "dependencies": {
48
- "@ultimat3/core": "31.49.0"
48
+ "@ultimat3/core": "31.50.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@electric-sql/pglite": ">=0.5.0"
package/src/errors.ts CHANGED
@@ -308,6 +308,9 @@ export const driverError = (statement: string, sourceError: unknown): DbError =>
308
308
  // `X_DB_UNAVAILABLE` said "set DATABASE_URL" to a developer whose database was fine.
309
309
  const code = sqlStateCode(sourceError) ?? 'X_DB_STATEMENT_FAILED';
310
310
  const constraint = stringField(sourceError, 'constraint');
311
+ // The relation the constraint belongs to: a constraint name is unique per table, never per
312
+ // database, so a reader matching one by name must know whose it is.
313
+ const table = stringField(sourceError, 'table');
311
314
  // `08P01` is the protocol's general complaint, so the pooler's own sentence is what decides, and
312
315
  // this one reached no statement: it is the connection every statement of the role would use.
313
316
  if (
@@ -346,6 +349,7 @@ export const driverError = (statement: string, sourceError: unknown): DbError =>
346
349
  meta: {
347
350
  sqlState: state,
348
351
  ...(constraint === undefined ? {} : { constraint }),
352
+ ...(table === undefined ? {} : { table }),
349
353
  },
350
354
  sourceError,
351
355
  });
package/src/index.ts CHANGED
@@ -144,6 +144,7 @@ export {
144
144
  constraintNameFor,
145
145
  declaredIndexes,
146
146
  invariantChecks,
147
+ namesConstraint,
147
148
  uniqueColumns,
148
149
  } from './invariant-ddl';
149
150
  export { constraintNameUnsafe } from './invariant-errors';
@@ -19,6 +19,9 @@ import type { CheckDescription } from './introspect';
19
19
  import { constraintNameUnsafe } from './invariant-errors';
20
20
  import { identifier, MAX_IDENTIFIER_BYTES } from './sql';
21
21
 
22
+ /** The two fields a constraint's name is spelled from. */
23
+ type InvariantNaming = Pick<InvariantDescriptionLike, 'name' | 'kind'>;
24
+
22
25
  /**
23
26
  * `NAMEDATALEN - 1`. Postgres truncates a longer identifier and says nothing, so two constraints
24
27
  * sharing their first 63 bytes are ONE constraint on the server while both names still differ in
@@ -34,7 +37,7 @@ import { identifier, MAX_IDENTIFIER_BYTES } from './sql';
34
37
  * and `namesConstraint` can never disagree about what a rule's constraint is called — the two
35
38
  * questions "what do I emit" and "is this recorded constraint that rule's" are the same string.
36
39
  */
37
- function spellConstraintName(table: string, invariant: InvariantDescriptionLike): string {
40
+ function spellConstraintName(table: string, invariant: InvariantNaming): string {
38
41
  return `${table}_${invariant.name}_${invariant.kind === 'unique' ? 'key' : 'check'}`;
39
42
  }
40
43
 
@@ -51,7 +54,7 @@ function spellConstraintName(table: string, invariant: InvariantDescriptionLike)
51
54
  */
52
55
  export function namesConstraint(
53
56
  table: string,
54
- invariant: InvariantDescriptionLike,
57
+ invariant: InvariantNaming,
55
58
  recorded: string,
56
59
  ): boolean {
57
60
  if (!isIdentifier(recorded)) return false;
@@ -20,13 +20,21 @@ const TYPE = /^[a-z][a-z0-9 ]*$/;
20
20
 
21
21
  /**
22
22
  * Unset is `''` (a `set_config` local value reads back as `''` once its transaction ends) and
23
- * `nullif` turns it into NULL, so an unset tenant matches NO row: fail closed, never open.
23
+ * `nullif` turns it into NULL, so an unset tenant matches NO row: fail closed, never open. An unset
24
+ * cross switch reads NULL on a connection that never set it, and `''` on a pooled one whose earlier
25
+ * transaction set it locally; neither is `'on'`, so in both the `else` — the tenant — decides.
26
+ *
27
+ * A `case`, never `tenant or cross`, and the two read the same rows. The difference is the PLAN:
28
+ * the planner multiplies an `or` of two guessed selectivities (~5% together) into every row
29
+ * estimate, so a tenant's 2,500-row keyset page read as ~140 rows and was answered by a bitmap scan
30
+ * and a sort instead of the ordered index scan its `(org_id, …)` index exists for. A `case` is
31
+ * estimated at one half, which keeps that scan (`row-security-plan-shape.test.ts`).
24
32
  */
25
33
  function predicate(column: string, type: string): string {
26
34
  if (!TYPE.test(type)) throw identifierUnsafe(type);
27
35
  return (
28
- `(${identifier(column).text} = nullif(current_setting('${TENANT_SETTING}', true), '')::${type}` +
29
- ` or current_setting('${CROSS_TENANT_SETTING}', true) = 'on')`
36
+ `(case when current_setting('${CROSS_TENANT_SETTING}', true) = 'on' then true` +
37
+ ` else ${identifier(column).text} = nullif(current_setting('${TENANT_SETTING}', true), '')::${type} end)`
30
38
  );
31
39
  }
32
40