Postgres -> ERROR: invalid memory alloc request size 1212052384

Postgres -> ERROR: invalid memory alloc request size 1212052384

当 运行 向 table.

中插入查询时,我在 psql (10.4) 中收到此错误
ERROR:  invalid memory alloc request size 1212052384

我要插入的数据是地理点数据,我猜测(因为文件大小为 303MB)大约有 2-3 百万个点,即单个记录。这对于一次性 INSERT 来说太大了吗? sql 查询如下;它从文本文件复制 JSON 数据并插入数据库。尝试将单个形状存储为记录而不是点会更好,即更少的内存吗?

delete from shapes;

create temporary table temp_json (values text);
copy temp_json from '/Users/me/model/json/shapes_routes.json';
insert into shapes

select  values->>'shape_id' as shape_id,
        (CAST(values->>'shape_pt_lat' as real)) as shape_pt_lat,
        (CAST(values->>'shape_pt_lon' as real)) as shape_pt_lon,
        (CAST(values->>'shape_pt_sequence' as integer)) as shape_pt_sequence,
        (CAST(values->>'shape_dist_traveled' as real)) as shape_dist_traveled,
        values->>'route_id' as route_id

from   (
           select json_array_elements(replace(values,'\','\')::json) as values 
           from   temp_json
       ) a;

drop table temp_json;

在大多数情况下,关系数据库不能很好地处理大量数据的批处理,其中最重要的是 RDBMS 日志记录要求,它设置了一次事务中数据更改的最大限制。

移动大量数据是一个操作问题,因此操作方法是一个很好且合乎逻辑的选择。

在数据库外部(使用命令行工具)将文件分解为许多较小的文件,每个文件大小为 10K,然后像加载单个大文件一样加载它们 - 将分块逻辑 放在外部 数据库。

300MB 确实很小,我今天早些时候插入了 28GB。 (但我用了 COPY tablename FROM STDIN

您可以尝试将 a 重写为 CTE 而不是子查询。