alter_table.out 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. --
  2. -- ALTER_TABLE
  3. --
  4. -- Clean up in case a prior regression run failed
  5. SET client_min_messages TO 'warning';
  6. RESET client_min_messages;
  7. --
  8. -- add attribute
  9. --
  10. CREATE TABLE attmp (initial int4);
  11. INSERT INTO attmp (a, b, c, d, e, f, g, i, k, l, m, n, p, q, r, s, t,
  12. v, w, x, y, z)
  13. VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
  14. 'c',
  15. 314159, '(1,1)', '512',
  16. '1 2 3 4 5 6 7 8', true, '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
  17. '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)',
  18. 'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
  19. DROP TABLE attmp;
  20. -- the wolf bug - schema mods caused inconsistent row descriptors
  21. CREATE TABLE attmp (
  22. initial int4
  23. );
  24. INSERT INTO attmp (a, b, c, d, e, f, g, i, k, l, m, n, p, q, r, s, t,
  25. v, w, x, y, z)
  26. VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
  27. 'c',
  28. 314159, '(1,1)', '512',
  29. '1 2 3 4 5 6 7 8', true, '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
  30. '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)',
  31. 'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
  32. ALTER INDEX attmp_idx ALTER COLUMN 0 SET STATISTICS 1000;
  33. ERROR: column number must be in range from 1 to 32767
  34. LINE 1: ALTER INDEX attmp_idx ALTER COLUMN 0 SET STATISTICS 1000;
  35. ^
  36. DROP TABLE attmp;
  37. --
  38. -- rename - check on both non-temp and temp tables
  39. --
  40. CREATE TABLE attmp (regtable int);
  41. CREATE TEMP TABLE attmp (attmptable int);
  42. SELECT * FROM attmp;
  43. regtable
  44. ----------
  45. (0 rows)
  46. --
  47. -- check renaming to a table's array type's autogenerated name
  48. -- (the array type's name should get out of the way)
  49. --
  50. CREATE TABLE attmp_array (id int);
  51. CREATE TABLE attmp_array2 (id int);
  52. DROP TABLE attmp_array;
  53. -- renaming to table's own array type's name is an interesting corner case
  54. CREATE TABLE attmp_array (id int);
  55. -- rename statements with mismatching statement and object types
  56. CREATE TABLE alter_idx_rename_test (a INT);
  57. CREATE INDEX alter_idx_rename_test_idx ON alter_idx_rename_test (a);
  58. CREATE INDEX alter_idx_rename_test_parted_idx ON alter_idx_rename_test_parted (a);
  59. BEGIN;
  60. COMMIT;
  61. BEGIN;
  62. COMMIT;
  63. BEGIN;
  64. COMMIT;
  65. -- FOREIGN KEY CONSTRAINT adding TEST
  66. CREATE TABLE attmp2 (a int primary key);
  67. CREATE TABLE attmp3 (a int, b int);
  68. CREATE TABLE attmp4 (a int, b int, unique(a,b));
  69. CREATE TABLE attmp5 (a int, b int);
  70. -- Insert rows into attmp2 (pktable)
  71. INSERT INTO attmp2 values (1);
  72. INSERT INTO attmp2 values (2);
  73. INSERT INTO attmp2 values (3);
  74. INSERT INTO attmp2 values (4);
  75. -- Insert rows into attmp3
  76. INSERT INTO attmp3 values (1,10);
  77. INSERT INTO attmp3 values (1,20);
  78. INSERT INTO attmp3 values (5,50);
  79. INSERT INTO attmp3 values (5,50);
  80. -- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
  81. create table parent_noinh_convalid (a int);
  82. insert into parent_noinh_convalid values (1);
  83. DROP TABLE attmp5;
  84. DROP TABLE attmp4;
  85. DROP TABLE attmp3;
  86. DROP TABLE attmp2;
  87. -- we leave nv_parent and children around to help test pg_dump logic
  88. -- Foreign key adding test with mixed types
  89. -- Note: these tables are TEMP to avoid name conflicts when this test
  90. -- is run in parallel with foreign_key.sql.
  91. CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
  92. INSERT INTO PKTABLE VALUES(42);
  93. CREATE TEMP TABLE FKTABLE (ftest1 inet);
  94. DROP TABLE FKTABLE;
  95. -- This should succeed, even though they are different types,
  96. -- because int=int8 exists and is a member of the integer opfamily
  97. CREATE TEMP TABLE FKTABLE (ftest1 int8);
  98. -- Check it actually works
  99. INSERT INTO FKTABLE VALUES(42); -- should succeed
  100. DROP TABLE FKTABLE;
  101. -- This should fail, because we'd have to cast numeric to int which is
  102. -- not an implicit coercion (or use numeric=numeric, but that's not part
  103. -- of the integer opfamily)
  104. CREATE TEMP TABLE FKTABLE (ftest1 numeric);
  105. DROP TABLE FKTABLE;
  106. DROP TABLE PKTABLE;
  107. -- On the other hand, this should work because int implicitly promotes to
  108. -- numeric, and we allow promotion on the FK side
  109. CREATE TEMP TABLE PKTABLE (ptest1 numeric PRIMARY KEY);
  110. INSERT INTO PKTABLE VALUES(42);
  111. CREATE TEMP TABLE FKTABLE (ftest1 int);
  112. -- Check it actually works
  113. INSERT INTO FKTABLE VALUES(42); -- should succeed
  114. DROP TABLE FKTABLE;
  115. DROP TABLE PKTABLE;
  116. CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
  117. PRIMARY KEY(ptest1, ptest2));
  118. -- This should fail, because we just chose really odd types
  119. CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
  120. DROP TABLE FKTABLE;
  121. -- Again, so should this...
  122. CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
  123. DROP TABLE FKTABLE;
  124. -- This fails because we mixed up the column ordering
  125. CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
  126. DROP TABLE FKTABLE;
  127. DROP TABLE PKTABLE;
  128. -- Test that ALTER CONSTRAINT updates trigger deferrability properly
  129. CREATE TEMP TABLE PKTABLE (ptest1 int primary key);
  130. CREATE TEMP TABLE FKTABLE (ftest1 int);
  131. -- temp tables should go away by themselves, need not drop them.
  132. -- test check constraint adding
  133. create table atacc1 ( test int );
  134. -- should succeed
  135. insert into atacc1 (test) values (4);
  136. drop table atacc1;
  137. -- let's do one where the check fails when added
  138. create table atacc1 ( test int );
  139. -- insert a soon to be failing row
  140. insert into atacc1 (test) values (2);
  141. insert into atacc1 (test) values (4);
  142. drop table atacc1;
  143. -- let's do one where the check fails because the column doesn't exist
  144. create table atacc1 ( test int );
  145. drop table atacc1;
  146. -- something a little more complicated
  147. create table atacc1 ( test int, test2 int, test3 int);
  148. -- should succeed
  149. insert into atacc1 (test,test2,test3) values (4,4,5);
  150. drop table atacc1;
  151. -- inheritance related tests
  152. create table atacc1 (test int);
  153. create table atacc2 (test2 int);
  154. insert into atacc2 (test2) values (3);
  155. drop table atacc2;
  156. drop table atacc1;
  157. -- same things with one created with INHERIT
  158. create table atacc1 (test int);
  159. create table atacc2 (test2 int);
  160. select test2 from atacc2;
  161. test2
  162. -------
  163. (0 rows)
  164. drop table atacc1;
  165. -- adding only to a parent is allowed as of 9.2
  166. create table atacc1 (test int);
  167. -- check constraint is not there on child
  168. insert into atacc2 (test) values (-3);
  169. insert into atacc1 (test) values (3);
  170. drop table atacc2;
  171. drop table atacc1;
  172. -- test unique constraint adding
  173. create table atacc1 ( test int ) ;
  174. -- insert first value
  175. insert into atacc1 (test) values (2);
  176. -- should succeed
  177. insert into atacc1 (test) values (4);
  178. drop table atacc1;
  179. -- let's do one where the unique constraint fails when added
  180. create table atacc1 ( test int );
  181. -- insert soon to be failing rows
  182. insert into atacc1 (test) values (2);
  183. insert into atacc1 (test) values (2);
  184. insert into atacc1 (test) values (3);
  185. drop table atacc1;
  186. -- let's do one where the unique constraint fails
  187. -- because the column doesn't exist
  188. create table atacc1 ( test int );
  189. drop table atacc1;
  190. -- something a little more complicated
  191. create table atacc1 ( test int, test2 int);
  192. -- insert initial value
  193. insert into atacc1 (test,test2) values (4,4);
  194. -- should all succeed
  195. insert into atacc1 (test,test2) values (4,5);
  196. insert into atacc1 (test,test2) values (5,4);
  197. insert into atacc1 (test,test2) values (5,5);
  198. drop table atacc1;
  199. -- lets do some naming tests
  200. create table atacc1 (test int, test2 int, unique(test));
  201. -- should fail for @@ second one @@
  202. insert into atacc1 (test2, test) values (3, 3);
  203. drop table atacc1;
  204. -- test primary key constraint adding
  205. create table atacc1 ( id serial, test int) ;
  206. -- insert first value
  207. insert into atacc1 (test) values (2);
  208. -- should succeed
  209. insert into atacc1 (test) values (4);
  210. drop table atacc1;
  211. -- let's do one where the primary key constraint fails when added
  212. create table atacc1 ( test int );
  213. -- insert soon to be failing rows
  214. insert into atacc1 (test) values (2);
  215. insert into atacc1 (test) values (2);
  216. insert into atacc1 (test) values (3);
  217. drop table atacc1;
  218. -- let's do another one where the primary key constraint fails when added
  219. create table atacc1 ( test int );
  220. -- insert soon to be failing row
  221. insert into atacc1 (test) values (NULL);
  222. insert into atacc1 (test) values (3);
  223. drop table atacc1;
  224. -- let's do one where the primary key constraint fails
  225. -- because the column doesn't exist
  226. create table atacc1 ( test int );
  227. drop table atacc1;
  228. -- adding a new column as primary key to a non-empty table.
  229. -- should fail unless the column has a non-null default value.
  230. create table atacc1 ( test int );
  231. insert into atacc1 (test) values (0);
  232. drop table atacc1;
  233. -- this combination used to have order-of-execution problems (bug #15580)
  234. create table atacc1 (a int);
  235. insert into atacc1 values(1);
  236. drop table atacc1;
  237. -- additionally, we've seen issues with foreign key validation not being
  238. -- properly delayed until after a table rewrite. Check that works ok.
  239. create table atacc1 (a int primary key);
  240. drop table atacc1;
  241. -- we've also seen issues with check constraints being validated at the wrong
  242. -- time when there's a pending table rewrite.
  243. create table atacc1 (a bigint, b int);
  244. insert into atacc1 values(1,1);
  245. drop table atacc1;
  246. -- same as above, but ensure the constraint violation is detected
  247. create table atacc1 (a bigint, b int);
  248. insert into atacc1 values(1,2);
  249. drop table atacc1;
  250. -- something a little more complicated
  251. create table atacc1 ( test int, test2 int);
  252. -- insert initial value
  253. insert into atacc1 (test,test2) values (4,4);
  254. -- should all succeed
  255. insert into atacc1 (test,test2) values (4,5);
  256. insert into atacc1 (test,test2) values (5,4);
  257. insert into atacc1 (test,test2) values (5,5);
  258. drop table atacc1;
  259. -- lets do some naming tests
  260. create table atacc1 (test int, test2 int, primary key(test));
  261. -- only first should succeed
  262. insert into atacc1 (test2, test) values (3, 3);
  263. drop table atacc1;
  264. -- test setting columns to null and not null and vice versa
  265. -- test checking for null values and primary key
  266. create table atacc1 (test int not null);
  267. insert into atacc1 values (null);
  268. drop table atacc1;
  269. -- set not null verified by constraints
  270. create table atacc1 (test_a int, test_b int);
  271. insert into atacc1 values (null, 1);
  272. insert into atacc1 values (2, null);
  273. drop table atacc1;
  274. -- test inheritance
  275. create table parent (a int);
  276. insert into parent values (NULL);
  277. drop table parent;
  278. -- test setting and removing default values
  279. create table def_test (
  280. c1 int4 default 5,
  281. c2 text default 'initial_default'
  282. );