RFC: Making TRUNCATE more "MVCC-safe"
От | Marti Raudsepp |
---|---|
Тема | RFC: Making TRUNCATE more "MVCC-safe" |
Дата | |
Msg-id | CABRT9RBRMdsoz8KxgeHfb4LG-ev9u67-6DLqvoiibpkKhTLQfw@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: RFC: Making TRUNCATE more "MVCC-safe"
|
Список | pgsql-hackers |
Hi! I've always been a little wary of using the TRUNCATE command due to the warning in the documentation about it not being "MVCC-safe": queries may silently give wrong results and it's hard to tell when they are affected. That got me thinking, why can't we handle this like a standby server does -- if some query has data removed from underneath it, it aborts with a serialization failure. Does this solution sound like a good idea? The attached patch is a lame attempt at implementing this. I added a new pg_class.relvalidxmin attribute which tracks the Xid of the last TRUNCATE (maybe it should be called reltruncatexid?). Whenever starting a relation scan with a snapshot older than relvalidxmin, an error is thrown. This seems to work out well since TRUNCATE updates pg_class anyway, and anyone who sees the new relfilenode automatically knows when it was truncated. Am I on the right track? Are there any better ways to attach this information to a relation? Should I also add another counter to pg_stat_database_conflicts? Currently this table is only used on standby servers. Since I wrote it just this afternoon, there are a few things still wrong with the patch (it doesn't handle xid wraparound for one), so don't be too picky about the code yet. :) Example: CREATE TABLE foo (i int); Session A: BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT txid_current(); -- Force snapshot open Session B: TRUNCATE TABLE foo; Session A: SELECT * FROM foo; ERROR: canceling statement due to conflict with TRUNCATE TABLE on foo DETAIL: Rows visible to this transaction have been removed. Patch also available in my github 'truncate' branch: https://github.com/intgr/postgres/commits/truncate Regards, Marti
Вложения
В списке pgsql-hackers по дате отправления: