<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="349273" data-post-id="349273">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sbuttgereit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/120/30162_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sbuttgereit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I imagine there’s a dependency on operation order.  If in the transaction, you update a row in way that will ultimately avoid the conflict, and then insert (or update) the conflicting row… then MVCC will have you covered.</p>
<p>However, if first you want to insert the conflicting row and the update the old row to be non-conflicting, then the deferral is something you’d need.</p>
<p>Admittedly, don’t run into this that often so I’d likely run a couple scenarios on-database to be sure I had it right, but that’s my understanding why the unique constraint is deferrable and mentioned later in the documentation you posted.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349273" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-a-unique-column-constraint-in-postgresql/68011/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-349273" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349273"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="349282" data-post-id="349282">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sbuttgereit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/120/30162_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sbuttgereit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sbuttgereit" data-post="12" data-topic="68011">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/48/30162_2.png" class="avatar"> sbuttgereit:</div>
<blockquote>
<p>Admittedly, don’t run into this that often so I’d likely run a couple scenarios on-database to be sure I had it right, but that’s my understanding why the unique constraint is deferrable and mentioned later in the documentation you posted.</p>
</blockquote>
</aside>
<p>I couldn’t let it go…</p>
<p>Here’s a walk through of transactions involving just MVCC and constraint deferability.</p>
<p>First we’ll set up the scenario with a simple table and the kind of scenario where you might need a deferrable unique constraint… but I’ll start with <code>INITIALLY IMMEDIATE</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 09:59:23 PST 2024]
&gt; create table scb_test ( 
   id bigint generated by default as identity , 
   u1_id integer not null, 
   u2_id text not null, 
   constraint test_udx unique (u1_id, u2_id) deferrable initially immediate);
CREATE TABLE
Time: 4.533 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:00:11 PST 2024]
&gt; insert into scb_test (u1_id, u2_id) values (1, 'a'),(2, 'b'),(3, 'c');
INSERT 0 3
Time: 2.640 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:02:24 PST 2024]
&gt; select * from scb_test;
id | u1_id | u2_id
----+-------+-------
 1 |     1 | a
 2 |     2 | b
 3 |     3 | c
(3 rows)

Time: 0.467 ms
</code></pre>
<p>OK… let’s do a transaction where we’ll update an existing record and insert a new one that would be conflicting if not for the update.  We’ll update the first record in a way to avoid the conflict, and then insert an otherwise conflicting record.   All of this is in one transaction, so to anyone outside of the transaction, the updated record will still have it’s original values until commit:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:02:57 PST 2024]
&gt; begin;
BEGIN
Time: 0.207 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:03:00 PST 2024]
&gt; update scb_test set u1_id = 4, u2_id = 'd' where u1_id = 3 and u2_id = 'c';
UPDATE 1
Time: 0.376 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:03:04 PST 2024]
&gt; insert into scb_test (u1_id, u2_id) values (3, 'c');
INSERT 0 1
Time: 0.279 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:03:10 PST 2024]
&gt; commit;
COMMIT
Time: 1.926 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:03:25 PST 2024]
&gt; select * from scb_test;
 id | u1_id | u2_id
----+-------+-------
  1 |     1 | a
  2 |     2 | b
  3 |     4 | d
  4 |     3 | c
(4 rows)

Time: 0.383 ms
</code></pre>
<p>Not deferrable and no conflict.  Because we resolved the uniqueness conflict with an order of operations which, inside of the transaction, avoided the conflict, we did not need deferability on commit.  However, if we switch that operation ordering with a similar set of commands:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:03:29 PST 2024]
&gt; begin;
BEGIN
Time: 0.274 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:03:45 PST 2024]
&gt; insert into scb_test (u1_id, u2_id) values (3, 'c');
ERROR: duplicate key value violates unique constraint "test_udx"
DETAIL: Key (u1_id, u2_id)=(3, c) already exists.
Time: 0.489 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:04:12 PST 2024]
&gt; rollback;
ROLLBACK
Time: 0.207 ms
</code></pre>
<p>Because we tried to insert the conflicting record before updating the original record with those values, we got the constraint violation.  Now let’s try that same operation with the constraint deferred:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:05:50 PST 2024]
&gt; begin;
BEGIN
Time: 0.352 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:05:57 PST 2024]
&gt; set constraints test_udx deferred;
SET CONSTRAINTS
Time: 0.400 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:06:00 PST 2024]
&gt; insert into scb_test (u1_id, u2_id) values (3, 'c');
INSERT 0 1
Time: 0.414 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:06:05 PST 2024]
&gt; update scb_test set u1_id = 5, u2_id = 'e' where id = 4;
UPDATE 1
Time: 0.336 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:06:52 PST 2024]
&gt; commit;
COMMIT
Time: 2.065 ms

localhost(from SCB-MUSE-BOXX).postgres.scb.5432 [Tue Dec 10 10:06:55 PST 2024]
&gt; select * from scb_test;
 id | u1_id | u2_id
----+-------+-------
  1 |     1 | a
  2 |     2 | b
  3 |     4 | d
  6 |     3 | c
  4 |     5 | e
(5 rows)

Time: 0.524 ms
</code></pre>
<p>Our conflicting insert worked and… since we resolved the conflict prior to committing the transaction we were able to commit the transaction successfully.  Had we not resolved the conflict, the transaction would have failed because of the constraint violation.</p>
<p>Anyway, wanted to be sure that we were straight for future readers.</p>
<hr>
<p>Bringing this back to something to do with Elixir/Ecto SQL…</p>
<p>If all you need is enforced uniqueness, I see no problem with the <code>unique_index</code> method.  You’re going to end up with a unique index whether you’re creating the constraint or creating the index directly.  But, if you do have the scenario where you’ll be resolving unique conflicts in your code and don’t want to worry about detailed database operation ordering within the transaction, I believe a constraint is worth the extra Elixir/Ecto ceremony to get it created.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349282" data-batch-url="/posts/batch_likers">
                        8
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-a-unique-column-constraint-in-postgresql/68011/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-349282" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349282"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="349432" data-post-id="349432">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mmmrrr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mmmrrr/120/12227_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mmmrrr
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you all for the great discussion and insights! It’s much appreciated <img src="https://forum.elixirforum.com/images/emoji/apple/heart.png?v=15" title=":heart:" class="emoji" alt=":heart:" loading="lazy" width="20" height="20"></p>
<p><a class="mention" href="/u/lostkobrakai" rel="nofollow">@LostKobrakai</a> I agree that Ecto shouldn’t support every feature of every storage engine under the sun but in this case a little syntax sugar would have been nice <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> actually I already did use execute as you suggested but it felt a little heavy handed for something seemingly simple - hence my question.</p>
<p><a class="mention" href="/u/sbuttgereit" rel="nofollow">@sbuttgereit</a> thank you so much for the in depth analysis. This was a fantastic answer - and the kind of answer why I adore this community. You all are really an asset to this ecosystem! <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349432" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-a-unique-column-constraint-in-postgresql/68011/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-349432" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349432"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>