Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Gaps in sequences are <strong>inevitable</strong> when dealing with <strong>transactional</strong> databases.</p> <p>First, you cannot use <code>SELECT max(id)+1</code> because it may give the same <code>id</code> to 2 transactions which execute at the same time. This means you have to use database native auto-increment column (MySQL, SQLite) or database sequence (PostgreSQL, MSSQL, Oracle) to obtain next available <code>id</code>.</p> <p>But even using auto-increment sequence does NOT solve this problem. Imagine that you have 2 database connections that started 2 parallel transactions almost at the same time. First one acquired some <code>id</code> from auto-increment sequence and it became previously used value +1. One nanosecond later, second transaction acquired next <code>id</code>, which is now +2. Now imagine that first transaction rolled back for some reason (encountered error, your code decided to abort it, program crashed - you name it). After that, second transaction committed with <code>id</code> +2, creating a gap in <code>id</code> numbering.</p> <p>But what if number of such parallel transactions was more than 2? You cannot predict, and you also cannot tell currently running transactions to reuse <code>id</code>s that were abandoned.</p> <p>It is theoretically possible to reuse abandoned ids. However, in practice it is prohibitively expensive on database, and creates more problems when multiple sessions try to do the same thing.</p> <p><strong>TDLR:</strong> stop fighting it, gaps in used ids are perfectly normal.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload