I’m going to store records in a single table with 2 fields:
-
id -> 4 characters
-
password_hash -> 64 characters
How many records like the one above will I be able to store in a 5mb PostgreSQL on Heroku?
P.S.: given a single table with x columns and a length of y – how can I calculate the space it will take in a database?
Disk space occupied
Calculating the space on disk is not trivial. You have to take into account:
The overhead per table. Small, basically the entries in the system catalog.
The overhead per row (
HeapTupleHeader) and per data page (PageHeaderData). Details about page layout in the manual.Space lost to column alignment, depending on data types.
Space for a NULL bitmap. Effectively free for tables of 8 columns or less, irrelevant for your case.
Dead rows after
UPDATE/DELETE. (Until the space is eventually vacuumed and reused.)Size of index(es). You’ll have a primary key, right? Index size is similar to that of a table with just the indexed columns and less overhead per row.
The actual space requirement of the data, depending on respective data types. Details for character types (incl. fixed length types) in the manual:
More details for all types in the system catalog
pg_type.The database encoding in particular for character types. UTF-8 uses up to four bytes to store one character (But 7-Bit-ASCII characters always occupy just one byte, even in UTF-8.)
Other small things that may affect your case, like TOAST – which should not affect you with 64-character strings.
Calculate with test case
A simple method to find an estimate is to create a test table, fill it with dummy data and measure with database object size functions::
Including indexes:
See:
A quick test shows the following results:
After adding a primary key:
So, I’d expect a maximum of around 44k rows without and around 36k rows with primary key.