Postgresql: SERIAL incremented on failed constraint INSERT : <p>Having a simple table structure like this:</p>

<pre><code>CREATE TABLE test (
    id INT PRIMARY KEY,
    sid SERIAL
);
</code></pre>

<p>I noticed if I attempt to insert a row but it fails a constraint test (i.e. PRIMARY KEY constraint), the <code>SERIAL</code> counter will increment anyway, so the next successful insert, <code>sid</code> will be <code>sid + 2</code> instead of <code>sid + 1</code>.</p>

<p>Is this normal behavior? Any way to prevent this?</p>
