• Teodor Sigaev's avatar
    Prevent multiple cleanup process for pending list in GIN. · e2c79e14
    Teodor Sigaev authored
    Previously, ginInsertCleanup could exit early if it detects that someone else
    is cleaning up the pending list, without waiting for that someone else to
    finish the job. But in this case vacuum could miss tuples to be deleted.
    
    Cleanup process now locks metapage with a help of heavyweight
    LockPage(ExclusiveLock), and it guarantees that there is no another cleanup
    process at the same time. Lock is taken differently depending on caller of
    cleanup process: any vacuums and gin_clean_pending_list() will be blocked
    until lock becomes available, ordinary insert uses conditional lock to
    prevent indefinite waiting on lock.
    
    Insert into pending list doesn't use this lock, so insertion isn't blocked.
    
    Also, patch adds stopping of cleanup process when at-start-cleanup-tail is
    reached in order to prevent infinite cleanup in case of massive insertion. But
    it will stop only for automatic maintenance tasks like autovacuum.
    
    Patch introduces choice of limit of memory to use: autovacuum_work_mem,
    maintenance_work_mem or work_mem depending on call path.
    
    Patch for previous releases should be reworked due to changes between 9.6 and
    previous ones in this area.
    
    Discover and diagnostics by Jeff Janes and Tomas Vondra
    
    Patch by me with some ideas of Jeff Janes
    e2c79e14
ginfast.c 26 KB