117 points · by mirceasoaica · 10y ago · 59 comments
- Temp tables cannot be vacuumed by anyone else than connection that owns the table (no way around it) - do not keep temp tables too long, nightly vacuum or auto vacuum will not clear these,
- Temp tables do not get automatically dropped on recovery if your DB cluster crashes. They get reused but if you have many connections before crash and few after some namespaces will linger - you may need to manually cascade drop temp namespaces if you see the warning,
- Full vacuum does not touch XIDs - use freeze or just normal vacuum,
- Check oldest XID per table:
SELECT c.oid::regclass as table_name,
greatest(age(c.relfrozenxid),age(t.relfrozenxid)) as age
FROM pg_class c
LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
WHERE c.relkind = 'r'
ORDER by age DESC
LIMIT %s
- Check oldest XID per db: select datname db, age(datfrozenxid) FROM pg_database ORDER BY age DESCDon't think that's right.
copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
bool *pSwapToastByContent, TransactionId *pFreezeXid,
MultiXactId *pCutoffMulti)
...
/*
* Compute xids used to freeze and weed out dead tuples and multixacts.
* Since we're going to rewrite the whole table anyway, there's no reason
* not to be aggressive about this.
*/
vacuum_set_xid_limits(OldHeap, 0, 0, 0, 0,
&OldestXmin, &FreezeXid, NULL, &MultiXactCutoff,
NULL);
/*
* FreezeXid will become the table's new relfrozenxid, and that mustn't go
* backwards, so take the max.
*/
if (TransactionIdPrecedes(FreezeXid, OldHeap->rd_rel->relfrozenxid))
FreezeXid = OldHeap->rd_rel->relfrozenxid;
i.e. the cutoff is computed in copy_heap_data(). And then in void
rewrite_heap_tuple(RewriteState state,
HeapTuple old_tuple, HeapTuple new_tuple)
{
...
/*
* While we have our hands on the tuple, we may as well freeze any
* eligible xmin or xmax, so that future VACUUM effort can be saved.
*/
heap_freeze_tuple(new_tuple->t_data, state->rs_freeze_xid,
state->rs_cutoff_multi);I ran a test:
XID age in callback 354015 (0% of max)
task=# vacuum full callback;
XID age in callback 354015 (0% of max)
task=# vacuum freeze callback;
XID age in callback 11 (0% of max)
static void do_autovacuum(void) {
...
if (classForm->relpersistence == RELPERSISTENCE_TEMP)
{
int backendID;
backendID = GetTempNamespaceBackendId(classForm->relnamespace);
/* We just ignore it if the owning backend is still active */
if (backendID == MyBackendId || BackendIdGetProc(backendID) == NULL)
{
/*
* We found an orphan temp table (which was probably left
* behind by a crashed backend). If it's so old as to need
* vacuum for wraparound, forcibly drop it. Otherwise just
* log a complaint.
*/
if (wraparound)
{
ObjectAddress object;
ereport(LOG,
(errmsg("autovacuum: dropping orphan temp table \"%s\".\"%s\" in database \"%s\"",
get_namespace_name(classForm->relnamespace),
NameStr(classForm->relname),
get_database_name(MyDatabaseId))));
object.classId = RelationRelationId;
object.objectId = relid;
object.objectSubId = 0;
performDeletion(&object, DROP_CASCADE, PERFORM_DELETION_INTERNAL);
}
else
{
ereport(LOG,
(errmsg("autovacuum: found orphan temp table \"%s\".\"%s\" in database \"%s\"",
get_namespace_name(classForm->relnamespace),
NameStr(classForm->relname),
get_database_name(MyDatabaseId))));
}
}- Queries that perform complex operations involving the creation of temp tables cannot be joined onto themselves[1].
- Operations that create temp tables can never be used as part of the definition of a materialized view[1]. This is a permissions issue, but not one that can be resolved by altering permissions. It's baked in.
- Confusion over how to test for the existance of a temp table, "it's in the catalog but it's not in my session".
[1] - Without resorting to dblink shenanigans, or such like.
In some of my cases, the temporary data is some kind of projection of future stock requirements, arising from a user doing a "what if" operation. Since multiple users could be doing this, the replacement unlogged non-temporary table needs to be "multi-tennant", which involves an extra column (usually called "invocation_id"), and a sequence, such that each invocation of the operation gets a new invocation_id value, which it uses in all of the rows it then generates. The extra complexity is ensuring the invocation_id gets passed around to all of the functions that are operating on the data, including the clean-up when we're done with it.
http://rhaas.blogspot.com/2016/03/no-more-full-table-vacuums...
"No More Full-Table Vacuums" - Robert Haas
For a second I was conserned about the level of my geekiness.
Then I moved on the the next HN article...
So imagine now that autovacuum was turned off...
The user tables in such a system can survive if they are not updated often, with an occasional vacuum. However, the situation here was such that for every GB of user data, another GB has accumulated in dead records in system tables. Half of disk usage has gone to bloat in system tables!
It's easy to fix, of course, with a full vacuum of everything, if you have the time. Or more quickly, a full vacuum of system tables should be very fast, right? Right, except in that particular old version of PostgreSQL there was a race condition bug with vacuuming system tables which locks up the entire cluster with a possibility of data corruption for the db in question. Guess what happened.
A restore from backup.
But on the other hand, it starts warning you, first just in the log file, then on every query you issue way before it actually stops working, so if you're not ignoring warnings in your logs / your client-code, there should be ample time to react.
At least, there was for me when I first ran into this a few years ago.
If these were to be partitioned by state, instead of all records for all states in a single table, then we're looking at 270,000.
So, it's not _that_ difficult.
You want it by state, sounds like a column to add to my existing tables, not a way to make hundreds of new ones, or making a table with an FK and a state column.
I can't even remember how many times I have explained what would be correct and maintainable, only to be told to do it exactly the same way the old system did it.
So if I were working with the census, I'd consider myself lucky to not have to build a GUI for poking the holes in virtual Hollerith punch cards.
Also, the state is part of the identifier already, however, you may want to pull it out into tables for a variety of reasons.
My gut feeling for a database of this size is to suggest sharding. While I don't claim to be an expert in this sort of thing, here's how Pintrest approached a similar problem: https://engineering.pinterest.com/blog/sharding-pinterest-ho...
At my job, in our largest environment we only have 30,000 tables in our biggest database, but that's only a year or two's worth of data and the rate of increase is increasing.
We (du.co) make a reconciliation product for finance. Reconciliation is basically comparing two lists. The size of the data entirely depends on what the customer wants to compare.
The closest is RethinkDB, but its memory requirements per table are too high.
If you have other suggestions for something that can live on a smallish box (4G) yet support thousands of tables, fairly arbitrary queries, and joins against a relational DB, I'm open to suggestions. Fast bulk updates and inserts are a bonus; a relational DML is a requirement, since a good chunk of our logic is in SQL, and much more readable for it.