we are currently running into a bit of an issue with insertion speed into one of our tables.
The table holds events from users and in peak times we reach a few hundreds events per second and during those times the execution times spikes hard.
Between 1 and about 250 inserts per second we have response times of 30ms, but at 300 inserts per second this already spikes to 1.7 seconds and after 450 requests per second inserts are timing out.
We already looked into the following possible solutions:
- Batch up inserts: not possible because the events are very important and after a user got a success message they cannot be lost and in case of a crash all events in the current batch could be lost.
- Remove Foreign-Keys and Indices: Indices are required for the processing of the data since the table normally holds about 300 million entries from the last 24h-36h. Removing the foreign keys would be something of a last resort because those are used for cleaning up old entries
- Partition the table: Since the session-id is a number identifying the users session we tried to partition by session-id with 10 partition-tables using this:
ALTER TABLE some_table RENAME TO some_table_old;
CREATE TABLE some_table (
"id" int NOT NULL DEFAULT nextval('id_seq'::regclass),
"sessionid" int,
...
PRIMARY KEY (id,sessionid)
)
CREATE TABLE some_table_0 PARTITION OF some_table FOR VALUES WITH (MODULUS 10, REMAINDER 0);
CREATE TABLE some_table_1 PARTITION OF some_table FOR VALUES WITH (MODULUS 10, REMAINDER 1);
CREATE TABLE some_table_2 PARTITION OF some_table FOR VALUES WITH (MODULUS 10, REMAINDER 2);
...
INSERT INTO some_table
SELECT * FROM some_table_old;
In contrast to what we expected this even slowed it all down. Our best guess is that the overhead on partitions is heavier on the load than the improvement we get because of the smaller tables.
- Upgrade Database: we are currently running on pg-13 but migrating it to pg-16.3 did not bring any noticeable improvements.
After trying all of this we are a bit puzzled on what to try next. Any ideas?