已合并
[Feat] pg genrated as identity #9137
XXXCA创建于 4月3日
[Feat] pg genrated as identity #9137
已合并
共 142 个文件变更+10362-1641
| @@ -2379,10 +2379,6 @@ static void ParsePgAttributeTupleData(binary tupdata, int len, binary nullBitmap | |||
| 2379 | "attcollation: %d", | 2379 | "attcollation: %d", |
| 2380 | indents[indentLevel], | 2380 | indents[indentLevel], |
| 2381 | (pgAttributeTupData->attcollation)); | 2381 | (pgAttributeTupData->attcollation)); |
| 2382 | - fprintf(stdout, | ||
| 2383 | - "\n%s" "attkvtype: %d", | ||
| 2384 | - indents[indentLevel], | ||
| 2385 | - (pgAttributeTupData->attkvtype)); | ||
| 2386 | 2382 | ||
| 2387 | 2383 | ||
| 2388 | int remain = len - ATTRIBUTE_FIXED_PART_SIZE; | 2384 | int remain = len - ATTRIBUTE_FIXED_PART_SIZE; |
| @@ -1645,6 +1645,12 @@ static void JumbleExpr(pgssJumbleState* jstate, Node* node) | |||
| 1645 | APP_JUMB_STRING(ce->cursor_name); | 1645 | APP_JUMB_STRING(ce->cursor_name); |
| 1646 | APP_JUMB(ce->cursor_param); | 1646 | APP_JUMB(ce->cursor_param); |
| 1647 | } break; | 1647 | } break; |
| 1648 | + case T_NextValueExpr: { | ||
| 1649 | + NextValueExpr* nve = (NextValueExpr*)node; | ||
| 1650 | + | ||
| 1651 | + APP_JUMB(nve->seqid); | ||
| 1652 | + APP_JUMB(nve->typeId); | ||
| 1653 | + } break; | ||
| 1648 | case T_TargetEntry: { | 1654 | case T_TargetEntry: { |
| 1649 | TargetEntry* tle = (TargetEntry*)node; | 1655 | TargetEntry* tle = (TargetEntry*)node; |
| 1650 | 1656 | ||
| @@ -354,7 +354,11 @@ select @@fetch_status; | |||
| 354 | fetch next from c2; -- expect error | 354 | fetch next from c2; -- expect error |
| 355 | ERROR: cursor "c2" does not exist | 355 | ERROR: cursor "c2" does not exist |
| 356 | select @@fetch_status; | 356 | select @@fetch_status; |
| 357 | -ERROR: current transaction is aborted, commands ignored until end of transaction block, firstChar[Q] | 357 | + fetch_status |
| 358 | +-------------- | ||
| 359 | + -1 | ||
| 360 | +(1 row) | ||
| 361 | + | ||
| 358 | rollback transaction; | 362 | rollback transaction; |
| 359 | -- multi cursors | 363 | -- multi cursors |
| 360 | begin transaction; | 364 | begin transaction; |
| @@ -39,13 +39,14 @@ select * from book; | |||
| 39 | (4 rows) | 39 | (4 rows) |
| 40 | 40 | ||
| 41 | ALTER table book add column id int identity; | 41 | ALTER table book add column id int identity; |
| 42 | -ERROR: multiple identity specifications for column "bookid" of table "book" | 42 | +NOTICE: ALTER TABLE will create implicit sequence "book_id_seq_identity" for serial column "book.id" |
| 43 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 43 | ALTER table book alter bookId type text; | 44 | ALTER table book alter bookId type text; |
| 44 | -ERROR: Identity column 'bookid' data type invalid. | 45 | +ERROR: Not supported alter identity column bookid type |
| 45 | drop table if exists book; | 46 | drop table if exists book; |
| 46 | CREATE TABLE book(id int identity, bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); | 47 | CREATE TABLE book(id int identity, bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); |
| 47 | NOTICE: CREATE TABLE will create implicit sequence "book_id_seq_identity" for serial column "book.id" | 48 | NOTICE: CREATE TABLE will create implicit sequence "book_id_seq_identity" for serial column "book.id" |
| 48 | -ERROR: table can only have one identity column | 49 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. |
| 49 | CREATE TABLE book | 50 | CREATE TABLE book |
| 50 | ( | 51 | ( |
| 51 | bookId int NOT NULL PRIMARY KEY IDENTITY, | 52 | bookId int NOT NULL PRIMARY KEY IDENTITY, |
| @@ -265,6 +266,8 @@ CREATE TABLE book(id serial, bookId int identity default 5, bookname char(20)); | |||
| 265 | NOTICE: CREATE TABLE will create implicit sequence "book_id_seq" for serial column "book.id" | 266 | NOTICE: CREATE TABLE will create implicit sequence "book_id_seq" for serial column "book.id" |
| 266 | NOTICE: CREATE TABLE will create implicit sequence "book_bookid_seq_identity" for serial column "book.bookid" | 267 | NOTICE: CREATE TABLE will create implicit sequence "book_bookid_seq_identity" for serial column "book.bookid" |
| 267 | ERROR: multiple default values specified for column "bookid" of table "book" | 268 | ERROR: multiple default values specified for column "bookid" of table "book" |
| 269 | +LINE 1: CREATE TABLE book(id serial, bookId int identity default 5, ... | ||
| 270 | + ^ | ||
| 268 | CREATE TABLE t_identity(id serial, col int identity(5,10), col2 text) | 271 | CREATE TABLE t_identity(id serial, col int identity(5,10), col2 text) |
| 269 | partition by range(col)( | 272 | partition by range(col)( |
| 270 | partition p1 VALUES LESS THAN (10), | 273 | partition p1 VALUES LESS THAN (10), |
| @@ -477,9 +480,8 @@ insert into t_identity_0013(name) values('lisi'); | |||
| 477 | insert into t_identity_0013(name) values('wangwu'); | 480 | insert into t_identity_0013(name) values('wangwu'); |
| 478 | --error | 481 | --error |
| 479 | update t_identity_0013 set id=4 where name='zhangsan'; | 482 | update t_identity_0013 set id=4 where name='zhangsan'; |
| 480 | -ERROR: Cannot update identity column "id" | 483 | +ERROR: column "id" can only be updated to DEFAULT |
| 481 | -LINE 1: update t_identity_0013 set id=4 where name='zhangsan'; | 484 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 482 | - ^ | ||
| 483 | drop table t_identity_0013; | 485 | drop table t_identity_0013; |
| 484 | -- ## ident_current after truncate table. expected: success. | 486 | -- ## ident_current after truncate table. expected: success. |
| 485 | create table t_identity_0021(id int identity(100,1), name varchar(10)); | 487 | create table t_identity_0021(id int identity(100,1), name varchar(10)); |
| @@ -533,14 +535,30 @@ select scope_identity(); | |||
| 533 | (1 row) | 535 | (1 row) |
| 534 | 536 | ||
| 535 | -- no option. | 537 | -- no option. |
| 536 | -create table t_identity_0023_1 (like t_identity_0023); | 538 | +create table t_identity_0023_1_1 (like t_identity_0023); |
| 539 | +\d+ t_identity_0023_1_1 | ||
| 540 | + Table "identity_schema.t_identity_0023_1_1" | ||
| 541 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 542 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- | ||
| 543 | + id | integer | not null | plain | | | ||
| 544 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 545 | +Has OIDs: no | ||
| 546 | +Options: orientation=row, compression=no, collate=1537 | ||
| 547 | +Character Set: UTF8 | ||
| 548 | +Collate: utf8mb4_general_ci | ||
| 549 | + | ||
| 550 | +insert t_identity_0023_1_1 (name) values('xxx1'); | ||
| 551 | +ERROR: null value in column "id" violates not-null constraint | ||
| 552 | +DETAIL: Failing row contains (null, xxx1). | ||
| 553 | +-- including IDENTITY. | ||
| 554 | +create table t_identity_0023_1 (like t_identity_0023 INCLUDING IDENTITY); | ||
| 537 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0023_1_id_seq_identity" for serial column "t_identity_0023_1.id" | 555 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0023_1_id_seq_identity" for serial column "t_identity_0023_1.id" |
| 538 | \d+ t_identity_0023_1 | 556 | \d+ t_identity_0023_1 |
| 539 | - Table "identity_schema.t_identity_0023_1" | 557 | + Table "identity_schema.t_identity_0023_1" |
| 540 | - Column | Type | Modifiers | Storage | Stats target | Description | 558 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 541 | ---------+-----------------------+-------------------------------------------------------------------------+----------+--------------+------------- | 559 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 542 | - id | integer | not null default nextval('t_identity_0023_1_id_seq_identity'::regclass) | plain | | | 560 | + id | integer | not null identity | plain | | |
| 543 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 561 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 544 | Has OIDs: no | 562 | Has OIDs: no |
| 545 | Options: orientation=row, compression=no, collate=1537 | 563 | Options: orientation=row, compression=no, collate=1537 |
| 546 | Character Set: UTF8 | 564 | Character Set: UTF8 |
| @@ -590,24 +608,27 @@ select * from t_identity_0023_1 order by id; | |||
| 590 | 608 | ||
| 591 | -- including DEFAULTS. | 609 | -- including DEFAULTS. |
| 592 | create table t_identity_0023_2 (like t_identity_0023 INCLUDING DEFAULTS); | 610 | create table t_identity_0023_2 (like t_identity_0023 INCLUDING DEFAULTS); |
| 593 | -NOTICE: CREATE TABLE will create implicit sequence "t_identity_0023_2_id_seq_identity" for serial column "t_identity_0023_2.id" | ||
| 594 | \d+ t_identity_0023_2 | 611 | \d+ t_identity_0023_2 |
| 595 | - Table "identity_schema.t_identity_0023_2" | 612 | + Table "identity_schema.t_identity_0023_2" |
| 596 | - Column | Type | Modifiers | Storage | Stats target | Description | 613 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 597 | ---------+-----------------------+-------------------------------------------------------------------------+----------+--------------+------------- | 614 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 598 | - id | integer | not null default nextval('t_identity_0023_2_id_seq_identity'::regclass) | plain | | | 615 | + id | integer | not null | plain | | |
| 599 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 616 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 600 | Has OIDs: no | 617 | Has OIDs: no |
| 601 | Options: orientation=row, compression=no, collate=1537 | 618 | Options: orientation=row, compression=no, collate=1537 |
| 602 | Character Set: UTF8 | 619 | Character Set: UTF8 |
| 603 | Collate: utf8mb4_general_ci | 620 | Collate: utf8mb4_general_ci |
| 604 | 621 | ||
| 605 | insert t_identity_0023_2 (name) values('xxx1'); | 622 | insert t_identity_0023_2 (name) values('xxx1'); |
| 623 | +ERROR: null value in column "id" violates not-null constraint | ||
| 624 | +DETAIL: Failing row contains (null, xxx1). | ||
| 606 | insert t_identity_0023_2 (name) values('xxx1'); | 625 | insert t_identity_0023_2 (name) values('xxx1'); |
| 626 | +ERROR: null value in column "id" violates not-null constraint | ||
| 627 | +DETAIL: Failing row contains (null, xxx1). | ||
| 607 | select ident_current('t_identity_0023_2'); | 628 | select ident_current('t_identity_0023_2'); |
| 608 | ident_current | 629 | ident_current |
| 609 | --------------- | 630 | --------------- |
| 610 | - 101 | 631 | + |
| 611 | (1 row) | 632 | (1 row) |
| 612 | 633 | ||
| 613 | select scope_identity(); | 634 | select scope_identity(); |
| @@ -616,7 +637,7 @@ select scope_identity(); | |||
| 616 | 101 | 637 | 101 |
| 617 | (1 row) | 638 | (1 row) |
| 618 | 639 | ||
| 619 | -drop table t_identity_0023; | 640 | +drop table t_identity_0023, t_identity_0023_1_1; |
| 620 | drop table t_identity_0023_1; | 641 | drop table t_identity_0023_1; |
| 621 | drop table t_identity_0023_2; | 642 | drop table t_identity_0023_2; |
| 622 | -- ## can't insert identity column, expected error. | 643 | -- ## can't insert identity column, expected error. |
| @@ -626,9 +647,9 @@ create table t_identity(id int identity, col text); | |||
| 626 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_id_seq_identity" for serial column "t_identity.id" | 647 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_id_seq_identity" for serial column "t_identity.id" |
| 627 | -- expected error. | 648 | -- expected error. |
| 628 | insert into t_identity(id, col) values(2,'zshan'); | 649 | insert into t_identity(id, col) values(2,'zshan'); |
| 629 | -ERROR: Cannot insert identity column "id" | 650 | +ERROR: cannot insert a non-DEFAULT value into column "id" |
| 630 | -LINE 1: insert into t_identity(id, col) values(2,'zshan'); | 651 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 631 | - ^ | 652 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". |
| 632 | select ident_current('t_identity'); | 653 | select ident_current('t_identity'); |
| 633 | ident_current | 654 | ident_current |
| 634 | --------------- | 655 | --------------- |
| @@ -665,9 +686,9 @@ insert into t_identity(col) select 'test'; | |||
| 665 | insert into t_identity(col) select 'test'; | 686 | insert into t_identity(col) select 'test'; |
| 666 | -- expected error. | 687 | -- expected error. |
| 667 | insert into t_identity(id, col) select 1, 'test'; | 688 | insert into t_identity(id, col) select 1, 'test'; |
| 668 | -ERROR: Cannot insert identity column "id" | 689 | +ERROR: cannot insert a non-DEFAULT value into column "id" |
| 669 | -LINE 1: insert into t_identity(id, col) select 1, 'test'; | 690 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 670 | - ^ | 691 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". |
| 671 | -- expected success. | 692 | -- expected success. |
| 672 | set identity_insert = on; | 693 | set identity_insert = on; |
| 673 | insert into t_identity(id, col) select 1, 'test'; | 694 | insert into t_identity(id, col) select 1, 'test'; |
| @@ -699,9 +720,9 @@ select * from t_identity order by id; | |||
| 699 | -- expected error. | 720 | -- expected error. |
| 700 | insert into t_identity(id, col) values(100, 'newtest') | 721 | insert into t_identity(id, col) values(100, 'newtest') |
| 701 | on duplicate key update col = VALUES(col); | 722 | on duplicate key update col = VALUES(col); |
| 702 | -ERROR: Cannot insert identity column "id" | 723 | +ERROR: cannot insert a non-DEFAULT value into column "id" |
| 703 | -LINE 1: insert into t_identity(id, col) values(100, 'newtest') | 724 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 704 | - ^ | 725 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". |
| 705 | -- expected error. | 726 | -- expected error. |
| 706 | insert into t_identity(col) values(100, 'newtest') | 727 | insert into t_identity(col) values(100, 'newtest') |
| 707 | on duplicate key update col = VALUES(col); | 728 | on duplicate key update col = VALUES(col); |
| @@ -740,9 +761,9 @@ when not matched then | |||
| 740 | merge into target_table AS t using source_table AS s ON t.id = s.id | 761 | merge into target_table AS t using source_table AS s ON t.id = s.id |
| 741 | when not matched then | 762 | when not matched then |
| 742 | insert (id, name, age) values (s.id, s.name, s.age); | 763 | insert (id, name, age) values (s.id, s.name, s.age); |
| 743 | -ERROR: Cannot insert identity column "id" | 764 | +ERROR: cannot insert a non-DEFAULT value into column "id" |
| 744 | -LINE 3: insert (id, name, age) values (s.id, s.name, s.age); | 765 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 745 | - ^ | 766 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert" |
| 746 | -- expected success. | 767 | -- expected success. |
| 747 | set identity_insert = on; | 768 | set identity_insert = on; |
| 748 | merge into target_table AS t using source_table AS s ON t.id = s.id | 769 | merge into target_table AS t using source_table AS s ON t.id = s.id |
| @@ -795,10 +816,10 @@ NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_02_id_seq_i | |||
| 795 | select id as id_alias into t_identity_0024_02_1 from t_identity_0024; | 816 | select id as id_alias into t_identity_0024_02_1 from t_identity_0024; |
| 796 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_02_1_id_alias_seq_identity" for serial column "t_identity_0024_02_1.id_alias" | 817 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_02_1_id_alias_seq_identity" for serial column "t_identity_0024_02_1.id_alias" |
| 797 | \d+ t_identity_0024_02_1 | 818 | \d+ t_identity_0024_02_1 |
| 798 | - Table "identity_schema.t_identity_0024_02_1" | 819 | + Table "identity_schema.t_identity_0024_02_1" |
| 799 | - Column | Type | Modifiers | Storage | Stats target | Description | 820 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 800 | -----------+---------+----------------------------------------------------------------------------------+---------+--------------+------------- | 821 | +----------+---------+-------------------+---------+--------------+------------- |
| 801 | - id_alias | integer | not null default nextval('t_identity_0024_02_1_id_alias_seq_identity'::regclass) | plain | | | 822 | + id_alias | integer | not null identity | plain | | |
| 802 | Has OIDs: no | 823 | Has OIDs: no |
| 803 | Options: orientation=row, compression=no, collate=1537 | 824 | Options: orientation=row, compression=no, collate=1537 |
| 804 | Character Set: UTF8 | 825 | Character Set: UTF8 |
| @@ -813,12 +834,12 @@ select ident_current('t_identity_0024_02_1'); | |||
| 813 | select * into t_identity_0024_03 from t_identity_0024; | 834 | select * into t_identity_0024_03 from t_identity_0024; |
| 814 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_03_id_seq_identity" for serial column "t_identity_0024_03.id" | 835 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_03_id_seq_identity" for serial column "t_identity_0024_03.id" |
| 815 | \d+ t_identity_0024_03 | 836 | \d+ t_identity_0024_03 |
| 816 | - Table "identity_schema.t_identity_0024_03" | 837 | + Table "identity_schema.t_identity_0024_03" |
| 817 | - Column | Type | Modifiers | Storage | Stats target | Description | 838 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 818 | ---------+-----------------------+--------------------------------------------------------------------------+----------+--------------+------------- | 839 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 819 | - id | integer | not null default nextval('t_identity_0024_03_id_seq_identity'::regclass) | plain | | | 840 | + id | integer | not null identity | plain | | |
| 820 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 841 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 821 | - age | integer | | plain | | | 842 | + age | integer | | plain | | |
| 822 | Has OIDs: no | 843 | Has OIDs: no |
| 823 | Options: orientation=row, compression=no, collate=1537 | 844 | Options: orientation=row, compression=no, collate=1537 |
| 824 | Character Set: UTF8 | 845 | Character Set: UTF8 |
| @@ -833,11 +854,11 @@ select ident_current('t_identity_0024_03'); | |||
| 833 | select id, age as alias_age into t_identity_0024_04 from t_identity_0024; | 854 | select id, age as alias_age into t_identity_0024_04 from t_identity_0024; |
| 834 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_04_id_seq_identity" for serial column "t_identity_0024_04.id" | 855 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_0024_04_id_seq_identity" for serial column "t_identity_0024_04.id" |
| 835 | \d+ t_identity_0024_04 | 856 | \d+ t_identity_0024_04 |
| 836 | - Table "identity_schema.t_identity_0024_04" | 857 | + Table "identity_schema.t_identity_0024_04" |
| 837 | - Column | Type | Modifiers | Storage | Stats target | Description | 858 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 838 | ------------+---------+--------------------------------------------------------------------------+---------+--------------+------------- | 859 | +-----------+---------+-------------------+---------+--------------+------------- |
| 839 | - id | integer | not null default nextval('t_identity_0024_04_id_seq_identity'::regclass) | plain | | | 860 | + id | integer | not null identity | plain | | |
| 840 | - alias_age | integer | | plain | | | 861 | + alias_age | integer | | plain | | |
| 841 | Has OIDs: no | 862 | Has OIDs: no |
| 842 | Options: orientation=row, compression=no, collate=1537 | 863 | Options: orientation=row, compression=no, collate=1537 |
| 843 | Character Set: UTF8 | 864 | Character Set: UTF8 |
| @@ -868,9 +889,9 @@ select * from t_identity_0024_04 order by id; | |||
| 868 | set identity_insert = off; | 889 | set identity_insert = off; |
| 869 | -- expected: error | 890 | -- expected: error |
| 870 | insert into t_identity_0024_04(id, alias_age) values (1000, 22); | 891 | insert into t_identity_0024_04(id, alias_age) values (1000, 22); |
| 871 | -ERROR: Cannot insert identity column "id" | 892 | +ERROR: cannot insert a non-DEFAULT value into column "id" |
| 872 | -LINE 1: insert into t_identity_0024_04(id, alias_age) values (1000, ... | 893 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 873 | - ^ | 894 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". |
| 874 | set identity_insert = on; | 895 | set identity_insert = on; |
| 875 | -- expected: success | 896 | -- expected: success |
| 876 | insert into t_identity_0024_04(id, alias_age) values (1000, 22); | 897 | insert into t_identity_0024_04(id, alias_age) values (1000, 22); |
| @@ -947,9 +968,9 @@ NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_id1_s | |||
| 947 | select * into t_serial_identity_001_1 from t_serial_identity_001; | 968 | select * into t_serial_identity_001_1 from t_serial_identity_001; |
| 948 | NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id_seq_identity" for serial column "t_serial_identity_001_1.id" | 969 | NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id_seq_identity" for serial column "t_serial_identity_001_1.id" |
| 949 | NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id1_seq" for serial column "t_serial_identity_001_1.id1" | 970 | NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id1_seq" for serial column "t_serial_identity_001_1.id1" |
| 950 | -ERROR: Cannot insert identity column "id" | 971 | +ERROR: cannot insert a non-DEFAULT value into column "id" |
| 951 | -LINE 1: ...t * into t_serial_identity_001_1 from t_serial_identity_001; | 972 | +DETAIL: Column "id" is an identity column defined as "IDENTITY". |
| 952 | - ^ | 973 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". |
| 953 | set identity_insert = on; | 974 | set identity_insert = on; |
| 954 | -- expected: success | 975 | -- expected: success |
| 955 | select * into t_serial_identity_001_1 from t_serial_identity_001; | 976 | select * into t_serial_identity_001_1 from t_serial_identity_001; |
| @@ -957,12 +978,12 @@ NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id_ | |||
| 957 | NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id1_seq" for serial column "t_serial_identity_001_1.id1" | 978 | NOTICE: CREATE TABLE will create implicit sequence "t_serial_identity_001_1_id1_seq" for serial column "t_serial_identity_001_1.id1" |
| 958 | set identity_insert = off; | 979 | set identity_insert = off; |
| 959 | \d+ t_serial_identity_001_1 | 980 | \d+ t_serial_identity_001_1 |
| 960 | - Table "identity_schema.t_serial_identity_001_1" | 981 | + Table "identity_schema.t_serial_identity_001_1" |
| 961 | - Column | Type | Modifiers | Storage | Stats target | Description | 982 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 962 | ---------+-----------------------+-------------------------------------------------------------------------------+----------+--------------+------------- | 983 | +--------+-----------------------+-----------------------------------------------------------------------+----------+--------------+------------- |
| 963 | - id | integer | not null default nextval('t_serial_identity_001_1_id_seq_identity'::regclass) | plain | | | 984 | + id | integer | not null identity | plain | | |
| 964 | - id1 | integer | not null default nextval('t_serial_identity_001_1_id1_seq'::regclass) | plain | | | 985 | + id1 | integer | not null default nextval('t_serial_identity_001_1_id1_seq'::regclass) | plain | | |
| 965 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 986 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 966 | Has OIDs: no | 987 | Has OIDs: no |
| 967 | Options: orientation=row, compression=no, collate=1537 | 988 | Options: orientation=row, compression=no, collate=1537 |
| 968 | Character Set: UTF8 | 989 | Character Set: UTF8 |
| @@ -992,11 +1013,11 @@ set identity_insert = on; | |||
| 992 | select id, name into t_identity_numeric_t1_1 from t_identity_numeric_t1; | 1013 | select id, name into t_identity_numeric_t1_1 from t_identity_numeric_t1; |
| 993 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t1_1_id_seq_identity" for serial column "t_identity_numeric_t1_1.id" | 1014 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t1_1_id_seq_identity" for serial column "t_identity_numeric_t1_1.id" |
| 994 | \d+ t_identity_numeric_t1_1 | 1015 | \d+ t_identity_numeric_t1_1 |
| 995 | - Table "identity_schema.t_identity_numeric_t1_1" | 1016 | + Table "identity_schema.t_identity_numeric_t1_1" |
| 996 | - Column | Type | Modifiers | Storage | Stats target | Description | 1017 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 997 | ---------+-----------------------+-------------------------------------------------------------------------------+----------+--------------+------------- | 1018 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 998 | - id | numeric(25,0) | not null default nextval('t_identity_numeric_t1_1_id_seq_identity'::regclass) | main | | | 1019 | + id | numeric(25,0) | not null identity | main | | |
| 999 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 1020 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 1000 | Has OIDs: no | 1021 | Has OIDs: no |
| 1001 | Options: orientation=row, compression=no, collate=1537 | 1022 | Options: orientation=row, compression=no, collate=1537 |
| 1002 | Character Set: UTF8 | 1023 | Character Set: UTF8 |
| @@ -1021,11 +1042,11 @@ select id, name into t_identity_numeric_t2_1 from t_identity_numeric_t2; | |||
| 1021 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t2_1_id_seq_identity" for serial column "t_identity_numeric_t2_1.id" | 1042 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t2_1_id_seq_identity" for serial column "t_identity_numeric_t2_1.id" |
| 1022 | set identity_insert = off; | 1043 | set identity_insert = off; |
| 1023 | \d+ t_identity_numeric_t2_1 | 1044 | \d+ t_identity_numeric_t2_1 |
| 1024 | - Table "identity_schema.t_identity_numeric_t2_1" | 1045 | + Table "identity_schema.t_identity_numeric_t2_1" |
| 1025 | - Column | Type | Modifiers | Storage | Stats target | Description | 1046 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 1026 | ---------+-----------------------+-------------------------------------------------------------------------------+----------+--------------+------------- | 1047 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 1027 | - id | numeric(25,0) | not null default nextval('t_identity_numeric_t2_1_id_seq_identity'::regclass) | main | | | 1048 | + id | numeric(25,0) | not null identity | main | | |
| 1028 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 1049 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 1029 | Has OIDs: no | 1050 | Has OIDs: no |
| 1030 | Options: orientation=row, compression=no, collate=1537 | 1051 | Options: orientation=row, compression=no, collate=1537 |
| 1031 | Character Set: UTF8 | 1052 | Character Set: UTF8 |
| @@ -1049,11 +1070,11 @@ select id, name into t_identity_numeric_t3_1 from t_identity_numeric_t3; | |||
| 1049 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t3_1_id_seq_identity" for serial column "t_identity_numeric_t3_1.id" | 1070 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t3_1_id_seq_identity" for serial column "t_identity_numeric_t3_1.id" |
| 1050 | set identity_insert = off; | 1071 | set identity_insert = off; |
| 1051 | \d+ t_identity_numeric_t3_1 | 1072 | \d+ t_identity_numeric_t3_1 |
| 1052 | - Table "identity_schema.t_identity_numeric_t3_1" | 1073 | + Table "identity_schema.t_identity_numeric_t3_1" |
| 1053 | - Column | Type | Modifiers | Storage | Stats target | Description | 1074 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 1054 | ---------+-----------------------+-------------------------------------------------------------------------------+----------+--------------+------------- | 1075 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 1055 | - id | numeric(25,0) | not null default nextval('t_identity_numeric_t3_1_id_seq_identity'::regclass) | main | | | 1076 | + id | numeric(25,0) | not null identity | main | | |
| 1056 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 1077 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 1057 | Has OIDs: no | 1078 | Has OIDs: no |
| 1058 | Options: orientation=row, compression=no, collate=1537 | 1079 | Options: orientation=row, compression=no, collate=1537 |
| 1059 | Character Set: UTF8 | 1080 | Character Set: UTF8 |
| @@ -1074,14 +1095,14 @@ drop table t_identity_numeric_t3_1; | |||
| 1074 | -- max/min value | 1095 | -- max/min value |
| 1075 | create table t_identity_numeric_t4(id numeric(38, 0) identity, name varchar(10)); | 1096 | create table t_identity_numeric_t4(id numeric(38, 0) identity, name varchar(10)); |
| 1076 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t4_id_seq_identity" for serial column "t_identity_numeric_t4.id" | 1097 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t4_id_seq_identity" for serial column "t_identity_numeric_t4.id" |
| 1077 | -create table t_identity_numeric_t4_1 (like t_identity_numeric_t4); | 1098 | +create table t_identity_numeric_t4_1 (like t_identity_numeric_t4 including identity); |
| 1078 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t4_1_id_seq_identity" for serial column "t_identity_numeric_t4_1.id" | 1099 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t4_1_id_seq_identity" for serial column "t_identity_numeric_t4_1.id" |
| 1079 | \d+ t_identity_numeric_t4_1; | 1100 | \d+ t_identity_numeric_t4_1; |
| 1080 | - Table "identity_schema.t_identity_numeric_t4_1" | 1101 | + Table "identity_schema.t_identity_numeric_t4_1" |
| 1081 | - Column | Type | Modifiers | Storage | Stats target | Description | 1102 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 1082 | ---------+-----------------------+-------------------------------------------------------------------------------+----------+--------------+------------- | 1103 | +--------+-----------------------+-----------------------------------------------+----------+--------------+------------- |
| 1083 | - id | numeric(38,0) | not null default nextval('t_identity_numeric_t4_1_id_seq_identity'::regclass) | main | | | 1104 | + id | numeric(38,0) | not null identity | main | | |
| 1084 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 1105 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 1085 | Has OIDs: no | 1106 | Has OIDs: no |
| 1086 | Options: orientation=row, compression=no, collate=1537 | 1107 | Options: orientation=row, compression=no, collate=1537 |
| 1087 | Character Set: UTF8 | 1108 | Character Set: UTF8 |
| @@ -1102,7 +1123,7 @@ drop table t_identity_numeric_t4_1; | |||
| 1102 | create table t_identity_numeric_t5(id numeric(38, 0) identity(987654321098765432100000, 2), id1 serial, name varchar(10)); | 1123 | create table t_identity_numeric_t5(id numeric(38, 0) identity(987654321098765432100000, 2), id1 serial, name varchar(10)); |
| 1103 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_id_seq_identity" for serial column "t_identity_numeric_t5.id" | 1124 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_id_seq_identity" for serial column "t_identity_numeric_t5.id" |
| 1104 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_id1_seq" for serial column "t_identity_numeric_t5.id1" | 1125 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_id1_seq" for serial column "t_identity_numeric_t5.id1" |
| 1105 | -create table t_identity_numeric_t5_1 (like t_identity_numeric_t5); | 1126 | +create table t_identity_numeric_t5_1 (like t_identity_numeric_t5 including identity); |
| 1106 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_1_id_seq_identity" for serial column "t_identity_numeric_t5_1.id" | 1127 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_1_id_seq_identity" for serial column "t_identity_numeric_t5_1.id" |
| 1107 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_1_id1_seq" for serial column "t_identity_numeric_t5_1.id1" | 1128 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t5_1_id1_seq" for serial column "t_identity_numeric_t5_1.id1" |
| 1108 | insert into t_identity_numeric_t5_1(name) values('xxx'); -- over int64 | 1129 | insert into t_identity_numeric_t5_1(name) values('xxx'); -- over int64 |
| @@ -1118,16 +1139,16 @@ drop table t_identity_numeric_t5_1; | |||
| 1118 | create table t_identity_numeric_t6(id numeric(38, 0) identity(2, 987654321098765432100000), id1 serial, name varchar(10)); | 1139 | create table t_identity_numeric_t6(id numeric(38, 0) identity(2, 987654321098765432100000), id1 serial, name varchar(10)); |
| 1119 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_id_seq_identity" for serial column "t_identity_numeric_t6.id" | 1140 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_id_seq_identity" for serial column "t_identity_numeric_t6.id" |
| 1120 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_id1_seq" for serial column "t_identity_numeric_t6.id1" | 1141 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_id1_seq" for serial column "t_identity_numeric_t6.id1" |
| 1121 | -create table t_identity_numeric_t6_1 (like t_identity_numeric_t6); | 1142 | +create table t_identity_numeric_t6_1 (like t_identity_numeric_t6 including identity); |
| 1122 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_1_id_seq_identity" for serial column "t_identity_numeric_t6_1.id" | 1143 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_1_id_seq_identity" for serial column "t_identity_numeric_t6_1.id" |
| 1123 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_1_id1_seq" for serial column "t_identity_numeric_t6_1.id1" | 1144 | NOTICE: CREATE TABLE will create implicit sequence "t_identity_numeric_t6_1_id1_seq" for serial column "t_identity_numeric_t6_1.id1" |
| 1124 | \d+ t_identity_numeric_t6_1 | 1145 | \d+ t_identity_numeric_t6_1 |
| 1125 | - Table "identity_schema.t_identity_numeric_t6_1" | 1146 | + Table "identity_schema.t_identity_numeric_t6_1" |
| 1126 | - Column | Type | Modifiers | Storage | Stats target | Description | 1147 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 1127 | ---------+-----------------------+-------------------------------------------------------------------------------+----------+--------------+------------- | 1148 | +--------+-----------------------+-----------------------------------------------------------------------+----------+--------------+------------- |
| 1128 | - id | numeric(38,0) | not null default nextval('t_identity_numeric_t6_1_id_seq_identity'::regclass) | main | | | 1149 | + id | numeric(38,0) | not null identity | main | | |
| 1129 | - id1 | integer | not null default nextval('t_identity_numeric_t6_1_id1_seq'::regclass) | plain | | | 1150 | + id1 | integer | not null default nextval('t_identity_numeric_t6_1_id1_seq'::regclass) | plain | | |
| 1130 | - name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | | 1151 | + name | character varying(10) | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 1131 | Has OIDs: no | 1152 | Has OIDs: no |
| 1132 | Options: orientation=row, compression=no, collate=1537 | 1153 | Options: orientation=row, compression=no, collate=1537 |
| 1133 | Character Set: UTF8 | 1154 | Character Set: UTF8 |
| @@ -1148,11 +1169,11 @@ drop table t_identity_numeric_t6_1; | |||
| 1148 | create table t_ident (id int identity, col text) with (orientation=column); | 1169 | create table t_ident (id int identity, col text) with (orientation=column); |
| 1149 | NOTICE: CREATE TABLE will create implicit sequence "t_ident_id_seq_identity" for serial column "t_ident.id" | 1170 | NOTICE: CREATE TABLE will create implicit sequence "t_ident_id_seq_identity" for serial column "t_ident.id" |
| 1150 | \d+ t_ident; | 1171 | \d+ t_ident; |
| 1151 | - Table "identity_schema.t_ident" | 1172 | + Table "identity_schema.t_ident" |
| 1152 | - Column | Type | Modifiers | Storage | Stats target | Description | 1173 | + Column | Type | Modifiers | Storage | Stats target | Description |
| 1153 | ---------+---------+---------------------------------------------------------------+----------+--------------+------------- | 1174 | +--------+---------+-----------------------------------------------+----------+--------------+------------- |
| 1154 | - id | integer | not null default nextval('t_ident_id_seq_identity'::regclass) | plain | | | 1175 | + id | integer | not null identity | plain | | |
| 1155 | - col | text | character set UTF8 collate utf8mb4_general_ci | extended | | | 1176 | + col | text | character set UTF8 collate utf8mb4_general_ci | extended | | |
| 1156 | Has OIDs: no | 1177 | Has OIDs: no |
| 1157 | Options: orientation=column, compression=low, collate=1537 | 1178 | Options: orientation=column, compression=low, collate=1537 |
| 1158 | Character Set: UTF8 | 1179 | Character Set: UTF8 |
| @@ -1178,7 +1199,7 @@ drop table t_ident; | |||
| 1178 | create sequence identity_t2_id_seq_identity; | 1199 | create sequence identity_t2_id_seq_identity; |
| 1179 | create table identity_t2(id numeric(38, 0) identity(10, 2), b int); | 1200 | create table identity_t2(id numeric(38, 0) identity(10, 2), b int); |
| 1180 | NOTICE: CREATE TABLE will create implicit sequence "identity_t2_id_seq_identity1" for serial column "identity_t2.id" | 1201 | NOTICE: CREATE TABLE will create implicit sequence "identity_t2_id_seq_identity1" for serial column "identity_t2.id" |
| 1181 | -create table identity_t2_1 (like identity_t2); | 1202 | +create table identity_t2_1 (like identity_t2 including identity); |
| 1182 | NOTICE: CREATE TABLE will create implicit sequence "identity_t2_1_id_seq_identity" for serial column "identity_t2_1.id" | 1203 | NOTICE: CREATE TABLE will create implicit sequence "identity_t2_1_id_seq_identity" for serial column "identity_t2_1.id" |
| 1183 | insert into identity_t2_1(b) values (10); | 1204 | insert into identity_t2_1(b) values (10); |
| 1184 | insert into identity_t2_1(b) values (10); | 1205 | insert into identity_t2_1(b) values (10); |
| @@ -1211,5 +1232,16 @@ select id, b from identity_t3_1; | |||
| 1211 | drop sequence identity_t3_id_seq_identity; | 1232 | drop sequence identity_t3_id_seq_identity; |
| 1212 | drop table identity_t3; | 1233 | drop table identity_t3; |
| 1213 | drop table identity_t3_1; | 1234 | drop table identity_t3_1; |
| 1235 | +-- compatiable with pg identity | ||
| 1236 | +-- error | ||
| 1237 | +create table identity_t1(id numeric(38, 0) identity(1, 2) generated always as identity); | ||
| 1238 | +NOTICE: CREATE TABLE will create implicit sequence "identity_t1_id_seq_identity" for serial column "identity_t1.id" | ||
| 1239 | +ERROR: multiple identity specifications for column "id" of table "identity_t1" | ||
| 1240 | +LINE 1: ...able identity_t1(id numeric(38, 0) identity(1, 2) generated ... | ||
| 1241 | + ^ | ||
| 1242 | +-- error | ||
| 1243 | +create table identity_t1(id numeric(38, 0) identity(1, 2), id2 numeric(37, 0) generated always as identity); | ||
| 1244 | +NOTICE: CREATE TABLE will create implicit sequence "identity_t1_id_seq_identity" for serial column "identity_t1.id" | ||
| 1245 | +ERROR: Multiple identity columns specified for table "identity_t1". Only one identity column per table is allowed. | ||
| 1214 | reset current_schema; | 1246 | reset current_schema; |
| 1215 | drop schema identity_schema; | 1247 | drop schema identity_schema; |
| @@ -0,0 +1,1707 @@ | |||
| 1 | +create database s_test_pg_identity dbcompatibility 'D'; | ||
| 2 | +\c s_test_pg_identity | ||
| 3 | +create extension shark; | ||
| 4 | +--## create table | ||
| 5 | +-- error | ||
| 6 | +CREATE TABLE test_t1 (a serial GENERATED ALWAYS AS IDENTITY, b text); | ||
| 7 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 8 | +ERROR: conflicting serial/identity declarations for column "a" of table "test_t1" | ||
| 9 | +LINE 1: CREATE TABLE test_t1 (a serial GENERATED ALWAYS AS IDENTITY,... | ||
| 10 | + ^ | ||
| 11 | +-- error | ||
| 12 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY default 3, b text); | ||
| 13 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 14 | +ERROR: multiple default values specified for column "a" of table "test_t1" | ||
| 15 | +LINE 1: ...TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY default 3,... | ||
| 16 | + ^ | ||
| 17 | +-- error | ||
| 18 | +CREATE TABLE test_t1 (id int1 GENERATED ALWAYS AS IDENTITY (START WITH 44) NULL); | ||
| 19 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_id_seq" for serial column "test_t1.id" | ||
| 20 | +ERROR: conflicting NULL/NOT NULL declarations for column "id" of table "test_t1" | ||
| 21 | +LINE 1: ...(id int1 GENERATED ALWAYS AS IDENTITY (START WITH 44) NULL); | ||
| 22 | + ^ | ||
| 23 | +CREATE TABLE test_t1 (id int1 NULL GENERATED ALWAYS AS IDENTITY (START WITH 44)); | ||
| 24 | +ERROR: conflicting NULL/NOT NULL declarations for column "id" of table "test_t1" | ||
| 25 | +LINE 1: CREATE TABLE test_t1 (id int1 NULL GENERATED ALWAYS AS IDENT... | ||
| 26 | + ^ | ||
| 27 | +-- error | ||
| 28 | +CREATE TABLE par_row_t1 (a int GENERATED ALWAYS AS IDENTITY, b text) partition by range (a) | ||
| 29 | +( | ||
| 30 | + partition par_row_t1_p0 values less than(10), | ||
| 31 | + partition par_row_t1_p3 values less than (maxvalue) | ||
| 32 | +); | ||
| 33 | +NOTICE: CREATE TABLE will create implicit sequence "par_row_t1_a_seq" for serial column "par_row_t1.a" | ||
| 34 | +ERROR: identity column currently not supported partitioned table "par_row_t1" | ||
| 35 | +CREATE temp TABLE test_t4 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 36 | +NOTICE: CREATE TABLE will create implicit sequence "test_t4_a_seq" for serial column "test_t4.a" | ||
| 37 | +-- success | ||
| 38 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 39 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 40 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY, b text); | ||
| 41 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 42 | +CREATE TABLE test_t3 (id int1 NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 44)); | ||
| 43 | +NOTICE: CREATE TABLE will create implicit sequence "test_t3_id_seq" for serial column "test_t3.id" | ||
| 44 | +\d+ test_t1 | ||
| 45 | + Table "public.test_t1" | ||
| 46 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 47 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 48 | + a | integer | not null generated always as identity | plain | | | ||
| 49 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 50 | +Has OIDs: no | ||
| 51 | +Options: orientation=row, compression=no, collate=1537 | ||
| 52 | +Character Set: UTF8 | ||
| 53 | +Collate: utf8mb4_general_ci | ||
| 54 | + | ||
| 55 | +\d+ test_t2 | ||
| 56 | + Table "public.test_t2" | ||
| 57 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 58 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 59 | + a | integer | not null generated by default as identity | plain | | | ||
| 60 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 61 | +Has OIDs: no | ||
| 62 | +Options: orientation=row, compression=no, collate=1537 | ||
| 63 | +Character Set: UTF8 | ||
| 64 | +Collate: utf8mb4_general_ci | ||
| 65 | + | ||
| 66 | +select pg_get_tabledef('test_t3'::regclass); | ||
| 67 | + pg_get_tabledef | ||
| 68 | +------------------------------------------------------- | ||
| 69 | + SET search_path = public; + | ||
| 70 | + CREATE TABLE test_t3 ( + | ||
| 71 | + id tinyint GENERATED ALWAYS AS IDENTITY NOT NULL + | ||
| 72 | + ) + | ||
| 73 | + WITH (orientation=row, compression=no, collate=1537); | ||
| 74 | +(1 row) | ||
| 75 | + | ||
| 76 | +-- identity sequence options | ||
| 77 | +CREATE TABLE test_t5 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 10 MAXVALUE 30 MINVALUE 12), b text); -- error | ||
| 78 | +NOTICE: CREATE TABLE will create implicit sequence "test_t5_a_seq" for serial column "test_t5.a" | ||
| 79 | +ERROR: START value (10) cannot be less than MINVALUE (12) | ||
| 80 | +CREATE TABLE test_t5 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 10 MAXVALUE 30 MINVALUE 9), b text); -- success | ||
| 81 | +NOTICE: CREATE TABLE will create implicit sequence "test_t5_a_seq" for serial column "test_t5.a" | ||
| 82 | +\d+ test_t5; | ||
| 83 | + Table "public.test_t5" | ||
| 84 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 85 | +--------+----------+-----------------------------------------------+----------+--------------+------------- | ||
| 86 | + a | smallint | not null generated always as identity | plain | | | ||
| 87 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 88 | +Has OIDs: no | ||
| 89 | +Options: orientation=row, compression=no, collate=1537 | ||
| 90 | +Character Set: UTF8 | ||
| 91 | +Collate: utf8mb4_general_ci | ||
| 92 | + | ||
| 93 | +select start_value, max_value, min_value, increment_by from test_t5_a_seq; | ||
| 94 | + start_value | max_value | min_value | increment_by | ||
| 95 | +-------------+-----------+-----------+-------------- | ||
| 96 | + 10 | 30 | 9 | 1 | ||
| 97 | +(1 row) | ||
| 98 | + | ||
| 99 | +INSERT INTO test_t5 DEFAULT VALUES; -- success | ||
| 100 | +INSERT INTO test_t5 OVERRIDING SYSTEM VALUE VALUES(8, 'xxxx'); -- success | ||
| 101 | +SELECT * FROM test_t5 order by 1, 2; | ||
| 102 | + a | b | ||
| 103 | +----+------ | ||
| 104 | + 8 | xxxx | ||
| 105 | + 10 | | ||
| 106 | +(2 rows) | ||
| 107 | + | ||
| 108 | +CREATE TABLE test_t6 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 11 NOMAXVALUE MINVALUE 9), b text); -- success | ||
| 109 | +NOTICE: CREATE TABLE will create implicit sequence "test_t6_a_seq" for serial column "test_t6.a" | ||
| 110 | +\d+ test_t6; | ||
| 111 | + Table "public.test_t6" | ||
| 112 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 113 | +--------+----------+-----------------------------------------------+----------+--------------+------------- | ||
| 114 | + a | smallint | not null generated always as identity | plain | | | ||
| 115 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 116 | +Has OIDs: no | ||
| 117 | +Options: orientation=row, compression=no, collate=1537 | ||
| 118 | +Character Set: UTF8 | ||
| 119 | +Collate: utf8mb4_general_ci | ||
| 120 | + | ||
| 121 | +select start_value, max_value, min_value, increment_by from test_t6_a_seq; | ||
| 122 | + start_value | max_value | min_value | increment_by | ||
| 123 | +-------------+---------------------+-----------+-------------- | ||
| 124 | + 11 | 9223372036854775807 | 9 | 1 | ||
| 125 | +(1 row) | ||
| 126 | + | ||
| 127 | +CREATE TABLE test_t7 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 12 MAXVALUE 15 INCREMENT 2 NOMINVALUE), b text); -- success | ||
| 128 | +NOTICE: CREATE TABLE will create implicit sequence "test_t7_a_seq" for serial column "test_t7.a" | ||
| 129 | +\d+ test_t7; | ||
| 130 | + Table "public.test_t7" | ||
| 131 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 132 | +--------+----------+-----------------------------------------------+----------+--------------+------------- | ||
| 133 | + a | smallint | not null generated always as identity | plain | | | ||
| 134 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 135 | +Has OIDs: no | ||
| 136 | +Options: orientation=row, compression=no, collate=1537 | ||
| 137 | +Character Set: UTF8 | ||
| 138 | +Collate: utf8mb4_general_ci | ||
| 139 | + | ||
| 140 | +select start_value, max_value, min_value, increment_by from test_t7_a_seq; | ||
| 141 | + start_value | max_value | min_value | increment_by | ||
| 142 | +-------------+-----------+-----------+-------------- | ||
| 143 | + 12 | 15 | 1 | 2 | ||
| 144 | +(1 row) | ||
| 145 | + | ||
| 146 | +INSERT INTO test_t7 DEFAULT VALUES; -- success | ||
| 147 | +INSERT INTO test_t7 DEFAULT VALUES; -- success | ||
| 148 | +INSERT INTO test_t7 DEFAULT VALUES; -- error | ||
| 149 | +ERROR: nextval: reached maximum value of sequence "test_t7_a_seq" (15) | ||
| 150 | +CONTEXT: referenced column: a | ||
| 151 | +ALTER TABLE test_t7 ALTER COLUMN a RESTART; | ||
| 152 | +select start_value, max_value, min_value, increment_by from test_t7_a_seq; | ||
| 153 | + start_value | max_value | min_value | increment_by | ||
| 154 | +-------------+-----------+-----------+-------------- | ||
| 155 | + 12 | 15 | 1 | 2 | ||
| 156 | +(1 row) | ||
| 157 | + | ||
| 158 | +INSERT INTO test_t7 DEFAULT VALUES; -- success | ||
| 159 | +select * from test_t7 order by 1, 2; | ||
| 160 | + a | b | ||
| 161 | +----+--- | ||
| 162 | + 12 | | ||
| 163 | + 12 | | ||
| 164 | + 14 | | ||
| 165 | +(3 rows) | ||
| 166 | + | ||
| 167 | +DROP TABLE test_t1, test_t2, test_t3, test_t5, test_t6, test_t7; | ||
| 168 | +CREATE TABLE itest1 (a int generated by default as identity, b text); | ||
| 169 | +NOTICE: CREATE TABLE will create implicit sequence "itest1_a_seq" for serial column "itest1.a" | ||
| 170 | +CREATE TABLE itest2 (a bigint generated always as identity, b text); | ||
| 171 | +NOTICE: CREATE TABLE will create implicit sequence "itest2_a_seq" for serial column "itest2.a" | ||
| 172 | +CREATE TABLE itest3 (a smallint generated by default as identity (start with 7 increment by 5), b text); | ||
| 173 | +NOTICE: CREATE TABLE will create implicit sequence "itest3_a_seq" for serial column "itest3.a" | ||
| 174 | +ALTER TABLE itest3 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error | ||
| 175 | +NOTICE: ALTER TABLE will create implicit sequence "itest3_a_seq1" for serial column "itest3.a" | ||
| 176 | +ERROR: column "a" of relation "itest3" is already an identity column | ||
| 177 | +SELECT table_name, column_name, column_default, is_nullable, is_identity, identity_generation FROM information_schema.columns WHERE table_name LIKE 'itest_' ORDER BY 1, 2; | ||
| 178 | + table_name | column_name | column_default | is_nullable | is_identity | identity_generation | ||
| 179 | +------------+-------------+----------------+-------------+-------------+--------------------- | ||
| 180 | + itest1 | a | | NO | YES | BY DEFAULT | ||
| 181 | + itest1 | b | | YES | NO | | ||
| 182 | + itest2 | a | | NO | YES | ALWAYS | ||
| 183 | + itest2 | b | | YES | NO | | ||
| 184 | + itest3 | a | | NO | YES | BY DEFAULT | ||
| 185 | + itest3 | b | | YES | NO | | ||
| 186 | +(6 rows) | ||
| 187 | + | ||
| 188 | +-- internal sequences should not be shown here | ||
| 189 | +SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE 'itest%'; | ||
| 190 | + sequence_name | ||
| 191 | +--------------- | ||
| 192 | +(0 rows) | ||
| 193 | + | ||
| 194 | +-- invalid column type | ||
| 195 | +CREATE TABLE itest_err_1 (a text generated by default as identity); | ||
| 196 | +ERROR: Identity column 'a' data type invalid. | ||
| 197 | +-- duplicate identity | ||
| 198 | +CREATE TABLE itest_err_2 (a int generated always as identity generated by default as identity); | ||
| 199 | +NOTICE: CREATE TABLE will create implicit sequence "itest_err_2_a_seq" for serial column "itest_err_2.a" | ||
| 200 | +ERROR: multiple identity specifications for column "a" of table "itest_err_2" | ||
| 201 | +LINE 1: ...E itest_err_2 (a int generated always as identity generated ... | ||
| 202 | + ^ | ||
| 203 | +-- cannot have default and identity | ||
| 204 | +CREATE TABLE itest_err_3 (a int default 5 generated by default as identity); | ||
| 205 | +ERROR: Defaults cannot be created on columns with an IDENTITY attribute.Table 'itest_err_3', column 'a' | ||
| 206 | +LINE 1: CREATE TABLE itest_err_3 (a int default 5 generated by defau... | ||
| 207 | + ^ | ||
| 208 | +-- cannot combine serial and identity | ||
| 209 | +CREATE TABLE itest_err_4 (a serial generated by default as identity); | ||
| 210 | +NOTICE: CREATE TABLE will create implicit sequence "itest_err_4_a_seq" for serial column "itest_err_4.a" | ||
| 211 | +ERROR: conflicting serial/identity declarations for column "a" of table "itest_err_4" | ||
| 212 | +LINE 1: CREATE TABLE itest_err_4 (a serial generated by default as i... | ||
| 213 | + ^ | ||
| 214 | +--## insert/update OVERRIDING tests | ||
| 215 | +INSERT INTO itest1 DEFAULT VALUES; | ||
| 216 | +INSERT INTO itest1 DEFAULT VALUES; | ||
| 217 | +INSERT INTO itest2 DEFAULT VALUES; | ||
| 218 | +INSERT INTO itest2 DEFAULT VALUES; | ||
| 219 | +INSERT INTO itest3 DEFAULT VALUES; | ||
| 220 | +INSERT INTO itest3 DEFAULT VALUES; | ||
| 221 | +SELECT * FROM itest1 order by a; | ||
| 222 | + a | b | ||
| 223 | +---+--- | ||
| 224 | + 1 | | ||
| 225 | + 2 | | ||
| 226 | +(2 rows) | ||
| 227 | + | ||
| 228 | +SELECT * FROM itest2 order by a; | ||
| 229 | + a | b | ||
| 230 | +---+--- | ||
| 231 | + 1 | | ||
| 232 | + 2 | | ||
| 233 | +(2 rows) | ||
| 234 | + | ||
| 235 | +SELECT * FROM itest3 order by a; | ||
| 236 | + a | b | ||
| 237 | +----+--- | ||
| 238 | + 7 | | ||
| 239 | + 12 | | ||
| 240 | +(2 rows) | ||
| 241 | + | ||
| 242 | +INSERT INTO itest1 VALUES (10, 'xyz'); | ||
| 243 | +INSERT INTO itest1 OVERRIDING USER VALUE VALUES (10, 'xyz'); | ||
| 244 | +SELECT * FROM itest1 order by a; | ||
| 245 | + a | b | ||
| 246 | +----+----- | ||
| 247 | + 1 | | ||
| 248 | + 2 | | ||
| 249 | + 3 | xyz | ||
| 250 | + 10 | xyz | ||
| 251 | +(4 rows) | ||
| 252 | + | ||
| 253 | +INSERT INTO itest2 VALUES (10, 'xyz'); -- error | ||
| 254 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 255 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 256 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 257 | +INSERT INTO itest2 OVERRIDING SYSTEM VALUE VALUES (10, 'xyz'); | ||
| 258 | +SELECT * FROM itest2; | ||
| 259 | + a | b | ||
| 260 | +----+----- | ||
| 261 | + 1 | | ||
| 262 | + 2 | | ||
| 263 | + 10 | xyz | ||
| 264 | +(3 rows) | ||
| 265 | + | ||
| 266 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 267 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 268 | +INSERT INTO test_t1 VALUES('xxxx'); -- error | ||
| 269 | +ERROR: invalid input syntax for integer: "xxxx" | ||
| 270 | +LINE 1: INSERT INTO test_t1 VALUES('xxxx'); | ||
| 271 | + ^ | ||
| 272 | +CONTEXT: referenced column: a | ||
| 273 | +INSERT INTO test_t1(b) VALUES('xxxx'); -- success | ||
| 274 | +INSERT INTO test_t1(a, b) VALUES(1, 'xxxx'); -- error | ||
| 275 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 276 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 277 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 278 | +INSERT INTO test_t1 VALUES(1, 'xxxx'); -- error | ||
| 279 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 280 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 281 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 282 | +INSERT INTO test_t1 VALUES(DEFAULT, 'xxxx'); -- success | ||
| 283 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1111, 'xx'); -- success | ||
| 284 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'xxxx'); -- success | ||
| 285 | +INSERT INTO test_t1(a, b) OVERRIDING USER VALUE VALUES(DEFAULT, 'xxxx'); -- success | ||
| 286 | +INSERT INTO test_t1(a, b) OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- success | ||
| 287 | +INSERT INTO test_t1(a, b) select 1, 'xxxxx'; -- error | ||
| 288 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 289 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 290 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 291 | +INSERT INTO test_t1(a, b) OVERRIDING USER VALUE select 1, 'xxxxx'; -- success | ||
| 292 | +INSERT INTO test_t1(a, b) OVERRIDING SYSTEM VALUE select 12222, 'xxxxx'; -- success | ||
| 293 | +SELECT * FROM test_t1 ORDER BY a; | ||
| 294 | + a | b | ||
| 295 | +-------+------- | ||
| 296 | + 1 | xxxx | ||
| 297 | + 1 | xxxx | ||
| 298 | + 2 | xxxx | ||
| 299 | + 3 | xxxx | ||
| 300 | + 4 | xxxx | ||
| 301 | + 5 | xxxxx | ||
| 302 | + 1111 | xx | ||
| 303 | + 12222 | xxxxx | ||
| 304 | +(8 rows) | ||
| 305 | + | ||
| 306 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY, b text); | ||
| 307 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 308 | +INSERT INTO test_t2 VALUES('xxxx'); -- error | ||
| 309 | +ERROR: invalid input syntax for integer: "xxxx" | ||
| 310 | +LINE 1: INSERT INTO test_t2 VALUES('xxxx'); | ||
| 311 | + ^ | ||
| 312 | +CONTEXT: referenced column: a | ||
| 313 | +INSERT INTO test_t2(b) VALUES('xxxx'); -- success | ||
| 314 | +INSERT INTO test_t2(a, b) VALUES(1, 'xxxx'); -- success | ||
| 315 | +INSERT INTO test_t2 VALUES(1, 'xxxx'); -- success | ||
| 316 | +INSERT INTO test_t2 VALUES(DEFAULT, 'xxxx'); -- success | ||
| 317 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1111, 'xx'); -- success | ||
| 318 | +INSERT INTO test_t2 OVERRIDING USER VALUE VALUES(1111, 'xxxx'); -- success | ||
| 319 | +INSERT INTO test_t2(a, b) OVERRIDING USER VALUE VALUES(DEFAULT, 'xxxx'); -- success | ||
| 320 | +INSERT INTO test_t2(a, b) OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- success | ||
| 321 | +INSERT INTO test_t2(a, b) select 1, 'xxxxx'; -- success | ||
| 322 | +INSERT INTO test_t2(a, b) OVERRIDING USER VALUE select 1, 'xxxxx'; -- success | ||
| 323 | +INSERT INTO test_t2(a, b) OVERRIDING SYSTEM VALUE select 12222, 'xxxxx'; -- success | ||
| 324 | +SELECT * FROM test_t2 ORDER BY a; | ||
| 325 | + a | b | ||
| 326 | +-------+------- | ||
| 327 | + 1 | xxxx | ||
| 328 | + 1 | xxxx | ||
| 329 | + 1 | xxxx | ||
| 330 | + 1 | xxxxx | ||
| 331 | + 1 | xxxx | ||
| 332 | + 2 | xxxx | ||
| 333 | + 3 | xxxx | ||
| 334 | + 4 | xxxx | ||
| 335 | + 5 | xxxxx | ||
| 336 | + 1111 | xx | ||
| 337 | + 12222 | xxxxx | ||
| 338 | +(11 rows) | ||
| 339 | + | ||
| 340 | +DROP TABLE test_t1, test_t2; | ||
| 341 | +--## inline view tests | ||
| 342 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text, c int); | ||
| 343 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 344 | +CREATE TABLE test_t2 (a int GENERATED ALWAYS AS IDENTITY, b text, c int); | ||
| 345 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 346 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 347 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 348 | +-- insert | ||
| 349 | +INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 350 | + SELECT 2, 'inline view'; -- error | ||
| 351 | +ERROR: syntax error at or near "(" | ||
| 352 | +LINE 1: INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 353 | + ^ | ||
| 354 | +INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 355 | + OVERRIDING SYSTEM VALUE SELECT 22, 'inline view'; -- success | ||
| 356 | +ERROR: syntax error at or near "(" | ||
| 357 | +LINE 1: INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 358 | + ^ | ||
| 359 | +INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 360 | + OVERRIDING USER VALUE SELECT 12, 'inline view'; -- success | ||
| 361 | +ERROR: syntax error at or near "(" | ||
| 362 | +LINE 1: INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 363 | + ^ | ||
| 364 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 365 | + a | b | c | ||
| 366 | +---+---+--- | ||
| 367 | + 1 | | | ||
| 368 | +(1 row) | ||
| 369 | + | ||
| 370 | +-- update | ||
| 371 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup -- SET TO DEFAULT is not supported for subquery "sup" | ||
| 372 | + SET sup.a = 2; -- error | ||
| 373 | +ERROR: UPDATE/DELETE/INSERT through subquery is only supported in A-format database | ||
| 374 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 375 | + SET sup.a = DEFAULT; -- error | ||
| 376 | +ERROR: UPDATE/DELETE/INSERT through subquery is only supported in A-format database | ||
| 377 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 12) sup | ||
| 378 | + SET sup.a = DEFAULT, b = 'inline view updated'; -- error | ||
| 379 | +ERROR: UPDATE/DELETE/INSERT through subquery is only supported in A-format database | ||
| 380 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 381 | + a | b | c | ||
| 382 | +---+---+--- | ||
| 383 | + 1 | | | ||
| 384 | +(1 row) | ||
| 385 | + | ||
| 386 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 387 | + SET sup.a = DEFAULT; -- error | ||
| 388 | +ERROR: UPDATE/DELETE/INSERT through subquery is only supported in A-format database | ||
| 389 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 390 | + SET sup.b = 'inline view updated'; -- success | ||
| 391 | +ERROR: UPDATE/DELETE/INSERT through subquery is only supported in A-format database | ||
| 392 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 393 | + a | b | c | ||
| 394 | +---+---+--- | ||
| 395 | + 1 | | | ||
| 396 | +(1 row) | ||
| 397 | + | ||
| 398 | +DROP TABLE test_t1, test_t2; | ||
| 399 | +-- --## multi insert tests | ||
| 400 | +-- CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 401 | +-- CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY, b text); | ||
| 402 | +-- -- error | ||
| 403 | +-- INSERT ALL | ||
| 404 | +-- INTO test_t1 (a, b) VALUES (val1, val2) | ||
| 405 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 406 | +-- SELECT 1 as val1, 'x1' as val2; | ||
| 407 | +-- -- success | ||
| 408 | +-- INSERT ALL | ||
| 409 | +-- INTO test_t1 (a, b) OVERRIDING USER VALUE VALUES (val1, val2) | ||
| 410 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 411 | +-- SELECT 2 as val1, 'x1' as val2; | ||
| 412 | +-- -- success | ||
| 413 | +-- INSERT ALL | ||
| 414 | +-- INTO test_t1 (a, b) OVERRIDING SYSTEM VALUE VALUES (val1, val2) | ||
| 415 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 416 | +-- SELECT 3 as val1, 'x2' as val2; | ||
| 417 | +-- -- success | ||
| 418 | +-- INSERT ALL | ||
| 419 | +-- INTO test_t1 (a, b) OVERRIDING SYSTEM VALUE VALUES (DEFAULT, val2) | ||
| 420 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 421 | +-- SELECT 4 as val1, 'x3' as val2; | ||
| 422 | +-- -- success | ||
| 423 | +-- INSERT ALL | ||
| 424 | +-- INTO test_t1 (a, b) VALUES (DEFAULT, val2) | ||
| 425 | +-- INTO test_t2 (a, b) VALUES (DEFAULT, val2) | ||
| 426 | +-- SELECT 5 as val1, 'x4' as val2; | ||
| 427 | +-- -- success | ||
| 428 | +-- INSERT ALL | ||
| 429 | +-- INTO test_t1 (a, b) VALUES (DEFAULT, val2) | ||
| 430 | +-- INTO test_t2 (a, b) OVERRIDING USER VALUE VALUES (999, val2) | ||
| 431 | +-- SELECT 6 as val1, 'x5' as val2; | ||
| 432 | +-- -- success | ||
| 433 | +-- INSERT ALL | ||
| 434 | +-- INTO test_t1 (b) VALUES (DEFAULT, val2) | ||
| 435 | +-- INTO test_t2 (b) VALUES (DEFAULT, val2) | ||
| 436 | +-- SELECT 7 as val1, 'x6' as val2; | ||
| 437 | +-- SELECT * FROM test_t1 ORDER BY b; | ||
| 438 | +-- SELECT * FROM test_t2 ORDER BY b; | ||
| 439 | +-- DROP TABLE test_t1, test_t2; | ||
| 440 | +--## upserts tests | ||
| 441 | +CREATE TABLE test_t1 (a int PRIMARY KEY GENERATED ALWAYS AS IDENTITY , b text); | ||
| 442 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 443 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t1_pkey" for table "test_t1" | ||
| 444 | +INSERT INTO test_t1 VALUES (DEFAULT, default); | ||
| 445 | +SELECT * FROM test_t1; | ||
| 446 | + a | b | ||
| 447 | +---+--- | ||
| 448 | + 1 | | ||
| 449 | +(1 row) | ||
| 450 | + | ||
| 451 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- error | ||
| 452 | +ERROR: duplicate key value violates unique constraint "test_t1_pkey" | ||
| 453 | +DETAIL: Key (a)=(1) already exists. | ||
| 454 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'setb') ON DUPLICATE KEY UPDATE b = 'setb'; -- success | ||
| 455 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'nothing') ON DUPLICATE KEY UPDATE NOTHING; -- success | ||
| 456 | +SELECT * FROM test_t1 order by 1, 2; -- 1, setb | ||
| 457 | + a | b | ||
| 458 | +---+------ | ||
| 459 | + 1 | setb | ||
| 460 | +(1 row) | ||
| 461 | + | ||
| 462 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'nothing') ON DUPLICATE KEY UPDATE a = 2; -- error | ||
| 463 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 464 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'seta') ON DUPLICATE KEY UPDATE b = 'seta'; -- success | ||
| 465 | +SELECT * FROM test_t1 order by 1, 2; -- 1, seta | ||
| 466 | + a | b | ||
| 467 | +---+------ | ||
| 468 | + 1 | seta | ||
| 469 | +(1 row) | ||
| 470 | + | ||
| 471 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'seta') ON DUPLICATE KEY UPDATE b = 'setc', a = DEFAULT; -- error | ||
| 472 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 473 | +SELECT * FROM test_t1 order by 1, 2; -- 1, seta | ||
| 474 | + a | b | ||
| 475 | +---+------ | ||
| 476 | + 1 | seta | ||
| 477 | +(1 row) | ||
| 478 | + | ||
| 479 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(2, 'test1'); -- success | ||
| 480 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1'); -- error | ||
| 481 | +ERROR: duplicate key value violates unique constraint "test_t1_pkey" | ||
| 482 | +DETAIL: Key (a)=(2) already exists. | ||
| 483 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(3, 'test1'); -- success | ||
| 484 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE b = 'w3' WHERE a = 3; -- success | ||
| 485 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(4, 'test1'); -- success | ||
| 486 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE a = 5 WHERE a = 4; -- error, 未自增 | ||
| 487 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 488 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 489 | + a | b | ||
| 490 | +---+------- | ||
| 491 | + 1 | seta | ||
| 492 | + 2 | test1 | ||
| 493 | + 3 | w3 | ||
| 494 | + 4 | test1 | ||
| 495 | +(4 rows) | ||
| 496 | + | ||
| 497 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(5, 'test1'); -- success | ||
| 498 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE b = 'w5', a = DEFAULT WHERE a = 5; -- error, 未自增 | ||
| 499 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 500 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(6, 'test1'); -- success | ||
| 501 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE b = 'w6' WHERE a = 4; -- success | ||
| 502 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 503 | + a | b | ||
| 504 | +---+------- | ||
| 505 | + 1 | seta | ||
| 506 | + 2 | test1 | ||
| 507 | + 3 | w3 | ||
| 508 | + 4 | w6 | ||
| 509 | + 5 | test1 | ||
| 510 | + 6 | test1 | ||
| 511 | +(6 rows) | ||
| 512 | + | ||
| 513 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, b text); | ||
| 514 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 515 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t2_pkey" for table "test_t2" | ||
| 516 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 517 | +SELECT * FROM test_t2; | ||
| 518 | + a | b | ||
| 519 | +---+--- | ||
| 520 | + 1 | | ||
| 521 | +(1 row) | ||
| 522 | + | ||
| 523 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- error | ||
| 524 | +ERROR: duplicate key value violates unique constraint "test_t2_pkey" | ||
| 525 | +DETAIL: Key (a)=(1) already exists. | ||
| 526 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'setb') ON DUPLICATE KEY UPDATE b = 'setb'; -- success | ||
| 527 | +SELECT * FROM test_t2 order by 1, 2; | ||
| 528 | + a | b | ||
| 529 | +---+------ | ||
| 530 | + 1 | setb | ||
| 531 | +(1 row) | ||
| 532 | + | ||
| 533 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'setc') ON DUPLICATE KEY UPDATE a = 2; -- error | ||
| 534 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 535 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'setc') ON DUPLICATE KEY UPDATE a = DEFAULT, b = 'setc'; -- error | ||
| 536 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 537 | +SELECT * FROM test_t2 order by 1, 2; -- 1, setb | ||
| 538 | + a | b | ||
| 539 | +---+------ | ||
| 540 | + 1 | setb | ||
| 541 | +(1 row) | ||
| 542 | + | ||
| 543 | +INSERT INTO test_t2 OVERRIDING USER VALUE VALUES(2, 'test1'); -- success | ||
| 544 | +INSERT INTO test_t2 OVERRIDING USER VALUE VALUES(2, 'test2') ON DUPLICATE KEY UPDATE b = 'test3'; -- success | ||
| 545 | +SELECT * FROM test_t2 order by 1, 2; -- | ||
| 546 | + a | b | ||
| 547 | +---+------- | ||
| 548 | + 1 | setb | ||
| 549 | + 2 | test1 | ||
| 550 | + 3 | test2 | ||
| 551 | +(3 rows) | ||
| 552 | + | ||
| 553 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'test2') ON DUPLICATE KEY UPDATE a = 3; -- error | ||
| 554 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 555 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'test2') ON DUPLICATE KEY UPDATE a = DEFAULT, b = 'test2'; -- error | ||
| 556 | +ERROR: INSERT ON DUPLICATE KEY UPDATE don't allow update on primary key or unique key. | ||
| 557 | +SELECT * FROM test_t2 order by 1, 2; | ||
| 558 | + a | b | ||
| 559 | +---+------- | ||
| 560 | + 1 | setb | ||
| 561 | + 2 | test1 | ||
| 562 | + 3 | test2 | ||
| 563 | +(3 rows) | ||
| 564 | + | ||
| 565 | +DROP TABLE test_t1, test_t2; | ||
| 566 | +--## returning tests | ||
| 567 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY(START WITH 10) PRIMARY KEY, b text); | ||
| 568 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 569 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t1_pkey" for table "test_t1" | ||
| 570 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY(START WITH 12) PRIMARY KEY, b text); | ||
| 571 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 572 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t2_pkey" for table "test_t2" | ||
| 573 | +INSERT INTO test_t1 DEFAULT VALUES RETURNING a; | ||
| 574 | + a | ||
| 575 | +---- | ||
| 576 | + 10 | ||
| 577 | +(1 row) | ||
| 578 | + | ||
| 579 | +INSERT INTO test_t2 DEFAULT VALUES RETURNING a; | ||
| 580 | + a | ||
| 581 | +---- | ||
| 582 | + 12 | ||
| 583 | +(1 row) | ||
| 584 | + | ||
| 585 | +DROP TABLE test_t1, test_t2; | ||
| 586 | +--## UPDATE tests | ||
| 587 | +UPDATE itest1 SET a = 101 WHERE a = 1; | ||
| 588 | +UPDATE itest1 SET a = DEFAULT WHERE a = 2; | ||
| 589 | +UPDATE itest1 SET b = 'test' where a=101; | ||
| 590 | +SELECT * FROM itest1 order by 1, 2; | ||
| 591 | + a | b | ||
| 592 | +-----+------ | ||
| 593 | + 3 | xyz | ||
| 594 | + 4 | | ||
| 595 | + 10 | xyz | ||
| 596 | + 101 | test | ||
| 597 | +(4 rows) | ||
| 598 | + | ||
| 599 | +UPDATE itest2 SET a = 101 WHERE a = 1; -- error | ||
| 600 | +ERROR: column "a" can only be updated to DEFAULT | ||
| 601 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 602 | +UPDATE itest2 SET a = DEFAULT WHERE a = 2; | ||
| 603 | +UPDATE itest2 SET b = 'test' where a=101; | ||
| 604 | +SELECT * FROM itest2 order by 1, 2; | ||
| 605 | + a | b | ||
| 606 | +----+----- | ||
| 607 | + 1 | | ||
| 608 | + 3 | | ||
| 609 | + 10 | xyz | ||
| 610 | +(3 rows) | ||
| 611 | + | ||
| 612 | +--## merge tests | ||
| 613 | +-- when not matched insert | ||
| 614 | +create table target_table ( | ||
| 615 | + id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, | ||
| 616 | + name varchar(50), | ||
| 617 | + age int | ||
| 618 | +); | ||
| 619 | +NOTICE: CREATE TABLE will create implicit sequence "target_table_id_seq" for serial column "target_table.id" | ||
| 620 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "target_table_pkey" for table "target_table" | ||
| 621 | +create table source_table (id int, name varchar(50), age int); | ||
| 622 | +insert into source_table values (10, 'zliu', 28), (11, 'sqi', 30); | ||
| 623 | +-- expected: success. let id generated by system. | ||
| 624 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 625 | +when not matched then | ||
| 626 | + insert (name, age) values (s.name, s.age); | ||
| 627 | +-- expected: error | ||
| 628 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 629 | +when not matched then | ||
| 630 | + insert (id, name, age) values (1, 'error', 21); | ||
| 631 | +ERROR: cannot insert a non-DEFAULT value into column "id" | ||
| 632 | +DETAIL: Column "id" is an identity column defined as "GENERATED ALWAYS". | ||
| 633 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert" | ||
| 634 | +-- expected: success | ||
| 635 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 636 | +when not matched then | ||
| 637 | + insert (id, name, age) OVERRIDING USER VALUE values (1, 'success', 22); -- 3 | ||
| 638 | +-- expected: error | ||
| 639 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 640 | +when not matched then | ||
| 641 | + insert (id, name, age) OVERRIDING SYSTEM VALUE values (2, 'error', 23); | ||
| 642 | +ERROR: duplicate key value violates unique constraint "target_table_pkey" | ||
| 643 | +DETAIL: Key (id)=(2) already exists. | ||
| 644 | +-- expected: success | ||
| 645 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 646 | +when not matched then | ||
| 647 | + insert (id, name, age) OVERRIDING SYSTEM VALUE values (3, 'success', 24); | ||
| 648 | +ERROR: duplicate key value violates unique constraint "target_table_pkey" | ||
| 649 | +DETAIL: Key (id)=(3) already exists. | ||
| 650 | +-- expected: success | ||
| 651 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 652 | +when not matched then | ||
| 653 | + insert (id, name, age) OVERRIDING USER VALUE values (3, 'success', 25); | ||
| 654 | +-- expected: success | ||
| 655 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 656 | +when not matched then | ||
| 657 | + insert (id, name, age) OVERRIDING SYSTEM VALUE values (DEFAULT, 'success', 26); | ||
| 658 | +-- expected: success | ||
| 659 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 660 | +when not matched then | ||
| 661 | + insert (id, name, age) OVERRIDING USER VALUE values (DEFAULT, 'success', 27); | ||
| 662 | +SELECT * from target_table order by id; | ||
| 663 | + id | name | age | ||
| 664 | +----+---------+----- | ||
| 665 | + 1 | zliu | 28 | ||
| 666 | + 2 | sqi | 30 | ||
| 667 | + 3 | success | 22 | ||
| 668 | + 4 | success | 22 | ||
| 669 | + 5 | success | 25 | ||
| 670 | + 6 | success | 25 | ||
| 671 | + 7 | success | 26 | ||
| 672 | + 8 | success | 26 | ||
| 673 | + 9 | success | 27 | ||
| 674 | + 10 | success | 27 | ||
| 675 | +(10 rows) | ||
| 676 | + | ||
| 677 | +DROP TABLE target_table, source_table; | ||
| 678 | +-- when matched update | ||
| 679 | +create table target_table ( | ||
| 680 | + id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, | ||
| 681 | + name varchar(50), | ||
| 682 | + age int | ||
| 683 | +); | ||
| 684 | +NOTICE: CREATE TABLE will create implicit sequence "target_table_id_seq" for serial column "target_table.id" | ||
| 685 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "target_table_pkey" for table "target_table" | ||
| 686 | +create table source_table (id int, name varchar(50), age int); | ||
| 687 | +insert into source_table values (1, 'zliu', 28), (2, 'sqi', 30); | ||
| 688 | +-- success | ||
| 689 | +insert into target_table default values; | ||
| 690 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 691 | +WHEN MATCHED THEN | ||
| 692 | + UPDATE SET name = s.name, age = s.age + 1; | ||
| 693 | +-- error | ||
| 694 | +insert into target_table default values; | ||
| 695 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 696 | +WHEN MATCHED THEN | ||
| 697 | + UPDATE SET id = s.id + 1; | ||
| 698 | +ERROR: Columns referenced in the ON Clause cannot be updated: "target_table"."id" | ||
| 699 | +-- error | ||
| 700 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 701 | +WHEN MATCHED THEN | ||
| 702 | + UPDATE SET id = DEFAULT, name = s.name, age = s.age + 1; | ||
| 703 | +ERROR: Columns referenced in the ON Clause cannot be updated: "target_table"."id" | ||
| 704 | +SELECT * from target_table; | ||
| 705 | + id | name | age | ||
| 706 | +----+------+----- | ||
| 707 | + 1 | zliu | 29 | ||
| 708 | + 2 | | | ||
| 709 | +(2 rows) | ||
| 710 | + | ||
| 711 | +DROP TABLE target_table, source_table; | ||
| 712 | +-- CTE with modifying clause | ||
| 713 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY(START WITH 10) PRIMARY KEY, b text); | ||
| 714 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 715 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t1_pkey" for table "test_t1" | ||
| 716 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY(START WITH 12) PRIMARY KEY, b text); | ||
| 717 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 718 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t2_pkey" for table "test_t2" | ||
| 719 | +-- error | ||
| 720 | +WITH xxx AS ( | ||
| 721 | + INSERT INTO test_t1(a, b) VALUES(1, 'xxxx') RETURNING a | ||
| 722 | +) select * from xxx; | ||
| 723 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 724 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 725 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 726 | +-- success | ||
| 727 | +WITH xxx AS ( | ||
| 728 | + INSERT INTO test_t2(a, b) VALUES(1, 'x1') RETURNING a, b | ||
| 729 | +) select * from xxx; | ||
| 730 | + a | b | ||
| 731 | +---+---- | ||
| 732 | + 1 | x1 | ||
| 733 | +(1 row) | ||
| 734 | + | ||
| 735 | +-- success | ||
| 736 | +WITH xxx AS ( | ||
| 737 | + INSERT INTO test_t1(a, b) OVERRIDING SYSTEM VALUE VALUES(1, 'x1') RETURNING a, b | ||
| 738 | +) select * from xxx; | ||
| 739 | + a | b | ||
| 740 | +---+---- | ||
| 741 | + 1 | x1 | ||
| 742 | +(1 row) | ||
| 743 | + | ||
| 744 | +-- success | ||
| 745 | +WITH xxx AS ( | ||
| 746 | + INSERT INTO test_t1(a, b) OVERRIDING USER VALUE VALUES(3, 'x2') RETURNING a, b | ||
| 747 | +) select * from xxx; -- should be 2 | ||
| 748 | + a | b | ||
| 749 | +----+---- | ||
| 750 | + 10 | x2 | ||
| 751 | +(1 row) | ||
| 752 | + | ||
| 753 | +-- error | ||
| 754 | +WITH xxx AS ( | ||
| 755 | + UPDATE test_t1 SET a = 3 RETURNING a, b | ||
| 756 | +) select * from xxx; | ||
| 757 | +ERROR: column "a" can only be updated to DEFAULT | ||
| 758 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 759 | +-- error | ||
| 760 | +WITH xxx AS ( | ||
| 761 | + UPDATE test_t2 SET a = 3 RETURNING a, b | ||
| 762 | +) select * from xxx; | ||
| 763 | + a | b | ||
| 764 | +---+---- | ||
| 765 | + 3 | x1 | ||
| 766 | +(1 row) | ||
| 767 | + | ||
| 768 | +-- success | ||
| 769 | +WITH xxx AS ( | ||
| 770 | + UPDATE test_t1 SET a = DEFAULT, b = 't1_updated' RETURNING a, b | ||
| 771 | +) select * from xxx; | ||
| 772 | + a | b | ||
| 773 | +----+------------ | ||
| 774 | + 11 | t1_updated | ||
| 775 | + 12 | t1_updated | ||
| 776 | +(2 rows) | ||
| 777 | + | ||
| 778 | +-- success | ||
| 779 | +WITH xxx AS ( | ||
| 780 | + UPDATE test_t2 SET a = DEFAULT, b = 't2_updated' RETURNING a, b | ||
| 781 | +) select * from xxx; | ||
| 782 | + a | b | ||
| 783 | +----+------------ | ||
| 784 | + 12 | t2_updated | ||
| 785 | +(1 row) | ||
| 786 | + | ||
| 787 | +DROP TABLE test_t1, test_t2; | ||
| 788 | +--## alter table | ||
| 789 | +CREATE TABLE itest4 (a int, b text); | ||
| 790 | +ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL | ||
| 791 | +NOTICE: ALTER TABLE will create implicit sequence "itest4_a_seq" for serial column "itest4.a" | ||
| 792 | +ERROR: column "a" of relation "itest4" must be declared NOT NULL before identity can be added | ||
| 793 | +ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL; | ||
| 794 | +ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- ok | ||
| 795 | +NOTICE: ALTER TABLE will create implicit sequence "itest4_a_seq" for serial column "itest4.a" | ||
| 796 | +\d+ itest4 | ||
| 797 | + Table "public.itest4" | ||
| 798 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 799 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 800 | + a | integer | not null generated always as identity | plain | | | ||
| 801 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 802 | +Has OIDs: no | ||
| 803 | +Options: orientation=row, compression=no, collate=1537 | ||
| 804 | +Character Set: UTF8 | ||
| 805 | +Collate: utf8mb4_general_ci | ||
| 806 | + | ||
| 807 | +ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL; -- error, disallowed | ||
| 808 | +ERROR: column "a" of relation "itest4" is an identity column | ||
| 809 | +ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, already set | ||
| 810 | +NOTICE: ALTER TABLE will create implicit sequence "itest4_a_seq1" for serial column "itest4.a" | ||
| 811 | +ERROR: column "a" of relation "itest4" is already an identity column | ||
| 812 | +ALTER TABLE itest4 ALTER COLUMN b ADD GENERATED ALWAYS AS IDENTITY; -- error, wrong data type | ||
| 813 | +ERROR: Identity column 'b' data type invalid. | ||
| 814 | +INSERT INTO itest4 DEFAULT VALUES; | ||
| 815 | +INSERT INTO itest4 DEFAULT VALUES; | ||
| 816 | +SELECT * FROM itest4; | ||
| 817 | + a | b | ||
| 818 | +---+--- | ||
| 819 | + 1 | | ||
| 820 | + 2 | | ||
| 821 | +(2 rows) | ||
| 822 | + | ||
| 823 | +-- alter add column | ||
| 824 | +CREATE TABLE itest4_1 (a int, b text); | ||
| 825 | +ALTER TABLE itest4_1 ADD COLUMN c text GENERATED ALWAYS AS IDENTITY; -- error | ||
| 826 | +ERROR: Identity column 'c' data type invalid. | ||
| 827 | +ALTER TABLE itest4_1 ADD COLUMN c int GENERATED ALWAYS AS IDENTITY; -- success | ||
| 828 | +NOTICE: ALTER TABLE will create implicit sequence "itest4_1_c_seq" for serial column "itest4_1.c" | ||
| 829 | +\d+ itest4_1 | ||
| 830 | + Table "public.itest4_1" | ||
| 831 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 832 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 833 | + a | integer | | plain | | | ||
| 834 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 835 | + c | integer | not null generated always as identity | plain | | | ||
| 836 | +Has OIDs: no | ||
| 837 | +Options: orientation=row, compression=no, collate=1537 | ||
| 838 | +Character Set: UTF8 | ||
| 839 | +Collate: utf8mb4_general_ci | ||
| 840 | + | ||
| 841 | +INSERT INTO itest4_1 DEFAULT VALUES; | ||
| 842 | +INSERT INTO itest4_1 (c, a, b) VALUES(1, 2, 'x1'); -- error | ||
| 843 | +ERROR: cannot insert a non-DEFAULT value into column "c" | ||
| 844 | +DETAIL: Column "c" is an identity column defined as "GENERATED ALWAYS". | ||
| 845 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 846 | +INSERT INTO itest4_1 (c, a, b) OVERRIDING SYSTEM VALUE VALUES(111, 2, 'x2'); -- success | ||
| 847 | +INSERT INTO itest4_1 (c, a, b) VALUES(DEFAULT, 2, 'x3'); -- success | ||
| 848 | +SELECT c, a, b FROM itest4_1 order by c, b, a; | ||
| 849 | + c | a | b | ||
| 850 | +-----+---+---- | ||
| 851 | + 1 | | | ||
| 852 | + 2 | 2 | x3 | ||
| 853 | + 111 | 2 | x2 | ||
| 854 | +(3 rows) | ||
| 855 | + | ||
| 856 | +CREATE TABLE itest4_2 (a int, b text); | ||
| 857 | +ALTER TABLE itest4_2 ADD COLUMN c text GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 858 | +ERROR: Identity column 'c' data type invalid. | ||
| 859 | +ALTER TABLE itest4_2 ADD COLUMN c int GENERATED BY DEFAULT AS IDENTITY; -- success | ||
| 860 | +NOTICE: ALTER TABLE will create implicit sequence "itest4_2_c_seq" for serial column "itest4_2.c" | ||
| 861 | +ALTER TABLE itest4_2 ALTER COLUMN a ADD GENERATED BY DEFAULT AS IDENTITY; | ||
| 862 | +NOTICE: ALTER TABLE will create implicit sequence "itest4_2_a_seq" for serial column "itest4_2.a" | ||
| 863 | +ERROR: column "a" of relation "itest4_2" must be declared NOT NULL before identity can be added | ||
| 864 | +\d+ itest4_2 | ||
| 865 | + Table "public.itest4_2" | ||
| 866 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 867 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 868 | + a | integer | | plain | | | ||
| 869 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 870 | + c | integer | not null generated by default as identity | plain | | | ||
| 871 | +Has OIDs: no | ||
| 872 | +Options: orientation=row, compression=no, collate=1537 | ||
| 873 | +Character Set: UTF8 | ||
| 874 | +Collate: utf8mb4_general_ci | ||
| 875 | + | ||
| 876 | +INSERT INTO itest4_2 DEFAULT VALUES; | ||
| 877 | +INSERT INTO itest4_2 (c, a, b) VALUES(1, 2, 'x1'); -- success | ||
| 878 | +INSERT INTO itest4_2 (c, a, b) OVERRIDING SYSTEM VALUE VALUES(111, 2, 'x2'); -- success | ||
| 879 | +INSERT INTO itest4_2 (c, a, b) VALUES(DEFAULT, 2, 'x3'); -- success | ||
| 880 | +SELECT c, a, b FROM itest4_2 order by c, b, a; | ||
| 881 | + c | a | b | ||
| 882 | +-----+---+---- | ||
| 883 | + 1 | 2 | x1 | ||
| 884 | + 1 | | | ||
| 885 | + 2 | 2 | x3 | ||
| 886 | + 111 | 2 | x2 | ||
| 887 | +(4 rows) | ||
| 888 | + | ||
| 889 | +DROP TABLE itest4_1, itest4_2; | ||
| 890 | +-- for later | ||
| 891 | +ALTER TABLE itest4 ALTER COLUMN b SET DEFAULT ''; | ||
| 892 | +-- DROP IDENTITY tests | ||
| 893 | +ALTER TABLE itest4 ALTER COLUMN b DROP IDENTITY; -- error | ||
| 894 | +ERROR: column "b" of relation "itest4" is not an identity column | ||
| 895 | +ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY; | ||
| 896 | +INSERT INTO itest4 VALUES (NULL, 'nullable'); | ||
| 897 | +ERROR: null value in column "a" violates not-null constraint | ||
| 898 | +DETAIL: Failing row contains (null, nullable). | ||
| 899 | +ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY IF EXISTS; -- noop | ||
| 900 | +NOTICE: column "a" of relation "itest4" is not an identity column, skipping | ||
| 901 | +INSERT INTO itest4 DEFAULT VALUES; -- fails because NOT NULL is not dropped | ||
| 902 | +ERROR: null value in column "a" violates not-null constraint | ||
| 903 | +DETAIL: Failing row contains (null, ). | ||
| 904 | +ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL; | ||
| 905 | +INSERT INTO itest4 DEFAULT VALUES; | ||
| 906 | +SELECT * FROM itest4; | ||
| 907 | + a | b | ||
| 908 | +---+--- | ||
| 909 | + 1 | | ||
| 910 | + 2 | | ||
| 911 | + | | ||
| 912 | +(3 rows) | ||
| 913 | + | ||
| 914 | +-- check that sequence is removed | ||
| 915 | +SELECT sequence_name FROM itest4_a_seq; | ||
| 916 | +ERROR: relation "itest4_a_seq" does not exist on datanode1 | ||
| 917 | +LINE 1: SELECT sequence_name FROM itest4_a_seq; | ||
| 918 | + ^ | ||
| 919 | +-- fail, not allowed for identity columns | ||
| 920 | +ALTER TABLE itest1 ALTER COLUMN a SET DEFAULT 1; | ||
| 921 | +ERROR: column "a" of relation "itest1" is an identity column | ||
| 922 | +ALTER TABLE itest1 ALTER COLUMN a DROP DEFAULT; | ||
| 923 | +ERROR: column "a" of relation "itest1" is an identity column | ||
| 924 | +HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP IDENTITY instead. | ||
| 925 | +-- fail, not allowed, already has a default | ||
| 926 | +CREATE TABLE itest5 (a serial, b text); | ||
| 927 | +NOTICE: CREATE TABLE will create implicit sequence "itest5_a_seq" for serial column "itest5.a" | ||
| 928 | +ALTER TABLE itest5 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; | ||
| 929 | +NOTICE: ALTER TABLE will create implicit sequence "itest5_a_seq1" for serial column "itest5.a" | ||
| 930 | +ERROR: column "a" of relation "itest5" already has a default value | ||
| 931 | +--success | ||
| 932 | +ALTER TABLE itest5 DROP COLUMN a; | ||
| 933 | +SELECT sequence_name FROM itest5_a_seq; | ||
| 934 | +ERROR: relation "itest5_a_seq" does not exist on datanode1 | ||
| 935 | +LINE 1: SELECT sequence_name FROM itest5_a_seq; | ||
| 936 | + ^ | ||
| 937 | +DROP TABLE itest5; | ||
| 938 | +-- not supported | ||
| 939 | +\d+ itest3 | ||
| 940 | + Table "public.itest3" | ||
| 941 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 942 | +--------+----------+-----------------------------------------------+----------+--------------+------------- | ||
| 943 | + a | smallint | not null generated by default as identity | plain | | | ||
| 944 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 945 | +Has OIDs: no | ||
| 946 | +Options: orientation=row, compression=no, collate=1537 | ||
| 947 | +Character Set: UTF8 | ||
| 948 | +Collate: utf8mb4_general_ci | ||
| 949 | + | ||
| 950 | +ALTER TABLE itest3 ALTER COLUMN a TYPE int; | ||
| 951 | +ERROR: Not supported alter identity column a type | ||
| 952 | +ALTER TABLE itest3 ALTER COLUMN a TYPE text; | ||
| 953 | +ERROR: Not supported alter identity column a type | ||
| 954 | +-- ALTER COLUMN ... SET | ||
| 955 | +CREATE TABLE itest6 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 956 | +NOTICE: CREATE TABLE will create implicit sequence "itest6_a_seq" for serial column "itest6.a" | ||
| 957 | +\d+ itest6 | ||
| 958 | + Table "public.itest6" | ||
| 959 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 960 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 961 | + a | integer | not null generated always as identity | plain | | | ||
| 962 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 963 | +Has OIDs: no | ||
| 964 | +Options: orientation=row, compression=no, collate=1537 | ||
| 965 | +Character Set: UTF8 | ||
| 966 | +Collate: utf8mb4_general_ci | ||
| 967 | + | ||
| 968 | +\d+ itest6_a_seq | ||
| 969 | + Sequence "public.itest6_a_seq" | ||
| 970 | + Column | Type | Value | Storage | ||
| 971 | +---------------+---------+---------------------+--------- | ||
| 972 | + sequence_name | name | itest6_a_seq | plain | ||
| 973 | + last_value | bigint | 1 | plain | ||
| 974 | + start_value | bigint | 1 | plain | ||
| 975 | + increment_by | bigint | 1 | plain | ||
| 976 | + max_value | bigint | 9223372036854775807 | plain | ||
| 977 | + min_value | bigint | 1 | plain | ||
| 978 | + cache_value | bigint | 1 | plain | ||
| 979 | + log_cnt | bigint | 0 | plain | ||
| 980 | + is_cycled | boolean | f | plain | ||
| 981 | + is_called | boolean | f | plain | ||
| 982 | + uuid | bigint | 0 | plain | ||
| 983 | + is_global | boolean | f | plain | ||
| 984 | +Sequence for identity column: public.itest6.a | ||
| 985 | + | ||
| 986 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 987 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 988 | +SELECT * FROM itest6; | ||
| 989 | + a | b | ||
| 990 | +---+--- | ||
| 991 | + 1 | | ||
| 992 | + 2 | | ||
| 993 | +(2 rows) | ||
| 994 | + | ||
| 995 | +ALTER TABLE itest6 ALTER COLUMN a SET GENERATED BY DEFAULT SET INCREMENT BY 2 SET START WITH 100 RESTART; | ||
| 996 | +\d+ itest6 | ||
| 997 | + Table "public.itest6" | ||
| 998 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 999 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 1000 | + a | integer | not null generated by default as identity | plain | | | ||
| 1001 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 1002 | +Has OIDs: no | ||
| 1003 | +Options: orientation=row, compression=no, collate=1537 | ||
| 1004 | +Character Set: UTF8 | ||
| 1005 | +Collate: utf8mb4_general_ci | ||
| 1006 | + | ||
| 1007 | +\d+ itest6_a_seq | ||
| 1008 | + Sequence "public.itest6_a_seq" | ||
| 1009 | + Column | Type | Value | Storage | ||
| 1010 | +---------------+---------+---------------------+--------- | ||
| 1011 | + sequence_name | name | itest6_a_seq | plain | ||
| 1012 | + last_value | bigint | 100 | plain | ||
| 1013 | + start_value | bigint | 100 | plain | ||
| 1014 | + increment_by | bigint | 2 | plain | ||
| 1015 | + max_value | bigint | 9223372036854775807 | plain | ||
| 1016 | + min_value | bigint | 1 | plain | ||
| 1017 | + cache_value | bigint | 1 | plain | ||
| 1018 | + log_cnt | bigint | 0 | plain | ||
| 1019 | + is_cycled | boolean | f | plain | ||
| 1020 | + is_called | boolean | f | plain | ||
| 1021 | + uuid | bigint | 0 | plain | ||
| 1022 | + is_global | boolean | f | plain | ||
| 1023 | +Sequence for identity column: public.itest6.a | ||
| 1024 | + | ||
| 1025 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 1026 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 1027 | +SELECT * FROM itest6; | ||
| 1028 | + a | b | ||
| 1029 | +-----+--- | ||
| 1030 | + 1 | | ||
| 1031 | + 2 | | ||
| 1032 | + 100 | | ||
| 1033 | + 102 | | ||
| 1034 | +(4 rows) | ||
| 1035 | + | ||
| 1036 | +SELECT table_name, column_name, is_identity, identity_generation FROM information_schema.columns WHERE table_name = 'itest6' order by 1,2,3,4; | ||
| 1037 | + table_name | column_name | is_identity | identity_generation | ||
| 1038 | +------------+-------------+-------------+--------------------- | ||
| 1039 | + itest6 | a | YES | BY DEFAULT | ||
| 1040 | + itest6 | b | NO | | ||
| 1041 | +(2 rows) | ||
| 1042 | + | ||
| 1043 | +ALTER TABLE itest6 ALTER COLUMN b SET INCREMENT BY 2; -- fail, not identity | ||
| 1044 | +ERROR: column "b" of relation "itest6" is not an identity column | ||
| 1045 | +-- --## multiple steps in ALTER TABLE | ||
| 1046 | +-- cuase transformAlterTableStmt will analysis AT_Column and AT_AddIdentity | ||
| 1047 | +-- at same time, when AT_AddIdentity, new column has not been created yet. | ||
| 1048 | +-- CREATE TABLE itest5 (a int, b text); | ||
| 1049 | +-- ALTER TABLE itest5 | ||
| 1050 | +-- ADD COLUMN f21 int NULL, | ||
| 1051 | +-- ALTER COLUMN f21 ADD GENERATED ALWAYS AS IDENTITY; | ||
| 1052 | +-- ALTER TABLE itest5 | ||
| 1053 | +-- ADD COLUMN f22 int NOT NULL, | ||
| 1054 | +-- ALTER COLUMN f22 ADD GENERATED ALWAYS AS IDENTITY; | ||
| 1055 | +--## sequence | ||
| 1056 | +-- prohibited direct modification of sequence | ||
| 1057 | +ALTER SEQUENCE itest6_a_seq OWNED BY NONE; | ||
| 1058 | +ERROR: cannot change ownership of identity sequence | ||
| 1059 | +DETAIL: Sequence "itest6_a_seq" is linked to table "itest6". | ||
| 1060 | +CREATE SEQUENCE sequence_testx OWNED BY pg_class_oid_index.oid; -- not a table | ||
| 1061 | +ERROR: referenced relation "pg_class_oid_index" is not a table | ||
| 1062 | +CREATE SEQUENCE my_int_seq; | ||
| 1063 | +-- not supported 'AS' option | ||
| 1064 | +CREATE SEQUENCE my_int_seq AS INTEGER; | ||
| 1065 | +ERROR: syntax error at or near "AS" | ||
| 1066 | +LINE 1: CREATE SEQUENCE my_int_seq AS INTEGER; | ||
| 1067 | + ^ | ||
| 1068 | +-- not supported 'SEQUENCE NAME' option | ||
| 1069 | +ALTER SEQUENCE my_int_seq SEQUENCE NAME my_int_seq_1; | ||
| 1070 | +ERROR: invalid sequence option SEQUENCE NAME | ||
| 1071 | +-- alter sequence set schame | ||
| 1072 | +create schame seq1; | ||
| 1073 | +ERROR: syntax error at or near "schame" | ||
| 1074 | +LINE 1: create schame seq1; | ||
| 1075 | + ^ | ||
| 1076 | +create sequence s1; | ||
| 1077 | +-- error | ||
| 1078 | +alter sequence s1 set schame seq1; | ||
| 1079 | +ERROR: syntax error at or near "schame" | ||
| 1080 | +LINE 1: alter sequence s1 set schame seq1; | ||
| 1081 | + ^ | ||
| 1082 | +alter table seq1 set schame seq1; | ||
| 1083 | +ERROR: syntax error at or near "schame" | ||
| 1084 | +LINE 1: alter table seq1 set schame seq1; | ||
| 1085 | + ^ | ||
| 1086 | +--## truncate | ||
| 1087 | +CREATE TABLE truncate_b (id int GENERATED ALWAYS AS IDENTITY (START WITH 44)); | ||
| 1088 | +NOTICE: CREATE TABLE will create implicit sequence "truncate_b_id_seq" for serial column "truncate_b.id" | ||
| 1089 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 1090 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 1091 | +SELECT * FROM truncate_b; | ||
| 1092 | + id | ||
| 1093 | +---- | ||
| 1094 | + 44 | ||
| 1095 | + 45 | ||
| 1096 | +(2 rows) | ||
| 1097 | + | ||
| 1098 | +TRUNCATE truncate_b; | ||
| 1099 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 1100 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 1101 | +SELECT * FROM truncate_b; | ||
| 1102 | + id | ||
| 1103 | +---- | ||
| 1104 | + 46 | ||
| 1105 | + 47 | ||
| 1106 | +(2 rows) | ||
| 1107 | + | ||
| 1108 | +-- not supported | ||
| 1109 | +TRUNCATE truncate_b RESTART IDENTITY; | ||
| 1110 | +ERROR: PGXC does not support RESTART IDENTITY yet | ||
| 1111 | +DETAIL: The feature is not supported currently | ||
| 1112 | +--## create table like | ||
| 1113 | +CREATE TABLE test_like_id_1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 1114 | +NOTICE: CREATE TABLE will create implicit sequence "test_like_id_1_a_seq" for serial column "test_like_id_1.a" | ||
| 1115 | +\d test_like_id_1 | ||
| 1116 | + Table "public.test_like_id_1" | ||
| 1117 | + Column | Type | Modifiers | ||
| 1118 | +--------+---------+----------------------------------------------- | ||
| 1119 | + a | integer | not null generated always as identity | ||
| 1120 | + b | text | character set UTF8 collate utf8mb4_general_ci | ||
| 1121 | + | ||
| 1122 | +INSERT INTO test_like_id_1 (b) VALUES ('b1'); | ||
| 1123 | +SELECT * FROM test_like_id_1; | ||
| 1124 | + a | b | ||
| 1125 | +---+---- | ||
| 1126 | + 1 | b1 | ||
| 1127 | +(1 row) | ||
| 1128 | + | ||
| 1129 | +CREATE TABLE test_like_id_2 (LIKE test_like_id_1); | ||
| 1130 | +\d test_like_id_2 | ||
| 1131 | + Table "public.test_like_id_2" | ||
| 1132 | + Column | Type | Modifiers | ||
| 1133 | +--------+---------+----------------------------------------------- | ||
| 1134 | + a | integer | not null | ||
| 1135 | + b | text | character set UTF8 collate utf8mb4_general_ci | ||
| 1136 | + | ||
| 1137 | +INSERT INTO test_like_id_2 (b) VALUES ('b2'); | ||
| 1138 | +ERROR: null value in column "a" violates not-null constraint | ||
| 1139 | +DETAIL: Failing row contains (null, b2). | ||
| 1140 | +SELECT * FROM test_like_id_2; -- identity was not copied | ||
| 1141 | + a | b | ||
| 1142 | +---+--- | ||
| 1143 | +(0 rows) | ||
| 1144 | + | ||
| 1145 | +CREATE TABLE test_like_id_3 (LIKE test_like_id_1 INCLUDING IDENTITY); | ||
| 1146 | +NOTICE: CREATE TABLE will create implicit sequence "test_like_id_3_a_seq" for serial column "test_like_id_3.a" | ||
| 1147 | +\d test_like_id_3 | ||
| 1148 | + Table "public.test_like_id_3" | ||
| 1149 | + Column | Type | Modifiers | ||
| 1150 | +--------+---------+----------------------------------------------- | ||
| 1151 | + a | integer | not null generated always as identity | ||
| 1152 | + b | text | character set UTF8 collate utf8mb4_general_ci | ||
| 1153 | + | ||
| 1154 | +INSERT INTO test_like_id_3 (b) VALUES ('b3'); | ||
| 1155 | +SELECT * FROM test_like_id_3; -- identity was copied and applied | ||
| 1156 | + a | b | ||
| 1157 | +---+---- | ||
| 1158 | + 1 | b3 | ||
| 1159 | +(1 row) | ||
| 1160 | + | ||
| 1161 | +CREATE TABLE test_like_id_4 (like test_like_id_1 INCLUDING DEFAULTS INCLUDING IDENTITY); | ||
| 1162 | +NOTICE: CREATE TABLE will create implicit sequence "test_like_id_4_a_seq" for serial column "test_like_id_4.a" | ||
| 1163 | +\d test_like_id_4 | ||
| 1164 | + Table "public.test_like_id_4" | ||
| 1165 | + Column | Type | Modifiers | ||
| 1166 | +--------+---------+----------------------------------------------- | ||
| 1167 | + a | integer | not null generated always as identity | ||
| 1168 | + b | text | character set UTF8 collate utf8mb4_general_ci | ||
| 1169 | + | ||
| 1170 | +INSERT INTO test_like_id_4 (b) VALUES ('b3'); | ||
| 1171 | +SELECT * FROM test_like_id_4; -- identity was copied and applied | ||
| 1172 | + a | b | ||
| 1173 | +---+---- | ||
| 1174 | + 1 | b3 | ||
| 1175 | +(1 row) | ||
| 1176 | + | ||
| 1177 | +-- not copy, because no identity column | ||
| 1178 | +CREATE TABLE par_row_t1 (a int, b text) partition by range (a) | ||
| 1179 | +( | ||
| 1180 | + partition par_row_t1_p0 values less than(10), | ||
| 1181 | + partition par_row_t1_p3 values less than (maxvalue) | ||
| 1182 | +); | ||
| 1183 | +CREATE TABLE test_lik3_v (LIKE par_row_t1 INCLUDING IDENTITY); | ||
| 1184 | +DROP TABLE test_like_id_2, test_like_id_3, test_like_id_4, par_row_t1, test_lik3_v; | ||
| 1185 | +--## select into | ||
| 1186 | +-- not copy identity | ||
| 1187 | +SELECT a, b into test_like_id_5 from test_like_id_1; | ||
| 1188 | +\d test_like_id_5 | ||
| 1189 | + Table "public.test_like_id_5" | ||
| 1190 | + Column | Type | Modifiers | ||
| 1191 | +--------+---------+----------------------------------------------- | ||
| 1192 | + a | integer | | ||
| 1193 | + b | text | character set UTF8 collate utf8mb4_general_ci | ||
| 1194 | + | ||
| 1195 | +INSERT INTO test_like_id_5 (b) VALUES ('b1'); | ||
| 1196 | +SELECT * FROM test_like_id_5; | ||
| 1197 | + a | b | ||
| 1198 | +---+---- | ||
| 1199 | + 1 | b1 | ||
| 1200 | + | b1 | ||
| 1201 | +(2 rows) | ||
| 1202 | + | ||
| 1203 | +DROP TABLE test_like_id_1, test_like_id_5; | ||
| 1204 | +--## test views | ||
| 1205 | +CREATE TABLE itest10 (a int generated by default as identity, b text); | ||
| 1206 | +NOTICE: CREATE TABLE will create implicit sequence "itest10_a_seq" for serial column "itest10.a" | ||
| 1207 | +CREATE TABLE itest11 (a int generated always as identity, b text); | ||
| 1208 | +NOTICE: CREATE TABLE will create implicit sequence "itest11_a_seq" for serial column "itest11.a" | ||
| 1209 | +CREATE VIEW itestv10 AS SELECT * FROM itest10; | ||
| 1210 | +CREATE VIEW itestv11 AS SELECT * FROM itest11; | ||
| 1211 | +INSERT INTO itestv10 DEFAULT VALUES; | ||
| 1212 | +INSERT INTO itestv10 DEFAULT VALUES; | ||
| 1213 | +INSERT INTO itestv11 DEFAULT VALUES; | ||
| 1214 | +INSERT INTO itestv11 DEFAULT VALUES; | ||
| 1215 | +SELECT * FROM itestv10; | ||
| 1216 | + a | b | ||
| 1217 | +---+--- | ||
| 1218 | + 1 | | ||
| 1219 | + 2 | | ||
| 1220 | +(2 rows) | ||
| 1221 | + | ||
| 1222 | +SELECT * FROM itestv11; | ||
| 1223 | + a | b | ||
| 1224 | +---+--- | ||
| 1225 | + 1 | | ||
| 1226 | + 2 | | ||
| 1227 | +(2 rows) | ||
| 1228 | + | ||
| 1229 | +INSERT INTO itestv10 VALUES (10, 'xyz'); | ||
| 1230 | +INSERT INTO itestv10 OVERRIDING USER VALUE VALUES (11, 'xyz'); | ||
| 1231 | +SELECT * FROM itestv10 order by 1, 2; | ||
| 1232 | + a | b | ||
| 1233 | +----+----- | ||
| 1234 | + 1 | | ||
| 1235 | + 2 | | ||
| 1236 | + 3 | xyz | ||
| 1237 | + 10 | xyz | ||
| 1238 | +(4 rows) | ||
| 1239 | + | ||
| 1240 | +INSERT INTO itestv11 VALUES (10, 'xyz'); | ||
| 1241 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 1242 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 1243 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 1244 | +INSERT INTO itestv11 OVERRIDING SYSTEM VALUE VALUES (11, 'xyz'); | ||
| 1245 | +SELECT * FROM itestv11 order by 1, 2; | ||
| 1246 | + a | b | ||
| 1247 | +----+----- | ||
| 1248 | + 1 | | ||
| 1249 | + 2 | | ||
| 1250 | + 11 | xyz | ||
| 1251 | +(3 rows) | ||
| 1252 | + | ||
| 1253 | +CREATE TABLE itest12 (a int NOT NULL, b text); | ||
| 1254 | +CREATE VIEW itestv12 AS SELECT * FROM itest12; | ||
| 1255 | +ALTER VIEW itestv12 alter column a add generated by default as identity; -- error | ||
| 1256 | +NOTICE: ALTER TABLE will create implicit sequence "itestv12_a_seq" for serial column "itestv12.a" | ||
| 1257 | +ERROR: column "a" of relation "itestv12" must be declared NOT NULL before identity can be added | ||
| 1258 | +ALTER VIEW itestv12 alter column a SET NOT NULL; -- error | ||
| 1259 | +ERROR: "itestv12" is not a table or foreign table | ||
| 1260 | +ALTER VIEW itestv12 alter column a RESTART; -- error | ||
| 1261 | +ERROR: column "a" of relation "itestv12" is not an identity column | ||
| 1262 | +ALTER VIEW itestv12 alter column a drop identity; -- error | ||
| 1263 | +ERROR: column "a" of relation "itestv12" is not an identity column | ||
| 1264 | +\d+ itestv12; | ||
| 1265 | + View "public.itestv12" | ||
| 1266 | + Column | Type | Modifiers | Storage | Description | ||
| 1267 | +--------+---------+-----------------------------------------------+----------+------------- | ||
| 1268 | + a | integer | | plain | | ||
| 1269 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | ||
| 1270 | +View definition: | ||
| 1271 | + SELECT * | ||
| 1272 | + FROM itest12; | ||
| 1273 | + | ||
| 1274 | +DROP VIEW itestv12; | ||
| 1275 | +DROP TABLE itest12; | ||
| 1276 | +--## ALTER xxx alter column add/set/drop identity | ||
| 1277 | +create sequence saaa; | ||
| 1278 | +ALTER SEQUENCE saaa alter column max_value add generated always as identity; -- error | ||
| 1279 | +NOTICE: ALTER TABLE will create implicit sequence "saaa_max_value_seq" for serial column "saaa.max_value" | ||
| 1280 | +ERROR: "saaa" is of the wrong type | ||
| 1281 | +create table test_t1 (a int); | ||
| 1282 | +create materialized view mv as select * from test_t1; | ||
| 1283 | +ALTER MATERIALIZED VIEW mv alter column a add generated by default as identity; -- error | ||
| 1284 | +NOTICE: ALTER TABLE will create implicit sequence "mv_a_seq" for serial column "mv.a" | ||
| 1285 | +ERROR: ALTER MATERIALIZED VIEW is not yet supported. | ||
| 1286 | +DROP TABLE test_t1; | ||
| 1287 | +DROP sequence saaa; | ||
| 1288 | +DROP MATERIALIZED VIEW mv; | ||
| 1289 | +--## inheritance | ||
| 1290 | +CREATE TABLE itest7 (a int GENERATED ALWAYS AS IDENTITY (maxvalue 100)); | ||
| 1291 | +NOTICE: CREATE TABLE will create implicit sequence "itest7_a_seq" for serial column "itest7.a" | ||
| 1292 | +INSERT INTO itest7 DEFAULT VALUES; | ||
| 1293 | +SELECT * FROM itest7; | ||
| 1294 | + a | ||
| 1295 | +--- | ||
| 1296 | + 1 | ||
| 1297 | +(1 row) | ||
| 1298 | + | ||
| 1299 | +-- identity property is not inherited | ||
| 1300 | +CREATE TABLE itest7a (b text) INHERITS (itest7); | ||
| 1301 | +\d+ itest7a | ||
| 1302 | + Table "public.itest7a" | ||
| 1303 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 1304 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 1305 | + a | integer | not null | plain | | | ||
| 1306 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 1307 | +Inherits: itest7 | ||
| 1308 | +Has OIDs: no | ||
| 1309 | +Options: orientation=row, compression=no, collate=1537 | ||
| 1310 | +Character Set: UTF8 | ||
| 1311 | +Collate: utf8mb4_general_ci | ||
| 1312 | + | ||
| 1313 | +-- make column identity in child table | ||
| 1314 | +CREATE TABLE itest7b (a int); | ||
| 1315 | +CREATE TABLE itest7c (a int GENERATED ALWAYS AS IDENTITY) INHERITS (itest7b); | ||
| 1316 | +NOTICE: CREATE TABLE will create implicit sequence "itest7c_a_seq" for serial column "itest7c.a" | ||
| 1317 | +NOTICE: merging column "a" with inherited definition | ||
| 1318 | +INSERT INTO itest7c DEFAULT VALUES; | ||
| 1319 | +SELECT * FROM itest7c; | ||
| 1320 | + a | ||
| 1321 | +--- | ||
| 1322 | + 1 | ||
| 1323 | +(1 row) | ||
| 1324 | + | ||
| 1325 | +CREATE TABLE itest7d (a int not null); | ||
| 1326 | +CREATE TABLE itest7e () INHERITS (itest7d); | ||
| 1327 | +ALTER TABLE itest7d ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; | ||
| 1328 | +NOTICE: ALTER TABLE will create implicit sequence "itest7d_a_seq" for serial column "itest7d.a" | ||
| 1329 | +ALTER TABLE itest7d ADD COLUMN b int GENERATED ALWAYS AS IDENTITY; -- error | ||
| 1330 | +NOTICE: ALTER TABLE will create implicit sequence "itest7d_b_seq" for serial column "itest7d.b" | ||
| 1331 | +ERROR: cannot recursively add identity column to table that has child tables | ||
| 1332 | +SELECT table_name, column_name, is_nullable, is_identity, identity_generation FROM information_schema.columns WHERE table_name LIKE 'itest7%' ORDER BY 1, 2; | ||
| 1333 | + table_name | column_name | is_nullable | is_identity | identity_generation | ||
| 1334 | +------------+-------------+-------------+-------------+--------------------- | ||
| 1335 | + itest7 | a | NO | YES | ALWAYS | ||
| 1336 | + itest7a | a | NO | NO | | ||
| 1337 | + itest7a | b | YES | NO | | ||
| 1338 | + itest7b | a | YES | NO | | ||
| 1339 | + itest7c | a | NO | YES | ALWAYS | ||
| 1340 | + itest7d | a | NO | YES | ALWAYS | ||
| 1341 | + itest7e | a | NO | NO | | ||
| 1342 | +(7 rows) | ||
| 1343 | + | ||
| 1344 | +-- These ALTER TABLE variants will not recurse. | ||
| 1345 | +ALTER TABLE itest7 ALTER COLUMN a SET GENERATED BY DEFAULT; | ||
| 1346 | +ALTER TABLE itest7 ALTER COLUMN a RESTART; | ||
| 1347 | +ALTER TABLE itest7 ALTER COLUMN a DROP IDENTITY; | ||
| 1348 | +--## pbe | ||
| 1349 | +CREATE TABLE test_t1 (a int PRIMARY KEY GENERATED ALWAYS AS IDENTITY , b text); | ||
| 1350 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 1351 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t1_pkey" for table "test_t1" | ||
| 1352 | +INSERT INTO test_t1 VALUES (DEFAULT, default); | ||
| 1353 | +PREPARE p1 AS INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES($1, $2); -- error | ||
| 1354 | +PREPARE p2 AS INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES($1, $2) ON DUPLICATE KEY UPDATE b = $3; -- success | ||
| 1355 | +EXECUTE p1 (1, 'xxxx'); | ||
| 1356 | +ERROR: duplicate key value violates unique constraint "test_t1_pkey" | ||
| 1357 | +DETAIL: Key (a)=(1) already exists. | ||
| 1358 | +EXECUTE p2 (1, 'setb', 'setb'); | ||
| 1359 | +DEALLOCATE p1; | ||
| 1360 | +DEALLOCATE p2; | ||
| 1361 | +select * from test_t1 order by 1, 2; | ||
| 1362 | + a | b | ||
| 1363 | +---+------ | ||
| 1364 | + 1 | setb | ||
| 1365 | +(1 row) | ||
| 1366 | + | ||
| 1367 | +DROP TABLE test_t1; | ||
| 1368 | +--## partitoned table | ||
| 1369 | +CREATE TABLE par_row_t1 (a int, b text) partition by range (a) | ||
| 1370 | +( | ||
| 1371 | + partition par_row_t1_p0 values less than(10), | ||
| 1372 | + partition par_row_t1_p3 values less than (maxvalue) | ||
| 1373 | +); | ||
| 1374 | +ALTER TABLE par_row_t1 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error | ||
| 1375 | +NOTICE: ALTER TABLE will create implicit sequence "par_row_t1_a_seq" for serial column "par_row_t1.a" | ||
| 1376 | +ERROR: cannot add identity to partitioned table "par_row_t1" | ||
| 1377 | +ALTER TABLE par_row_t1 ALTER COLUMN a SET GENERATED BY DEFAULT; -- error | ||
| 1378 | +ERROR: cannot set identity to partitioned table "par_row_t1" | ||
| 1379 | +ALTER TABLE par_row_t1 ALTER COLUMN a DROP IDENTITY; -- error | ||
| 1380 | +ERROR: cannot drop identity of partitioned table "par_row_t1" | ||
| 1381 | +ALTER TABLE par_row_t1 ALTER COLUMN a DROP IDENTITY IF EXISTS; -- error | ||
| 1382 | +ERROR: cannot drop identity of partitioned table "par_row_t1" | ||
| 1383 | +ALTER TABLE par_row_t1 ADD COLUMN c int GENERATED ALWAYS AS IDENTITY; -- error | ||
| 1384 | +NOTICE: ALTER TABLE will create implicit sequence "par_row_t1_c_seq" for serial column "par_row_t1.c" | ||
| 1385 | +DROP TABLE par_row_t1; | ||
| 1386 | +--## NUMERIC test | ||
| 1387 | +CREATE TABLE test_t1 (a NUMERIC(12, 0) PRIMARY KEY GENERATED ALWAYS AS IDENTITY(START WITH 1010101010101), b text); -- error | ||
| 1388 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 1389 | +ERROR: START value (1010101010101) cannot be greater than MAXVALUE (999999999999) | ||
| 1390 | +CREATE TABLE test_t1 (a NUMERIC(12, 0) PRIMARY KEY GENERATED ALWAYS AS IDENTITY(START WITH 101010101010), b text); | ||
| 1391 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 1392 | +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_t1_pkey" for table "test_t1" | ||
| 1393 | +INSERT INTO test_t1 DEFAULT VALUES; -- success | ||
| 1394 | +INSERT INTO test_t1 VALUES(101010101010, 'xxxx'); -- error | ||
| 1395 | +ERROR: cannot insert a non-DEFAULT value into column "a" | ||
| 1396 | +DETAIL: Column "a" is an identity column defined as "GENERATED ALWAYS". | ||
| 1397 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 1398 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(10, 'xxxx'); -- success | ||
| 1399 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(10, 'xxxx'); -- success | ||
| 1400 | +SELECT * from test_t1 order by 1, 2; | ||
| 1401 | + a | b | ||
| 1402 | +--------------+------ | ||
| 1403 | + 10 | xxxx | ||
| 1404 | + 101010101010 | | ||
| 1405 | + 101010101011 | xxxx | ||
| 1406 | +(3 rows) | ||
| 1407 | + | ||
| 1408 | +DROP TABLE test_t1; | ||
| 1409 | +--## ALTER SET IDENTITY tests | ||
| 1410 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY(START WITH 4) , b text); | ||
| 1411 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 1412 | +\d+ test_t1_a_seq; | ||
| 1413 | + Sequence "public.test_t1_a_seq" | ||
| 1414 | + Column | Type | Value | Storage | ||
| 1415 | +---------------+---------+---------------------+--------- | ||
| 1416 | + sequence_name | name | test_t1_a_seq | plain | ||
| 1417 | + last_value | bigint | 4 | plain | ||
| 1418 | + start_value | bigint | 4 | plain | ||
| 1419 | + increment_by | bigint | 1 | plain | ||
| 1420 | + max_value | bigint | 9223372036854775807 | plain | ||
| 1421 | + min_value | bigint | 1 | plain | ||
| 1422 | + cache_value | bigint | 1 | plain | ||
| 1423 | + log_cnt | bigint | 0 | plain | ||
| 1424 | + is_cycled | boolean | f | plain | ||
| 1425 | + is_called | boolean | f | plain | ||
| 1426 | + uuid | bigint | 0 | plain | ||
| 1427 | + is_global | boolean | f | plain | ||
| 1428 | +Sequence for identity column: public.test_t1.a | ||
| 1429 | + | ||
| 1430 | +ALTER TABLE test_t1 ALTER COLUMN a SET START WITH 10; -- success | ||
| 1431 | +ALTER TABLE test_t1 ALTER COLUMN a SET INCREMENT BY 2; -- success | ||
| 1432 | +\d+ test_t1_a_seq; | ||
| 1433 | + Sequence "public.test_t1_a_seq" | ||
| 1434 | + Column | Type | Value | Storage | ||
| 1435 | +---------------+---------+---------------------+--------- | ||
| 1436 | + sequence_name | name | test_t1_a_seq | plain | ||
| 1437 | + last_value | bigint | 4 | plain | ||
| 1438 | + start_value | bigint | 10 | plain | ||
| 1439 | + increment_by | bigint | 2 | plain | ||
| 1440 | + max_value | bigint | 9223372036854775807 | plain | ||
| 1441 | + min_value | bigint | 1 | plain | ||
| 1442 | + cache_value | bigint | 1 | plain | ||
| 1443 | + log_cnt | bigint | 0 | plain | ||
| 1444 | + is_cycled | boolean | f | plain | ||
| 1445 | + is_called | boolean | f | plain | ||
| 1446 | + uuid | bigint | 0 | plain | ||
| 1447 | + is_global | boolean | f | plain | ||
| 1448 | +Sequence for identity column: public.test_t1.a | ||
| 1449 | + | ||
| 1450 | +ALTER TABLE test_t1 ALTER COLUMN a SET MAXVALUE 3; -- error | ||
| 1451 | +ERROR: START value (10) cannot be greater than MAXVALUE (3) | ||
| 1452 | +ALTER TABLE test_t1 ALTER COLUMN a SET MAXVALUE 20; -- success | ||
| 1453 | +ALTER TABLE test_t1 ALTER COLUMN a SET MINVALUE 9; -- error | ||
| 1454 | +ERROR: RESTART value (4) cannot be less than MINVALUE (9) | ||
| 1455 | +ALTER TABLE test_t1 ALTER COLUMN a SET MINVALUE 2; -- success | ||
| 1456 | +ALTER TABLE test_t1 ALTER COLUMN a SET RESTART; -- error | ||
| 1457 | +ERROR: sequence option "restart" not supported here | ||
| 1458 | +LINE 1: ALTER TABLE test_t1 ALTER COLUMN a SET RESTART; | ||
| 1459 | + ^ | ||
| 1460 | +ALTER TABLE test_t1 ALTER COLUMN a SET NOCYCLE; -- success | ||
| 1461 | +ALTER TABLE test_t1 ALTER COLUMN a SET CACHE 10; -- success | ||
| 1462 | +\d+ test_t1_a_seq; | ||
| 1463 | + Sequence "public.test_t1_a_seq" | ||
| 1464 | + Column | Type | Value | Storage | ||
| 1465 | +---------------+---------+---------------+--------- | ||
| 1466 | + sequence_name | name | test_t1_a_seq | plain | ||
| 1467 | + last_value | bigint | 4 | plain | ||
| 1468 | + start_value | bigint | 10 | plain | ||
| 1469 | + increment_by | bigint | 2 | plain | ||
| 1470 | + max_value | bigint | 20 | plain | ||
| 1471 | + min_value | bigint | 2 | plain | ||
| 1472 | + cache_value | bigint | 10 | plain | ||
| 1473 | + log_cnt | bigint | 0 | plain | ||
| 1474 | + is_cycled | boolean | f | plain | ||
| 1475 | + is_called | boolean | f | plain | ||
| 1476 | + uuid | bigint | 0 | plain | ||
| 1477 | + is_global | boolean | f | plain | ||
| 1478 | +Sequence for identity column: public.test_t1.a | ||
| 1479 | + | ||
| 1480 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 1481 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 1482 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 1483 | + a | b | ||
| 1484 | +---+--- | ||
| 1485 | + 4 | | ||
| 1486 | + 6 | | ||
| 1487 | +(2 rows) | ||
| 1488 | + | ||
| 1489 | +ALTER TABLE test_t1 ALTER COLUMN a SET NOMAXVALUE; -- success | ||
| 1490 | +ALTER TABLE test_t1 ALTER COLUMN a SET NOMINVALUE; -- success | ||
| 1491 | +ALTER TABLE test_t1 ALTER COLUMN a RESTART WITH 10; -- success | ||
| 1492 | +ALTER TABLE test_t1 ALTER COLUMN a RESTART 11111; -- success | ||
| 1493 | +ALTER TABLE test_t1 ALTER COLUMN a SET RESTART 11; -- error | ||
| 1494 | +ERROR: sequence option "restart" not supported here | ||
| 1495 | +LINE 1: ALTER TABLE test_t1 ALTER COLUMN a SET RESTART 11; | ||
| 1496 | + ^ | ||
| 1497 | +\d+ test_t1_a_seq; | ||
| 1498 | + Sequence "public.test_t1_a_seq" | ||
| 1499 | + Column | Type | Value | Storage | ||
| 1500 | +---------------+---------+---------------------+--------- | ||
| 1501 | + sequence_name | name | test_t1_a_seq | plain | ||
| 1502 | + last_value | bigint | 11111 | plain | ||
| 1503 | + start_value | bigint | 10 | plain | ||
| 1504 | + increment_by | bigint | 2 | plain | ||
| 1505 | + max_value | bigint | 9223372036854775807 | plain | ||
| 1506 | + min_value | bigint | 1 | plain | ||
| 1507 | + cache_value | bigint | 10 | plain | ||
| 1508 | + log_cnt | bigint | 0 | plain | ||
| 1509 | + is_cycled | boolean | f | plain | ||
| 1510 | + is_called | boolean | f | plain | ||
| 1511 | + uuid | bigint | 0 | plain | ||
| 1512 | + is_global | boolean | f | plain | ||
| 1513 | +Sequence for identity column: public.test_t1.a | ||
| 1514 | + | ||
| 1515 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 1516 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 1517 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 1518 | + a | b | ||
| 1519 | +-------+--- | ||
| 1520 | + 4 | | ||
| 1521 | + 6 | | ||
| 1522 | + 11111 | | ||
| 1523 | + 11113 | | ||
| 1524 | +(4 rows) | ||
| 1525 | + | ||
| 1526 | +-- numeric/large sequence | ||
| 1527 | +create table test_t2 (a int generated always as identity, b int); | ||
| 1528 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 1529 | +alter table test_t2 add column c numeric(20, 0) generated by default as identity; | ||
| 1530 | +NOTICE: ALTER TABLE will create implicit sequence "test_t2_c_seq" for serial column "test_t2.c" | ||
| 1531 | +alter table test_t2 alter column c set generated always set start with 10; | ||
| 1532 | +\d+ test_t2 | ||
| 1533 | + Table "public.test_t2" | ||
| 1534 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 1535 | +--------+---------------+---------------------------------------+---------+--------------+------------- | ||
| 1536 | + a | integer | not null generated always as identity | plain | | | ||
| 1537 | + b | integer | | plain | | | ||
| 1538 | + c | numeric(20,0) | not null generated always as identity | main | | | ||
| 1539 | +Has OIDs: no | ||
| 1540 | +Options: orientation=row, compression=no | ||
| 1541 | + | ||
| 1542 | +DROP sequence test_t1_a_seq; -- error | ||
| 1543 | +ERROR: cannot drop sequence test_t1_a_seq because table test_t1 column a requires it | ||
| 1544 | +HINT: You can drop table test_t1 column a instead. | ||
| 1545 | +DROP TABLE test_t1, test_t2; | ||
| 1546 | +--## privileges | ||
| 1547 | +CREATE USER regress_user1 WITH PASSWORD 'Gauss@123'; | ||
| 1548 | +CREATE TABLE itest8 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 1549 | +NOTICE: CREATE TABLE will create implicit sequence "itest8_a_seq" for serial column "itest8.a" | ||
| 1550 | +GRANT SELECT, INSERT ON itest8 TO regress_user1; | ||
| 1551 | +SET ROLE regress_user1 PASSWORD 'Gauss@123'; | ||
| 1552 | +INSERT INTO itest8 DEFAULT VALUES; | ||
| 1553 | +SELECT * FROM itest8; | ||
| 1554 | + a | b | ||
| 1555 | +---+--- | ||
| 1556 | + 1 | | ||
| 1557 | +(1 row) | ||
| 1558 | + | ||
| 1559 | +RESET ROLE; | ||
| 1560 | +DROP TABLE itest8; | ||
| 1561 | +DROP USER regress_user1; | ||
| 1562 | +--## gs_catalog_attribute_records | ||
| 1563 | +SELECT attidentity, attnum FROM gs_catalog_attribute_records(1249) ORDER BY attnum LIMIT 1; | ||
| 1564 | + attidentity | attnum | ||
| 1565 | +-------------+-------- | ||
| 1566 | + | 1 | ||
| 1567 | +(1 row) | ||
| 1568 | + | ||
| 1569 | +--## with identity for d | ||
| 1570 | +CREATE TABLE book(bookId int IDENTITY GENERATED BY DEFAULT AS IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); -- error | ||
| 1571 | +NOTICE: CREATE TABLE will create implicit sequence "book_bookid_seq_identity" for serial column "book.bookid" | ||
| 1572 | +ERROR: multiple identity specifications for column "bookid" of table "book" | ||
| 1573 | +LINE 1: CREATE TABLE book(bookId int IDENTITY GENERATED BY DEFAULT A... | ||
| 1574 | + ^ | ||
| 1575 | +CREATE TABLE book(bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50), c int GENERATED BY DEFAULT AS IDENTITY); -- error | ||
| 1576 | +NOTICE: CREATE TABLE will create implicit sequence "book_bookid_seq_identity" for serial column "book.bookid" | ||
| 1577 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 1578 | +CREATE TABLE book(bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); -- success | ||
| 1579 | +NOTICE: CREATE TABLE will create implicit sequence "book_bookid_seq_identity" for serial column "book.bookid" | ||
| 1580 | +ALTER TABLE book ADD column c int IDENTITY; -- error | ||
| 1581 | +NOTICE: ALTER TABLE will create implicit sequence "book_c_seq_identity" for serial column "book.c" | ||
| 1582 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 1583 | +ALTER TABLE book ADD column c int GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 1584 | +NOTICE: ALTER TABLE will create implicit sequence "book_c_seq" for serial column "book.c" | ||
| 1585 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 1586 | +ALTER TABLE book ADD column c int IDENTITY(1, 20); -- error | ||
| 1587 | +NOTICE: ALTER TABLE will create implicit sequence "book_c_seq_identity" for serial column "book.c" | ||
| 1588 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 1589 | +ALTER TABLE book ADD column note NVARCHAR(50); -- success | ||
| 1590 | +ALTER TABLE book DROP column note; -- success | ||
| 1591 | +ALTER TABLE book ALTER column c Add GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 1592 | +ERROR: column "c" of relation "book" does not exist | ||
| 1593 | +ALTER TABLE book ALTER column bookId Add GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 1594 | +NOTICE: ALTER TABLE will create implicit sequence "book_bookid_seq" for serial column "book.bookid" | ||
| 1595 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 1596 | +ALTER TABLE book ALTER column bookId ADD IDENTITY(1, 20); -- error | ||
| 1597 | +NOTICE: ALTER TABLE will create implicit sequence "book_bookid_seq_identity1" for serial column "book.bookid" | ||
| 1598 | +ERROR: Multiple identity columns specified for table "book". Only one identity column per table is allowed. | ||
| 1599 | +ALTER TABLE book ALTER COLUMN bookname SET GENERATED BY DEFAULT; -- error | ||
| 1600 | +ERROR: column "bookname" of relation "book" is not an identity column | ||
| 1601 | +ALTER TABLE book ALTER COLUMN bookId SET GENERATED BY DEFAULT; -- error | ||
| 1602 | +ERROR: identity column "bookid" of relation "book" in D format not supported SET IDENTITY | ||
| 1603 | +ALTER TABLE book ALTER COLUMN bookId DROP IDENTITY; -- error | ||
| 1604 | +ERROR: identity column "bookid" of relation "book" in D format not supported DROP IDENTITY | ||
| 1605 | +ALTER TABLE book ALTER COLUMN bookname DROP IDENTITY; -- error | ||
| 1606 | +ERROR: column "bookname" of relation "book" is not an identity column | ||
| 1607 | +ALTER TABLE book ALTER COLUMN bookId DROP DEFAULT; -- error | ||
| 1608 | +ERROR: column "bookid" of relation "book" is an identity column | ||
| 1609 | +HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP IDENTITY instead. | ||
| 1610 | +ALTER TABLE book ALTER COLUMN bookId DROP NOT NULL; -- error | ||
| 1611 | +ERROR: column "bookid" of relation "book" is an identity column | ||
| 1612 | +ALTER SEQUENCE book_bookid_seq_identity OWNED BY NONE; -- success | ||
| 1613 | +ALTER SEQUENCE book_bookid_seq_identity OWNED BY book.bookId; -- success | ||
| 1614 | +\d+ book | ||
| 1615 | + Table "public.book" | ||
| 1616 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 1617 | +----------+---------------+-----------------------------------------------+----------+--------------+------------- | ||
| 1618 | + bookid | integer | not null identity | plain | | | ||
| 1619 | + bookname | nvarchar2(50) | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 1620 | + author | nvarchar2(50) | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 1621 | +Has OIDs: no | ||
| 1622 | +Options: orientation=row, compression=no, collate=1537 | ||
| 1623 | +Character Set: UTF8 | ||
| 1624 | +Collate: utf8mb4_general_ci | ||
| 1625 | + | ||
| 1626 | +set identity_insert = off; | ||
| 1627 | +INSERT INTO book VALUES (2, 'xxx', 'xxx'); -- error; | ||
| 1628 | +ERROR: INSERT has more expressions than target columns | ||
| 1629 | +LINE 1: INSERT INTO book VALUES (2, 'xxx', 'xxx'); | ||
| 1630 | + ^ | ||
| 1631 | +INSERT INTO book (bookid, bookname, author) VALUES(11111, 'xxxx', 'xxx'); -- error, turn on identity_insert or use OVERRIDING | ||
| 1632 | +ERROR: cannot insert a non-DEFAULT value into column "bookid" | ||
| 1633 | +DETAIL: Column "bookid" is an identity column defined as "IDENTITY". | ||
| 1634 | +HINT: Use OVERRIDING SYSTEM VALUE to override, Or turn on "identity_insert". | ||
| 1635 | +INSERT INTO book (bookid, bookname, author) OVERRIDING SYSTEM VALUE VALUES (33, 'xxx', 'xxx'); -- success; -- 33 | ||
| 1636 | +INSERT INTO book (bookid, bookname, author) OVERRIDING USER VALUE VALUES (11111, 'xxx', 'xxx'); -- success; -- 1 | ||
| 1637 | +set identity_insert = on; | ||
| 1638 | +INSERT INTO book VALUES (44, 'xxx', 'xxx'); -- error; | ||
| 1639 | +ERROR: INSERT has more expressions than target columns | ||
| 1640 | +LINE 1: INSERT INTO book VALUES (44, 'xxx', 'xxx'); | ||
| 1641 | + ^ | ||
| 1642 | +INSERT INTO book (bookid, bookname, author) OVERRIDING USER VALUE VALUES (11111, 'xxx', 'xxx'); -- success; -- 2 | ||
| 1643 | +INSERT INTO book (bookid, bookname, author) OVERRIDING SYSTEM VALUE VALUES (55, 'xxx', 'xxx'); -- success; -- 55 | ||
| 1644 | +set identity_insert = off; | ||
| 1645 | +select * from book order by 1, 2; | ||
| 1646 | + bookid | bookname | author | ||
| 1647 | +--------+----------+-------- | ||
| 1648 | + 1 | xxx | xxx | ||
| 1649 | + 2 | xxx | xxx | ||
| 1650 | + 33 | xxx | xxx | ||
| 1651 | + 55 | xxx | xxx | ||
| 1652 | +(4 rows) | ||
| 1653 | + | ||
| 1654 | +CREATE TABLE book1(b int1 GENERATED BY DEFAULT AS IDENTITY(START WITH 4), c int GENERATED BY DEFAULT AS IDENTITY (START WITH 5)); -- success | ||
| 1655 | +NOTICE: CREATE TABLE will create implicit sequence "book1_b_seq" for serial column "book1.b" | ||
| 1656 | +NOTICE: CREATE TABLE will create implicit sequence "book1_c_seq" for serial column "book1.c" | ||
| 1657 | +ALTER TABLE book1 ADD column c int IDENTITY; -- error | ||
| 1658 | +NOTICE: ALTER TABLE will create implicit sequence "book1_c_seq_identity" for serial column "book1.c" | ||
| 1659 | +ERROR: column "c" of relation "book1" already exists | ||
| 1660 | +ALTER TABLE book1 ADD column a int IDENTITY(6, 2); -- success | ||
| 1661 | +NOTICE: ALTER TABLE will create implicit sequence "book1_a_seq_identity" for serial column "book1.a" | ||
| 1662 | +ALTER TABLE book1 ADD column d int IDENTITY; -- error | ||
| 1663 | +NOTICE: ALTER TABLE will create implicit sequence "book1_d_seq_identity" for serial column "book1.d" | ||
| 1664 | +ERROR: Multiple identity columns specified for table "book1". Only one identity column per table is allowed. | ||
| 1665 | +insert into book1 values (1, 2, 3); -- error, cam't specify the value of the identity column | ||
| 1666 | +ERROR: INSERT has more expressions than target columns | ||
| 1667 | +LINE 1: insert into book1 values (1, 2, 3); | ||
| 1668 | + ^ | ||
| 1669 | +set identity_insert = on; -- need to specify column names and turn on identity_insert | ||
| 1670 | +insert into book1 values (1, 2, 3); -- error | ||
| 1671 | +ERROR: INSERT has more expressions than target columns | ||
| 1672 | +LINE 1: insert into book1 values (1, 2, 3); | ||
| 1673 | + ^ | ||
| 1674 | +insert into book1(b, c, a) values (DEFAULT, DEFAULT, DEFAULT); -- success | ||
| 1675 | +select * from book1 order by 1, 2, 3; | ||
| 1676 | + b | c | a | ||
| 1677 | +---+---+--- | ||
| 1678 | + 4 | 5 | 6 | ||
| 1679 | +(1 row) | ||
| 1680 | + | ||
| 1681 | +set identity_insert = off; | ||
| 1682 | +select pg_get_tabledef('book'::regclass::oid); | ||
| 1683 | + pg_get_tabledef | ||
| 1684 | +----------------------------------------------------------------------------- | ||
| 1685 | + SET search_path = public; + | ||
| 1686 | + CREATE TABLE book ( + | ||
| 1687 | + bookid integer IDENTITY (1, 1) NOT NULL, + | ||
| 1688 | + bookname nvarchar2(50) CHARACTER SET "UTF8" COLLATE utf8mb4_general_ci,+ | ||
| 1689 | + author nvarchar2(50) CHARACTER SET "UTF8" COLLATE utf8mb4_general_ci + | ||
| 1690 | + ) + | ||
| 1691 | + WITH (orientation=row, compression=no, collate=1537); | ||
| 1692 | +(1 row) | ||
| 1693 | + | ||
| 1694 | +select pg_get_tabledef('book1'::regclass::oid); | ||
| 1695 | + pg_get_tabledef | ||
| 1696 | +---------------------------------------------------------- | ||
| 1697 | + SET search_path = public; + | ||
| 1698 | + CREATE TABLE book1 ( + | ||
| 1699 | + b tinyint GENERATED BY DEFAULT AS IDENTITY NOT NULL,+ | ||
| 1700 | + c integer GENERATED BY DEFAULT AS IDENTITY NOT NULL,+ | ||
| 1701 | + a integer IDENTITY (6, 2) NOT NULL + | ||
| 1702 | + ) + | ||
| 1703 | + WITH (orientation=row, compression=no, collate=1537); | ||
| 1704 | +(1 row) | ||
| 1705 | + | ||
| 1706 | +\c postgres | ||
| 1707 | +DROP DATABASE s_test_pg_identity; | ||
| @@ -1177,7 +1177,10 @@ from sys.sysdatabases; | |||
| 1177 | 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | | 1177 | 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | |
| 1178 | 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | | 1178 | 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | |
| 1179 | 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | | 1179 | 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | |
| 1180 | -(5 rows) | 1180 | + 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | |
| 1181 | + 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | | ||
| 1182 | + 10 | 0 | 0 | 0 | Mon Jan 01 00:00:00 1900 | Mon Jan 01 00:00:00 1900 | 0 | 0 | | | ||
| 1183 | +(8 rows) | ||
| 1181 | 1184 | ||
| 1182 | select name, schema_id, principal_id | 1185 | select name, schema_id, principal_id |
| 1183 | from sys.schemas where schema_id < 10000 order by schema_id; | 1186 | from sys.schemas where schema_id < 10000 order by schema_id; |
| @@ -1242,3 +1245,4 @@ drop table employees; | |||
| 1242 | drop view high_salary_employees; | 1245 | drop view high_salary_employees; |
| 1243 | reset search_path; | 1246 | reset search_path; |
| 1244 | drop schema sys_view_test_02 cascade; | 1247 | drop schema sys_view_test_02 cascade; |
| 1248 | +reset d_format_behavior_compat_options; | ||
| @@ -121,13 +121,13 @@ select count(*) from synonyms; | |||
| 121 | (1 row) | 121 | (1 row) |
| 122 | 122 | ||
| 123 | set d_format_behavior_compat_options = ''; | 123 | set d_format_behavior_compat_options = ''; |
| 124 | -select count(*) from syscolumns; | 124 | +select count(*) from syscolumns; |
| 125 | count | 125 | count |
| 126 | --?.* | 126 | --?.* |
| 127 | --?.* | 127 | --?.* |
| 128 | (1 row) | 128 | (1 row) |
| 129 | 129 | ||
| 130 | -set d_format_behavior_compat_options = 'enable_sbr_identifier, default_collation'; | 130 | +reset d_format_behavior_compat_options; |
| 131 | select count(*) from sysdatabases; | 131 | select count(*) from sysdatabases; |
| 132 | count | 132 | count |
| 133 | --?.* | 133 | --?.* |
| @@ -0,0 +1,48 @@ | |||
| 1 | +create database s_test_pg_identity_dump_restore dbcompatibility 'D'; | ||
| 2 | +create database s_test_pg_identity_dump_restore_1 dbcompatibility 'D'; | ||
| 3 | +\c s_test_pg_identity_dump_restore_1 | ||
| 4 | +create extension shark; | ||
| 5 | +\c s_test_pg_identity_dump_restore | ||
| 6 | +create extension shark; | ||
| 7 | + | ||
| 8 | +create schema s_test_pg_identity_dump_restore; | ||
| 9 | +set current_schema = s_test_pg_identity_dump_restore; | ||
| 10 | + | ||
| 11 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY (START WITH 20 INCREMENT 2 NOMAXVALUE NOMINVALUE), b text); | ||
| 12 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY ( START 10 INCREMENT 1 MAXVALUE 2222), b text); | ||
| 13 | +ALTER TABLE test_t2 ADD COLUMN c int GENERATED ALWAYS AS IDENTITY (START 20 NOMAXVALUE NOMINVALUE NOCYCLE); -- success | ||
| 14 | +ALTER TABLE test_t2 ALTER COLUMN c SET GENERATED BY DEFAULT SET INCREMENT BY 2 SET START WITH 100 RESTART; | ||
| 15 | +CREATE TABLE book(bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); | ||
| 16 | + | ||
| 17 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 18 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 19 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 20 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 21 | +INSERT INTO book VALUES('book1','author1'),('book2','author2'); | ||
| 22 | +INSERT INTO book(bookname,author) VALUES('book3','author3'),('book4','author4'); | ||
| 23 | + | ||
| 24 | + | ||
| 25 | +\! @abs_bindir@/gs_dump s_test_pg_identity_dump_restore -p @portstring@ -f @abs_bindir@/s_test_pg_identity_dump_restore.sql -n s_test_pg_identity_dump_restore -w >/dev/null 2>&1; echo $? | ||
| 26 | +\! @abs_bindir@/gsql -ds_test_pg_identity_dump_restore_1 -p @portstring@ -f "@abs_bindir@/s_test_pg_identity_dump_restore.sql" | ||
| 27 | + | ||
| 28 | +\c s_test_pg_identity_dump_restore_1 | ||
| 29 | +set current_schema = s_test_pg_identity_dump_restore; | ||
| 30 | + | ||
| 31 | +\d+ test_t1 | ||
| 32 | +\d+ test_t2 | ||
| 33 | +\d+ book | ||
| 34 | +truncate test_t1; | ||
| 35 | +truncate test_t2; | ||
| 36 | +truncate book; | ||
| 37 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 38 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 39 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 40 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 41 | +INSERT INTO book VALUES('book1','author1'),('book2','author2'); | ||
| 42 | +INSERT INTO book(bookname,author) VALUES('book3','author3'),('book4','author4'); | ||
| 43 | +select * from test_t1 order by 1, 2; | ||
| 44 | +select * from test_t2 order by 1, 2; | ||
| 45 | +select * from book order by 1, 2; | ||
| 46 | +\c postgres | ||
| 47 | +drop database s_test_pg_identity_dump_restore; | ||
| 48 | +drop database s_test_pg_identity_dump_restore_1; | ||
| @@ -0,0 +1,147 @@ | |||
| 1 | +create database s_test_pg_identity_dump_restore dbcompatibility 'D'; | ||
| 2 | +create database s_test_pg_identity_dump_restore_1 dbcompatibility 'D'; | ||
| 3 | +\c s_test_pg_identity_dump_restore_1 | ||
| 4 | +create extension shark; | ||
| 5 | +\c s_test_pg_identity_dump_restore | ||
| 6 | +create extension shark; | ||
| 7 | +create schema s_test_pg_identity_dump_restore; | ||
| 8 | +set current_schema = s_test_pg_identity_dump_restore; | ||
| 9 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY (START WITH 20 INCREMENT 2 NOMAXVALUE NOMINVALUE), b text); | ||
| 10 | +NOTICE: CREATE TABLE will create implicit sequence "test_t1_a_seq" for serial column "test_t1.a" | ||
| 11 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY ( START 10 INCREMENT 1 MAXVALUE 2222), b text); | ||
| 12 | +NOTICE: CREATE TABLE will create implicit sequence "test_t2_a_seq" for serial column "test_t2.a" | ||
| 13 | +ALTER TABLE test_t2 ADD COLUMN c int GENERATED ALWAYS AS IDENTITY (START 20 NOMAXVALUE NOMINVALUE NOCYCLE); -- success | ||
| 14 | +NOTICE: ALTER TABLE will create implicit sequence "test_t2_c_seq" for serial column "test_t2.c" | ||
| 15 | +ALTER TABLE test_t2 ALTER COLUMN c SET GENERATED BY DEFAULT SET INCREMENT BY 2 SET START WITH 100 RESTART; | ||
| 16 | +CREATE TABLE book(bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); | ||
| 17 | +NOTICE: CREATE TABLE will create implicit sequence "book_bookid_seq_identity" for serial column "book.bookid" | ||
| 18 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 19 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 20 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 21 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 22 | +INSERT INTO book VALUES('book1','author1'),('book2','author2'); | ||
| 23 | +INSERT INTO book(bookname,author) VALUES('book3','author3'),('book4','author4'); | ||
| 24 | +\! @abs_bindir@/gs_dump s_test_pg_identity_dump_restore -p @portstring@ -f @abs_bindir@/s_test_pg_identity_dump_restore.sql -n s_test_pg_identity_dump_restore -w >/dev/null 2>&1; echo $? | ||
| 25 | +0 | ||
| 26 | +\! @abs_bindir@/gsql -ds_test_pg_identity_dump_restore_1 -p @portstring@ -f "@abs_bindir@/s_test_pg_identity_dump_restore.sql" | ||
| 27 | +SET | ||
| 28 | +SET | ||
| 29 | +SET | ||
| 30 | +SET | ||
| 31 | +SET | ||
| 32 | +SET | ||
| 33 | +SET | ||
| 34 | +SET | ||
| 35 | +SET | ||
| 36 | +SET | ||
| 37 | +SET | ||
| 38 | +SET | ||
| 39 | +CREATE SCHEMA | ||
| 40 | +ALTER SCHEMA | ||
| 41 | +SET | ||
| 42 | +SET | ||
| 43 | +SET | ||
| 44 | +CREATE TABLE | ||
| 45 | +ALTER TABLE | ||
| 46 | +ALTER TABLE | ||
| 47 | +ALTER SEQUENCE | ||
| 48 | +CREATE TABLE | ||
| 49 | +ALTER TABLE | ||
| 50 | +ALTER TABLE | ||
| 51 | +CREATE TABLE | ||
| 52 | +ALTER TABLE | ||
| 53 | +ALTER TABLE | ||
| 54 | +ALTER TABLE | ||
| 55 | + setval | ||
| 56 | +-------- | ||
| 57 | + 4 | ||
| 58 | +(1 row) | ||
| 59 | + | ||
| 60 | + setval | ||
| 61 | +-------- | ||
| 62 | + 22 | ||
| 63 | +(1 row) | ||
| 64 | + | ||
| 65 | + setval | ||
| 66 | +-------- | ||
| 67 | + 11 | ||
| 68 | +(1 row) | ||
| 69 | + | ||
| 70 | + setval | ||
| 71 | +-------- | ||
| 72 | + 102 | ||
| 73 | +(1 row) | ||
| 74 | + | ||
| 75 | +--?total time:.* | ||
| 76 | +\c s_test_pg_identity_dump_restore_1 | ||
| 77 | +set current_schema = s_test_pg_identity_dump_restore; | ||
| 78 | +\d+ test_t1 | ||
| 79 | + Table "s_test_pg_identity_dump_restore.test_t1" | ||
| 80 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 81 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 82 | + a | integer | not null generated always as identity | plain | | | ||
| 83 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 84 | +Has OIDs: no | ||
| 85 | +Options: orientation=row, compression=no, collate=1537 | ||
| 86 | +Character Set: UTF8 | ||
| 87 | +Collate: utf8mb4_general_ci | ||
| 88 | + | ||
| 89 | +\d+ test_t2 | ||
| 90 | + Table "s_test_pg_identity_dump_restore.test_t2" | ||
| 91 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 92 | +--------+---------+-----------------------------------------------+----------+--------------+------------- | ||
| 93 | + a | integer | not null generated by default as identity | plain | | | ||
| 94 | + b | text | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 95 | + c | integer | not null generated by default as identity | plain | | | ||
| 96 | +Has OIDs: no | ||
| 97 | +Options: orientation=row, compression=no, collate=1537 | ||
| 98 | +Character Set: UTF8 | ||
| 99 | +Collate: utf8mb4_general_ci | ||
| 100 | + | ||
| 101 | +\d+ book | ||
| 102 | + Table "s_test_pg_identity_dump_restore.book" | ||
| 103 | + Column | Type | Modifiers | Storage | Stats target | Description | ||
| 104 | +----------+---------------+-----------------------------------------------+----------+--------------+------------- | ||
| 105 | + bookid | integer | not null identity | plain | | | ||
| 106 | + bookname | nvarchar2(50) | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 107 | + author | nvarchar2(50) | character set UTF8 collate utf8mb4_general_ci | extended | | | ||
| 108 | +Has OIDs: no | ||
| 109 | +Options: orientation=row, compression=no, collate=1537 | ||
| 110 | +Character Set: UTF8 | ||
| 111 | +Collate: utf8mb4_general_ci | ||
| 112 | + | ||
| 113 | +truncate test_t1; | ||
| 114 | +truncate test_t2; | ||
| 115 | +truncate book; | ||
| 116 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 117 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 118 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 119 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 120 | +INSERT INTO book VALUES('book1','author1'),('book2','author2'); | ||
| 121 | +INSERT INTO book(bookname,author) VALUES('book3','author3'),('book4','author4'); | ||
| 122 | +select * from test_t1 order by 1, 2; | ||
| 123 | + a | b | ||
| 124 | +----+--- | ||
| 125 | + 24 | | ||
| 126 | + 26 | | ||
| 127 | +(2 rows) | ||
| 128 | + | ||
| 129 | +select * from test_t2 order by 1, 2; | ||
| 130 | + a | b | c | ||
| 131 | +----+---+----- | ||
| 132 | + 12 | | 104 | ||
| 133 | + 13 | | 106 | ||
| 134 | +(2 rows) | ||
| 135 | + | ||
| 136 | +select * from book order by 1, 2; | ||
| 137 | + bookid | bookname | author | ||
| 138 | +--------+----------+--------- | ||
| 139 | + 1 | book1 | author1 | ||
| 140 | + 2 | book2 | author2 | ||
| 141 | + 3 | book3 | author3 | ||
| 142 | + 4 | book4 | author4 | ||
| 143 | +(4 rows) | ||
| 144 | + | ||
| 145 | +\c postgres | ||
| 146 | +drop database s_test_pg_identity_dump_restore; | ||
| 147 | +drop database s_test_pg_identity_dump_restore_1; | ||
| @@ -39,3 +39,4 @@ test: sharksysfunction | |||
| 39 | test: alter_sequence | 39 | test: alter_sequence |
| 40 | test: target_alias | 40 | test: target_alias |
| 41 | 41 | ||
| 42 | +test: pg_identity pg_identity_dump_restore | ||
| @@ -621,7 +621,7 @@ Oid get_seq_id_from_tle(Query* query, TargetEntry* tle) | |||
| 621 | Relation rel = try_relation_open(rte->relid, AccessShareLock); | 621 | Relation rel = try_relation_open(rte->relid, AccessShareLock); |
| 622 | if (rel->rd_rel->relkind == RELKIND_RELATION) { | 622 | if (rel->rd_rel->relkind == RELKIND_RELATION) { |
| 623 | relation_close(rel, AccessShareLock); | 623 | relation_close(rel, AccessShareLock); |
| 624 | - return pg_get_serial_sequence_internal(rte->relid, var->varattno, true, NULL); | 624 | + return getIdentitySequence(rte->relid, var->varattno, true, true); |
| 625 | } else if (rel->rd_rel->relkind == RELKIND_VIEW) { | 625 | } else if (rel->rd_rel->relkind == RELKIND_VIEW) { |
| 626 | sub_query = get_view_query(rel); | 626 | sub_query = get_view_query(rel); |
| 627 | } else if (rel->rd_rel->relkind == RELKIND_MATVIEW) { | 627 | } else if (rel->rd_rel->relkind == RELKIND_MATVIEW) { |
| @@ -643,7 +643,7 @@ static Oid get_identity_seq_id(Oid tableOid) | |||
| 643 | Relation rel = try_relation_open(tableOid, AccessShareLock); | 643 | Relation rel = try_relation_open(tableOid, AccessShareLock); |
| 644 | if (rel->rd_rel->relkind != RELKIND_VIEW && rel->rd_rel->relkind != RELKIND_MATVIEW) { | 644 | if (rel->rd_rel->relkind != RELKIND_VIEW && rel->rd_rel->relkind != RELKIND_MATVIEW) { |
| 645 | relation_close(rel, AccessShareLock); | 645 | relation_close(rel, AccessShareLock); |
| 646 | - return get_table_identity(tableOid); | 646 | + return getIdentitySequenceForD(tableOid, NULL, NULL); |
| 647 | } | 647 | } |
| 648 | 648 | ||
| 649 | Query* viewquery = rel->rd_rel->relkind == RELKIND_VIEW ? get_view_query(rel) : get_matview_query(rel); | 649 | Query* viewquery = rel->rd_rel->relkind == RELKIND_VIEW ? get_view_query(rel) : get_matview_query(rel); |
| @@ -1224,26 +1224,12 @@ int get_is_compute(Oid rel_oid, char* colname) | |||
| 1224 | return is_compute; | 1224 | return is_compute; |
| 1225 | } | 1225 | } |
| 1226 | 1226 | ||
| 1227 | -int get_is_identity(Oid rel_oid, char* colname) | 1227 | +int get_is_identity(Oid relid, char* colname) |
| 1228 | { | 1228 | { |
| 1229 | - bool is_identity = false; | 1229 | + Oid seqOid = InvalidOid; |
| 1230 | - char* relname = get_rel_name(rel_oid); | ||
| 1231 | 1230 | ||
| 1232 | - bool isRetNull = false; | 1231 | + seqOid = getIdentitySequenceForD(relid, colname, NULL); |
| 1233 | - Datum result = DirectCall2(&isRetNull, pg_get_serial_sequence, InvalidOid, | 1232 | + return OidIsValid(seqOid); |
| 1234 | - CStringGetTextDatum(relname), CStringGetTextDatum(colname)); | ||
| 1235 | - if (isRetNull) { | ||
| 1236 | - pfree_ext(relname); | ||
| 1237 | - return 0; | ||
| 1238 | - } | ||
| 1239 | - | ||
| 1240 | - char* seqname = text_to_cstring(DatumGetTextPP(result)); | ||
| 1241 | - if (StrEndWith(seqname, "_seq_identity")) { | ||
| 1242 | - is_identity = true; | ||
| 1243 | - } | ||
| 1244 | - pfree_ext(relname); | ||
| 1245 | - pfree_ext(seqname); | ||
| 1246 | - return is_identity; | ||
| 1247 | } | 1233 | } |
| 1248 | 1234 | ||
| 1249 | int get_is_hidden(Oid rel_oid, char* colname) | 1235 | int get_is_hidden(Oid rel_oid, char* colname) |
| @@ -312,7 +312,11 @@ insert into t_identity_0023(name) values('x3'); | |||
| 312 | select ident_current('t_identity_0023'); | 312 | select ident_current('t_identity_0023'); |
| 313 | select scope_identity(); | 313 | select scope_identity(); |
| 314 | -- no option. | 314 | -- no option. |
| 315 | -create table t_identity_0023_1 (like t_identity_0023); | 315 | +create table t_identity_0023_1_1 (like t_identity_0023); |
| 316 | +\d+ t_identity_0023_1_1 | ||
| 317 | +insert t_identity_0023_1_1 (name) values('xxx1'); | ||
| 318 | +-- including IDENTITY. | ||
| 319 | +create table t_identity_0023_1 (like t_identity_0023 INCLUDING IDENTITY); | ||
| 316 | \d+ t_identity_0023_1 | 320 | \d+ t_identity_0023_1 |
| 317 | select ident_current('t_identity_0023_1'); | 321 | select ident_current('t_identity_0023_1'); |
| 318 | insert t_identity_0023_1 (name) values('xxx1'); | 322 | insert t_identity_0023_1 (name) values('xxx1'); |
| @@ -333,7 +337,7 @@ insert t_identity_0023_2 (name) values('xxx1'); | |||
| 333 | select ident_current('t_identity_0023_2'); | 337 | select ident_current('t_identity_0023_2'); |
| 334 | select scope_identity(); | 338 | select scope_identity(); |
| 335 | 339 | ||
| 336 | -drop table t_identity_0023; | 340 | +drop table t_identity_0023, t_identity_0023_1_1; |
| 337 | drop table t_identity_0023_1; | 341 | drop table t_identity_0023_1; |
| 338 | drop table t_identity_0023_2; | 342 | drop table t_identity_0023_2; |
| 339 | -- ## can't insert identity column, expected error. | 343 | -- ## can't insert identity column, expected error. |
| @@ -527,7 +531,7 @@ drop table t_identity_numeric_t3_1; | |||
| 527 | ---- create table like. | 531 | ---- create table like. |
| 528 | -- max/min value | 532 | -- max/min value |
| 529 | create table t_identity_numeric_t4(id numeric(38, 0) identity, name varchar(10)); | 533 | create table t_identity_numeric_t4(id numeric(38, 0) identity, name varchar(10)); |
| 530 | -create table t_identity_numeric_t4_1 (like t_identity_numeric_t4); | 534 | +create table t_identity_numeric_t4_1 (like t_identity_numeric_t4 including identity); |
| 531 | \d+ t_identity_numeric_t4_1; | 535 | \d+ t_identity_numeric_t4_1; |
| 532 | set identity_insert = on; | 536 | set identity_insert = on; |
| 533 | insert into t_identity_numeric_t4_1(id, name) values(987654321098765432109876543210, 'xxx'); -- over int64 | 537 | insert into t_identity_numeric_t4_1(id, name) values(987654321098765432109876543210, 'xxx'); -- over int64 |
| @@ -538,7 +542,7 @@ drop table t_identity_numeric_t4_1; | |||
| 538 | 542 | ||
| 539 | -- start | 543 | -- start |
| 540 | create table t_identity_numeric_t5(id numeric(38, 0) identity(987654321098765432100000, 2), id1 serial, name varchar(10)); | 544 | create table t_identity_numeric_t5(id numeric(38, 0) identity(987654321098765432100000, 2), id1 serial, name varchar(10)); |
| 541 | -create table t_identity_numeric_t5_1 (like t_identity_numeric_t5); | 545 | +create table t_identity_numeric_t5_1 (like t_identity_numeric_t5 including identity); |
| 542 | insert into t_identity_numeric_t5_1(name) values('xxx'); -- over int64 | 546 | insert into t_identity_numeric_t5_1(name) values('xxx'); -- over int64 |
| 543 | select * from t_identity_numeric_t5_1 order by id; | 547 | select * from t_identity_numeric_t5_1 order by id; |
| 544 | drop table t_identity_numeric_t5; | 548 | drop table t_identity_numeric_t5; |
| @@ -546,7 +550,7 @@ drop table t_identity_numeric_t5_1; | |||
| 546 | 550 | ||
| 547 | -- increment | 551 | -- increment |
| 548 | create table t_identity_numeric_t6(id numeric(38, 0) identity(2, 987654321098765432100000), id1 serial, name varchar(10)); | 552 | create table t_identity_numeric_t6(id numeric(38, 0) identity(2, 987654321098765432100000), id1 serial, name varchar(10)); |
| 549 | -create table t_identity_numeric_t6_1 (like t_identity_numeric_t6); | 553 | +create table t_identity_numeric_t6_1 (like t_identity_numeric_t6 including identity); |
| 550 | \d+ t_identity_numeric_t6_1 | 554 | \d+ t_identity_numeric_t6_1 |
| 551 | insert into t_identity_numeric_t6_1(name) values('xxx'); -- over int64 | 555 | insert into t_identity_numeric_t6_1(name) values('xxx'); -- over int64 |
| 552 | insert into t_identity_numeric_t6_1(name) values('xxx1'); | 556 | insert into t_identity_numeric_t6_1(name) values('xxx1'); |
| @@ -568,7 +572,7 @@ drop table t_ident; | |||
| 568 | -- create table like | 572 | -- create table like |
| 569 | create sequence identity_t2_id_seq_identity; | 573 | create sequence identity_t2_id_seq_identity; |
| 570 | create table identity_t2(id numeric(38, 0) identity(10, 2), b int); | 574 | create table identity_t2(id numeric(38, 0) identity(10, 2), b int); |
| 571 | -create table identity_t2_1 (like identity_t2); | 575 | +create table identity_t2_1 (like identity_t2 including identity); |
| 572 | insert into identity_t2_1(b) values (10); | 576 | insert into identity_t2_1(b) values (10); |
| 573 | insert into identity_t2_1(b) values (10); | 577 | insert into identity_t2_1(b) values (10); |
| 574 | select id, b from identity_t2_1; | 578 | select id, b from identity_t2_1; |
| @@ -586,5 +590,14 @@ select id, b from identity_t3_1; | |||
| 586 | drop sequence identity_t3_id_seq_identity; | 590 | drop sequence identity_t3_id_seq_identity; |
| 587 | drop table identity_t3; | 591 | drop table identity_t3; |
| 588 | drop table identity_t3_1; | 592 | drop table identity_t3_1; |
| 593 | + | ||
| 594 | + | ||
| 595 | +-- compatiable with pg identity | ||
| 596 | +-- error | ||
| 597 | +create table identity_t1(id numeric(38, 0) identity(1, 2) generated always as identity); | ||
| 598 | +-- error | ||
| 599 | +create table identity_t1(id numeric(38, 0) identity(1, 2), id2 numeric(37, 0) generated always as identity); | ||
| 600 | + | ||
| 601 | + | ||
| 589 | reset current_schema; | 602 | reset current_schema; |
| 590 | drop schema identity_schema; | 603 | drop schema identity_schema; |
| @@ -0,0 +1,764 @@ | |||
| 1 | +create database s_test_pg_identity dbcompatibility 'D'; | ||
| 2 | +\c s_test_pg_identity | ||
| 3 | +create extension shark; | ||
| 4 | +--## create table | ||
| 5 | +-- error | ||
| 6 | +CREATE TABLE test_t1 (a serial GENERATED ALWAYS AS IDENTITY, b text); | ||
| 7 | +-- error | ||
| 8 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY default 3, b text); | ||
| 9 | +-- error | ||
| 10 | +CREATE TABLE test_t1 (id int1 GENERATED ALWAYS AS IDENTITY (START WITH 44) NULL); | ||
| 11 | +CREATE TABLE test_t1 (id int1 NULL GENERATED ALWAYS AS IDENTITY (START WITH 44)); | ||
| 12 | +-- error | ||
| 13 | +CREATE TABLE par_row_t1 (a int GENERATED ALWAYS AS IDENTITY, b text) partition by range (a) | ||
| 14 | +( | ||
| 15 | + partition par_row_t1_p0 values less than(10), | ||
| 16 | + partition par_row_t1_p3 values less than (maxvalue) | ||
| 17 | +); | ||
| 18 | +CREATE temp TABLE test_t4 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 19 | + | ||
| 20 | +-- success | ||
| 21 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 22 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY, b text); | ||
| 23 | +CREATE TABLE test_t3 (id int1 NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 44)); | ||
| 24 | +\d+ test_t1 | ||
| 25 | +\d+ test_t2 | ||
| 26 | +select pg_get_tabledef('test_t3'::regclass); | ||
| 27 | + | ||
| 28 | +-- identity sequence options | ||
| 29 | +CREATE TABLE test_t5 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 10 MAXVALUE 30 MINVALUE 12), b text); -- error | ||
| 30 | +CREATE TABLE test_t5 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 10 MAXVALUE 30 MINVALUE 9), b text); -- success | ||
| 31 | +\d+ test_t5; | ||
| 32 | +select start_value, max_value, min_value, increment_by from test_t5_a_seq; | ||
| 33 | +INSERT INTO test_t5 DEFAULT VALUES; -- success | ||
| 34 | +INSERT INTO test_t5 OVERRIDING SYSTEM VALUE VALUES(8, 'xxxx'); -- success | ||
| 35 | +SELECT * FROM test_t5 order by 1, 2; | ||
| 36 | +CREATE TABLE test_t6 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 11 NOMAXVALUE MINVALUE 9), b text); -- success | ||
| 37 | +\d+ test_t6; | ||
| 38 | +select start_value, max_value, min_value, increment_by from test_t6_a_seq; | ||
| 39 | +CREATE TABLE test_t7 (a int2 GENERATED ALWAYS AS IDENTITY (START WITH 12 MAXVALUE 15 INCREMENT 2 NOMINVALUE), b text); -- success | ||
| 40 | +\d+ test_t7; | ||
| 41 | +select start_value, max_value, min_value, increment_by from test_t7_a_seq; | ||
| 42 | +INSERT INTO test_t7 DEFAULT VALUES; -- success | ||
| 43 | +INSERT INTO test_t7 DEFAULT VALUES; -- success | ||
| 44 | +INSERT INTO test_t7 DEFAULT VALUES; -- error | ||
| 45 | +ALTER TABLE test_t7 ALTER COLUMN a RESTART; | ||
| 46 | +select start_value, max_value, min_value, increment_by from test_t7_a_seq; | ||
| 47 | +INSERT INTO test_t7 DEFAULT VALUES; -- success | ||
| 48 | +select * from test_t7 order by 1, 2; | ||
| 49 | + | ||
| 50 | +DROP TABLE test_t1, test_t2, test_t3, test_t5, test_t6, test_t7; | ||
| 51 | + | ||
| 52 | +CREATE TABLE itest1 (a int generated by default as identity, b text); | ||
| 53 | +CREATE TABLE itest2 (a bigint generated always as identity, b text); | ||
| 54 | +CREATE TABLE itest3 (a smallint generated by default as identity (start with 7 increment by 5), b text); | ||
| 55 | +ALTER TABLE itest3 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error | ||
| 56 | + | ||
| 57 | +SELECT table_name, column_name, column_default, is_nullable, is_identity, identity_generation FROM information_schema.columns WHERE table_name LIKE 'itest_' ORDER BY 1, 2; | ||
| 58 | +-- internal sequences should not be shown here | ||
| 59 | +SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE 'itest%'; | ||
| 60 | +-- invalid column type | ||
| 61 | +CREATE TABLE itest_err_1 (a text generated by default as identity); | ||
| 62 | +-- duplicate identity | ||
| 63 | +CREATE TABLE itest_err_2 (a int generated always as identity generated by default as identity); | ||
| 64 | +-- cannot have default and identity | ||
| 65 | +CREATE TABLE itest_err_3 (a int default 5 generated by default as identity); | ||
| 66 | +-- cannot combine serial and identity | ||
| 67 | +CREATE TABLE itest_err_4 (a serial generated by default as identity); | ||
| 68 | + | ||
| 69 | +--## insert/update OVERRIDING tests | ||
| 70 | +INSERT INTO itest1 DEFAULT VALUES; | ||
| 71 | +INSERT INTO itest1 DEFAULT VALUES; | ||
| 72 | +INSERT INTO itest2 DEFAULT VALUES; | ||
| 73 | +INSERT INTO itest2 DEFAULT VALUES; | ||
| 74 | +INSERT INTO itest3 DEFAULT VALUES; | ||
| 75 | +INSERT INTO itest3 DEFAULT VALUES; | ||
| 76 | + | ||
| 77 | +SELECT * FROM itest1 order by a; | ||
| 78 | +SELECT * FROM itest2 order by a; | ||
| 79 | +SELECT * FROM itest3 order by a; | ||
| 80 | + | ||
| 81 | +INSERT INTO itest1 VALUES (10, 'xyz'); | ||
| 82 | +INSERT INTO itest1 OVERRIDING USER VALUE VALUES (10, 'xyz'); | ||
| 83 | + | ||
| 84 | +SELECT * FROM itest1 order by a; | ||
| 85 | + | ||
| 86 | +INSERT INTO itest2 VALUES (10, 'xyz'); -- error | ||
| 87 | +INSERT INTO itest2 OVERRIDING SYSTEM VALUE VALUES (10, 'xyz'); | ||
| 88 | + | ||
| 89 | +SELECT * FROM itest2; | ||
| 90 | + | ||
| 91 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 92 | +INSERT INTO test_t1 VALUES('xxxx'); -- error | ||
| 93 | +INSERT INTO test_t1(b) VALUES('xxxx'); -- success | ||
| 94 | +INSERT INTO test_t1(a, b) VALUES(1, 'xxxx'); -- error | ||
| 95 | +INSERT INTO test_t1 VALUES(1, 'xxxx'); -- error | ||
| 96 | +INSERT INTO test_t1 VALUES(DEFAULT, 'xxxx'); -- success | ||
| 97 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1111, 'xx'); -- success | ||
| 98 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'xxxx'); -- success | ||
| 99 | +INSERT INTO test_t1(a, b) OVERRIDING USER VALUE VALUES(DEFAULT, 'xxxx'); -- success | ||
| 100 | +INSERT INTO test_t1(a, b) OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- success | ||
| 101 | +INSERT INTO test_t1(a, b) select 1, 'xxxxx'; -- error | ||
| 102 | +INSERT INTO test_t1(a, b) OVERRIDING USER VALUE select 1, 'xxxxx'; -- success | ||
| 103 | +INSERT INTO test_t1(a, b) OVERRIDING SYSTEM VALUE select 12222, 'xxxxx'; -- success | ||
| 104 | +SELECT * FROM test_t1 ORDER BY a; | ||
| 105 | + | ||
| 106 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY, b text); | ||
| 107 | +INSERT INTO test_t2 VALUES('xxxx'); -- error | ||
| 108 | +INSERT INTO test_t2(b) VALUES('xxxx'); -- success | ||
| 109 | +INSERT INTO test_t2(a, b) VALUES(1, 'xxxx'); -- success | ||
| 110 | +INSERT INTO test_t2 VALUES(1, 'xxxx'); -- success | ||
| 111 | +INSERT INTO test_t2 VALUES(DEFAULT, 'xxxx'); -- success | ||
| 112 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1111, 'xx'); -- success | ||
| 113 | +INSERT INTO test_t2 OVERRIDING USER VALUE VALUES(1111, 'xxxx'); -- success | ||
| 114 | +INSERT INTO test_t2(a, b) OVERRIDING USER VALUE VALUES(DEFAULT, 'xxxx'); -- success | ||
| 115 | +INSERT INTO test_t2(a, b) OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- success | ||
| 116 | +INSERT INTO test_t2(a, b) select 1, 'xxxxx'; -- success | ||
| 117 | +INSERT INTO test_t2(a, b) OVERRIDING USER VALUE select 1, 'xxxxx'; -- success | ||
| 118 | +INSERT INTO test_t2(a, b) OVERRIDING SYSTEM VALUE select 12222, 'xxxxx'; -- success | ||
| 119 | +SELECT * FROM test_t2 ORDER BY a; | ||
| 120 | + | ||
| 121 | +DROP TABLE test_t1, test_t2; | ||
| 122 | + | ||
| 123 | +--## inline view tests | ||
| 124 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text, c int); | ||
| 125 | +CREATE TABLE test_t2 (a int GENERATED ALWAYS AS IDENTITY, b text, c int); | ||
| 126 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 127 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 128 | +-- insert | ||
| 129 | +INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 130 | + SELECT 2, 'inline view'; -- error | ||
| 131 | +INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 132 | + OVERRIDING SYSTEM VALUE SELECT 22, 'inline view'; -- success | ||
| 133 | +INSERT INTO (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 134 | + OVERRIDING USER VALUE SELECT 12, 'inline view'; -- success | ||
| 135 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 136 | + | ||
| 137 | +-- update | ||
| 138 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup -- SET TO DEFAULT is not supported for subquery "sup" | ||
| 139 | + SET sup.a = 2; -- error | ||
| 140 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 141 | + SET sup.a = DEFAULT; -- error | ||
| 142 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 12) sup | ||
| 143 | + SET sup.a = DEFAULT, b = 'inline view updated'; -- error | ||
| 144 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 145 | + | ||
| 146 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 147 | + SET sup.a = DEFAULT; -- error | ||
| 148 | +UPDATE (SELECT a, b FROM test_t1 WHERE a = 1) sup | ||
| 149 | + SET sup.b = 'inline view updated'; -- success | ||
| 150 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 151 | + | ||
| 152 | +DROP TABLE test_t1, test_t2; | ||
| 153 | + | ||
| 154 | +-- --## multi insert tests | ||
| 155 | +-- CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 156 | +-- CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY, b text); | ||
| 157 | +-- -- error | ||
| 158 | +-- INSERT ALL | ||
| 159 | +-- INTO test_t1 (a, b) VALUES (val1, val2) | ||
| 160 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 161 | +-- SELECT 1 as val1, 'x1' as val2; | ||
| 162 | +-- -- success | ||
| 163 | +-- INSERT ALL | ||
| 164 | +-- INTO test_t1 (a, b) OVERRIDING USER VALUE VALUES (val1, val2) | ||
| 165 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 166 | +-- SELECT 2 as val1, 'x1' as val2; | ||
| 167 | +-- -- success | ||
| 168 | +-- INSERT ALL | ||
| 169 | +-- INTO test_t1 (a, b) OVERRIDING SYSTEM VALUE VALUES (val1, val2) | ||
| 170 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 171 | +-- SELECT 3 as val1, 'x2' as val2; | ||
| 172 | +-- -- success | ||
| 173 | +-- INSERT ALL | ||
| 174 | +-- INTO test_t1 (a, b) OVERRIDING SYSTEM VALUE VALUES (DEFAULT, val2) | ||
| 175 | +-- INTO test_t2 (a, b) VALUES (val1, val2) | ||
| 176 | +-- SELECT 4 as val1, 'x3' as val2; | ||
| 177 | +-- -- success | ||
| 178 | +-- INSERT ALL | ||
| 179 | +-- INTO test_t1 (a, b) VALUES (DEFAULT, val2) | ||
| 180 | +-- INTO test_t2 (a, b) VALUES (DEFAULT, val2) | ||
| 181 | +-- SELECT 5 as val1, 'x4' as val2; | ||
| 182 | +-- -- success | ||
| 183 | +-- INSERT ALL | ||
| 184 | +-- INTO test_t1 (a, b) VALUES (DEFAULT, val2) | ||
| 185 | +-- INTO test_t2 (a, b) OVERRIDING USER VALUE VALUES (999, val2) | ||
| 186 | +-- SELECT 6 as val1, 'x5' as val2; | ||
| 187 | +-- -- success | ||
| 188 | +-- INSERT ALL | ||
| 189 | +-- INTO test_t1 (b) VALUES (DEFAULT, val2) | ||
| 190 | +-- INTO test_t2 (b) VALUES (DEFAULT, val2) | ||
| 191 | +-- SELECT 7 as val1, 'x6' as val2; | ||
| 192 | + | ||
| 193 | +-- SELECT * FROM test_t1 ORDER BY b; | ||
| 194 | +-- SELECT * FROM test_t2 ORDER BY b; | ||
| 195 | + | ||
| 196 | +-- DROP TABLE test_t1, test_t2; | ||
| 197 | + | ||
| 198 | +--## upserts tests | ||
| 199 | +CREATE TABLE test_t1 (a int PRIMARY KEY GENERATED ALWAYS AS IDENTITY , b text); | ||
| 200 | +INSERT INTO test_t1 VALUES (DEFAULT, default); | ||
| 201 | +SELECT * FROM test_t1; | ||
| 202 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- error | ||
| 203 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'setb') ON DUPLICATE KEY UPDATE b = 'setb'; -- success | ||
| 204 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'nothing') ON DUPLICATE KEY UPDATE NOTHING; -- success | ||
| 205 | +SELECT * FROM test_t1 order by 1, 2; -- 1, setb | ||
| 206 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'nothing') ON DUPLICATE KEY UPDATE a = 2; -- error | ||
| 207 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'seta') ON DUPLICATE KEY UPDATE b = 'seta'; -- success | ||
| 208 | +SELECT * FROM test_t1 order by 1, 2; -- 1, seta | ||
| 209 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(1, 'seta') ON DUPLICATE KEY UPDATE b = 'setc', a = DEFAULT; -- error | ||
| 210 | +SELECT * FROM test_t1 order by 1, 2; -- 1, seta | ||
| 211 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(2, 'test1'); -- success | ||
| 212 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1'); -- error | ||
| 213 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(3, 'test1'); -- success | ||
| 214 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE b = 'w3' WHERE a = 3; -- success | ||
| 215 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(4, 'test1'); -- success | ||
| 216 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE a = 5 WHERE a = 4; -- error, 未自增 | ||
| 217 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 218 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(5, 'test1'); -- success | ||
| 219 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE b = 'w5', a = DEFAULT WHERE a = 5; -- error, 未自增 | ||
| 220 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(6, 'test1'); -- success | ||
| 221 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(1111, 'test1') ON DUPLICATE KEY UPDATE b = 'w6' WHERE a = 4; -- success | ||
| 222 | + | ||
| 223 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 224 | + | ||
| 225 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, b text); | ||
| 226 | +INSERT INTO test_t2 DEFAULT VALUES; | ||
| 227 | +SELECT * FROM test_t2; | ||
| 228 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'xxxx'); -- error | ||
| 229 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'setb') ON DUPLICATE KEY UPDATE b = 'setb'; -- success | ||
| 230 | +SELECT * FROM test_t2 order by 1, 2; | ||
| 231 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'setc') ON DUPLICATE KEY UPDATE a = 2; -- error | ||
| 232 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'setc') ON DUPLICATE KEY UPDATE a = DEFAULT, b = 'setc'; -- error | ||
| 233 | +SELECT * FROM test_t2 order by 1, 2; -- 1, setb | ||
| 234 | +INSERT INTO test_t2 OVERRIDING USER VALUE VALUES(2, 'test1'); -- success | ||
| 235 | +INSERT INTO test_t2 OVERRIDING USER VALUE VALUES(2, 'test2') ON DUPLICATE KEY UPDATE b = 'test3'; -- success | ||
| 236 | +SELECT * FROM test_t2 order by 1, 2; -- | ||
| 237 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'test2') ON DUPLICATE KEY UPDATE a = 3; -- error | ||
| 238 | +INSERT INTO test_t2 OVERRIDING SYSTEM VALUE VALUES(1, 'test2') ON DUPLICATE KEY UPDATE a = DEFAULT, b = 'test2'; -- error | ||
| 239 | +SELECT * FROM test_t2 order by 1, 2; | ||
| 240 | + | ||
| 241 | +DROP TABLE test_t1, test_t2; | ||
| 242 | + | ||
| 243 | +--## returning tests | ||
| 244 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY(START WITH 10) PRIMARY KEY, b text); | ||
| 245 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY(START WITH 12) PRIMARY KEY, b text); | ||
| 246 | +INSERT INTO test_t1 DEFAULT VALUES RETURNING a; | ||
| 247 | +INSERT INTO test_t2 DEFAULT VALUES RETURNING a; | ||
| 248 | +DROP TABLE test_t1, test_t2; | ||
| 249 | + | ||
| 250 | +--## UPDATE tests | ||
| 251 | +UPDATE itest1 SET a = 101 WHERE a = 1; | ||
| 252 | +UPDATE itest1 SET a = DEFAULT WHERE a = 2; | ||
| 253 | +UPDATE itest1 SET b = 'test' where a=101; | ||
| 254 | +SELECT * FROM itest1 order by 1, 2; | ||
| 255 | + | ||
| 256 | +UPDATE itest2 SET a = 101 WHERE a = 1; -- error | ||
| 257 | +UPDATE itest2 SET a = DEFAULT WHERE a = 2; | ||
| 258 | +UPDATE itest2 SET b = 'test' where a=101; | ||
| 259 | +SELECT * FROM itest2 order by 1, 2; | ||
| 260 | + | ||
| 261 | +--## merge tests | ||
| 262 | +-- when not matched insert | ||
| 263 | +create table target_table ( | ||
| 264 | + id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, | ||
| 265 | + name varchar(50), | ||
| 266 | + age int | ||
| 267 | +); | ||
| 268 | +create table source_table (id int, name varchar(50), age int); | ||
| 269 | +insert into source_table values (10, 'zliu', 28), (11, 'sqi', 30); | ||
| 270 | +-- expected: success. let id generated by system. | ||
| 271 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 272 | +when not matched then | ||
| 273 | + insert (name, age) values (s.name, s.age); | ||
| 274 | +-- expected: error | ||
| 275 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 276 | +when not matched then | ||
| 277 | + insert (id, name, age) values (1, 'error', 21); | ||
| 278 | +-- expected: success | ||
| 279 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 280 | +when not matched then | ||
| 281 | + insert (id, name, age) OVERRIDING USER VALUE values (1, 'success', 22); -- 3 | ||
| 282 | +-- expected: error | ||
| 283 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 284 | +when not matched then | ||
| 285 | + insert (id, name, age) OVERRIDING SYSTEM VALUE values (2, 'error', 23); | ||
| 286 | +-- expected: success | ||
| 287 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 288 | +when not matched then | ||
| 289 | + insert (id, name, age) OVERRIDING SYSTEM VALUE values (3, 'success', 24); | ||
| 290 | +-- expected: success | ||
| 291 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 292 | +when not matched then | ||
| 293 | + insert (id, name, age) OVERRIDING USER VALUE values (3, 'success', 25); | ||
| 294 | +-- expected: success | ||
| 295 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 296 | +when not matched then | ||
| 297 | + insert (id, name, age) OVERRIDING SYSTEM VALUE values (DEFAULT, 'success', 26); | ||
| 298 | +-- expected: success | ||
| 299 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 300 | +when not matched then | ||
| 301 | + insert (id, name, age) OVERRIDING USER VALUE values (DEFAULT, 'success', 27); | ||
| 302 | +SELECT * from target_table order by id; | ||
| 303 | +DROP TABLE target_table, source_table; | ||
| 304 | + | ||
| 305 | +-- when matched update | ||
| 306 | +create table target_table ( | ||
| 307 | + id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, | ||
| 308 | + name varchar(50), | ||
| 309 | + age int | ||
| 310 | +); | ||
| 311 | +create table source_table (id int, name varchar(50), age int); | ||
| 312 | +insert into source_table values (1, 'zliu', 28), (2, 'sqi', 30); | ||
| 313 | +-- success | ||
| 314 | +insert into target_table default values; | ||
| 315 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 316 | +WHEN MATCHED THEN | ||
| 317 | + UPDATE SET name = s.name, age = s.age + 1; | ||
| 318 | +-- error | ||
| 319 | +insert into target_table default values; | ||
| 320 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 321 | +WHEN MATCHED THEN | ||
| 322 | + UPDATE SET id = s.id + 1; | ||
| 323 | +-- error | ||
| 324 | +merge into target_table AS t using source_table AS s ON t.id = s.id | ||
| 325 | +WHEN MATCHED THEN | ||
| 326 | + UPDATE SET id = DEFAULT, name = s.name, age = s.age + 1; | ||
| 327 | + | ||
| 328 | +SELECT * from target_table; | ||
| 329 | +DROP TABLE target_table, source_table; | ||
| 330 | + | ||
| 331 | +-- CTE with modifying clause | ||
| 332 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY(START WITH 10) PRIMARY KEY, b text); | ||
| 333 | +CREATE TABLE test_t2 (a int GENERATED BY DEFAULT AS IDENTITY(START WITH 12) PRIMARY KEY, b text); | ||
| 334 | + | ||
| 335 | +-- error | ||
| 336 | +WITH xxx AS ( | ||
| 337 | + INSERT INTO test_t1(a, b) VALUES(1, 'xxxx') RETURNING a | ||
| 338 | +) select * from xxx; | ||
| 339 | +-- success | ||
| 340 | +WITH xxx AS ( | ||
| 341 | + INSERT INTO test_t2(a, b) VALUES(1, 'x1') RETURNING a, b | ||
| 342 | +) select * from xxx; | ||
| 343 | +-- success | ||
| 344 | +WITH xxx AS ( | ||
| 345 | + INSERT INTO test_t1(a, b) OVERRIDING SYSTEM VALUE VALUES(1, 'x1') RETURNING a, b | ||
| 346 | +) select * from xxx; | ||
| 347 | +-- success | ||
| 348 | +WITH xxx AS ( | ||
| 349 | + INSERT INTO test_t1(a, b) OVERRIDING USER VALUE VALUES(3, 'x2') RETURNING a, b | ||
| 350 | +) select * from xxx; -- should be 2 | ||
| 351 | +-- error | ||
| 352 | +WITH xxx AS ( | ||
| 353 | + UPDATE test_t1 SET a = 3 RETURNING a, b | ||
| 354 | +) select * from xxx; | ||
| 355 | +-- error | ||
| 356 | +WITH xxx AS ( | ||
| 357 | + UPDATE test_t2 SET a = 3 RETURNING a, b | ||
| 358 | +) select * from xxx; | ||
| 359 | +-- success | ||
| 360 | +WITH xxx AS ( | ||
| 361 | + UPDATE test_t1 SET a = DEFAULT, b = 't1_updated' RETURNING a, b | ||
| 362 | +) select * from xxx; | ||
| 363 | +-- success | ||
| 364 | +WITH xxx AS ( | ||
| 365 | + UPDATE test_t2 SET a = DEFAULT, b = 't2_updated' RETURNING a, b | ||
| 366 | +) select * from xxx; | ||
| 367 | + | ||
| 368 | +DROP TABLE test_t1, test_t2; | ||
| 369 | + | ||
| 370 | +--## alter table | ||
| 371 | +CREATE TABLE itest4 (a int, b text); | ||
| 372 | +ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL | ||
| 373 | +ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL; | ||
| 374 | +ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- ok | ||
| 375 | +\d+ itest4 | ||
| 376 | + | ||
| 377 | +ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL; -- error, disallowed | ||
| 378 | +ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, already set | ||
| 379 | +ALTER TABLE itest4 ALTER COLUMN b ADD GENERATED ALWAYS AS IDENTITY; -- error, wrong data type | ||
| 380 | + | ||
| 381 | +INSERT INTO itest4 DEFAULT VALUES; | ||
| 382 | +INSERT INTO itest4 DEFAULT VALUES; | ||
| 383 | +SELECT * FROM itest4; | ||
| 384 | + | ||
| 385 | +-- alter add column | ||
| 386 | +CREATE TABLE itest4_1 (a int, b text); | ||
| 387 | +ALTER TABLE itest4_1 ADD COLUMN c text GENERATED ALWAYS AS IDENTITY; -- error | ||
| 388 | +ALTER TABLE itest4_1 ADD COLUMN c int GENERATED ALWAYS AS IDENTITY; -- success | ||
| 389 | +\d+ itest4_1 | ||
| 390 | +INSERT INTO itest4_1 DEFAULT VALUES; | ||
| 391 | +INSERT INTO itest4_1 (c, a, b) VALUES(1, 2, 'x1'); -- error | ||
| 392 | +INSERT INTO itest4_1 (c, a, b) OVERRIDING SYSTEM VALUE VALUES(111, 2, 'x2'); -- success | ||
| 393 | +INSERT INTO itest4_1 (c, a, b) VALUES(DEFAULT, 2, 'x3'); -- success | ||
| 394 | +SELECT c, a, b FROM itest4_1 order by c, b, a; | ||
| 395 | + | ||
| 396 | +CREATE TABLE itest4_2 (a int, b text); | ||
| 397 | +ALTER TABLE itest4_2 ADD COLUMN c text GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 398 | +ALTER TABLE itest4_2 ADD COLUMN c int GENERATED BY DEFAULT AS IDENTITY; -- success | ||
| 399 | +ALTER TABLE itest4_2 ALTER COLUMN a ADD GENERATED BY DEFAULT AS IDENTITY; | ||
| 400 | +\d+ itest4_2 | ||
| 401 | +INSERT INTO itest4_2 DEFAULT VALUES; | ||
| 402 | +INSERT INTO itest4_2 (c, a, b) VALUES(1, 2, 'x1'); -- success | ||
| 403 | +INSERT INTO itest4_2 (c, a, b) OVERRIDING SYSTEM VALUE VALUES(111, 2, 'x2'); -- success | ||
| 404 | +INSERT INTO itest4_2 (c, a, b) VALUES(DEFAULT, 2, 'x3'); -- success | ||
| 405 | +SELECT c, a, b FROM itest4_2 order by c, b, a; | ||
| 406 | + | ||
| 407 | +DROP TABLE itest4_1, itest4_2; | ||
| 408 | + | ||
| 409 | +-- for later | ||
| 410 | +ALTER TABLE itest4 ALTER COLUMN b SET DEFAULT ''; | ||
| 411 | + | ||
| 412 | +-- DROP IDENTITY tests | ||
| 413 | +ALTER TABLE itest4 ALTER COLUMN b DROP IDENTITY; -- error | ||
| 414 | +ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY; | ||
| 415 | +INSERT INTO itest4 VALUES (NULL, 'nullable'); | ||
| 416 | +ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY IF EXISTS; -- noop | ||
| 417 | + | ||
| 418 | +INSERT INTO itest4 DEFAULT VALUES; -- fails because NOT NULL is not dropped | ||
| 419 | +ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL; | ||
| 420 | +INSERT INTO itest4 DEFAULT VALUES; | ||
| 421 | +SELECT * FROM itest4; | ||
| 422 | +-- check that sequence is removed | ||
| 423 | +SELECT sequence_name FROM itest4_a_seq; | ||
| 424 | + | ||
| 425 | +-- fail, not allowed for identity columns | ||
| 426 | +ALTER TABLE itest1 ALTER COLUMN a SET DEFAULT 1; | ||
| 427 | +ALTER TABLE itest1 ALTER COLUMN a DROP DEFAULT; | ||
| 428 | + | ||
| 429 | +-- fail, not allowed, already has a default | ||
| 430 | +CREATE TABLE itest5 (a serial, b text); | ||
| 431 | +ALTER TABLE itest5 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; | ||
| 432 | + | ||
| 433 | +--success | ||
| 434 | +ALTER TABLE itest5 DROP COLUMN a; | ||
| 435 | +SELECT sequence_name FROM itest5_a_seq; | ||
| 436 | +DROP TABLE itest5; | ||
| 437 | + | ||
| 438 | +-- not supported | ||
| 439 | +\d+ itest3 | ||
| 440 | +ALTER TABLE itest3 ALTER COLUMN a TYPE int; | ||
| 441 | +ALTER TABLE itest3 ALTER COLUMN a TYPE text; | ||
| 442 | + | ||
| 443 | +-- ALTER COLUMN ... SET | ||
| 444 | +CREATE TABLE itest6 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 445 | +\d+ itest6 | ||
| 446 | +\d+ itest6_a_seq | ||
| 447 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 448 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 449 | +SELECT * FROM itest6; | ||
| 450 | + | ||
| 451 | +ALTER TABLE itest6 ALTER COLUMN a SET GENERATED BY DEFAULT SET INCREMENT BY 2 SET START WITH 100 RESTART; | ||
| 452 | +\d+ itest6 | ||
| 453 | +\d+ itest6_a_seq | ||
| 454 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 455 | +INSERT INTO itest6 DEFAULT VALUES; | ||
| 456 | +SELECT * FROM itest6; | ||
| 457 | + | ||
| 458 | +SELECT table_name, column_name, is_identity, identity_generation FROM information_schema.columns WHERE table_name = 'itest6' order by 1,2,3,4; | ||
| 459 | + | ||
| 460 | +ALTER TABLE itest6 ALTER COLUMN b SET INCREMENT BY 2; -- fail, not identity | ||
| 461 | + | ||
| 462 | + | ||
| 463 | +-- --## multiple steps in ALTER TABLE | ||
| 464 | +-- cuase transformAlterTableStmt will analysis AT_Column and AT_AddIdentity | ||
| 465 | +-- at same time, when AT_AddIdentity, new column has not been created yet. | ||
| 466 | +-- CREATE TABLE itest5 (a int, b text); | ||
| 467 | +-- ALTER TABLE itest5 | ||
| 468 | +-- ADD COLUMN f21 int NULL, | ||
| 469 | +-- ALTER COLUMN f21 ADD GENERATED ALWAYS AS IDENTITY; | ||
| 470 | + | ||
| 471 | +-- ALTER TABLE itest5 | ||
| 472 | +-- ADD COLUMN f22 int NOT NULL, | ||
| 473 | +-- ALTER COLUMN f22 ADD GENERATED ALWAYS AS IDENTITY; | ||
| 474 | + | ||
| 475 | +--## sequence | ||
| 476 | +-- prohibited direct modification of sequence | ||
| 477 | +ALTER SEQUENCE itest6_a_seq OWNED BY NONE; | ||
| 478 | +CREATE SEQUENCE sequence_testx OWNED BY pg_class_oid_index.oid; -- not a table | ||
| 479 | +CREATE SEQUENCE my_int_seq; | ||
| 480 | +-- not supported 'AS' option | ||
| 481 | +CREATE SEQUENCE my_int_seq AS INTEGER; | ||
| 482 | +-- not supported 'SEQUENCE NAME' option | ||
| 483 | +ALTER SEQUENCE my_int_seq SEQUENCE NAME my_int_seq_1; | ||
| 484 | + | ||
| 485 | +-- alter sequence set schame | ||
| 486 | +create schame seq1; | ||
| 487 | +create sequence s1; | ||
| 488 | +-- error | ||
| 489 | +alter sequence s1 set schame seq1; | ||
| 490 | +alter table seq1 set schame seq1; | ||
| 491 | + | ||
| 492 | +--## truncate | ||
| 493 | +CREATE TABLE truncate_b (id int GENERATED ALWAYS AS IDENTITY (START WITH 44)); | ||
| 494 | + | ||
| 495 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 496 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 497 | +SELECT * FROM truncate_b; | ||
| 498 | + | ||
| 499 | +TRUNCATE truncate_b; | ||
| 500 | + | ||
| 501 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 502 | +INSERT INTO truncate_b DEFAULT VALUES; | ||
| 503 | +SELECT * FROM truncate_b; | ||
| 504 | + | ||
| 505 | +-- not supported | ||
| 506 | +TRUNCATE truncate_b RESTART IDENTITY; | ||
| 507 | + | ||
| 508 | +--## create table like | ||
| 509 | +CREATE TABLE test_like_id_1 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 510 | +\d test_like_id_1 | ||
| 511 | +INSERT INTO test_like_id_1 (b) VALUES ('b1'); | ||
| 512 | +SELECT * FROM test_like_id_1; | ||
| 513 | + | ||
| 514 | +CREATE TABLE test_like_id_2 (LIKE test_like_id_1); | ||
| 515 | +\d test_like_id_2 | ||
| 516 | +INSERT INTO test_like_id_2 (b) VALUES ('b2'); | ||
| 517 | +SELECT * FROM test_like_id_2; -- identity was not copied | ||
| 518 | + | ||
| 519 | +CREATE TABLE test_like_id_3 (LIKE test_like_id_1 INCLUDING IDENTITY); | ||
| 520 | +\d test_like_id_3 | ||
| 521 | +INSERT INTO test_like_id_3 (b) VALUES ('b3'); | ||
| 522 | +SELECT * FROM test_like_id_3; -- identity was copied and applied | ||
| 523 | + | ||
| 524 | +CREATE TABLE test_like_id_4 (like test_like_id_1 INCLUDING DEFAULTS INCLUDING IDENTITY); | ||
| 525 | +\d test_like_id_4 | ||
| 526 | +INSERT INTO test_like_id_4 (b) VALUES ('b3'); | ||
| 527 | +SELECT * FROM test_like_id_4; -- identity was copied and applied | ||
| 528 | + | ||
| 529 | +-- not copy, because no identity column | ||
| 530 | +CREATE TABLE par_row_t1 (a int, b text) partition by range (a) | ||
| 531 | +( | ||
| 532 | + partition par_row_t1_p0 values less than(10), | ||
| 533 | + partition par_row_t1_p3 values less than (maxvalue) | ||
| 534 | +); | ||
| 535 | +CREATE TABLE test_lik3_v (LIKE par_row_t1 INCLUDING IDENTITY); | ||
| 536 | + | ||
| 537 | +DROP TABLE test_like_id_2, test_like_id_3, test_like_id_4, par_row_t1, test_lik3_v; | ||
| 538 | + | ||
| 539 | +--## select into | ||
| 540 | +-- not copy identity | ||
| 541 | +SELECT a, b into test_like_id_5 from test_like_id_1; | ||
| 542 | +\d test_like_id_5 | ||
| 543 | +INSERT INTO test_like_id_5 (b) VALUES ('b1'); | ||
| 544 | +SELECT * FROM test_like_id_5; | ||
| 545 | + | ||
| 546 | +DROP TABLE test_like_id_1, test_like_id_5; | ||
| 547 | + | ||
| 548 | +--## test views | ||
| 549 | +CREATE TABLE itest10 (a int generated by default as identity, b text); | ||
| 550 | +CREATE TABLE itest11 (a int generated always as identity, b text); | ||
| 551 | + | ||
| 552 | +CREATE VIEW itestv10 AS SELECT * FROM itest10; | ||
| 553 | +CREATE VIEW itestv11 AS SELECT * FROM itest11; | ||
| 554 | + | ||
| 555 | +INSERT INTO itestv10 DEFAULT VALUES; | ||
| 556 | +INSERT INTO itestv10 DEFAULT VALUES; | ||
| 557 | + | ||
| 558 | +INSERT INTO itestv11 DEFAULT VALUES; | ||
| 559 | +INSERT INTO itestv11 DEFAULT VALUES; | ||
| 560 | + | ||
| 561 | +SELECT * FROM itestv10; | ||
| 562 | +SELECT * FROM itestv11; | ||
| 563 | + | ||
| 564 | +INSERT INTO itestv10 VALUES (10, 'xyz'); | ||
| 565 | +INSERT INTO itestv10 OVERRIDING USER VALUE VALUES (11, 'xyz'); | ||
| 566 | + | ||
| 567 | +SELECT * FROM itestv10 order by 1, 2; | ||
| 568 | + | ||
| 569 | +INSERT INTO itestv11 VALUES (10, 'xyz'); | ||
| 570 | +INSERT INTO itestv11 OVERRIDING SYSTEM VALUE VALUES (11, 'xyz'); | ||
| 571 | + | ||
| 572 | +SELECT * FROM itestv11 order by 1, 2; | ||
| 573 | + | ||
| 574 | +CREATE TABLE itest12 (a int NOT NULL, b text); | ||
| 575 | +CREATE VIEW itestv12 AS SELECT * FROM itest12; | ||
| 576 | +ALTER VIEW itestv12 alter column a add generated by default as identity; -- error | ||
| 577 | +ALTER VIEW itestv12 alter column a SET NOT NULL; -- error | ||
| 578 | +ALTER VIEW itestv12 alter column a RESTART; -- error | ||
| 579 | +ALTER VIEW itestv12 alter column a drop identity; -- error | ||
| 580 | +\d+ itestv12; | ||
| 581 | + | ||
| 582 | +DROP VIEW itestv12; | ||
| 583 | +DROP TABLE itest12; | ||
| 584 | + | ||
| 585 | +--## ALTER xxx alter column add/set/drop identity | ||
| 586 | +create sequence saaa; | ||
| 587 | +ALTER SEQUENCE saaa alter column max_value add generated always as identity; -- error | ||
| 588 | +create table test_t1 (a int); | ||
| 589 | +create materialized view mv as select * from test_t1; | ||
| 590 | +ALTER MATERIALIZED VIEW mv alter column a add generated by default as identity; -- error | ||
| 591 | + | ||
| 592 | +DROP TABLE test_t1; | ||
| 593 | +DROP sequence saaa; | ||
| 594 | +DROP MATERIALIZED VIEW mv; | ||
| 595 | + | ||
| 596 | +--## inheritance | ||
| 597 | +CREATE TABLE itest7 (a int GENERATED ALWAYS AS IDENTITY (maxvalue 100)); | ||
| 598 | +INSERT INTO itest7 DEFAULT VALUES; | ||
| 599 | +SELECT * FROM itest7; | ||
| 600 | + | ||
| 601 | +-- identity property is not inherited | ||
| 602 | +CREATE TABLE itest7a (b text) INHERITS (itest7); | ||
| 603 | +\d+ itest7a | ||
| 604 | + | ||
| 605 | +-- make column identity in child table | ||
| 606 | +CREATE TABLE itest7b (a int); | ||
| 607 | +CREATE TABLE itest7c (a int GENERATED ALWAYS AS IDENTITY) INHERITS (itest7b); | ||
| 608 | +INSERT INTO itest7c DEFAULT VALUES; | ||
| 609 | +SELECT * FROM itest7c; | ||
| 610 | + | ||
| 611 | +CREATE TABLE itest7d (a int not null); | ||
| 612 | +CREATE TABLE itest7e () INHERITS (itest7d); | ||
| 613 | +ALTER TABLE itest7d ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; | ||
| 614 | +ALTER TABLE itest7d ADD COLUMN b int GENERATED ALWAYS AS IDENTITY; -- error | ||
| 615 | + | ||
| 616 | +SELECT table_name, column_name, is_nullable, is_identity, identity_generation FROM information_schema.columns WHERE table_name LIKE 'itest7%' ORDER BY 1, 2; | ||
| 617 | + | ||
| 618 | +-- These ALTER TABLE variants will not recurse. | ||
| 619 | +ALTER TABLE itest7 ALTER COLUMN a SET GENERATED BY DEFAULT; | ||
| 620 | +ALTER TABLE itest7 ALTER COLUMN a RESTART; | ||
| 621 | +ALTER TABLE itest7 ALTER COLUMN a DROP IDENTITY; | ||
| 622 | + | ||
| 623 | +--## pbe | ||
| 624 | +CREATE TABLE test_t1 (a int PRIMARY KEY GENERATED ALWAYS AS IDENTITY , b text); | ||
| 625 | +INSERT INTO test_t1 VALUES (DEFAULT, default); | ||
| 626 | +PREPARE p1 AS INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES($1, $2); -- error | ||
| 627 | +PREPARE p2 AS INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES($1, $2) ON DUPLICATE KEY UPDATE b = $3; -- success | ||
| 628 | +EXECUTE p1 (1, 'xxxx'); | ||
| 629 | +EXECUTE p2 (1, 'setb', 'setb'); | ||
| 630 | +DEALLOCATE p1; | ||
| 631 | +DEALLOCATE p2; | ||
| 632 | +select * from test_t1 order by 1, 2; | ||
| 633 | +DROP TABLE test_t1; | ||
| 634 | + | ||
| 635 | +--## partitoned table | ||
| 636 | +CREATE TABLE par_row_t1 (a int, b text) partition by range (a) | ||
| 637 | +( | ||
| 638 | + partition par_row_t1_p0 values less than(10), | ||
| 639 | + partition par_row_t1_p3 values less than (maxvalue) | ||
| 640 | +); | ||
| 641 | +ALTER TABLE par_row_t1 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error | ||
| 642 | +ALTER TABLE par_row_t1 ALTER COLUMN a SET GENERATED BY DEFAULT; -- error | ||
| 643 | +ALTER TABLE par_row_t1 ALTER COLUMN a DROP IDENTITY; -- error | ||
| 644 | +ALTER TABLE par_row_t1 ALTER COLUMN a DROP IDENTITY IF EXISTS; -- error | ||
| 645 | +ALTER TABLE par_row_t1 ADD COLUMN c int GENERATED ALWAYS AS IDENTITY; -- error | ||
| 646 | + | ||
| 647 | +DROP TABLE par_row_t1; | ||
| 648 | + | ||
| 649 | +--## NUMERIC test | ||
| 650 | +CREATE TABLE test_t1 (a NUMERIC(12, 0) PRIMARY KEY GENERATED ALWAYS AS IDENTITY(START WITH 1010101010101), b text); -- error | ||
| 651 | +CREATE TABLE test_t1 (a NUMERIC(12, 0) PRIMARY KEY GENERATED ALWAYS AS IDENTITY(START WITH 101010101010), b text); | ||
| 652 | +INSERT INTO test_t1 DEFAULT VALUES; -- success | ||
| 653 | +INSERT INTO test_t1 VALUES(101010101010, 'xxxx'); -- error | ||
| 654 | +INSERT INTO test_t1 OVERRIDING SYSTEM VALUE VALUES(10, 'xxxx'); -- success | ||
| 655 | +INSERT INTO test_t1 OVERRIDING USER VALUE VALUES(10, 'xxxx'); -- success | ||
| 656 | +SELECT * from test_t1 order by 1, 2; | ||
| 657 | + | ||
| 658 | +DROP TABLE test_t1; | ||
| 659 | + | ||
| 660 | +--## ALTER SET IDENTITY tests | ||
| 661 | +CREATE TABLE test_t1 (a int GENERATED ALWAYS AS IDENTITY(START WITH 4) , b text); | ||
| 662 | +\d+ test_t1_a_seq; | ||
| 663 | +ALTER TABLE test_t1 ALTER COLUMN a SET START WITH 10; -- success | ||
| 664 | +ALTER TABLE test_t1 ALTER COLUMN a SET INCREMENT BY 2; -- success | ||
| 665 | +\d+ test_t1_a_seq; | ||
| 666 | +ALTER TABLE test_t1 ALTER COLUMN a SET MAXVALUE 3; -- error | ||
| 667 | +ALTER TABLE test_t1 ALTER COLUMN a SET MAXVALUE 20; -- success | ||
| 668 | +ALTER TABLE test_t1 ALTER COLUMN a SET MINVALUE 9; -- error | ||
| 669 | +ALTER TABLE test_t1 ALTER COLUMN a SET MINVALUE 2; -- success | ||
| 670 | +ALTER TABLE test_t1 ALTER COLUMN a SET RESTART; -- error | ||
| 671 | +ALTER TABLE test_t1 ALTER COLUMN a SET NOCYCLE; -- success | ||
| 672 | +ALTER TABLE test_t1 ALTER COLUMN a SET CACHE 10; -- success | ||
| 673 | +\d+ test_t1_a_seq; | ||
| 674 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 675 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 676 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 677 | +ALTER TABLE test_t1 ALTER COLUMN a SET NOMAXVALUE; -- success | ||
| 678 | +ALTER TABLE test_t1 ALTER COLUMN a SET NOMINVALUE; -- success | ||
| 679 | +ALTER TABLE test_t1 ALTER COLUMN a RESTART WITH 10; -- success | ||
| 680 | +ALTER TABLE test_t1 ALTER COLUMN a RESTART 11111; -- success | ||
| 681 | +ALTER TABLE test_t1 ALTER COLUMN a SET RESTART 11; -- error | ||
| 682 | +\d+ test_t1_a_seq; | ||
| 683 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 684 | +INSERT INTO test_t1 DEFAULT VALUES; | ||
| 685 | +SELECT * FROM test_t1 order by 1, 2; | ||
| 686 | + | ||
| 687 | +-- numeric/large sequence | ||
| 688 | +create table test_t2 (a int generated always as identity, b int); | ||
| 689 | +alter table test_t2 add column c numeric(20, 0) generated by default as identity; | ||
| 690 | +alter table test_t2 alter column c set generated always set start with 10; | ||
| 691 | +\d+ test_t2 | ||
| 692 | + | ||
| 693 | +DROP sequence test_t1_a_seq; -- error | ||
| 694 | +DROP TABLE test_t1, test_t2; | ||
| 695 | + | ||
| 696 | +--## privileges | ||
| 697 | +CREATE USER regress_user1 WITH PASSWORD 'Gauss@123'; | ||
| 698 | +CREATE TABLE itest8 (a int GENERATED ALWAYS AS IDENTITY, b text); | ||
| 699 | +GRANT SELECT, INSERT ON itest8 TO regress_user1; | ||
| 700 | +SET ROLE regress_user1 PASSWORD 'Gauss@123'; | ||
| 701 | +INSERT INTO itest8 DEFAULT VALUES; | ||
| 702 | +SELECT * FROM itest8; | ||
| 703 | +RESET ROLE; | ||
| 704 | +DROP TABLE itest8; | ||
| 705 | +DROP USER regress_user1; | ||
| 706 | + | ||
| 707 | +--## gs_catalog_attribute_records | ||
| 708 | +SELECT attidentity, attnum FROM gs_catalog_attribute_records(1249) ORDER BY attnum LIMIT 1; | ||
| 709 | + | ||
| 710 | +--## with identity for d | ||
| 711 | +CREATE TABLE book(bookId int IDENTITY GENERATED BY DEFAULT AS IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); -- error | ||
| 712 | +CREATE TABLE book(bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50), c int GENERATED BY DEFAULT AS IDENTITY); -- error | ||
| 713 | +CREATE TABLE book(bookId int IDENTITY, bookname NVARCHAR(50), author NVARCHAR(50)); -- success | ||
| 714 | +ALTER TABLE book ADD column c int IDENTITY; -- error | ||
| 715 | +ALTER TABLE book ADD column c int GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 716 | +ALTER TABLE book ADD column c int IDENTITY(1, 20); -- error | ||
| 717 | +ALTER TABLE book ADD column note NVARCHAR(50); -- success | ||
| 718 | +ALTER TABLE book DROP column note; -- success | ||
| 719 | + | ||
| 720 | +ALTER TABLE book ALTER column c Add GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 721 | +ALTER TABLE book ALTER column bookId Add GENERATED BY DEFAULT AS IDENTITY; -- error | ||
| 722 | +ALTER TABLE book ALTER column bookId ADD IDENTITY(1, 20); -- error | ||
| 723 | + | ||
| 724 | +ALTER TABLE book ALTER COLUMN bookname SET GENERATED BY DEFAULT; -- error | ||
| 725 | +ALTER TABLE book ALTER COLUMN bookId SET GENERATED BY DEFAULT; -- error | ||
| 726 | + | ||
| 727 | +ALTER TABLE book ALTER COLUMN bookId DROP IDENTITY; -- error | ||
| 728 | +ALTER TABLE book ALTER COLUMN bookname DROP IDENTITY; -- error | ||
| 729 | + | ||
| 730 | +ALTER TABLE book ALTER COLUMN bookId DROP DEFAULT; -- error | ||
| 731 | +ALTER TABLE book ALTER COLUMN bookId DROP NOT NULL; -- error | ||
| 732 | + | ||
| 733 | +ALTER SEQUENCE book_bookid_seq_identity OWNED BY NONE; -- success | ||
| 734 | +ALTER SEQUENCE book_bookid_seq_identity OWNED BY book.bookId; -- success | ||
| 735 | + | ||
| 736 | +\d+ book | ||
| 737 | +set identity_insert = off; | ||
| 738 | +INSERT INTO book VALUES (2, 'xxx', 'xxx'); -- error; | ||
| 739 | +INSERT INTO book (bookid, bookname, author) VALUES(11111, 'xxxx', 'xxx'); -- error, turn on identity_insert or use OVERRIDING | ||
| 740 | +INSERT INTO book (bookid, bookname, author) OVERRIDING SYSTEM VALUE VALUES (33, 'xxx', 'xxx'); -- success; -- 33 | ||
| 741 | +INSERT INTO book (bookid, bookname, author) OVERRIDING USER VALUE VALUES (11111, 'xxx', 'xxx'); -- success; -- 1 | ||
| 742 | +set identity_insert = on; | ||
| 743 | +INSERT INTO book VALUES (44, 'xxx', 'xxx'); -- error; | ||
| 744 | +INSERT INTO book (bookid, bookname, author) OVERRIDING USER VALUE VALUES (11111, 'xxx', 'xxx'); -- success; -- 2 | ||
| 745 | +INSERT INTO book (bookid, bookname, author) OVERRIDING SYSTEM VALUE VALUES (55, 'xxx', 'xxx'); -- success; -- 55 | ||
| 746 | +set identity_insert = off; | ||
| 747 | +select * from book order by 1, 2; | ||
| 748 | + | ||
| 749 | +CREATE TABLE book1(b int1 GENERATED BY DEFAULT AS IDENTITY(START WITH 4), c int GENERATED BY DEFAULT AS IDENTITY (START WITH 5)); -- success | ||
| 750 | +ALTER TABLE book1 ADD column c int IDENTITY; -- error | ||
| 751 | +ALTER TABLE book1 ADD column a int IDENTITY(6, 2); -- success | ||
| 752 | +ALTER TABLE book1 ADD column d int IDENTITY; -- error | ||
| 753 | +insert into book1 values (1, 2, 3); -- error, cam't specify the value of the identity column | ||
| 754 | +set identity_insert = on; -- need to specify column names and turn on identity_insert | ||
| 755 | +insert into book1 values (1, 2, 3); -- error | ||
| 756 | +insert into book1(b, c, a) values (DEFAULT, DEFAULT, DEFAULT); -- success | ||
| 757 | +select * from book1 order by 1, 2, 3; | ||
| 758 | +set identity_insert = off; | ||
| 759 | + | ||
| 760 | +select pg_get_tabledef('book'::regclass::oid); | ||
| 761 | +select pg_get_tabledef('book1'::regclass::oid); | ||
| 762 | + | ||
| 763 | +\c postgres | ||
| 764 | +DROP DATABASE s_test_pg_identity; | ||
| @@ -28,7 +28,7 @@ | |||
| 28 | */ | 28 | */ |
| 29 | %type <list> tsql_stmtmulti | 29 | %type <list> tsql_stmtmulti |
| 30 | 30 | ||
| 31 | -%type <list> identity_seed_increment | 31 | +%type <list> identity_seed_increment identity_seq_options |
| 32 | %type <node> DBCCCheckIdentStmt DBCCStmt tsql_stmt tsql_CreateProcedureStmt tsql_IndexStmt tsql_TransactionStmt tsql_InsertStmt | 32 | %type <node> DBCCCheckIdentStmt DBCCStmt tsql_stmt tsql_CreateProcedureStmt tsql_IndexStmt tsql_TransactionStmt tsql_InsertStmt |
| 33 | %type <node> tsql_UseStmt | 33 | %type <node> tsql_UseStmt |
| 34 | %token <keyword> CATCH CHECKIDENT DBCC NO_INFOMSGS NORESEED RESEED SAVE TRAN TRY TSQL_CLUSTERED TSQL_NONCLUSTERED TSQL_COLUMNSTORE TSQL_PERSISTED TSQL_TOP TSQL_PERCENT | 34 | %token <keyword> CATCH CHECKIDENT DBCC NO_INFOMSGS NORESEED RESEED SAVE TRAN TRY TSQL_CLUSTERED TSQL_NONCLUSTERED TSQL_COLUMNSTORE TSQL_PERSISTED TSQL_TOP TSQL_PERCENT |