electric-sql / pglite

Lightweight Postgres packaged as WASM into a TypeScript library for the browser, Node.js, Bun and Deno
https://electric-sql.com
Apache License 2.0
4.81k stars 82 forks source link

RuntimeError: memory access out of bounds #5

Closed nalgeon closed 4 months ago

nalgeon commented 4 months ago

I've found that PGlite works fine for simple queries, but fails with the "memory access out of bounds" for more complex queries (when using the browser version).

For example, this code:

import { PGlite } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/index.js";

const sql = `
create table department (
    dep_id varchar(10) primary key,
    name varchar(50),
    manager_id integer
);

create table employee (
    emp_id integer primary key,
    first_name varchar(50),
    last_name varchar(50),
    birth_dt date,
    hire_dt date,
    dep_id varchar(10),
    city varchar(50),
    salary integer,

    constraint department_fk foreign key (dep_id)
    references department(dep_id)
);

insert into department
(dep_id, name, manager_id)
values
('hr', 'HR', 12),
('it', 'IT', 24),
('sales', 'Sales', 31);

insert into employee
(emp_id, first_name, last_name, birth_dt, hire_dt, dep_id, city, salary)
values
(11, 'Diane', 'Wood', '1973-07-21', '2018-03-15', 'hr', 'London', 70),
(12, 'Bob', 'Yakamoto', '1985-11-09', '2019-07-22', 'hr', 'London', 78),
(21, 'Emma', 'Verde', '1978-03-05', '2020-11-08', 'it', 'London', 84),
(22, 'Grace', 'Tanner', '1994-10-17', '2021-02-27', 'it', 'Berlin', 90),
(23, 'Henry', 'Sivers', '1982-01-29', '2022-06-13', 'it', 'London', 104),
(24, 'Irene', 'Romen', '1971-05-14', '2018-09-04', 'it', 'Berlin', 104),
(25, 'Frank', 'Utrecht', '1989-08-23', '2019-12-19', 'it', 'Berlin', 120),
(31, 'Cindy', 'Xerst', '1976-12-06', '2020-05-26', 'sales', 'Berlin', 96),
(32, 'Dave', 'Walsh', '1999-04-08', '2021-08-03', 'sales', 'London', 96),
(33, 'Alice', 'Zakas', '1987-02-19', '2023-01-29', 'sales', 'Berlin', 100);

select 'ok' as message;
`

const db = new PGlite();
const res = await db.query(sql);
console.log(res);

Leads to the following error:

Uncaught (in promise) RuntimeError: memory access out of bounds
    at postgres.wasm:0x59d76
    at postgres.wasm:0x725c6
    at postgres.wasm:0x46ab2
    at postgres.wasm:0x9c8b61
    at postgres.wasm:0x9ddb89
    at ret.<computed> (postgres.js:9:128289)
    at invoke_vii (postgres.js:9:145948)
    at postgres.wasm:0x4ff2e8
    at postgres.wasm:0x882051
    at ret.<computed> (postgres.js:9:128289)

JSFiddle to reproduce the problem: https://jsfiddle.net/zgc5jy7o/

samwillis commented 4 months ago

This is now fixed in the latest version.