by h1fra · 3y ago · 0 comments
- Configure Vacuum and maintenance_work_mem regularly if your DB size increases, if you allocate too much or too often it can clog up your memory.
- If you plan on deleting more than a 10000 rows regularly, maybe you should look at partition, it's surprisingly very slow to delete that "much" data. And even more with foreign key.
- Index on Boolean is useless, it's an easy mistake that will take memory and space disk for nothing.
- Broad indices are easier to maintain but if you can have multiple smaller indices with WHERE condition it will be much faster
- You can speed up, by a huge margin, big string indices with md5/hash index (only relevant for exact match)
- Postgres as a queue is definitely working and scales pretty far
- Related: be sure to understand the difference between transaction vs explicit locking, a lot of people assume too much from transaction and it will eventually breaks in prod.
However if the field is highly biased (e.g. 90 or 99% one value) it can be useful to create a partial index on the rarer value. Though even better is to create a partial index on the other stuff filtered by that value, especially if the smaller set is the commonly queried one (e.g. soft-deletes).
A complete index over a column will have entries for all records, and can be used on "x is null" and "x is not null" filters.
I was referring of indexing the column without distinction, the last time I checked (years ago) Postgres didn't do any statistical distribution so the query planner was always discarding the index anyway.
I recently went from:
* somewhat understanding the concept of transactions and combining that with a bunch of manual locking to ensure data integrity in our web-app;
to: * realizing how powerful modern Postgres actually is and delegating integrity concerns to it via the right configs (e.g., applying "serializable" isolation level), and removing the manual locks.
So I'm curious what situations are there that should make me reconsider controlling locks manually instead of blindly trusting Postgres capabilities. Any transaction which is run at a transaction isolation level other than SERIALIZABLE will not be affected by SSI. If you want to enforce business rules through SSI, all transactions should be run at the SERIALIZABLE transaction isolation level, and that should probably be set as the default.
Given that running everything at SERIALIZABLE probably isn’t practical for you, I think it’s more clear code wise to use explicit locks. That way, you can grep for what queries are related synchronization wise, vs. SERIALIZABLE being implicit.Explicit locks can mean just calling LOCK TABLE account_balances IN SHARE ROW EXCLUSIVE MODE; early in the transaction and then doing SELECT ... FOR UPDATE; or similar configurations to enforce business rules where it matters.
If you manually SELECT FOR UPDATE SKIP LOCKED LIMIT 1, then the second process will be forced to select the next task without waiting for the lock.
Do you mean a https://www.postgresql.org/docs/current/indexes-types.html#I... index? It's a 32-bit hash (but which hash is it, is it CRC32?). How to do a MD5 index?
Anyway, MD5 is slow, does Postgres offer fast hashes like SipHash (DoS resistant) or FNV (not DoS resistant)?
What I did to not maintain a second column is to use the function directly in the index:
``` CREATE UNIQUE INDEX CONCURRENTLY "groupid_md5_uniq" ON "crawls" ("group_id", md5("url")); ```
``` SELECT * FROM crawls WHERE group_id= $0 AND md5(url) = md5($1) ```
This simple trick, that did not required an extensive refactor, speed up the query time by a factor of thousand.
I’ve seen this advice elsewhere as well, but recently tried it and found it wasn’t the case on my data set. I have about 5m rows, with an extremely heavy bias on one column being ‘false’. Adding a plain index on this column cut query time in about half. We’re in the millisecond ranges here, but still.
CREATE INDEX ON session(is_active) WHERE is_active;CREATE INDEX ON session(id) WHERE is_active;
Why? Is it because an index on the bool alone, with symmetric distribution, will still leave you with half the table to scan? In other words, does that statement apply to biased distribution (as mentioned by another response) or indices on multiple fields of which one is a boolean?
But then, given the number of things that very rarely happen in a database, you are prone to have 1 or 2 of them happening every time you do something. Just not that specific thing; but if you keep all of those rules in mind, you will always be surprised.
Dumb question: what's the use case for having a md5/hash field in your database?
For equality comparisons of large types it's quite beneficial
In my case, I had to index big tables with URLs, with no need for partial match. I did it naively but did help a lot.
You mean with triggers and listen/notify ?
But I find that listen/notify seem to be drastically underused.