• Peter Eisentraut's avatar
    Fix ALTER SEQUENCE locking · f8dc1985
    Peter Eisentraut authored
    In 1753b1b0, the pg_sequence system
    catalog was introduced.  This made sequence metadata changes
    transactional, while the actual sequence values are still behaving
    nontransactionally.  This requires some refinement in how ALTER
    SEQUENCE, which operates on both, locks the sequence and the catalog.
    
    The main problems were:
    
    - Concurrent ALTER SEQUENCE causes "tuple concurrently updated" error,
      caused by updates to pg_sequence catalog.
    
    - Sequence WAL writes and catalog updates are not protected by same
      lock, which could lead to inconsistent recovery order.
    
    - nextval() disregarding uncommitted ALTER SEQUENCE changes.
    
    To fix, nextval() and friends now lock the sequence using
    RowExclusiveLock instead of AccessShareLock.  ALTER SEQUENCE locks the
    sequence using ShareRowExclusiveLock.  This means that nextval() and
    ALTER SEQUENCE block each other, and ALTER SEQUENCE on the same sequence
    blocks itself.  (This was already the case previously for the OWNER TO,
    RENAME, and SET SCHEMA variants.)  Also, rearrange some code so that the
    entire AlterSequence is protected by the lock on the sequence.
    
    As an exception, use reduced locking for ALTER SEQUENCE ... RESTART.
    Since that is basically a setval(), it does not require the full locking
    of other ALTER SEQUENCE actions.  So check whether we are only running a
    RESTART and run with less locking if so.
    Reviewed-by: default avatarMichael Paquier <michael.paquier@gmail.com>
    Reported-by: default avatarJason Petersen <jason@citusdata.com>
    Reported-by: default avatarAndres Freund <andres@anarazel.de>
    f8dc1985
sequence-ddl.out 2.03 KB