A < 4 GB csv became a 7.7 GB table in my AWS Postgres instance. And a 14 GB csv wouldn't load into 22 GB of space, I'm guessing because it is also going to double in size! Is this factor of two normal? And if so, why, and is it reliable?
The easiest but the most efficient way to export data from a Postgres table to a CSV file is by using the COPY command. COPY command generates a CSV file on the Database Server. You can export the entire table or the results of a query to a CSV file with the COPY TO command.
To determine the size of a Postgres database table from the command line, log in to your server using SSH and access the database with the table you want to check. Type this code in the command line to determine the size of any of the database's tables. SELECT pg_size_pretty( pg_total_relation_size('tablename') );
Goto solution for bulk loading into PostgreSQL is the native copy command.
There are many possible reasons:
Indexes take up space. If you have lots of indexes, especially multi-column indexes or GiST / GIN indexes, they can be a big space hog.
Some data types are represented more compactly in text form than in a table. For example, 1
consumes 1 byte in csv (or 2 if you count the comma delimiter) but if you store it in a bigint
column it requires 8 bytes.
If there's a FILLFACTOR
set, PostgreSQL will intentionally waste space so make later UPDATE
s and INSERT
s faster. If you don't know what FILLFACTOR
is, then there isn't one set.
PostgreSQL has a much larger per-row overhead than CSV. In CSV, the per-row overhead is 2 bytes for a newline and carriage return. Rows in a PostgreSQL table require 24 to 28 bytes, plus data values, mainly because of the metadata required for multiversion concurrency control. So a CSV with very many narrow rows will produce a significantly bigger table than one the same size in bytes that has fewer wider rows.
PostgreSQL can do out-of-line storage and compression of values using TOAST
. This can make big text strings significantly smaller in the database than in CSV.
You can use octet_size
and pg_column_size
to get PostgreSQL to tell you how big rows are. Because of TOAST
out-of-line compressed storage, the pg_column_size
might be different for a tuple produced by a VALUES
expression vs one SELECT
ed from a table.
You can also use pg_total_relation_size
to find out how big the table for a given sample input is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With