While creating a gin index in Postgres 9.5.8 I got the following error message:
ERROR: invalid memory alloc request size 2013265920
After a number of attempts to independently reproduce this issue, i have found that this error comes up when the following conditions are met:
- Table contains more than 200M rows
- The maintenance_work_mem is around 1GB or higher
Here is a test run which completes successfully:
postgres=# show maintenance_work_mem ;
maintenance_work_mem
----------------------
64MB
(1 row)
postgres=#
postgres=# CREATE TABLE gin_test (a int, b jsonb);
CREATE TABLE
postgres=# INSERT INTO gin_test (a) VALUES (generate_series(1,200000000,1));
INSERT 0 200000000
postgres=# CREATE INDEX CONCURRENTLY ON gin_test USING gin(b jsonb_path_ops);
CREATE INDEX
postgres=#
Here is another test run within the same psql session which fails. The only difference between the two is that maintenance_work_mem is set to 1GB:
postgres=# drop table gin_test ;
DROP TABLE
postgres=# set maintenance_work_mem = '1GB';
SET
postgres=# show maintenance_work_mem ;
maintenance_work_mem
----------------------
1GB
(1 row)
postgres=#
postgres=# CREATE TABLE gin_test (a int, b jsonb);
CREATE TABLE
postgres=# INSERT INTO gin_test (a) VALUES (generate_series(1,200000000,1));
INSERT 0 200000000
postgres=# CREATE INDEX CONCURRENTLY ON gin_test USING gin(b jsonb_path_ops);
ERROR: invalid memory alloc request size 2013265920
postgres=#
The index creation fails with the same error with and without "concurrently". Also the issue comes up without "jsonb_path_ops" as well. I have tested this on Linux and OSX.
I was not able to reproduce this issue in Postgres 9.6.4.
Regards
Yaser