From e3926ab8289ff02c58de52842a7a3c9cbf2c8472 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Wed, 21 Oct 2026 10:31:11 -0400 Subject: [PATCH] fix(db): chunk drizzle json_build_array under postgres argument cap Postgres caps function arguments at 100. `payload_locked_documents_rels` holds one foreign-key column per collection, and Drizzle hydrates the locked document's polymorphic relationship with a single `json_build_array(...)` over all of them. At 103 collections the query throws 54023 and every admin edit view breaks while a lock row exists. Patch drizzle-orm 0.45.2 to split the call into 100-argument groups joined with `::jsonb || ::jsonb`, mirroring the unmerged upstream PR drizzle-team/drizzle-orm#4495. Relations with 100 or fewer columns keep the original code path unchanged. Add tests/int/locked-documents-query.int.spec.ts, which creates a lock row and reads it back at depth 1 (what getIsLocked and handleFormStateLocking do). It fails on pristine drizzle with the exact 54023 error and passes with the patch. --- bun.lock | 1 + package.json | 3 +- patches/drizzle-orm@0.45.2.patch | 54 +++++++++++++ tests/int/locked-documents-query.int.spec.ts | 81 ++++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 patches/drizzle-orm@0.45.2.patch create mode 100644 tests/int/locked-documents-query.int.spec.ts diff --git a/bun.lock b/bun.lock index ebde95da..cb8aeeea 100644 --- a/bun.lock +++ b/bun.lock @@ -103,6 +103,7 @@ "patchedDependencies": { "@payloadcms/ui@3.88.0": "patches/@payloadcms%2Fui@3.88.0.patch", "@ai-stack/payloadcms@3.2.30": "patches/@ai-stack%2Fpayloadcms@3.2.30.patch", + "drizzle-orm@0.45.2": "patches/drizzle-orm@0.45.2.patch", }, "packages": { "@acemir/cssom": ["@acemir/cssom@0.9.29", "", {}, "sha512-G90x0VW+9nW4dFajtjCoT+NM0scAfH9Mb08IcjgFHYbfiL/lU04dTF9JuVOi3/OH+DJCQdcIseSXkdCB9Ky6JA=="], diff --git a/package.json b/package.json index 2a9c90a9..c985409b 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,7 @@ "packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0", "patchedDependencies": { "@ai-stack/payloadcms@3.2.30": "patches/@ai-stack%2Fpayloadcms@3.2.30.patch", - "@payloadcms/ui@3.88.0": "patches/@payloadcms%2Fui@3.88.0.patch" + "@payloadcms/ui@3.88.0": "patches/@payloadcms%2Fui@3.88.0.patch", + "drizzle-orm@0.45.2": "patches/drizzle-orm@0.45.2.patch" } } diff --git a/patches/drizzle-orm@0.45.2.patch b/patches/drizzle-orm@0.45.2.patch new file mode 100644 index 00000000..e1021e76 --- /dev/null +++ b/patches/drizzle-orm@0.45.2.patch @@ -0,0 +1,54 @@ +diff --git a/pg-core/dialect.js b/pg-core/dialect.js +--- a/pg-core/dialect.js ++++ b/pg-core/dialect.js +@@ -1061,12 +1061,17 @@ + let result; + where = and(joinOn, where); + if (nestedQueryRelation) { +- let field = sql`json_build_array(${sql.join( +- selection.map( +- ({ field: field2, tsKey, isJson }) => isJson ? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier("data")}` : is(field2, SQL.Aliased) ? field2.sql : field2 +- ), +- sql`, ` +- )})`; ++ const fieldChunks = []; ++ for (let i = 0; i < selection.length; i += 100) { ++ const chunk = selection.slice(i, i + 100); ++ fieldChunks.push(sql`json_build_array(${sql.join( ++ chunk.map( ++ ({ field: field2, tsKey, isJson }) => isJson ? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier("data")}` : is(field2, SQL.Aliased) ? field2.sql : field2 ++ ), ++ sql`, ` ++ )})`); ++ } ++ let field = fieldChunks.length === 1 ? fieldChunks[0] : fieldChunks.reduce((acc, chunk) => sql`${acc}::jsonb || ${chunk}::jsonb`); + if (is(nestedQueryRelation, Many)) { + field = sql`coalesce(json_agg(${field}${orderBy.length > 0 ? sql` order by ${sql.join(orderBy, sql`, `)}` : void 0}), '[]'::json)`; + } +diff --git a/pg-core/dialect.cjs b/pg-core/dialect.cjs +--- a/pg-core/dialect.cjs ++++ b/pg-core/dialect.cjs +@@ -1063,12 +1063,17 @@ + let result; + where = (0, import_sql.and)(joinOn, where); + if (nestedQueryRelation) { +- let field = import_sql2.sql`json_build_array(${import_sql2.sql.join( +- selection.map( +- ({ field: field2, tsKey, isJson }) => isJson ? import_sql2.sql`${import_sql2.sql.identifier(`${tableAlias}_${tsKey}`)}.${import_sql2.sql.identifier("data")}` : (0, import_entity.is)(field2, import_sql2.SQL.Aliased) ? field2.sql : field2 +- ), +- import_sql2.sql`, ` +- )})`; ++ const fieldChunks = []; ++ for (let i = 0; i < selection.length; i += 100) { ++ const chunk = selection.slice(i, i + 100); ++ fieldChunks.push(import_sql2.sql`json_build_array(${import_sql2.sql.join( ++ chunk.map( ++ ({ field: field2, tsKey, isJson }) => isJson ? import_sql2.sql`${import_sql2.sql.identifier(`${tableAlias}_${tsKey}`)}.${import_sql2.sql.identifier("data")}` : (0, import_entity.is)(field2, import_sql2.SQL.Aliased) ? field2.sql : field2 ++ ), ++ import_sql2.sql`, ` ++ )})`); ++ } ++ let field = fieldChunks.length === 1 ? fieldChunks[0] : fieldChunks.reduce((acc, chunk) => import_sql2.sql`${acc}::jsonb || ${chunk}::jsonb`); + if ((0, import_entity.is)(nestedQueryRelation, import_relations.Many)) { + field = import_sql2.sql`coalesce(json_agg(${field}${orderBy.length > 0 ? import_sql2.sql` order by ${import_sql2.sql.join(orderBy, import_sql2.sql`, `)}` : void 0}), '[]'::json)`; + } diff --git a/tests/int/locked-documents-query.int.spec.ts b/tests/int/locked-documents-query.int.spec.ts new file mode 100644 index 00000000..3b79c95a --- /dev/null +++ b/tests/int/locked-documents-query.int.spec.ts @@ -0,0 +1,81 @@ +import { getPayload, Payload } from "payload"; +import config from "@/payload.config"; + +import { beforeAll, describe, expect, it } from "vitest"; + +let payload: Payload; + +/** + * Regression test for the Postgres `FUNC_MAX_ARGS` limit (error 54023, + * "cannot pass more than 100 arguments to a function"). + * + * Document locking stores one foreign-key column per collection in + * `payload_locked_documents_rels`. Drizzle hydrates polymorphic relationships + * with a single `json_build_array(...)` that includes every one of those + * columns; once the unit has more than ~100 collections that call exceeds + * Postgres' hard limit and every admin edit view throws while a lock row + * exists. The local bun patch on drizzle-orm chunks the call into groups of + * 100 joined with `||`. + * + * This test creates a real lock row so the hydration query actually runs + * (with zero matching locks Payload skips the wide relational select), then + * reads it back with `depth: 1`, which is exactly what `getIsLocked` and + * `handleFormStateLocking` do. + */ +describe("payload-locked-documents relational query", () => { + beforeAll(async () => { + payload = await getPayload({ config: await config }); + }); + + it("hydrates a lock document at depth 1 without exceeding the argument limit", async () => { + const roles = await payload.find({ collection: "roles", limit: 1, overrideAccess: true }); + expect(roles.docs.length).toBeGreaterThan(0); + const roleId = roles.docs[0].id; + + const username = `lock-query-${Date.now()}`; + const user = await payload.create({ + collection: "users", + overrideAccess: true, + data: { + username, + discordUsername: username, + displayName: "Lock Query Tester", + steamId: `7656119${Math.floor(Math.random() * 1e9)}`, + password: "Test123", + }, + }); + + const lock = await payload.create({ + collection: "payload-locked-documents", + overrideAccess: true, + data: { + document: { relationTo: "roles", value: roleId }, + user: { relationTo: "users", value: user.id }, + }, + }); + + try { + const lockDocs = await payload.find({ + collection: "payload-locked-documents", + depth: 1, + limit: 1, + overrideAccess: true, + where: { + and: [ + { "document.relationTo": { equals: "roles" } }, + { "document.value": { equals: roleId } }, + ], + }, + }); + + expect(lockDocs.docs).toHaveLength(1); + expect(lockDocs.docs[0].id).toBe(lock.id); + } finally { + await payload.delete({ + collection: "payload-locked-documents", + id: lock.id, + overrideAccess: true, + }); + } + }); +});