-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
alter_primary_key
1718 lines (1351 loc) · 37.9 KB
/
alter_primary_key
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
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, z INT NOT NULL, w INT, INDEX i (x), INDEX i2 (z))
statement ok
INSERT INTO t VALUES (1, 2, 3, 4), (5, 6, 7, 8)
query T noticetrace
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y, z)
----
query IIII rowsort
SELECT * FROM t@t_pkey
----
1 2 3 4
5 6 7 8
statement ok
INSERT INTO t VALUES (9, 10, 11, 12)
query IIII rowsort
SELECT * from t@t_pkey
----
1 2 3 4
5 6 7 8
9 10 11 12
statement ok
UPDATE t SET x = 2 WHERE z = 7
query IIII rowsort
SELECT * from t@t_pkey
----
1 2 3 4
2 6 7 8
9 10 11 12
query T
SELECT feature_name FROM crdb_internal.feature_usage
WHERE feature_name IN ('sql.schema.alter_table.alter_primary_key') AND usage_count > 0
ORDER BY feature_name
----
sql.schema.alter_table.alter_primary_key
# Test primary key changes on storing indexes with different column families (the randomizer will do this for us).
statement ok
DROP TABLE t;
statement ok
CREATE TABLE t (
x INT PRIMARY KEY, y INT, z INT NOT NULL, w INT, v INT,
INDEX i1 (y) STORING (w, v), INDEX i2 (z) STORING (y, v)
);
statement ok
INSERT INTO t VALUES (1, 2, 3, 4, 5), (6, 7, 8, 9, 10), (11, 12, 13, 14, 15);
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (z);
statement ok
INSERT INTO t VALUES (16, 17, 18, 19, 20)
query III rowsort
SELECT y, w, v FROM t@i1
----
2 4 5
7 9 10
12 14 15
17 19 20
query III rowsort
SELECT y, z, v FROM t@i2
----
2 3 5
7 8 10
12 13 15
17 18 20
# Regression test for issue #81522.
statement error pgcode 42611 expressions such as \"gen_random_uuid\(\)\" are not allowed in primary index definition
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (gen_random_uuid());
# Test that composite values are encoded correctly in covering indexes.
statement ok
CREATE TABLE t_composite (x INT PRIMARY KEY, y DECIMAL NOT NULL);
statement ok
INSERT INTO t_composite VALUES (1, 1.0), (2, 1.001)
statement ok
ALTER TABLE t_composite ALTER PRIMARY KEY USING COLUMNS (y)
query IT rowsort
SELECT * FROM t_composite@t_composite_pkey
----
1 1.0
2 1.001
# Test that we can drop tables after a primary key change.
statement ok
DROP TABLE t_composite
subtest foreign_keys
# Test primary key changes on tables with inbound and outbound FK's.
statement ok
CREATE TABLE fk1 (x INT NOT NULL);
statement ok
CREATE TABLE fk2 (x INT NOT NULL, UNIQUE INDEX i (x));
statement ok
ALTER TABLE fk1 ADD CONSTRAINT fk FOREIGN KEY (x) REFERENCES fk2(x);
statement ok
INSERT INTO fk2 VALUES (1);
statement ok
INSERT INTO fk1 VALUES (1)
statement ok
ALTER TABLE fk1 ALTER PRIMARY KEY USING COLUMNS (x)
statement ok
INSERT INTO fk2 VALUES (2);
statement ok
INSERT INTO fk1 VALUES (2)
statement ok
ALTER TABLE fk2 ALTER PRIMARY KEY USING COLUMNS (x)
statement ok
INSERT INTO fk2 VALUES (3);
INSERT INTO fk1 VALUES (3)
# Test some self-referencing foreign keys.
statement ok
CREATE TABLE self (a INT PRIMARY KEY, x INT, y INT, z INT, w INT NOT NULL,
INDEX (x), UNIQUE INDEX (y), INDEX (z));
statement ok
INSERT INTO self VALUES (1, 1, 1, 1, 1);
statement ok
ALTER TABLE self ADD CONSTRAINT fk1 FOREIGN KEY (z) REFERENCES self (y);
statement ok
ALTER TABLE self ADD CONSTRAINT fk2 FOREIGN KEY (x) REFERENCES self (y);
statement ok
ALTER TABLE self ALTER PRIMARY KEY USING COLUMNS (w)
statement ok
INSERT INTO self VALUES (2, 1, 2, 1, 2);
INSERT INTO self VALUES (3, 2, 3, 2, 3)
# Set up a bunch of foreign key references pointing into and out of a table.
statement ok
CREATE TABLE t1 (x INT PRIMARY KEY, y INT NOT NULL, z INT, w INT, INDEX (y), INDEX (z), UNIQUE INDEX (w));
statement ok
CREATE TABLE t2 (y INT, UNIQUE INDEX (y));
statement ok
CREATE TABLE t3 (z INT, UNIQUE INDEX (z));
statement ok
CREATE TABLE t4 (w INT, INDEX (w));
statement ok
CREATE TABLE t5 (x INT, INDEX (x));
statement ok
INSERT INTO t1 VALUES (1, 1, 1, 1);
INSERT INTO t2 VALUES (1);
INSERT INTO t3 VALUES (1);
INSERT INTO t4 VALUES (1);
INSERT INTO t5 VALUES (1);
statement ok
ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY (y) REFERENCES t2(y);
statement ok
ALTER TABLE t1 ADD CONSTRAINT fk2 FOREIGN KEY (z) REFERENCES t3(z);
statement ok
ALTER TABLE t4 ADD CONSTRAINT fk3 FOREIGN KEY (w) REFERENCES t1(w);
statement ok
ALTER TABLE t5 ADD CONSTRAINT fk4 FOREIGN KEY (x) REFERENCES t1(x);
statement ok
ALTER TABLE t1 ALTER PRIMARY KEY USING COLUMNS (y)
statement ok
INSERT INTO t2 VALUES (5);
INSERT INTO t3 VALUES (6);
INSERT INTO t1 VALUES (7, 5, 6, 8);
INSERT INTO t4 VALUES (8);
INSERT INTO t5 VALUES (7)
statement error insert on table "t1" violates foreign key constraint "fk1"
INSERT INTO t1 VALUES (100, 100, 100, 100)
statement error insert on table "t4" violates foreign key constraint "fk3"
INSERT INTO t4 VALUES (101)
# Ensure that we still rewrite a primary index if the index column has name "rowid".
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (rowid INT PRIMARY KEY, y INT NOT NULL, FAMILY (rowid, y));
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
rowid INT8 NOT NULL,
y INT8 NOT NULL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
UNIQUE INDEX t_rowid_key (rowid ASC),
FAMILY fam_0_rowid_y (rowid, y)
)
subtest index_rewrites
# Test that indexes that need to get rewritten indeed get rewritten.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (
x INT PRIMARY KEY,
y INT NOT NULL, -- will be new primary key.
z INT NOT NULL,
w INT,
v JSONB,
INDEX i1 (w), -- will get rewritten.
INDEX i2 (y), -- will get rewritten.
UNIQUE INDEX i3 (z) STORING (y), -- will not be rewritten.
UNIQUE INDEX i4 (z), -- will be rewritten.
UNIQUE INDEX i5 (w) STORING (y), -- will be rewritten.
INVERTED INDEX i6 (v), -- will be rewritten.
INDEX i7 (z) USING HASH WITH (bucket_count=4), -- will be rewritten.
FAMILY (x, y, z, w, v)
);
statement ok
INSERT INTO t VALUES (1, 2, 3, 4, '{}');
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NOT NULL,
z INT8 NOT NULL,
w INT8 NULL,
v JSONB NULL,
crdb_internal_z_shard_4 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(z)), 4:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
UNIQUE INDEX i3 (z ASC) STORING (y),
UNIQUE INDEX t_x_key (x ASC),
INDEX i1 (w ASC),
INDEX i2 (y ASC),
UNIQUE INDEX i4 (z ASC),
UNIQUE INDEX i5 (w ASC) STORING (y),
INVERTED INDEX i6 (v),
INDEX i7 (z ASC) USING HASH WITH (bucket_count=4),
FAMILY fam_0_x_y_z_w_v (x, y, z, w, v)
)
# Test that the indexes we expect got rewritten. All but i3 should have been rewritten,
# so all but i3's indexID should be larger than 7.
query IT
SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_name = 't' ORDER BY index_id
----
4 i3
9 t_pkey
11 t_x_key
13 i1
15 i2
17 i4
19 i5
21 i6
23 i7
# Make sure that each index can index join against the new primary key;
query T
SELECT * FROM [EXPLAIN SELECT * FROM t@i1] OFFSET 2
----
·
• index join
│ table: t@t_pkey
│
└── • scan
missing stats
table: t@i1
spans: FULL SCAN
query IIIIT
SELECT * FROM t@i1
----
1 2 3 4 {}
query T
SELECT * FROM [EXPLAIN SELECT * FROM t@i2] OFFSET 2
----
·
• index join
│ table: t@t_pkey
│
└── • scan
missing stats
table: t@i2
spans: FULL SCAN
query IIIIT
SELECT * FROM t@i2
----
1 2 3 4 {}
query T
SELECT * FROM [EXPLAIN SELECT * FROM t@i3] OFFSET 2
----
·
• index join
│ table: t@t_pkey
│
└── • scan
missing stats
table: t@i3
spans: FULL SCAN
query IIIIT
SELECT * FROM t@i3
----
1 2 3 4 {}
query T
SELECT * FROM [EXPLAIN SELECT * FROM t@i4] OFFSET 2
----
·
• index join
│ table: t@t_pkey
│
└── • scan
missing stats
table: t@i4
spans: FULL SCAN
query IIIIT
SELECT * FROM t@i4
----
1 2 3 4 {}
query T
SELECT * FROM [EXPLAIN SELECT * FROM t@i5] OFFSET 2
----
·
• index join
│ table: t@t_pkey
│
└── • scan
missing stats
table: t@i5
spans: FULL SCAN
query IIIIT
SELECT * FROM t@i5
----
1 2 3 4 {}
query T
SELECT * FROM [EXPLAIN SELECT * FROM t@i7] OFFSET 2
----
·
• index join
│ table: t@t_pkey
│
└── • scan
missing stats
table: t@i7
spans: FULL SCAN
query IIIIT
SELECT * FROM t@i5
----
1 2 3 4 {}
subtest hash_sharded
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (
x INT PRIMARY KEY,
y INT NOT NULL,
z INT,
INDEX i1 (z) USING HASH WITH (bucket_count=5),
FAMILY (x, y, z)
);
statement ok
INSERT INTO t VALUES (1, 2, 3);
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y) USING HASH WITH (bucket_count=10)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NOT NULL,
z INT8 NULL,
crdb_internal_z_shard_5 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(z)), 5:::INT8)) VIRTUAL,
crdb_internal_y_shard_10 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(y)), 10:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC) USING HASH WITH (bucket_count=10),
UNIQUE INDEX t_x_key (x ASC),
INDEX i1 (z ASC) USING HASH WITH (bucket_count=5),
FAMILY fam_0_x_y_z (x, y, z)
)
query T
SELECT * FROM [EXPLAIN INSERT INTO t VALUES (4, 5, 6)] OFFSET 2
----
·
• insert fast path
into: t(x, y, z, crdb_internal_z_shard_5, crdb_internal_y_shard_10)
auto commit
size: 7 columns, 1 row
# Ensure that all of the indexes have been rewritten.
query IT
SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_name = 't' ORDER BY index_id
----
3 t_pkey
5 t_x_key
7 i1
query III
SELECT * FROM t@t_pkey
----
1 2 3
query III
SELECT * FROM t@t_x_key
----
1 2 3
query III
SELECT * FROM t@i1
----
1 2 3
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (
x INT PRIMARY KEY USING HASH WITH (bucket_count=5),
y INT NOT NULL,
z INT,
INDEX i (z),
FAMILY (x, y, z)
);
statement ok
INSERT INTO t VALUES (1, 2, 3);
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
crdb_internal_x_shard_5 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(x)), 5:::INT8)) VIRTUAL,
x INT8 NOT NULL,
y INT8 NOT NULL,
z INT8 NULL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH (bucket_count=5),
INDEX i (z ASC),
FAMILY fam_0_x_y_z (x, y, z)
)
query III
SELECT * FROM t@t_x_key
----
1 2 3
query III
SELECT * FROM t@i
----
1 2 3
# Ensure we don't rewrite default primary index even if its name isn't rowid.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (rowid INT NOT NULL);
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
rowid INT8 NOT NULL,
rowid_1 INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (rowid_1 ASC)
)
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (rowid)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
rowid INT8 NOT NULL,
rowid_1 INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (rowid ASC)
)
# Regression for old primary key not using PrimaryIndexEncoding as its encoding type.
subtest encoding_bug
# This test ensures that while the old primary key is in the mutations list it is
# able to be updated and deleted with the primary index encoding.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, z INT NOT NULL, FAMILY (x, y, z));
statement ok
INSERT INTO t VALUES (1, 2, 3);
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (z);
statement ok
UPDATE t SET y = 3 WHERE z = 3
# Test for #45363.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL)
statement ok
BEGIN
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y)
statement error pq: relation "t" \([0-9]+\): unimplemented: cannot perform other schema changes in the same transaction as a primary key change
CREATE INDEX ON t (y)
statement ok
ROLLBACK
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL)
statement ok
BEGIN
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y)
statement error pq: relation "t" \([0-9]+\): unimplemented: cannot perform other schema changes in the same transaction as a primary key change
ALTER TABLE t ADD COLUMN z INT
statement ok
ROLLBACK
subtest add_pk_rowid
# Tests for #45509.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY)
statement error pq: multiple primary keys for table "t" are not allowed
ALTER TABLE t ADD PRIMARY KEY (x)
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT NOT NULL)
statement ok
ALTER TABLE t ADD PRIMARY KEY (x)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (x ASC)
)
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT NOT NULL);
statement ok
ALTER TABLE t ADD PRIMARY KEY (x) USING HASH WITH (bucket_count=4)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
crdb_internal_x_shard_4 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(x)), 4:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (x ASC) USING HASH WITH (bucket_count=4)
)
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT NOT NULL);
statement ok
ALTER TABLE t ADD CONSTRAINT "my_pk" PRIMARY KEY (x)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT my_pk PRIMARY KEY (x ASC)
)
statement ok
CREATE INDEX i ON t (x);
statement error pgcode 42710 constraint with name \"i\" already exists
ALTER TABLE t DROP CONSTRAINT "my_pk", ADD CONSTRAINT "i" PRIMARY KEY (x);
# Regression for #45362.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT NOT NULL)
statement ok
BEGIN
statement ok
ALTER TABLE t ADD COLUMN y INT
statement error pq: unimplemented: cannot perform a primary key change on t with other schema changes on t in the same transaction
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (x)
statement ok
ROLLBACK
# Ensure that starting a primary key change that does not
# enqueue any mutations doesn't start a job.
# TODO (rohany): This test might become obselete when #44923 is fixed.
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT NOT NULL);
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (x)
query I
SELECT job_id FROM [SHOW JOBS] WHERE
description = 'CLEANUP JOB for ''ALTER TABLE test.public.t ALTER PRIMARY KEY USING COLUMNS (y)''' AND
status = 'running'
----
subtest add_drop_pk
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, FAMILY (x), FAMILY (y))
statement error pq: relation "t" \([0-9]+\): unimplemented: primary key dropped without subsequent addition of new primary key in same transaction
ALTER TABLE t DROP CONSTRAINT "t_pkey"
statement error pq: multiple primary keys for table "t" are not allowed
ALTER TABLE t ADD CONSTRAINT "t_pkey" PRIMARY KEY (y), DROP CONSTRAINT "t_pkey"
statement error pq: multiple primary keys for table "t" are not allowed
ALTER TABLE t ADD CONSTRAINT "t_pkey" PRIMARY KEY (y)
statement ok
ALTER TABLE t DROP CONSTRAINT "t_pkey", ADD CONSTRAINT "t_pkey" PRIMARY KEY (y)
statement ok
ALTER TABLE t DROP CONSTRAINT "t_pkey", ADD CONSTRAINT "t_pkey_v2" PRIMARY KEY (y)
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NOT NULL,
CONSTRAINT t_pkey_v2 PRIMARY KEY (y ASC),
FAMILY fam_0_x (x),
FAMILY fam_1_y (y)
)
statement ok
ALTER TABLE t ADD CONSTRAINT IF NOT EXISTS "t_pkey" PRIMARY KEY (x)
# Test that we can issue a DROP CONSTRAINT + ADD PRIMARY KEY
# in the same transaction.
statement ok
DROP TABLE t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, FAMILY (x), FAMILY (y))
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT "t_pkey"
statement ok
ALTER TABLE t ADD CONSTRAINT "t_pkey" PRIMARY KEY (y)
statement ok
ROLLBACK;
statement ok
DROP TABLE t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, FAMILY (x), FAMILY (y))
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT "t_pkey"
statement ok
ALTER TABLE t ADD CONSTRAINT "t_pkey_v2" PRIMARY KEY (y)
statement ok
COMMIT
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NOT NULL,
CONSTRAINT t_pkey_v2 PRIMARY KEY (y ASC),
FAMILY fam_0_x (x),
FAMILY fam_1_y (y)
)
# Ensure that we can't use a table with a dropped primary key
# in any DML statements.
statement ok
DROP TABLE t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL)
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 requested table does not have a primary key
INSERT INTO t VALUES (1, 1)
statement ok
ROLLBACK
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 pq: requested table does not have a primary key
DELETE FROM t WHERE x = 1
statement ok
ROLLBACK
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 pq: requested table does not have a primary key
UPDATE t SET x = 1 WHERE y = 1
statement ok
ROLLBACK
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 pq: requested table does not have a primary key
SELECT * FROM t
statement ok
ROLLBACK
# Ensure that DDL statements that don't add a primary key
# as their first operation don't succeed either.
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 pq: requested table does not have a primary key
CREATE INDEX ON t(x)
statement ok
ROLLBACK
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 pq: requested table does not have a primary key
ALTER TABLE t ADD COLUMN z INT
statement ok
ROLLBACK
statement ok
BEGIN
statement ok
ALTER TABLE t DROP CONSTRAINT t_pkey
statement error pgcode 55C02 pq: requested table does not have a primary key
ALTER TABLE t ADD COLUMN z INT, ADD PRIMARY KEY (x)
statement ok
ROLLBACK
# Ensure that other changes in the same transaction
# as a DROP PRIMARY KEY get rolled back on failure.
statement ok
DROP TABLE IF EXISTS t1, t2 CASCADE;
statement ok
CREATE TABLE t1 (x INT PRIMARY KEY, y INT NOT NULL);
statement ok
CREATE TABLE t2 (x INT)
statement ok
BEGIN
statement ok
ALTER TABLE t1 DROP CONSTRAINT t1_pkey
statement ok
INSERT INTO t2 VALUES (1)
statement error pq: relation "t1" \([0-9]+\): unimplemented: primary key dropped without subsequent addition of new primary key in same transaction
COMMIT
query I
SELECT * FROM t2
----
statement ok
DROP TABLE IF EXISTS t;
statement ok
CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL)
statement error pq: table "t" does not have a primary key, cannot perform ADD COLUMN z INT8 AS \(x \+ 1\) STORED
ALTER TABLE t DROP CONSTRAINT t_pkey, ADD COLUMN z INT AS (x + 1) STORED, ADD PRIMARY KEY (y)
subtest create_table_change_pk
statement ok
DROP TABLE IF EXISTS t CASCADE
statement ok
BEGIN
statement ok
CREATE TABLE t (x INT NOT NULL, y INT, FAMILY (x, y), INDEX (y))
statement ok
ALTER TABLE t ADD PRIMARY KEY (x)
statement ok
COMMIT
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (x ASC),
INDEX t_y_idx (y ASC),
FAMILY fam_0_x_y_rowid (x, y, rowid)
)
# Ensure that index y got rewritten. If it was not rewritten,
# it would have an id less than 3.
query IT
SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_name = 't' ORDER BY index_id
----
3 t_pkey
5 t_y_idx
# Repeat the above test using ALTER PRIMARY KEY.
statement ok
DROP TABLE IF EXISTS t
statement ok
BEGIN
statement ok
CREATE TABLE t (x INT NOT NULL, y INT, FAMILY (x, y), INDEX (y))
statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (x)
statement ok
COMMIT
query TT
SHOW CREATE t
----
t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (x ASC),
INDEX t_y_idx (y ASC),
FAMILY fam_0_x_y_rowid (x, y, rowid)
)
# Ensure that index y got rewritten. If it was not rewritten,
# it would have an id less than 3.
query IT
SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_name = 't' ORDER BY index_id
----
3 t_pkey
5 t_y_idx
# Test when multiple indexes get created and destroyed.
statement ok
DROP TABLE IF EXISTS t