Beautiful solution! Maybe we can remove the WHEN clause from the trigger to make it do a single scan on the table instead of 2 I suppose that the size_t column is on the index to make the engine use only the index b-tree on the SUM OVER sub-query, not needing to read the table b-tree. Only the DELETE would touch the original table