Vitaliy Garnashevich wrote: > > > Have not worked through all of the above, but a first draft suggestion: > > > > Move the SELECT minus the aggregation functions into a sub-query that > > uses FOR UPDATE. Then do the aggregation on the results of the > > sub-query. > > The aggregated table has hundreds of millions of rows, and the query runs > for many hours (which is one of the reasons why it's better not to fail). I > really doubt that row level locking would work. That would be a lot of RAM > just to hold all the locks. Row locks are not stored in memory. Of course, a FOR KEY SHARE lock would block DELETEs that try to remove the locked row. I think your proposed strategy of trying to merge what other processes did while you were copying is very problematic. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services