-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1173 lines (1042 loc) · 39.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>migrations.guide</title>
<!-- Text
Non-breaking space: <
En dash:-
Em dash:—
-->
<style>
html {
/* Prevent shifting page when scrollbar appears */
width: 100vw;
overflow-x: hidden;
}
main {
text-align: center;
}
h1 {
text-align: center;
}
/* Decision Flow Chart */
input[type=radio] {
display: none;
}
label {
display: inline-block;
cursor: pointer;
margin: 1em;
margin-top: 0;
border: 1px solid grey;
padding: 1em;
font-weight: bold;
}
input[type=radio]:checked + label {
font-style: normal;
color: black;
background-color: yellow;
}
/* Results */
#results {
text-align: left;
max-width: 40em;
margin: 0 auto;
}
.safe {
color: green;
}
.alternative {
color: orange;
}
.unsafe {
color: red;
}
details {
border: 2px solid black;
padding: 1em;
}
summary {
margin: -1em;
padding: 1em;
font-weight: bold;
}
pre {
background-color: #111;
color: #eee;
padding: 1em;
}
/* Other */
video {
display: block;
margin: 0 auto;
}
</style>
<!-- Create Elements Script -->
<script>
"use strict";
function createElement(elementName, slotValues) {
slotValues = slotValues || [];
const templateName = elementToTemplateName(elementName);
const template = document.querySelector(`#${templateName}`);
if (template == null) {
console.log("No template found: ", templateName)
return null;
}
const baseElement = template.content.firstElementChild;
const extraSlotValues = baseElement.querySelectorAll("[slot]");
const allSlotValues = [...slotValues, ...extraSlotValues];
const baseName = baseElement.localName;
let filledElement;
if (baseName.includes("-")) {
filledElement = createElement(baseName, allSlotValues);
} else {
const newElement = baseElement.cloneNode(true);
filledElement = replaceSlotsWithValues(newElement, allSlotValues);
}
for (let customElement of getDescendants(filledElement)) {
if (customElement.localName.includes("-")) {
const newCustomElement = createElement(customElement.localName);
customElement.parentNode.insertBefore(newCustomElement, customElement);
customElement.parentNode.removeChild(customElement);
}
}
return filledElement;
};
function replaceSlotsWithValues(element, slotValues) {
const newElement = element.cloneNode(true);
for (let slot of newElement.querySelectorAll('slot')) {
const slotName = slot.getAttribute("name");
for (let slotValue of slotValues) {
if (slotValue.getAttribute("slot") == slotName) {
const copy = slotValue.cloneNode(true);
slot.parentNode.insertBefore(copy, slot);
}
}
slot.parentNode.removeChild(slot);
}
return newElement;
};
function templateToElementName(templateName) {
const baseName = templateName.replace("template-", "");
return `x-${baseName}`;
}
function elementToTemplateName(templateName) {
const baseName = templateName.replace("x-", "");
return `template-${baseName}`;
};
function getDescendants(node) {
let children = childNodesToArray(node);
for (let i = 0; i < node.childElementCount; i++) {
children = [...children, ...getDescendants(node.children[i])];
}
return children;
};
function childNodesToArray(node) {
let all = [];
for (let i = 0; i < node.childElementCount; i++) {
all.push(node.children[i]);
}
return all;
};
</script>
<div></div>
<!-- Decision Base Templates -->
<template id="template-decision">
<section class="decision">
<p class="question">
<slot name="question">NEED QUESTION</slot>
</p>
<form class="choices">
<slot name="choices">NEED CHOICSE</slot>
</form>
</section>
</template>
<!-- Decision Templates -->
<div>
<template id="template-decision-root">
<x-decision>
<span slot="question">I am using...</span>
<input slot="choices" type="radio" name="x" id="root1" value="pg96" next="verb">
<label slot="choices" for="root1">Postgres 9.6</label>
<input slot="choices" type="radio" name="x" id="root2" value="pg11" next="verb">
<label slot="choices" for="root2">Postgres 11</label>
<input slot="choices" type="radio" name="x" id="root3" value="pg12" next="verb">
<label slot="choices" for="root3">Postgres 12</label>
<input slot="choices" type="radio" name="x" id="root4" value="pg13" next="verb">
<label slot="choices" for="root4">Postgres 13</label>
</x-decision>
</template>
<template id="template-decision-verb">
<x-decision>
<span slot="question">I want to...</span>
<input slot="choices" type="radio" name="x" id="verb1" value="add" next="add">
<label slot="choices" for="verb1">Add Something</label>
<input slot="choices" type="radio" name="x" id="verb2" value="change" next="change">
<label slot="choices" for="verb2">Change Something</label>
<!--<option slot="choices" next="drop" value="drop">Remove Something</option>-->
<input slot="choices" type="radio" name="x" id="verb4" value="data" next="result">
<label slot="choices" for="verb4">Alter Data</label>
</x-decision>
</template>
<template id="template-decision-add">
<x-decision>
<span slot="question">I want to...</span>
<input slot="choices" type="radio" name="x" id="add1" value="column" next="is-null">
<label slot="choices" for="add1">Add a Column</label>
<input slot="choices" type="radio" name="x" id="add2" value="index" next="result" >
<label slot="choices" for="add2">Add a (Unique) Index</label>
<input slot="choices" type="radio" name="x" id="add3" value="constraint" next="result" >
<label slot="choices" for="add3">Add a Constraint</label>
<input slot="choices" type="radio" name="x" id="add4" value="foreign_key" next="result" >
<label slot="choices" for="add4">Add a Foreign Key</label>
</x-decision>
</template>
<template id="template-decision-is-null">
<x-decision>
<span slot="question">Is the column NULLable?</span>
<input slot="choices" type="radio" name="x" id="is-null1" value="null" next="has-default">
<label slot="choices" for="is-null1">NULLable</label>
<input slot="choices" type="radio" name="x" id="is-null2" value="not_null" next="result">
<label slot="choices" for="is-null2">NOT NULLable</label>
</x-decision>
</template>
<template id="template-decision-has-default">
<x-decision>
<span slot="question">Is there a database-side default value?</span>
<input slot="choices" type="radio" name="x" id="has-default1" value="with_default" next="result">
<label slot="choices" for="has-default1">With a Default</label>
<input slot="choices" type="radio" name="x" id="has-default2" value="without_default" next="result">
<label slot="choices" for="has-default2">Without a Default</label>
</x-decision>
</template>
<template id="template-decision-change">
<x-decision>
<span slot="question">I want to...</span>
<input slot="choices" type="radio" name="x" id="change1" value="table" next="result">
<label slot="choices" for="change1">Rename a Table</label>
<input slot="choices" type="radio" name="x" id="change2" value="column" next="result">
<label slot="choices" for="change2">Rename a Column</label>
<input slot="choices" type="radio" name="x" id="change3" value="to_not_null" next="result">
<label slot="choices" for="change3">Make a Column NOT NULLable</label>
<!--
Make Column NOT NULL
-->
</x-decision>
</template>
<template id="template-decision-drop">
<x-decision>
<span slot="question">I want to...</span>
<!--
Drop a Column
-->
</x-decision>
</template>
</div>
<!-- Create Decisions Script -->
<script>
function addDecision(name) {
if (name == "result") {
return;
}
const decisions = document.querySelector("#decisions");
const tag = `x-decision-${name}`;
const results = document.querySelector("#results").children;
for (let element of results) {
element.remove();
}
// Add new decision
const decision = createDecisionElement(tag);
decisions.appendChild(decision);
};
function getActiveRadio(radioElements) {
const active = Array.from(radioElements).find(r => r.checked)
return (active && active.value) || "";
}
function getAnswer() {
let result = ""
const decisions = document.querySelector("#decisions").children;
for (let decision of decisions) {
const answer = getActiveRadio(
decision
.querySelectorAll('input[type="radio"]')
);
result += `__${answer}`
}
console.log("current result =", result);
return result;
};
function addResult() {
const results = document.querySelector("#results");
const answer = getAnswer();
const result = createElement(`x-result${answer}`);
if (result !== null) {
results.appendChild(result);
}
};
function createDecisionElement(elementName) {
const newElement = createElement(elementName);
const radioElements = newElement.querySelectorAll('input[type="radio"]');
for (let radioElement of radioElements) {
radioElement.addEventListener('change', (event) => {
if (event.target.checked) {
removeFuture(event.target.parentNode.parentNode);
addDecision(event.target.getAttribute("next"));
addResult();
}
});
}
return newElement;
}
function removeFuture(decisionElement) {
removeNextSiblings(decisionElement);
const results = document.querySelector('#results');
while (results.firstChild) {
results.removeChild(results.firstChild);
}
}
function removeNextSiblings(element) {
let toRemove = []
let curElement = element.nextElementSibling;
while (curElement) {
toRemove.push(curElement)
curElement = curElement.nextElementSibling;
}
for (ele of toRemove) {
ele.remove();
}
}
</script>
<!-- Result Base Templates -->
<template id="template-result-safe">
<div>
<h1 class="safe">Safe</h1>
<div><slot name="content"></slot></div>
</div>
</template>
<template id="template-result-alternative">
<div>
<h1 class="alternative">Safe Alternative Possible</h1>
<div><slot name="content"></slot></div>
</div>
</template>
<template id="template-result-unsafe">
<div>
<h1 class="unsafe">Unsafe</h1>
<div><slot name="content"></slot></div>
</div>
</template>
<!-- Result Templates -->
<div>
<template id="template-result__pg96__add__column__null__with_default">
<x-result-altenative>
<div slot="content">
<p>Unsafe due to default, but alternative is possible.
</div>
</x-result-altenative>
</template>
<template id="template-result__pg11__add__column__null__with_default">
<x-result-alternative>
<div slot="content">
<p>It is unsafe to add a new column:
<pre><code>ALTER TABLE table ADD COLUMN column INT DEFAULT 0;</code></pre>
<p>
All queries of every kind will be blocked until the migration is
complete because an <code>ACCESS EXCLUSIVE</code> lock is required.
<p>
However, if the application is written with this in mind and migration
timeouts are used, then it is possible to <em>eventually</em> do this
safely.
<p>
See the <strong>HOW-TO: Obtain a lock safely for a migration</strong> section
below for a step-by-step guide on what to do.
<x-result-snippet-access-exclusive-lock></x-result-snippet-access-exclusive-lock>
<x-result-snippet-lock-queue></x-result-snippet-lock-queue>
<x-result-snippet-apps-blocking-migrations></x-result-snippet-apps-blocking-migrations>
<x-result-snippet-how-to-safe-apps></x-result-snippet-how-to-safe-apps>
<x-result-snippet-how-to-get-lock></x-result-snippet-how-to-get-lock>
</div>
</x-result-unsafe>
</template>
<template id="template-result__pg12__add__column__null__with_default">
<x-result__pg11__add__column__null__with_default></x-result__pg11__add__column__null__with_default>
</template>
<template id="template-result__pg13__add__column__null__with_default">
<x-result__pg11__add__column__null__with_default></x-result__pg11__add__column__null__with_default>
</template>
<template id="template-result__pg96__add__column__null__without_default">
<x-result-alternative>
<div slot="content">
<p>Somewhat safe.
</div>
</x-result-alternative>
</template>
<template id="template-result__pg11__add__column__null__without_default">
<x-result-alternative>
<div slot="content">
<p>It is unsafe to add a new column:
<pre><code>ALTER TABLE table ADD COLUMN column INT;</code></pre>
<p>
All queries of every kind will be blocked until the migration is
complete because an <code>ACCESS EXCLUSIVE</code> lock is required.
<p>
However, if the application is written with this in mind and migration
timeouts are used, then it is possible to <em>eventually</em> do this
safely.
<p>
See the <strong>HOW-TO: Obtain a lock safely for a migration</strong> section
below for a step-by-step guide on what to do.
<p>
<strong>Warning:</strong> <code>NULL</code> columns cannot be made
into <code>NOT NULL</code> columns safely after-the-fact until
Postgres 12. So if you are actually intending to make a <code>NOT NULL</code>
column, then create a <code>NOT NULL</code> column with a dummy default value.
<x-result-snippet-access-exclusive-lock></x-result-snippet-access-exclusive-lock>
<x-result-snippet-lock-queue></x-result-snippet-lock-queue>
<x-result-snippet-apps-blocking-migrations></x-result-snippet-apps-blocking-migrations>
<x-result-snippet-how-to-safe-apps></x-result-snippet-how-to-safe-apps>
<x-result-snippet-how-to-get-lock></x-result-snippet-how-to-get-lock>
</div>
</x-result-unsafe>
</template>
<template id="template-result__pg12__add__column__null__without_default">
<x-result__pg11__add__column__null__without_default></x-result__pg11__add__column__null__without_default>
</template>
<template id="template-result__pg13__add__column__null__without_default">
<x-result__pg11__add__column__null__without_default></x-result__pg11__add__column__null__without_default>
</template>
<template id="template-result__pg96__add__column__not_null">
<x-result-alternative>
<div slot="content">
<p>This is unsafe.
<p>You're best bet is to compromise and add a NULL column, and a separate
check constraint.
</div>
</x-result-alternative>
</template>
<template id="template-result__pg11__add__column__not_null">
<x-result-alternative>
<div slot="content">
<p>Add not null - safe alternative possible.
<x-result-snippet-access-exclusive-lock></x-result-snippet-access-exclusive-lock>
</div>
</x-result-alternative>
</template>
<template id="template-result__pg12__add__column__not_null">
<x-result__pg11__add__column__not_null></x-result__pg11__add__column__not_null>
</template>
<template id="template-result__pg13__add__column__not_null">
<x-result__pg11__add__column__not_null></x-result__pg11__add__column__not_null>
</template>
<template id="template-result__pg11__add__index">
<x-result-alternative>
<div slot="content">
<p>It is strictly unsafe to add an index the "normal" way:
<pre style="white-space: nowrap"><code>
CREATE INDEX name_idx ON table (column);
<br>
CREATE UNIQUE INDEX name_idx ON table (column);
</code></pre>
<p>
A full table scan is required which will take a long time to complete
for large tables, and, as a <code>SHARE</code> lock is obtained, all
<strong>writes will be blocked</strong> until the migration is complete.
<h2>Safe Alternative</h2>
<p>
Create an index <code>CONCURRENTLY</code>. This happens in the background
and will not block writes.
<ol>
<li>
Execute the following command in a persistent shell (this could take
many hours):
<pre style="white-space: nowrap"><code>
SET statement_timeout = 0;
<br>
SET lock_timeout = 0;
<br>
<br>
CREATE INDEX CONCURRENTLY idx_name ON table (column);
</code></pre>
<li>
When the command returns, check the index was created successfully—invalid indexes are marked with <code>INVALID</code>:
<pre style="white-space: nowrap"><code>
\d table
</code></pre>
<li>
If it was unsuccessful, try again after dropping
the index:
<pre><code>DROP INDEX name_idx;</code></pre>
(Note: it could be unsuccessful due to deadlocks or a uniqueness
violation.)
</ol>
<hr>
<x-result-snippet-concurrently-downside></x-result-snippet-concurrently-downside>
<p>
The explanations below describe the failure conditions of the unsafe
way of adding indexes. If the safe alternative is followed, then these
problems do not apply.
<x-result-snippet-share-lock></x-result-snippet-share-lock>
<x-result-snippet-lock-queue></x-result-snippet-lock-queue>
<x-result-snippet-apps-blocking-migrations></x-result-snippet-apps-blocking-migrations>
<x-result-snippet-how-to-get-lock></x-result-snippet-how-to-get-lock>
</div>
</x-result-alternative>
</template>
<template id="template-result__pg96__add__constraint">
<x-result-alternative>
<div slot="content">
<p>Incomplete - add constraint.
</div>
</x-result-alternative>
</template>
<template id="template-result__pg11__add__constraint">
<x-result__pg96__add__constraint></x-result__pg96__add__constraint>
</template>
<template id="template-result__pg12__add__constraint">
<x-result__pg96__add__constraint></x-result__pg96__add__constraint>
</template>
<template id="template-result__pg13__add__constraint">
<x-result__pg96__add__constraint></x-result__pg96__add__constraint>
</template>
<template id="template-result__pg96__add__foreign_key">
<x-result-alternative>
<div slot="content">
<p>Incomplete - add foreign key.
</div>
</x-result-alternative>
</template>
<template id="template-result__pg11__add__foreign_key">
<x-result__pg96__add__foreign_key></x-result__pg96__add__foreign_key>
</template>
<template id="template-result__pg12__add__foreign_key">
<x-result__pg96__add__foreign_key></x-result__pg96__add__foreign_key>
</template>
<template id="template-result__pg13__add__foreign_key">
<x-result__pg96__add__foreign_key></x-result__pg96__add__foreign_key>
</template>
<template id="template-result__pg96__change__table">
<x-result-unsafe>
<div slot="content">
<p style="text-align: center">Good luck with that mate.</p>
<video src="abandon-hope.mp4" autoplay playsinline loop muted>
<source src="abandon-hope.mp4" type="video/mp4">
<p>
Your browser does not support playing inline videos, this is the direct
link to the video: <a href="abandon-hope.mp4">abandon-hope.mp4</a>
</video>
</div>
</x-result-unsafe>
</template>
<template id="template-result__pg11__change__table">
<x-result__pg96__change__table></x-result__pg96__change__table>
</template>
<template id="template-result__pg12__change__table">
<x-result__pg96__change__table></x-result__pg96__change__table>
</template>
<template id="template-result__pg13__change__table">
<x-result__pg96__change__table></x-result__pg96__change__table>
</template>
<template id="template-result__pg96__change__column">
<x-result-alternative>
<div slot="content">
<p>
A rename must be treated as <strong>adding a column</strong>,
then <strong>backfilling data</strong>, then
<strong>removing a column</strong>, which means it has the pitfalls
of all three, along with the problem of
<strong>synchronising writes</strong> to the old and new columns.
<h2>Safe Alternative</h2>
<ol>
<li>Add the new column.
<li>Synchronise writes to both columns.
<li>Backfill data to the new column.
<li>Read and write to the new column in the application.
<li>Remove the old column and any remaining synchronisation.
</ol>
<p>
This remaining part of this section will detail the synchronisation
process only. Other sections of this guide (by following the flow
chart again) detail how to do each of the other above steps safely.
<p>
There are three possible strategies for synchronising writes to both
columns:
<ol>
<li>
Use database-level triggers to synchronise the new column with the
old column and vica versa.
<li>
Use ORM framework-level synchronisation to synchronise the fields
across the whole application automatically.
<li>
<em>(Not recommended.)</em> Use application-level synchronisation,
where the fields are manually synchronised in code at each line the
fields are modified.
</ol>
<p>
<strong>NOTE:</strong> This section is incompletely and does not
tell you how to do each strategy <em>yet</em>.
</div>
</x-result-alternative>
</template>
<template id="template-result__pg11__change__column">
<x-result__pg96__change__column></x-result__pg96__change__column>
</template>
<template id="template-result__pg12__change__column">
<x-result__pg96__change__column></x-result__pg96__change__column>
</template>
<template id="template-result__pg13__change__column">
<x-result__pg96__change__column></x-result__pg96__change__column>
</template>
<template id="template-result__pg96__change__to_not_null">
<x-result-alternative>
<div slot="content">
<p>
It is <strong>unsafe</strong> to make a column <code>NOT NULL</code>:
<pre><code><!--
-->ALTER TABLE table COLUMN column SET NOT NULL;
</code></pre>
<p>
All queries of every kind will be blocked until the migration is
complete because an <code>ACCESS EXCLUSIVE</code> lock is required.
Since a full table scan is required to validate the constraint, this
could take a long time for large tables.
<h2>Safe Alternative</h2>
<p>
Create a <code>NOT NULL</code> check constraint instead.
<pre><code><!--
-->ALTER TABLE table ADD CONSTRAINT constraint CHECK (column IS NOT NULL) NOT VALID;
ALTER TABLE table VALIDATE CONSTRAINT constraint;
</code></pre>
<p>
In order to do this safely, please see
<strong>Add Something > Add a Constraint</strong>.
<em>Sorry to redirect you!</em>
<p>
That's it... Until Postgres 12 where this check constraint can be
converted into a proper <code>NOT NULL</code> constraint.
<hr>
<x-result-snippet-access-exclusive-lock></x-result-snippet-access-exclusive-lock>
<x-result-snippet-lock-queue></x-result-snippet-lock-queue>
<x-result-snippet-how-to-get-lock></x-result-snippet-how-to-get-lock>
<p>
<details>
<summary>
What are the disadvantages of a <code>NOT NULL</code> check constraint
</summary>
<ul>
<li>
People may not realise the column is <code>NOT NULL</code> because
the constraint belongs to the table rather than being an option
on the column.
<li>
Writes are slower (~0.5-1% hit)
<li>
Managing a constraint may be more complicated with your ORM and
migration framework(s).
</ul>
</details>
</div>
</x-result-alternative>
</template>
<template id="template-result__pg11__change__to_not_null">
<x-result__pg96__change__to_not_null></x-result__pg96__change__to_not_null>
</template>
<template id="template-result__pg12__change__to_not_null">
<x-result-alternative>
<div slot="content">
<p>
It is <strong>unsafe</strong> to make a column <code>NOT NULL</code>:
<pre><code><!--
-->ALTER TABLE table COLUMN column SET NOT NULL;
</code></pre>
<p>...
</div>
</x-result-alternative>
</template>
<template id="template-result__pg13__change__to_not_null">
<x-result__pg12__change__to_not_null></x-result__pg12__change__to_not_null>
</template>
<template id="template-result__pg96__data">
<x-result-alternative>
<div slot="content">
<p>The following kinds of data migrations are unsafe:
<pre><code><!--
-->UPDATE table SET column = value;
UPDATE table SET column = value WHERE condition;
</code></pre>
<p>
Where many rows are updated, this will take a long time to complete,
and, as a <code>FOR UPDATE</code> lock is obtained on the selected rows,
<strong>writes to those rows will be blocked</strong> until the
migration is complete.
<h2>Safe Alternative</h2>
<p>
Modify the data in batches that take about a second each to execute.
This will reduce the amount of time the lock is held.
<p>Execute the script in a persistent shell:
<pre><code><!--
-->#!/bin/bash
set -e
for i in {0..100}; do
echo "=== === === Batch#{i}"
psql -v ON_ERROR_STOP=1 -v v1="${i}" -f migration.sql
done
</code></pre>
<p><code>migration.sql</code> file:
<pre><code><!--
-->SET statement_timeout = 2000;
begin;
\timing on
UPDATE table
SET column = value
WHERE
id >= (:v1 + 0) * 1000
AND id < (:v1 + 1) * 1000
;
commit;
</code></pre>
<p>
In this example, we use the primary key of the table to chunk the rows
into batches of 1000, and the start and stop range <code>0..100</code>
was determined manually. Also, the transaction is useless, but it may be
necessary for more complex migrations. Furthermore, we enable timing so
that we have insight on how long each update takes. Finally, it is
trivial to stop/resume by altering the start of the range.
<hr>
<x-result-snippet-for-update-lock></x-result-snippet-for-update-lock>
<x-result-snippet-row-exclusive-lock></x-result-snippet-row-exclusive-lock>
</div>
</x-result-unsafe>
</template>
<template id="template-result__pg11__data">
<x-result__pg96__data></x-result__pg96__data>
</template>
<template id="template-result__pg12__data">
<x-result__pg96__data></x-result__pg96__data>
</template>
<template id="template-result__pg13__data">
<x-result__pg96__data></x-result__pg96__data>
</template>
</div>
<!-- Result Snippets -->
</div>
<template id="template-result-snippet-access-exclusive-lock">
<aside>
<p>
<details>
<summary>How can the <code>ACCESS EXCLUSIVE</code> lock break applications?</summary>
<p>
An <code>ACCESS EXCLUSIVE</code> lock conflicts with
<strong>everything</strong>, including:
<ul>
<li><code>SELECT</code>
<li><code>INSERT</code>
<li><code>UPDATE</code>
<li><code>DELETE</code>
</ul>
<p>
If a transaction is used, all rows will be locked until the
transaction is committed.
<p>
<strong>Warning:</strong> A migration needing this lock will block
other queries as soon as the SQL is sent to Postgres (even while waiting
for a lock). See <strong>How can the lock queue break applications?</strong>
</details>
</aside>
</template>
<template id="template-result-snippet-share-lock">
<aside>
<p>
<details>
<summary><!--
-->How can the <code>SHARE</code> lock break applications?
</summary>
<p>
From the perspective of an application, the <code>SHARE</code> lock
will block <strong>writes</strong> to the table, i.e.:
<ul>
<li><code>INSERT</code>
<li><code>UPDATE</code>
<li><code>DELETE</code>
</ul>
<p>
If a transaction is used, the rows will be locked until the
transaction is committed.
<p>
<strong>Warning:</strong> A migration needing this lock will block
writes as soon as the SQL is sent to Postgres (even while waiting
for a lock). See <strong>How can the lock queue break applications?</strong>
</details>
</aside>
</template>
<template id="template-result-snippet-row-exclusive-lock">
<aside>
<p>
<details>
<summary><!--
-->Can the <code>ROW EXCLUSIVE</code> lock break applications?
</summary>
<p>
Technically, a table-level <code>ROW EXCLUSIVE</code> is obtained as
well as this is an <code>UPDATE</code>. However, this doesn't conflict
with the kind of queries that applications do including other
<code>UPDATE</code> SQL.
<p>
On the other hand, it <em>can</em> conflict with other migrations, so
make sure you only do one migration at a time and this includes data
migrations.
</details>
</aside>
</template>
<template id="template-result-snippet-for-update-lock">
<aside>
<p>
<details>
<summary><!--
-->How can the <code>FOR UPDATE</code> lock break applications?
</summary>
<p>
From the perspective of an application, the row-level <code>FOR UPDATE</code>
lock will block <strong>some writes</strong> to the table, e.g.:
<ul>
<li><code>UPDATE</code>
<li><code>SELECT FOR UPDATE</code>
<li><code>DELETE</code>
</ul>
<p>
This lock only blocks the selected rows, rather than the whole table.
However, <code>UPDATE table SET column = value;</code> will effectively
lock the entire table.
<p>
If a transaction is used, the rows will be locked until the
transaction is committed.
<p>
Therefore it's important to reduce the number of rows blocked, and to
reduce the amount of time those rows are blocked, in order to prevent
the application from being blocked with its update operations.
</details>
</aside>
</template>
<template id="template-result-snippet-lock-queue">
<aside>
<p>
<details>
<summary>How can the lock queue break applications?</summary>
<p>
A migration can block application queries even while it is waiting
for its turn to execute.
<ol>
<li>Migration SQL is sent to Postgres
<li>It cannot execute right now because it needs to obtain a lock
<li>The migration enters the lock queue
<li>
Now, the application executes another query that needs a lock that
would conflict with the migration's lock.
<li>
The application query enters the lock queue <em>behind</em> the
migration SQL.
<li>
The application query is blocked on the migration SQL which is blocked
on some other application query.
</ol>
<p>
So even though the application query <em>could</em> execute because
the migration is not executing, Postgres will not do this!
<p>
So even if the migration <em>would</em> execute instantly
<em>if only it had the lock</em>, it doesn't matter. It starts to block
other conflicting queries immediately.
<p>
This could mean the application has temporarily degraded performance,
and worse, if the migration is taking too long, actual queries and
requests will start to fail until the migration has completed.
<p>
Therefore, you <em>should</em> use a <code>statement_timeout</code>
that is about a second long.
<pre><code>SET statement_timeout = 1000;</code></pre>
<p>
(Note that a <code>lock_timeout</code> is a subset of
a <code>statement_timeout</code>, but queries are blocked while in
the lock queue and while the migration is executing therefore
a <code>statement_timeout</code> is the appropriate timeout to use.)
<p>
Since this is likely to timeout for busy tables, an automatic retry
script <em>should</em> be used. See
<strong>HOW-TO: Obtain a lock safely for a migration</strong>.
</details>
</aside>
</template>
<template id="template-result-snippet-apps-blocking-migrations">
<aside>
<p>
<details>
<summary>How can applications block migrations?</summary>
<p>
Lock queue.
<p>
Migration can never obtain a lock because it uses a