You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bun:sql incorrectly serializes JS arrays in sql(object) for PostgreSQL array columns #29551
correctly serialize JS arrays for PostgreSQL array columns so the insert succeeds, or
throw a clear Bun-side error before sending malformed values to PostgreSQL if this usage is unsupported.
What do you see instead?
PostgreSQL receives malformed array values and returns:
PostgresError: malformed array literal: "A"
errno: "22P02",
detail: "Array value must start with \"{\" or dimension information.",
severity: "ERROR",
where: "unnamed portal parameter $1 = '...'",
file: "arrayfuncs.c",
routine: "array_in",
code: "ERR_POSTGRES_SERVER_ERROR"
Additional information
This issue is specifically about sql(object).
It is not a request for sql.array(...) as a workaround.
The problem is that sql(object) currently accepts object properties whose values are JS arrays, but appears to serialize them as malformed scalar/string values (for example "A" or "A,B") instead of a valid PostgreSQL array representation for text[] columns.
If arrays in sql(object) are unsupported, Bun should reject them explicitly before sending the query. (I would however advocate for the support of JS arrays since that has the potential to improve DX by dramatically shortening some SQL queries).
These look related, especially #22155 and #22281, and I agree they appear to describe the same underlying sql(object) / PostgreSQL array-column bug. The problem is that all three suggested issues are currently closed, while this still reproduces on Bun 1.3.13.
For clarity, this is specifically about sql(object) accepting JS arrays for PostgreSQL array columns and then sending malformed values that PostgreSQL rejects with malformed array literal.
✅ Reproduced — sql({ text_col: ["A"] }) sent comma-joined "A,B" to Postgres, which rejected it as a malformed text[] literal. Same bug affected int[], bool[], date[], jsonb[], bytea[], etc.
Fix in #29552 — serializes JS arrays as PostgreSQL text array literals when the server-inferred parameter type is any *_array OID. Scalar jsonb columns with array values continue to stringify as JSON.
Reacted by Jan Iłowski, Facundo Villa and Ievgen Makukh
I'm hitting this through sql.unsafe() too, which is how query builders pass parameters (Kysely's kysely-postgres-js dialect calls sql.unsafe(query, params)), so every write to an array column fails there. int4[] fails differently from the text types, with a protocol error instead of a malformed literal, uuid[] fails like text[], and sql.array() doesn't help as an unsafe() parameter:
import{SQL}from"bun";constsql=newSQL("postgres://postgres:postgres@localhost:5432/postgres");awaitsql`drop table if exists arrays`;awaitsql`create table arrays (ints int4[], texts text[], ids uuid[])`;constinserts={"int4[]": ()=>sql.unsafe("insert into arrays (ints) values ($1)",[[1,2]]),"text[]": ()=>sql.unsafe("insert into arrays (texts) values ($1)",[["a","b"]]),"uuid[]": ()=>sql.unsafe("insert into arrays (ids) values ($1)",[["0199d0b2-7a3c-7cc0-9b7e-3f6a1c2d4e5f"]]),"int4[] via sql.array()": ()=>sql.unsafe("insert into arrays (ints) values ($1)",[sql.array([1,2],"int4")]),};for(const[label,insert]ofObject.entries(inserts)){try{awaitinsert();console.log(label,"ok");}catch(error){console.log(label,error.code,error.message);}}awaitsql.close();
int4[] ERR_POSTGRES_SERVER_ERROR insufficient data left in message
text[] ERR_POSTGRES_SERVER_ERROR malformed array literal: "a,b"
uuid[] ERR_POSTGRES_SERVER_ERROR malformed array literal: "0199d0b2-7a3c-7cc0-9b7e-3f6a1c2d4e5f"
int4[] via sql.array() ERR_POSTGRES_SERVER_ERROR insufficient data left in message
#33579 and #41976 look like they'd cover these. Bun 1.4.3, PostgreSQL 18.6.
Activity
github-actions commentedon Apr 21, 2026
Found 3 possible duplicate issues:
sql()helper incorrectly formats PostgreSQL array columns, causing "malformed array literal" error #22155This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
janilowski commentedon Apr 21, 2026
These look related, especially #22155 and #22281, and I agree they appear to describe the same underlying
sql(object)/ PostgreSQL array-column bug. The problem is that all three suggested issues are currently closed, while this still reproduces on Bun 1.3.13.For clarity, this is specifically about
sql(object)accepting JS arrays for PostgreSQL array columns and then sending malformed values that PostgreSQL rejects withmalformed array literal.robobun commentedon Apr 21, 2026
✅ Reproduced —
sql({ text_col: ["A"] })sent comma-joined"A,B"to Postgres, which rejected it as a malformedtext[]literal. Same bug affectedint[],bool[],date[],jsonb[],bytea[], etc.Fix in #29552 — serializes JS arrays as PostgreSQL text array literals when the server-inferred parameter type is any
*_arrayOID. Scalarjsonbcolumns with array values continue to stringify as JSON.Revert "sql: mark parseArray's ArrayList as owned by the cell"
postgres: encode array parameters as text array literals
postgres: encode array parameters as text array literals
postgres: encode array parameters as text array literals
sql.unsafe()is serialized as a bare CSV, not an array literal #39878 as a duplicate of this issuetetsuodev commentedon Oct 12, 2026
I'm hitting this through
sql.unsafe()too, which is how query builders pass parameters (Kysely's kysely-postgres-js dialect callssql.unsafe(query, params)), so every write to an array column fails there.int4[]fails differently from the text types, with a protocol error instead of a malformed literal,uuid[]fails liketext[], andsql.array()doesn't help as anunsafe()parameter:#33579 and #41976 look like they'd cover these. Bun 1.4.3, PostgreSQL 18.6.
sql: consolidate the open Bun.sql, bun:sqlite and node:sqlite pull re…