{"record":{"id":"3142a362b0f6e8b4","repo":"cube-js/cube","slug":"postgresql-protocol-does-not-support-more-than-655","errorCode":null,"errorMessage":"PostgreSQL protocol does not support more than 65535 parameters, but ${length} passed","messagePattern":"PostgreSQL protocol does not support more than 65535 parameters, but (.+?) passed","errorType":"exception","errorClass":"PostgresError","httpStatus":null,"severity":"error","filePath":"packages/cubejs-postgres-driver/src/PostgresDriver.ts","lineNumber":413,"sourceCode":"        }\n      };\n    } catch (e) {\n      await this.pool.release(conn);\n\n      throw e;\n    }\n  }\n\n  protected static checkValuesLimit(values?: unknown[]) {\n    // PostgreSQL protocol allows sending up to 65535 params in a single bind message\n    // See https://github.com/postgres/postgres/blob/REL_16_0/src/backend/tcop/postgres.c#L1698-L1708\n    // See https://github.com/postgres/postgres/blob/REL_16_0/src/backend/libpq/pqformat.c#L428-L431\n    // But 'pg' module does not check for params count, and ends up sending incorrect bind message\n    // See https://github.com/brianc/node-postgres/blob/92cb640fd316972e323ced6256b2acd89b1b58e0/packages/pg-protocol/src/serializer.ts#L155\n    // See https://github.com/brianc/node-postgres/blob/92cb640fd316972e323ced6256b2acd89b1b58e0/packages/pg-protocol/src/buffer-writer.ts#L32-L37\n    const length = (values?.length ?? 0);\n    if (length >= 65536) {\n      throw new PostgresError(`PostgreSQL protocol does not support more than 65535 parameters, but ${length} passed`);\n    }\n  }\n\n  protected async withConnection<T>(fn: (conn: PgClient) => Promise<T>): Promise<T> {\n    const conn = await this.pool.acquire();\n\n    try {\n      return await fn(conn);\n    } finally {\n      await this.pool.release(conn);\n    }\n  }\n\n  protected async queryResponse(query: string, values: unknown[]) {\n    PostgresDriver.checkValuesLimit(values);\n\n    return this.withConnection(async (conn) => {\n      await this.prepareConnection(conn);","sourceCodeStart":395,"sourceCodeEnd":431,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-postgres-driver/src/PostgresDriver.ts#L395-L431","documentation":"The PostgreSQL wire protocol caps bind-message parameters at 65535 (the count field is a 16-bit integer). The pg module does not validate this and would send a corrupted bind message, so PostgresDriver.checkValuesLimit proactively throws when the parameter array length reaches 65536 or more.","triggerScenarios":"Executing a parameterized query (e.g., setParameterTypes/bind path used by query/downloadQueryResults) where the values array has >= 65536 entries — typically a giant IN-list or bulk insert built by the caller.","commonSituations":"Uploading very large tables in one statement; generating filters with tens of thousands of values; custom code passing unbatched row data to driver queries.","solutions":["Split the query into batches, keeping each under 65535 parameters (e.g., chunk the IN-list or use BatchInsert via the driver's uploadTable API).","Use a temporary/staging table plus JOIN instead of a huge IN-list.","Reduce parameter count with range predicates (BETWEEN) or unnest/array passing where appropriate.","Use the driver's built-in uploadTableWithIndexes flow which batches inserts (~1000 rows per batch in MySQL driver equivalents)."],"exampleFix":"// before\nawait driver.query(`SELECT * FROM t WHERE id IN (${ids.map((_, i) => `$${i + 1}`).join(',')})`, ids); // ids.length = 100000\n// after\nfor (const chunk of _.chunk(ids, 50000)) {\n  await driver.query(`SELECT * FROM t WHERE id IN (${chunk.map((_, i) => `$${i + 1}`).join(',')})`, chunk);\n}","handlingStrategy":"validation","validationCode":"function assertParamsUnderLimit(values = []) {\n  if (values.length >= 65536) {\n    throw new Error(`${values.length} params exceeds the 65535 PostgreSQL protocol limit; batch the query`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await driver.query(sql, values);\n} catch (e) {\n  if (e.message.includes('65535 parameters')) {\n    console.error('Chunk the parameter list below 65536 or use a staging table');\n  }\n  throw e;\n}","preventionTips":["Chunk IN-lists well below 65535 parameters (e.g., 10k per batch).","Prefer staging tables + JOIN over huge parameterized filters.","Use range predicates instead of enumerating values.","Add a guard asserting values.length < 65536 before parameterized calls."],"tags":["postgres","protocol-limit","parameters","batching"],"backgroundTag":"too-many-parameters","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}