I am trying to dump the entire contents of a large table, from the command line using pqsl
, but am running into a problem where memory usage goes up, to the point where the process is killed, before any data is even dumped.
What I don't understand is: why isn't the query returning results immediately, and completing without running out of memory?
Here is an explanation of exactly what I'm attempting:
I have a table, say:
CREATE TABLE big
(
id integer,
rand double precision
)
A large number of rows is inserted (50 million):
insert into big
select generate_series(1, 50000000) AS id, random();
The query plan to select every row looks like (not surprisingly):
$ psql -d big -c "explain select * from big;"
QUERY PLAN
----------------------------------------------------------------
Seq Scan on big (cost=0.00..924326.24 rows=50000124 width=12)
(1 row)
I then attempt to dump the contents to file:
$ psql -d big -c "select * from big;" > big.dump
As I said above, this command is failing before any data is written, seemingly by taking up an ever increasing amount of memory before being killed by the OS (by "OOM killer").
Note: I understand I could use pg_dump
to accomplish something similar, but in reality, my query is more complex than this - specifically, I would like to encode each row as JSON when dumping.
Some configuration details:
- postgresql version = 9.3.4
- work_mem = 1MB
- shared_buffers = 128MB
- effective_cache_size = 128MB
COPY
command:psql -d big -c "copy (select * from big) to stdout" > big.dump
psql
process or the Postgres backend process for your connection? I guess the client (psql
) buffers the result somehow (or forces the backend process to do so) When you usecopy
the data is never transferred to the client (thepsql
program) because this is all done on the server side.psql
process, from syslog:Out of memory: Kill process 26465 (psql)
. FYI: I'm running the client on the same machine as the server.psql
runs - it's still a "client" to the server. Does this also happen when you use the\o
command to write the output to a file? In that casepsql
"knows" that you don't need to display the data, maybe it takes retrieves the data more efficiently then.