alter_table.sql 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030
  1. --
  2. -- ALTER_TABLE
  3. --
  4. -- Clean up in case a prior regression run failed
  5. SET client_min_messages TO 'warning';
  6. DROP ROLE IF EXISTS regress_alter_table_user1;
  7. RESET client_min_messages;
  8. CREATE USER regress_alter_table_user1;
  9. --
  10. -- add attribute
  11. --
  12. CREATE TABLE attmp (initial int4);
  13. COMMENT ON TABLE attmp_wrong IS 'table comment';
  14. COMMENT ON TABLE attmp IS 'table comment';
  15. COMMENT ON TABLE attmp IS NULL;
  16. ALTER TABLE attmp ADD COLUMN xmin integer; -- fails
  17. ALTER TABLE attmp ADD COLUMN a int4 default 3;
  18. ALTER TABLE attmp ADD COLUMN b name;
  19. ALTER TABLE attmp ADD COLUMN c text;
  20. ALTER TABLE attmp ADD COLUMN d float8;
  21. ALTER TABLE attmp ADD COLUMN e float4;
  22. ALTER TABLE attmp ADD COLUMN f int2;
  23. ALTER TABLE attmp ADD COLUMN g polygon;
  24. ALTER TABLE attmp ADD COLUMN i char;
  25. ALTER TABLE attmp ADD COLUMN k int4;
  26. ALTER TABLE attmp ADD COLUMN l tid;
  27. ALTER TABLE attmp ADD COLUMN m xid;
  28. ALTER TABLE attmp ADD COLUMN n oidvector;
  29. --ALTER TABLE attmp ADD COLUMN o lock;
  30. ALTER TABLE attmp ADD COLUMN p boolean;
  31. ALTER TABLE attmp ADD COLUMN q point;
  32. ALTER TABLE attmp ADD COLUMN r lseg;
  33. ALTER TABLE attmp ADD COLUMN s path;
  34. ALTER TABLE attmp ADD COLUMN t box;
  35. ALTER TABLE attmp ADD COLUMN v timestamp;
  36. ALTER TABLE attmp ADD COLUMN w interval;
  37. ALTER TABLE attmp ADD COLUMN x float8[];
  38. ALTER TABLE attmp ADD COLUMN y float4[];
  39. ALTER TABLE attmp ADD COLUMN z int2[];
  40. INSERT INTO attmp (a, b, c, d, e, f, g, i, k, l, m, n, p, q, r, s, t,
  41. v, w, x, y, z)
  42. VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
  43. 'c',
  44. 314159, '(1,1)', '512',
  45. '1 2 3 4 5 6 7 8', true, '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
  46. '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)',
  47. 'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
  48. SELECT * FROM attmp;
  49. DROP TABLE attmp;
  50. -- the wolf bug - schema mods caused inconsistent row descriptors
  51. CREATE TABLE attmp (
  52. initial int4
  53. );
  54. ALTER TABLE attmp ADD COLUMN a int4;
  55. ALTER TABLE attmp ADD COLUMN b name;
  56. ALTER TABLE attmp ADD COLUMN c text;
  57. ALTER TABLE attmp ADD COLUMN d float8;
  58. ALTER TABLE attmp ADD COLUMN e float4;
  59. ALTER TABLE attmp ADD COLUMN f int2;
  60. ALTER TABLE attmp ADD COLUMN g polygon;
  61. ALTER TABLE attmp ADD COLUMN i char;
  62. ALTER TABLE attmp ADD COLUMN k int4;
  63. ALTER TABLE attmp ADD COLUMN l tid;
  64. ALTER TABLE attmp ADD COLUMN m xid;
  65. ALTER TABLE attmp ADD COLUMN n oidvector;
  66. --ALTER TABLE attmp ADD COLUMN o lock;
  67. ALTER TABLE attmp ADD COLUMN p boolean;
  68. ALTER TABLE attmp ADD COLUMN q point;
  69. ALTER TABLE attmp ADD COLUMN r lseg;
  70. ALTER TABLE attmp ADD COLUMN s path;
  71. ALTER TABLE attmp ADD COLUMN t box;
  72. ALTER TABLE attmp ADD COLUMN v timestamp;
  73. ALTER TABLE attmp ADD COLUMN w interval;
  74. ALTER TABLE attmp ADD COLUMN x float8[];
  75. ALTER TABLE attmp ADD COLUMN y float4[];
  76. ALTER TABLE attmp ADD COLUMN z int2[];
  77. INSERT INTO attmp (a, b, c, d, e, f, g, i, k, l, m, n, p, q, r, s, t,
  78. v, w, x, y, z)
  79. VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
  80. 'c',
  81. 314159, '(1,1)', '512',
  82. '1 2 3 4 5 6 7 8', true, '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
  83. '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)',
  84. 'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
  85. SELECT * FROM attmp;
  86. CREATE INDEX attmp_idx ON attmp (a, (d + e), b);
  87. ALTER INDEX attmp_idx ALTER COLUMN 0 SET STATISTICS 1000;
  88. ALTER INDEX attmp_idx ALTER COLUMN 1 SET STATISTICS 1000;
  89. ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS 1000;
  90. \d+ attmp_idx
  91. ALTER INDEX attmp_idx ALTER COLUMN 3 SET STATISTICS 1000;
  92. ALTER INDEX attmp_idx ALTER COLUMN 4 SET STATISTICS 1000;
  93. ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS -1;
  94. DROP TABLE attmp;
  95. -- fails with incorrect object type
  96. CREATE VIEW at_v1 AS SELECT 1 as a;
  97. ALTER TABLE at_v1 ALTER COLUMN a SET STATISTICS 0;
  98. DROP VIEW at_v1;
  99. --
  100. -- rename - check on both non-temp and temp tables
  101. --
  102. CREATE TABLE attmp (regtable int);
  103. CREATE TEMP TABLE attmp (attmptable int);
  104. ALTER TABLE attmp RENAME TO attmp_new;
  105. SELECT * FROM attmp;
  106. SELECT * FROM attmp_new;
  107. ALTER TABLE attmp RENAME TO attmp_new2;
  108. SELECT * FROM attmp; -- should fail
  109. SELECT * FROM attmp_new;
  110. SELECT * FROM attmp_new2;
  111. DROP TABLE attmp_new;
  112. DROP TABLE attmp_new2;
  113. -- check rename of partitioned tables and indexes also
  114. CREATE TABLE part_attmp (a int primary key) partition by range (a);
  115. CREATE TABLE part_attmp1 PARTITION OF part_attmp FOR VALUES FROM (0) TO (100);
  116. ALTER INDEX part_attmp_pkey RENAME TO part_attmp_index;
  117. ALTER INDEX part_attmp1_pkey RENAME TO part_attmp1_index;
  118. ALTER TABLE part_attmp RENAME TO part_at2tmp;
  119. ALTER TABLE part_attmp1 RENAME TO part_at2tmp1;
  120. SET ROLE regress_alter_table_user1;
  121. ALTER INDEX part_attmp_index RENAME TO fail;
  122. ALTER INDEX part_attmp1_index RENAME TO fail;
  123. ALTER TABLE part_at2tmp RENAME TO fail;
  124. ALTER TABLE part_at2tmp1 RENAME TO fail;
  125. RESET ROLE;
  126. DROP TABLE part_at2tmp;
  127. --
  128. -- check renaming to a table's array type's autogenerated name
  129. -- (the array type's name should get out of the way)
  130. --
  131. CREATE TABLE attmp_array (id int);
  132. CREATE TABLE attmp_array2 (id int);
  133. SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
  134. SELECT typname FROM pg_type WHERE oid = 'attmp_array2[]'::regtype;
  135. ALTER TABLE attmp_array2 RENAME TO _attmp_array;
  136. SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
  137. SELECT typname FROM pg_type WHERE oid = '_attmp_array[]'::regtype;
  138. DROP TABLE _attmp_array;
  139. DROP TABLE attmp_array;
  140. -- renaming to table's own array type's name is an interesting corner case
  141. CREATE TABLE attmp_array (id int);
  142. SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
  143. ALTER TABLE attmp_array RENAME TO _attmp_array;
  144. SELECT typname FROM pg_type WHERE oid = '_attmp_array[]'::regtype;
  145. DROP TABLE _attmp_array;
  146. -- ALTER TABLE ... RENAME on non-table relations
  147. -- renaming indexes (FIXME: this should probably test the index's functionality)
  148. ALTER INDEX IF EXISTS __onek_unique1 RENAME TO attmp_onek_unique1;
  149. ALTER INDEX IF EXISTS __attmp_onek_unique1 RENAME TO onek_unique1;
  150. ALTER INDEX onek_unique1 RENAME TO attmp_onek_unique1;
  151. ALTER INDEX attmp_onek_unique1 RENAME TO onek_unique1;
  152. SET ROLE regress_alter_table_user1;
  153. ALTER INDEX onek_unique1 RENAME TO fail; -- permission denied
  154. RESET ROLE;
  155. -- rename statements with mismatching statement and object types
  156. CREATE TABLE alter_idx_rename_test (a INT);
  157. CREATE INDEX alter_idx_rename_test_idx ON alter_idx_rename_test (a);
  158. CREATE TABLE alter_idx_rename_test_parted (a INT) PARTITION BY LIST (a);
  159. CREATE INDEX alter_idx_rename_test_parted_idx ON alter_idx_rename_test_parted (a);
  160. BEGIN;
  161. ALTER INDEX alter_idx_rename_test RENAME TO alter_idx_rename_test_2;
  162. ALTER INDEX alter_idx_rename_test_parted RENAME TO alter_idx_rename_test_parted_2;
  163. SELECT relation::regclass, mode FROM pg_locks
  164. WHERE pid = pg_backend_pid() AND locktype = 'relation'
  165. AND relation::regclass::text LIKE 'alter\_idx%'
  166. ORDER BY relation::regclass::text COLLATE "C";
  167. COMMIT;
  168. BEGIN;
  169. ALTER INDEX alter_idx_rename_test_idx RENAME TO alter_idx_rename_test_idx_2;
  170. ALTER INDEX alter_idx_rename_test_parted_idx RENAME TO alter_idx_rename_test_parted_idx_2;
  171. SELECT relation::regclass, mode FROM pg_locks
  172. WHERE pid = pg_backend_pid() AND locktype = 'relation'
  173. AND relation::regclass::text LIKE 'alter\_idx%'
  174. ORDER BY relation::regclass::text COLLATE "C";
  175. COMMIT;
  176. BEGIN;
  177. ALTER TABLE alter_idx_rename_test_idx_2 RENAME TO alter_idx_rename_test_idx_3;
  178. ALTER TABLE alter_idx_rename_test_parted_idx_2 RENAME TO alter_idx_rename_test_parted_idx_3;
  179. SELECT relation::regclass, mode FROM pg_locks
  180. WHERE pid = pg_backend_pid() AND locktype = 'relation'
  181. AND relation::regclass::text LIKE 'alter\_idx%'
  182. ORDER BY relation::regclass::text COLLATE "C";
  183. COMMIT;
  184. DROP TABLE alter_idx_rename_test_2;
  185. -- renaming views
  186. CREATE VIEW attmp_view (unique1) AS SELECT unique1 FROM tenk1;
  187. ALTER TABLE attmp_view RENAME TO attmp_view_new;
  188. SET ROLE regress_alter_table_user1;
  189. ALTER VIEW attmp_view_new RENAME TO fail; -- permission denied
  190. RESET ROLE;
  191. -- hack to ensure we get an indexscan here
  192. set enable_seqscan to off;
  193. set enable_bitmapscan to off;
  194. -- 5 values, sorted
  195. SELECT unique1 FROM tenk1 WHERE unique1 < 5;
  196. reset enable_seqscan;
  197. reset enable_bitmapscan;
  198. DROP VIEW attmp_view_new;
  199. -- toast-like relation name
  200. alter table stud_emp rename to pg_toast_stud_emp;
  201. alter table pg_toast_stud_emp rename to stud_emp;
  202. -- renaming index should rename constraint as well
  203. ALTER TABLE onek ADD CONSTRAINT onek_unique1_constraint UNIQUE (unique1);
  204. ALTER INDEX onek_unique1_constraint RENAME TO onek_unique1_constraint_foo;
  205. ALTER TABLE onek DROP CONSTRAINT onek_unique1_constraint_foo;
  206. -- renaming constraint
  207. ALTER TABLE onek ADD CONSTRAINT onek_check_constraint CHECK (unique1 >= 0);
  208. ALTER TABLE onek RENAME CONSTRAINT onek_check_constraint TO onek_check_constraint_foo;
  209. ALTER TABLE onek DROP CONSTRAINT onek_check_constraint_foo;
  210. -- renaming constraint should rename index as well
  211. ALTER TABLE onek ADD CONSTRAINT onek_unique1_constraint UNIQUE (unique1);
  212. DROP INDEX onek_unique1_constraint; -- to see whether it's there
  213. ALTER TABLE onek RENAME CONSTRAINT onek_unique1_constraint TO onek_unique1_constraint_foo;
  214. DROP INDEX onek_unique1_constraint_foo; -- to see whether it's there
  215. ALTER TABLE onek DROP CONSTRAINT onek_unique1_constraint_foo;
  216. -- renaming constraints vs. inheritance
  217. CREATE TABLE constraint_rename_test (a int CONSTRAINT con1 CHECK (a > 0), b int, c int);
  218. \d constraint_rename_test
  219. CREATE TABLE constraint_rename_test2 (a int CONSTRAINT con1 CHECK (a > 0), d int) INHERITS (constraint_rename_test);
  220. \d constraint_rename_test2
  221. ALTER TABLE constraint_rename_test2 RENAME CONSTRAINT con1 TO con1foo; -- fail
  222. ALTER TABLE ONLY constraint_rename_test RENAME CONSTRAINT con1 TO con1foo; -- fail
  223. ALTER TABLE constraint_rename_test RENAME CONSTRAINT con1 TO con1foo; -- ok
  224. \d constraint_rename_test
  225. \d constraint_rename_test2
  226. ALTER TABLE constraint_rename_test ADD CONSTRAINT con2 CHECK (b > 0) NO INHERIT;
  227. ALTER TABLE ONLY constraint_rename_test RENAME CONSTRAINT con2 TO con2foo; -- ok
  228. ALTER TABLE constraint_rename_test RENAME CONSTRAINT con2foo TO con2bar; -- ok
  229. \d constraint_rename_test
  230. \d constraint_rename_test2
  231. ALTER TABLE constraint_rename_test ADD CONSTRAINT con3 PRIMARY KEY (a);
  232. ALTER TABLE constraint_rename_test RENAME CONSTRAINT con3 TO con3foo; -- ok
  233. \d constraint_rename_test
  234. \d constraint_rename_test2
  235. DROP TABLE constraint_rename_test2;
  236. DROP TABLE constraint_rename_test;
  237. ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
  238. ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
  239. -- renaming constraints with cache reset of target relation
  240. CREATE TABLE constraint_rename_cache (a int,
  241. CONSTRAINT chk_a CHECK (a > 0),
  242. PRIMARY KEY (a));
  243. ALTER TABLE constraint_rename_cache
  244. RENAME CONSTRAINT chk_a TO chk_a_new;
  245. ALTER TABLE constraint_rename_cache
  246. RENAME CONSTRAINT constraint_rename_cache_pkey TO constraint_rename_pkey_new;
  247. CREATE TABLE like_constraint_rename_cache
  248. (LIKE constraint_rename_cache INCLUDING ALL);
  249. \d like_constraint_rename_cache
  250. DROP TABLE constraint_rename_cache;
  251. DROP TABLE like_constraint_rename_cache;
  252. -- FOREIGN KEY CONSTRAINT adding TEST
  253. CREATE TABLE attmp2 (a int primary key);
  254. CREATE TABLE attmp3 (a int, b int);
  255. CREATE TABLE attmp4 (a int, b int, unique(a,b));
  256. CREATE TABLE attmp5 (a int, b int);
  257. -- Insert rows into attmp2 (pktable)
  258. INSERT INTO attmp2 values (1);
  259. INSERT INTO attmp2 values (2);
  260. INSERT INTO attmp2 values (3);
  261. INSERT INTO attmp2 values (4);
  262. -- Insert rows into attmp3
  263. INSERT INTO attmp3 values (1,10);
  264. INSERT INTO attmp3 values (1,20);
  265. INSERT INTO attmp3 values (5,50);
  266. -- Try (and fail) to add constraint due to invalid source columns
  267. ALTER TABLE attmp3 add constraint attmpconstr foreign key(c) references attmp2 match full;
  268. -- Try (and fail) to add constraint due to invalid destination columns explicitly given
  269. ALTER TABLE attmp3 add constraint attmpconstr foreign key(a) references attmp2(b) match full;
  270. -- Try (and fail) to add constraint due to invalid data
  271. ALTER TABLE attmp3 add constraint attmpconstr foreign key (a) references attmp2 match full;
  272. -- Delete failing row
  273. DELETE FROM attmp3 where a=5;
  274. -- Try (and succeed)
  275. ALTER TABLE attmp3 add constraint attmpconstr foreign key (a) references attmp2 match full;
  276. ALTER TABLE attmp3 drop constraint attmpconstr;
  277. INSERT INTO attmp3 values (5,50);
  278. -- Try NOT VALID and then VALIDATE CONSTRAINT, but fails. Delete failure then re-validate
  279. ALTER TABLE attmp3 add constraint attmpconstr foreign key (a) references attmp2 match full NOT VALID;
  280. ALTER TABLE attmp3 validate constraint attmpconstr;
  281. -- Delete failing row
  282. DELETE FROM attmp3 where a=5;
  283. -- Try (and succeed) and repeat to show it works on already valid constraint
  284. ALTER TABLE attmp3 validate constraint attmpconstr;
  285. ALTER TABLE attmp3 validate constraint attmpconstr;
  286. -- Try a non-verified CHECK constraint
  287. ALTER TABLE attmp3 ADD CONSTRAINT b_greater_than_ten CHECK (b > 10); -- fail
  288. ALTER TABLE attmp3 ADD CONSTRAINT b_greater_than_ten CHECK (b > 10) NOT VALID; -- succeeds
  289. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- fails
  290. DELETE FROM attmp3 WHERE NOT b > 10;
  291. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- succeeds
  292. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- succeeds
  293. -- Test inherited NOT VALID CHECK constraints
  294. select * from attmp3;
  295. CREATE TABLE attmp6 () INHERITS (attmp3);
  296. CREATE TABLE attmp7 () INHERITS (attmp3);
  297. INSERT INTO attmp6 VALUES (6, 30), (7, 16);
  298. ALTER TABLE attmp3 ADD CONSTRAINT b_le_20 CHECK (b <= 20) NOT VALID;
  299. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_le_20; -- fails
  300. DELETE FROM attmp6 WHERE b > 20;
  301. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_le_20; -- succeeds
  302. -- An already validated constraint must not be revalidated
  303. CREATE FUNCTION boo(int) RETURNS int IMMUTABLE STRICT LANGUAGE plpgsql AS $$ BEGIN RAISE NOTICE 'boo: %', $1; RETURN $1; END; $$;
  304. INSERT INTO attmp7 VALUES (8, 18);
  305. ALTER TABLE attmp7 ADD CONSTRAINT identity CHECK (b = boo(b));
  306. ALTER TABLE attmp3 ADD CONSTRAINT IDENTITY check (b = boo(b)) NOT VALID;
  307. ALTER TABLE attmp3 VALIDATE CONSTRAINT identity;
  308. -- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
  309. create table parent_noinh_convalid (a int);
  310. create table child_noinh_convalid () inherits (parent_noinh_convalid);
  311. insert into parent_noinh_convalid values (1);
  312. insert into child_noinh_convalid values (1);
  313. alter table parent_noinh_convalid add constraint check_a_is_2 check (a = 2) no inherit not valid;
  314. -- fail, because of the row in parent
  315. alter table parent_noinh_convalid validate constraint check_a_is_2;
  316. delete from only parent_noinh_convalid;
  317. -- ok (parent itself contains no violating rows)
  318. alter table parent_noinh_convalid validate constraint check_a_is_2;
  319. select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid'::regclass and conname = 'check_a_is_2';
  320. -- cleanup
  321. drop table parent_noinh_convalid, child_noinh_convalid;
  322. -- Try (and fail) to create constraint from attmp5(a) to attmp4(a) - unique constraint on
  323. -- attmp4 is a,b
  324. ALTER TABLE attmp5 add constraint attmpconstr foreign key(a) references attmp4(a) match full;
  325. DROP TABLE attmp7;
  326. DROP TABLE attmp6;
  327. DROP TABLE attmp5;
  328. DROP TABLE attmp4;
  329. DROP TABLE attmp3;
  330. DROP TABLE attmp2;
  331. -- NOT VALID with plan invalidation -- ensure we don't use a constraint for
  332. -- exclusion until validated
  333. set constraint_exclusion TO 'partition';
  334. create table nv_parent (d date, check (false) no inherit not valid);
  335. -- not valid constraint added at creation time should automatically become valid
  336. \d nv_parent
  337. create table nv_child_2010 () inherits (nv_parent);
  338. create table nv_child_2011 () inherits (nv_parent);
  339. alter table nv_child_2010 add check (d between '2010-01-01'::date and '2010-12-31'::date) not valid;
  340. alter table nv_child_2011 add check (d between '2011-01-01'::date and '2011-12-31'::date) not valid;
  341. explain (costs off) select * from nv_parent where d between '2011-08-01' and '2011-08-31';
  342. create table nv_child_2009 (check (d between '2009-01-01'::date and '2009-12-31'::date)) inherits (nv_parent);
  343. explain (costs off) select * from nv_parent where d between '2011-08-01'::date and '2011-08-31'::date;
  344. explain (costs off) select * from nv_parent where d between '2009-08-01'::date and '2009-08-31'::date;
  345. -- after validation, the constraint should be used
  346. alter table nv_child_2011 VALIDATE CONSTRAINT nv_child_2011_d_check;
  347. explain (costs off) select * from nv_parent where d between '2009-08-01'::date and '2009-08-31'::date;
  348. -- add an inherited NOT VALID constraint
  349. alter table nv_parent add check (d between '2001-01-01'::date and '2099-12-31'::date) not valid;
  350. \d nv_child_2009
  351. -- we leave nv_parent and children around to help test pg_dump logic
  352. -- Foreign key adding test with mixed types
  353. -- Note: these tables are TEMP to avoid name conflicts when this test
  354. -- is run in parallel with foreign_key.sql.
  355. CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
  356. INSERT INTO PKTABLE VALUES(42);
  357. CREATE TEMP TABLE FKTABLE (ftest1 inet);
  358. -- This next should fail, because int=inet does not exist
  359. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  360. -- This should also fail for the same reason, but here we
  361. -- give the column name
  362. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
  363. DROP TABLE FKTABLE;
  364. -- This should succeed, even though they are different types,
  365. -- because int=int8 exists and is a member of the integer opfamily
  366. CREATE TEMP TABLE FKTABLE (ftest1 int8);
  367. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  368. -- Check it actually works
  369. INSERT INTO FKTABLE VALUES(42); -- should succeed
  370. INSERT INTO FKTABLE VALUES(43); -- should fail
  371. DROP TABLE FKTABLE;
  372. -- This should fail, because we'd have to cast numeric to int which is
  373. -- not an implicit coercion (or use numeric=numeric, but that's not part
  374. -- of the integer opfamily)
  375. CREATE TEMP TABLE FKTABLE (ftest1 numeric);
  376. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  377. DROP TABLE FKTABLE;
  378. DROP TABLE PKTABLE;
  379. -- On the other hand, this should work because int implicitly promotes to
  380. -- numeric, and we allow promotion on the FK side
  381. CREATE TEMP TABLE PKTABLE (ptest1 numeric PRIMARY KEY);
  382. INSERT INTO PKTABLE VALUES(42);
  383. CREATE TEMP TABLE FKTABLE (ftest1 int);
  384. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  385. -- Check it actually works
  386. INSERT INTO FKTABLE VALUES(42); -- should succeed
  387. INSERT INTO FKTABLE VALUES(43); -- should fail
  388. DROP TABLE FKTABLE;
  389. DROP TABLE PKTABLE;
  390. CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
  391. PRIMARY KEY(ptest1, ptest2));
  392. -- This should fail, because we just chose really odd types
  393. CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
  394. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
  395. DROP TABLE FKTABLE;
  396. -- Again, so should this...
  397. CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
  398. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
  399. references pktable(ptest1, ptest2);
  400. DROP TABLE FKTABLE;
  401. -- This fails because we mixed up the column ordering
  402. CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
  403. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
  404. references pktable(ptest2, ptest1);
  405. -- As does this...
  406. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
  407. references pktable(ptest1, ptest2);
  408. DROP TABLE FKTABLE;
  409. DROP TABLE PKTABLE;
  410. -- Test that ALTER CONSTRAINT updates trigger deferrability properly
  411. CREATE TEMP TABLE PKTABLE (ptest1 int primary key);
  412. CREATE TEMP TABLE FKTABLE (ftest1 int);
  413. ALTER TABLE FKTABLE ADD CONSTRAINT fknd FOREIGN KEY(ftest1) REFERENCES pktable
  414. ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE;
  415. ALTER TABLE FKTABLE ADD CONSTRAINT fkdd FOREIGN KEY(ftest1) REFERENCES pktable
  416. ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED;
  417. ALTER TABLE FKTABLE ADD CONSTRAINT fkdi FOREIGN KEY(ftest1) REFERENCES pktable
  418. ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE;
  419. ALTER TABLE FKTABLE ADD CONSTRAINT fknd2 FOREIGN KEY(ftest1) REFERENCES pktable
  420. ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED;
  421. ALTER TABLE FKTABLE ALTER CONSTRAINT fknd2 NOT DEFERRABLE;
  422. ALTER TABLE FKTABLE ADD CONSTRAINT fkdd2 FOREIGN KEY(ftest1) REFERENCES pktable
  423. ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE;
  424. ALTER TABLE FKTABLE ALTER CONSTRAINT fkdd2 DEFERRABLE INITIALLY DEFERRED;
  425. ALTER TABLE FKTABLE ADD CONSTRAINT fkdi2 FOREIGN KEY(ftest1) REFERENCES pktable
  426. ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE;
  427. ALTER TABLE FKTABLE ALTER CONSTRAINT fkdi2 DEFERRABLE INITIALLY IMMEDIATE;
  428. SELECT conname, tgfoid::regproc, tgtype, tgdeferrable, tginitdeferred
  429. FROM pg_trigger JOIN pg_constraint con ON con.oid = tgconstraint
  430. WHERE tgrelid = 'pktable'::regclass
  431. ORDER BY 1,2,3;
  432. SELECT conname, tgfoid::regproc, tgtype, tgdeferrable, tginitdeferred
  433. FROM pg_trigger JOIN pg_constraint con ON con.oid = tgconstraint
  434. WHERE tgrelid = 'fktable'::regclass
  435. ORDER BY 1,2,3;
  436. -- temp tables should go away by themselves, need not drop them.
  437. -- test check constraint adding
  438. create table atacc1 ( test int );
  439. -- add a check constraint
  440. alter table atacc1 add constraint atacc_test1 check (test>3);
  441. -- should fail
  442. insert into atacc1 (test) values (2);
  443. -- should succeed
  444. insert into atacc1 (test) values (4);
  445. drop table atacc1;
  446. -- let's do one where the check fails when added
  447. create table atacc1 ( test int );
  448. -- insert a soon to be failing row
  449. insert into atacc1 (test) values (2);
  450. -- add a check constraint (fails)
  451. alter table atacc1 add constraint atacc_test1 check (test>3);
  452. insert into atacc1 (test) values (4);
  453. drop table atacc1;
  454. -- let's do one where the check fails because the column doesn't exist
  455. create table atacc1 ( test int );
  456. -- add a check constraint (fails)
  457. alter table atacc1 add constraint atacc_test1 check (test1>3);
  458. drop table atacc1;
  459. -- something a little more complicated
  460. create table atacc1 ( test int, test2 int, test3 int);
  461. -- add a check constraint (fails)
  462. alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
  463. -- should fail
  464. insert into atacc1 (test,test2,test3) values (4,4,2);
  465. -- should succeed
  466. insert into atacc1 (test,test2,test3) values (4,4,5);
  467. drop table atacc1;
  468. -- lets do some naming tests
  469. create table atacc1 (test int check (test>3), test2 int);
  470. alter table atacc1 add check (test2>test);
  471. -- should fail for $2
  472. insert into atacc1 (test2, test) values (3, 4);
  473. drop table atacc1;
  474. -- inheritance related tests
  475. create table atacc1 (test int);
  476. create table atacc2 (test2 int);
  477. create table atacc3 (test3 int) inherits (atacc1, atacc2);
  478. alter table atacc2 add constraint foo check (test2>0);
  479. -- fail and then succeed on atacc2
  480. insert into atacc2 (test2) values (-3);
  481. insert into atacc2 (test2) values (3);
  482. -- fail and then succeed on atacc3
  483. insert into atacc3 (test2) values (-3);
  484. insert into atacc3 (test2) values (3);
  485. drop table atacc3;
  486. drop table atacc2;
  487. drop table atacc1;
  488. -- same things with one created with INHERIT
  489. create table atacc1 (test int);
  490. create table atacc2 (test2 int);
  491. create table atacc3 (test3 int) inherits (atacc1, atacc2);
  492. alter table atacc3 no inherit atacc2;
  493. -- fail
  494. alter table atacc3 no inherit atacc2;
  495. -- make sure it really isn't a child
  496. insert into atacc3 (test2) values (3);
  497. select test2 from atacc2;
  498. -- fail due to missing constraint
  499. alter table atacc2 add constraint foo check (test2>0);
  500. alter table atacc3 inherit atacc2;
  501. -- fail due to missing column
  502. alter table atacc3 rename test2 to testx;
  503. alter table atacc3 inherit atacc2;
  504. -- fail due to mismatched data type
  505. alter table atacc3 add test2 bool;
  506. alter table atacc3 inherit atacc2;
  507. alter table atacc3 drop test2;
  508. -- succeed
  509. alter table atacc3 add test2 int;
  510. update atacc3 set test2 = 4 where test2 is null;
  511. alter table atacc3 add constraint foo check (test2>0);
  512. alter table atacc3 inherit atacc2;
  513. -- fail due to duplicates and circular inheritance
  514. alter table atacc3 inherit atacc2;
  515. alter table atacc2 inherit atacc3;
  516. alter table atacc2 inherit atacc2;
  517. -- test that we really are a child now (should see 4 not 3 and cascade should go through)
  518. select test2 from atacc2;
  519. drop table atacc2 cascade;
  520. drop table atacc1;
  521. -- adding only to a parent is allowed as of 9.2
  522. create table atacc1 (test int);
  523. create table atacc2 (test2 int) inherits (atacc1);
  524. -- ok:
  525. alter table atacc1 add constraint foo check (test>0) no inherit;
  526. -- check constraint is not there on child
  527. insert into atacc2 (test) values (-3);
  528. -- check constraint is there on parent
  529. insert into atacc1 (test) values (-3);
  530. insert into atacc1 (test) values (3);
  531. -- fail, violating row:
  532. alter table atacc2 add constraint foo check (test>0) no inherit;
  533. drop table atacc2;
  534. drop table atacc1;
  535. -- test unique constraint adding
  536. create table atacc1 ( test int ) ;
  537. -- add a unique constraint
  538. alter table atacc1 add constraint atacc_test1 unique (test);
  539. -- insert first value
  540. insert into atacc1 (test) values (2);
  541. -- should fail
  542. insert into atacc1 (test) values (2);
  543. -- should succeed
  544. insert into atacc1 (test) values (4);
  545. -- try to create duplicates via alter table using - should fail
  546. alter table atacc1 alter column test type integer using 0;
  547. drop table atacc1;
  548. -- let's do one where the unique constraint fails when added
  549. create table atacc1 ( test int );
  550. -- insert soon to be failing rows
  551. insert into atacc1 (test) values (2);
  552. insert into atacc1 (test) values (2);
  553. -- add a unique constraint (fails)
  554. alter table atacc1 add constraint atacc_test1 unique (test);
  555. insert into atacc1 (test) values (3);
  556. drop table atacc1;
  557. -- let's do one where the unique constraint fails
  558. -- because the column doesn't exist
  559. create table atacc1 ( test int );
  560. -- add a unique constraint (fails)
  561. alter table atacc1 add constraint atacc_test1 unique (test1);
  562. drop table atacc1;
  563. -- something a little more complicated
  564. create table atacc1 ( test int, test2 int);
  565. -- add a unique constraint
  566. alter table atacc1 add constraint atacc_test1 unique (test, test2);
  567. -- insert initial value
  568. insert into atacc1 (test,test2) values (4,4);
  569. -- should fail
  570. insert into atacc1 (test,test2) values (4,4);
  571. -- should all succeed
  572. insert into atacc1 (test,test2) values (4,5);
  573. insert into atacc1 (test,test2) values (5,4);
  574. insert into atacc1 (test,test2) values (5,5);
  575. drop table atacc1;
  576. -- lets do some naming tests
  577. create table atacc1 (test int, test2 int, unique(test));
  578. alter table atacc1 add unique (test2);
  579. -- should fail for @@ second one @@
  580. insert into atacc1 (test2, test) values (3, 3);
  581. insert into atacc1 (test2, test) values (2, 3);
  582. drop table atacc1;
  583. -- test primary key constraint adding
  584. create table atacc1 ( id serial, test int) ;
  585. -- add a primary key constraint
  586. alter table atacc1 add constraint atacc_test1 primary key (test);
  587. -- insert first value
  588. insert into atacc1 (test) values (2);
  589. -- should fail
  590. insert into atacc1 (test) values (2);
  591. -- should succeed
  592. insert into atacc1 (test) values (4);
  593. -- inserting NULL should fail
  594. insert into atacc1 (test) values(NULL);
  595. -- try adding a second primary key (should fail)
  596. alter table atacc1 add constraint atacc_oid1 primary key(id);
  597. -- drop first primary key constraint
  598. alter table atacc1 drop constraint atacc_test1 restrict;
  599. -- try adding a primary key on oid (should succeed)
  600. alter table atacc1 add constraint atacc_oid1 primary key(id);
  601. drop table atacc1;
  602. -- let's do one where the primary key constraint fails when added
  603. create table atacc1 ( test int );
  604. -- insert soon to be failing rows
  605. insert into atacc1 (test) values (2);
  606. insert into atacc1 (test) values (2);
  607. -- add a primary key (fails)
  608. alter table atacc1 add constraint atacc_test1 primary key (test);
  609. insert into atacc1 (test) values (3);
  610. drop table atacc1;
  611. -- let's do another one where the primary key constraint fails when added
  612. create table atacc1 ( test int );
  613. -- insert soon to be failing row
  614. insert into atacc1 (test) values (NULL);
  615. -- add a primary key (fails)
  616. alter table atacc1 add constraint atacc_test1 primary key (test);
  617. insert into atacc1 (test) values (3);
  618. drop table atacc1;
  619. -- let's do one where the primary key constraint fails
  620. -- because the column doesn't exist
  621. create table atacc1 ( test int );
  622. -- add a primary key constraint (fails)
  623. alter table atacc1 add constraint atacc_test1 primary key (test1);
  624. drop table atacc1;
  625. -- adding a new column as primary key to a non-empty table.
  626. -- should fail unless the column has a non-null default value.
  627. create table atacc1 ( test int );
  628. insert into atacc1 (test) values (0);
  629. -- add a primary key column without a default (fails).
  630. alter table atacc1 add column test2 int primary key;
  631. -- now add a primary key column with a default (succeeds).
  632. alter table atacc1 add column test2 int default 0 primary key;
  633. drop table atacc1;
  634. -- this combination used to have order-of-execution problems (bug #15580)
  635. create table atacc1 (a int);
  636. insert into atacc1 values(1);
  637. alter table atacc1
  638. add column b float8 not null default random(),
  639. add primary key(a);
  640. drop table atacc1;
  641. -- additionally, we've seen issues with foreign key validation not being
  642. -- properly delayed until after a table rewrite. Check that works ok.
  643. create table atacc1 (a int primary key);
  644. alter table atacc1 add constraint atacc1_fkey foreign key (a) references atacc1 (a) not valid;
  645. alter table atacc1 validate constraint atacc1_fkey, alter a type bigint;
  646. drop table atacc1;
  647. -- we've also seen issues with check constraints being validated at the wrong
  648. -- time when there's a pending table rewrite.
  649. create table atacc1 (a bigint, b int);
  650. insert into atacc1 values(1,1);
  651. alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
  652. alter table atacc1 validate constraint atacc1_chk, alter a type int;
  653. drop table atacc1;
  654. -- same as above, but ensure the constraint violation is detected
  655. create table atacc1 (a bigint, b int);
  656. insert into atacc1 values(1,2);
  657. alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
  658. alter table atacc1 validate constraint atacc1_chk, alter a type int;
  659. drop table atacc1;
  660. -- something a little more complicated
  661. create table atacc1 ( test int, test2 int);
  662. -- add a primary key constraint
  663. alter table atacc1 add constraint atacc_test1 primary key (test, test2);
  664. -- try adding a second primary key - should fail
  665. alter table atacc1 add constraint atacc_test2 primary key (test);
  666. -- insert initial value
  667. insert into atacc1 (test,test2) values (4,4);
  668. -- should fail
  669. insert into atacc1 (test,test2) values (4,4);
  670. insert into atacc1 (test,test2) values (NULL,3);
  671. insert into atacc1 (test,test2) values (3, NULL);
  672. insert into atacc1 (test,test2) values (NULL,NULL);
  673. -- should all succeed
  674. insert into atacc1 (test,test2) values (4,5);
  675. insert into atacc1 (test,test2) values (5,4);
  676. insert into atacc1 (test,test2) values (5,5);
  677. drop table atacc1;
  678. -- lets do some naming tests
  679. create table atacc1 (test int, test2 int, primary key(test));
  680. -- only first should succeed
  681. insert into atacc1 (test2, test) values (3, 3);
  682. insert into atacc1 (test2, test) values (2, 3);
  683. insert into atacc1 (test2, test) values (1, NULL);
  684. drop table atacc1;
  685. -- alter table / alter column [set/drop] not null tests
  686. -- try altering system catalogs, should fail
  687. alter table pg_class alter column relname drop not null;
  688. alter table pg_class alter relname set not null;
  689. -- try altering non-existent table, should fail
  690. alter table non_existent alter column bar set not null;
  691. alter table non_existent alter column bar drop not null;
  692. -- test setting columns to null and not null and vice versa
  693. -- test checking for null values and primary key
  694. create table atacc1 (test int not null);
  695. alter table atacc1 add constraint "atacc1_pkey" primary key (test);
  696. alter table atacc1 alter column test drop not null;
  697. alter table atacc1 drop constraint "atacc1_pkey";
  698. alter table atacc1 alter column test drop not null;
  699. insert into atacc1 values (null);
  700. alter table atacc1 alter test set not null;
  701. delete from atacc1;
  702. alter table atacc1 alter test set not null;
  703. -- try altering a non-existent column, should fail
  704. alter table atacc1 alter bar set not null;
  705. alter table atacc1 alter bar drop not null;
  706. -- try creating a view and altering that, should fail
  707. create view myview as select * from atacc1;
  708. alter table myview alter column test drop not null;
  709. alter table myview alter column test set not null;
  710. drop view myview;
  711. drop table atacc1;
  712. -- set not null verified by constraints
  713. create table atacc1 (test_a int, test_b int);
  714. insert into atacc1 values (null, 1);
  715. -- constraint not cover all values, should fail
  716. alter table atacc1 add constraint atacc1_constr_or check(test_a is not null or test_b < 10);
  717. alter table atacc1 alter test_a set not null;
  718. alter table atacc1 drop constraint atacc1_constr_or;
  719. -- not valid constraint, should fail
  720. alter table atacc1 add constraint atacc1_constr_invalid check(test_a is not null) not valid;
  721. alter table atacc1 alter test_a set not null;
  722. alter table atacc1 drop constraint atacc1_constr_invalid;
  723. -- with valid constraint
  724. update atacc1 set test_a = 1;
  725. alter table atacc1 add constraint atacc1_constr_a_valid check(test_a is not null);
  726. alter table atacc1 alter test_a set not null;
  727. delete from atacc1;
  728. insert into atacc1 values (2, null);
  729. alter table atacc1 alter test_a drop not null;
  730. -- test multiple set not null at same time
  731. -- test_a checked by atacc1_constr_a_valid, test_b should fail by table scan
  732. alter table atacc1 alter test_a set not null, alter test_b set not null;
  733. -- commands order has no importance
  734. alter table atacc1 alter test_b set not null, alter test_a set not null;
  735. -- valid one by table scan, one by check constraints
  736. update atacc1 set test_b = 1;
  737. alter table atacc1 alter test_b set not null, alter test_a set not null;
  738. alter table atacc1 alter test_a drop not null, alter test_b drop not null;
  739. -- both column has check constraints
  740. alter table atacc1 add constraint atacc1_constr_b_valid check(test_b is not null);
  741. alter table atacc1 alter test_b set not null, alter test_a set not null;
  742. drop table atacc1;
  743. -- test inheritance
  744. create table parent (a int);
  745. create table child (b varchar(255)) inherits (parent);
  746. alter table parent alter a set not null;
  747. insert into parent values (NULL);
  748. insert into child (a, b) values (NULL, 'foo');
  749. alter table parent alter a drop not null;
  750. insert into parent values (NULL);
  751. insert into child (a, b) values (NULL, 'foo');
  752. alter table only parent alter a set not null;
  753. alter table child alter a set not null;
  754. delete from parent;
  755. alter table only parent alter a set not null;
  756. insert into parent values (NULL);
  757. alter table child alter a set not null;
  758. insert into child (a, b) values (NULL, 'foo');
  759. delete from child;
  760. alter table child alter a set not null;
  761. insert into child (a, b) values (NULL, 'foo');
  762. drop table child;
  763. drop table parent;
  764. -- test setting and removing default values
  765. create table def_test (
  766. c1 int4 default 5,
  767. c2 text default 'initial_default'
  768. );
  769. insert into def_test default values;
  770. alter table def_test alter column c1 drop default;
  771. insert into def_test default values;
  772. alter table def_test alter column c2 drop default;
  773. insert into def_test default values;
  774. alter table def_test alter column c1 set default 10;
  775. alter table def_test alter column c2 set default 'new_default';
  776. insert into def_test default values;
  777. select * from def_test;
  778. -- set defaults to an incorrect type: this should fail
  779. alter table def_test alter column c1 set default 'wrong_datatype';
  780. alter table def_test alter column c2 set default 20;
  781. -- set defaults on a non-existent column: this should fail
  782. alter table def_test alter column c3 set default 30;
  783. -- set defaults on views: we need to create a view, add a rule
  784. -- to allow insertions into it, and then alter the view to add
  785. -- a default
  786. create view def_view_test as select * from def_test;
  787. create rule def_view_test_ins as
  788. on insert to def_view_test
  789. do instead insert into def_test select new.*;
  790. insert into def_view_test default values;
  791. alter table def_view_test alter column c1 set default 45;
  792. insert into def_view_test default values;
  793. alter table def_view_test alter column c2 set default 'view_default';
  794. insert into def_view_test default values;
  795. select * from def_view_test;
  796. drop rule def_view_test_ins on def_view_test;
  797. drop view def_view_test;
  798. drop table def_test;
  799. -- alter table / drop column tests
  800. -- try altering system catalogs, should fail
  801. alter table pg_class drop column relname;
  802. -- try altering non-existent table, should fail
  803. alter table nosuchtable drop column bar;
  804. -- test dropping columns
  805. create table atacc1 (a int4 not null, b int4, c int4 not null, d int4);
  806. insert into atacc1 values (1, 2, 3, 4);
  807. alter table atacc1 drop a;
  808. alter table atacc1 drop a;
  809. -- SELECTs
  810. select * from atacc1;
  811. select * from atacc1 order by a;
  812. select * from atacc1 order by "........pg.dropped.1........";
  813. select * from atacc1 group by a;
  814. select * from atacc1 group by "........pg.dropped.1........";
  815. select atacc1.* from atacc1;
  816. select a from atacc1;
  817. select atacc1.a from atacc1;
  818. select b,c,d from atacc1;
  819. select a,b,c,d from atacc1;
  820. select * from atacc1 where a = 1;
  821. select "........pg.dropped.1........" from atacc1;
  822. select atacc1."........pg.dropped.1........" from atacc1;
  823. select "........pg.dropped.1........",b,c,d from atacc1;
  824. select * from atacc1 where "........pg.dropped.1........" = 1;
  825. -- UPDATEs
  826. update atacc1 set a = 3;
  827. update atacc1 set b = 2 where a = 3;
  828. update atacc1 set "........pg.dropped.1........" = 3;
  829. update atacc1 set b = 2 where "........pg.dropped.1........" = 3;
  830. -- INSERTs
  831. insert into atacc1 values (10, 11, 12, 13);
  832. insert into atacc1 values (default, 11, 12, 13);
  833. insert into atacc1 values (11, 12, 13);
  834. insert into atacc1 (a) values (10);
  835. insert into atacc1 (a) values (default);
  836. insert into atacc1 (a,b,c,d) values (10,11,12,13);
  837. insert into atacc1 (a,b,c,d) values (default,11,12,13);
  838. insert into atacc1 (b,c,d) values (11,12,13);
  839. insert into atacc1 ("........pg.dropped.1........") values (10);
  840. insert into atacc1 ("........pg.dropped.1........") values (default);
  841. insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);
  842. insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);
  843. -- DELETEs
  844. delete from atacc1 where a = 3;
  845. delete from atacc1 where "........pg.dropped.1........" = 3;
  846. delete from atacc1;
  847. -- try dropping a non-existent column, should fail
  848. alter table atacc1 drop bar;
  849. -- try removing an oid column, should succeed (as it's nonexistent)
  850. alter table atacc1 SET WITHOUT OIDS;
  851. -- try adding an oid column, should fail (not supported)
  852. alter table atacc1 SET WITH OIDS;
  853. -- try dropping the xmin column, should fail
  854. alter table atacc1 drop xmin;
  855. -- try creating a view and altering that, should fail
  856. create view myview as select * from atacc1;
  857. select * from myview;
  858. alter table myview drop d;
  859. drop view myview;
  860. -- test some commands to make sure they fail on the dropped column
  861. analyze atacc1(a);
  862. analyze atacc1("........pg.dropped.1........");
  863. vacuum analyze atacc1(a);
  864. vacuum analyze atacc1("........pg.dropped.1........");
  865. comment on column atacc1.a is 'testing';
  866. comment on column atacc1."........pg.dropped.1........" is 'testing';
  867. alter table atacc1 alter a set storage plain;
  868. alter table atacc1 alter "........pg.dropped.1........" set storage plain;
  869. alter table atacc1 alter a set statistics 0;
  870. alter table atacc1 alter "........pg.dropped.1........" set statistics 0;
  871. alter table atacc1 alter a set default 3;
  872. alter table atacc1 alter "........pg.dropped.1........" set default 3;
  873. alter table atacc1 alter a drop default;
  874. alter table atacc1 alter "........pg.dropped.1........" drop default;
  875. alter table atacc1 alter a set not null;
  876. alter table atacc1 alter "........pg.dropped.1........" set not null;
  877. alter table atacc1 alter a drop not null;
  878. alter table atacc1 alter "........pg.dropped.1........" drop not null;
  879. alter table atacc1 rename a to x;
  880. alter table atacc1 rename "........pg.dropped.1........" to x;
  881. alter table atacc1 add primary key(a);
  882. alter table atacc1 add primary key("........pg.dropped.1........");
  883. alter table atacc1 add unique(a);
  884. alter table atacc1 add unique("........pg.dropped.1........");
  885. alter table atacc1 add check (a > 3);
  886. alter table atacc1 add check ("........pg.dropped.1........" > 3);
  887. create table atacc2 (id int4 unique);
  888. alter table atacc1 add foreign key (a) references atacc2(id);
  889. alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);
  890. alter table atacc2 add foreign key (id) references atacc1(a);
  891. alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");
  892. drop table atacc2;
  893. create index "testing_idx" on atacc1(a);
  894. create index "testing_idx" on atacc1("........pg.dropped.1........");
  895. -- test create as and select into
  896. insert into atacc1 values (21, 22, 23);
  897. create table attest1 as select * from atacc1;
  898. select * from attest1;
  899. drop table attest1;
  900. select * into attest2 from atacc1;
  901. select * from attest2;
  902. drop table attest2;
  903. -- try dropping all columns
  904. alter table atacc1 drop c;
  905. alter table atacc1 drop d;
  906. alter table atacc1 drop b;
  907. select * from atacc1;
  908. drop table atacc1;
  909. -- test constraint error reporting in presence of dropped columns
  910. create table atacc1 (id serial primary key, value int check (value < 10));
  911. insert into atacc1(value) values (100);
  912. alter table atacc1 drop column value;
  913. alter table atacc1 add column value int check (value < 10);
  914. insert into atacc1(value) values (100);
  915. insert into atacc1(id, value) values (null, 0);
  916. drop table atacc1;
  917. -- test inheritance
  918. create table parent (a int, b int, c int);
  919. insert into parent values (1, 2, 3);
  920. alter table parent drop a;
  921. create table child (d varchar(255)) inherits (parent);
  922. insert into child values (12, 13, 'testing');
  923. select * from parent;
  924. select * from child;
  925. alter table parent drop c;
  926. select * from parent;
  927. select * from child;
  928. drop table child;
  929. drop table parent;
  930. -- check error cases for inheritance column merging
  931. create table parent (a float8, b numeric(10,4), c text collate "C");
  932. create table child (a float4) inherits (parent); -- fail
  933. create table child (b decimal(10,7)) inherits (parent); -- fail
  934. create table child (c text collate "POSIX") inherits (parent); -- fail
  935. create table child (a double precision, b decimal(10,4)) inherits (parent);
  936. drop table child;
  937. drop table parent;
  938. -- test copy in/out
  939. create table attest (a int4, b int4, c int4);
  940. insert into attest values (1,2,3);
  941. alter table attest drop a;
  942. copy attest to stdout;
  943. copy attest(a) to stdout;
  944. copy attest("........pg.dropped.1........") to stdout;
  945. copy attest from stdin;
  946. 10 11 12
  947. \.
  948. select * from attest;
  949. copy attest from stdin;
  950. 21 22
  951. \.
  952. select * from attest;
  953. copy attest(a) from stdin;
  954. copy attest("........pg.dropped.1........") from stdin;
  955. copy attest(b,c) from stdin;
  956. 31 32
  957. \.
  958. select * from attest;
  959. drop table attest;
  960. -- test inheritance
  961. create table dropColumn (a int, b int, e int);
  962. create table dropColumnChild (c int) inherits (dropColumn);
  963. create table dropColumnAnother (d int) inherits (dropColumnChild);
  964. -- these two should fail
  965. alter table dropColumnchild drop column a;
  966. alter table only dropColumnChild drop column b;
  967. -- these three should work
  968. alter table only dropColumn drop column e;
  969. alter table dropColumnChild drop column c;
  970. alter table dropColumn drop column a;
  971. create table renameColumn (a int);
  972. create table renameColumnChild (b int) inherits (renameColumn);
  973. create table renameColumnAnother (c int) inherits (renameColumnChild);
  974. -- these three should fail
  975. alter table renameColumnChild rename column a to d;
  976. alter table only renameColumnChild rename column a to d;
  977. alter table only renameColumn rename column a to d;
  978. -- these should work
  979. alter table renameColumn rename column a to d;
  980. alter table renameColumnChild rename column b to a;
  981. -- these should work
  982. alter table if exists doesnt_exist_tab rename column a to d;
  983. alter table if exists doesnt_exist_tab rename column b to a;
  984. -- this should work
  985. alter table renameColumn add column w int;
  986. -- this should fail
  987. alter table only renameColumn add column x int;
  988. -- Test corner cases in dropping of inherited columns
  989. create table p1 (f1 int, f2 int);
  990. create table c1 (f1 int not null) inherits(p1);
  991. -- should be rejected since c1.f1 is inherited
  992. alter table c1 drop column f1;
  993. -- should work
  994. alter table p1 drop column f1;
  995. -- c1.f1 is still there, but no longer inherited
  996. select f1 from c1;
  997. alter table c1 drop column f1;
  998. select f1 from c1;
  999. drop table p1 cascade;
  1000. create table p1 (f1 int, f2 int);
  1001. create table c1 () inherits(p1);
  1002. -- should be rejected since c1.f1 is inherited
  1003. alter table c1 drop column f1;
  1004. alter table p1 drop column f1;
  1005. -- c1.f1 is dropped now, since there is no local definition for it
  1006. select f1 from c1;
  1007. drop table p1 cascade;
  1008. create table p1 (f1 int, f2 int);
  1009. create table c1 () inherits(p1);
  1010. -- should be rejected since c1.f1 is inherited
  1011. alter table c1 drop column f1;
  1012. alter table only p1 drop column f1;
  1013. -- c1.f1 is NOT dropped, but must now be considered non-inherited
  1014. alter table c1 drop column f1;
  1015. drop table p1 cascade;
  1016. create table p1 (f1 int, f2 int);
  1017. create table c1 (f1 int not null) inherits(p1);
  1018. -- should be rejected since c1.f1 is inherited
  1019. alter table c1 drop column f1;
  1020. alter table only p1 drop column f1;
  1021. -- c1.f1 is still there, but no longer inherited
  1022. alter table c1 drop column f1;
  1023. drop table p1 cascade;
  1024. create table p1(id int, name text);
  1025. create table p2(id2 int, name text, height int);
  1026. create table c1(age int) inherits(p1,p2);
  1027. create table gc1() inherits (c1);
  1028. select relname, attname, attinhcount, attislocal
  1029. from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
  1030. where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
  1031. order by relname, attnum;
  1032. -- should work
  1033. alter table only p1 drop column name;
  1034. -- should work. Now c1.name is local and inhcount is 0.
  1035. alter table p2 drop column name;
  1036. -- should be rejected since its inherited
  1037. alter table gc1 drop column name;
  1038. -- should work, and drop gc1.name along
  1039. alter table c1 drop column name;
  1040. -- should fail: column does not exist
  1041. alter table gc1 drop column name;
  1042. -- should work and drop the attribute in all tables
  1043. alter table p2 drop column height;
  1044. -- IF EXISTS test
  1045. create table dropColumnExists ();
  1046. alter table dropColumnExists drop column non_existing; --fail
  1047. alter table dropColumnExists drop column if exists non_existing; --succeed
  1048. select relname, attname, attinhcount, attislocal
  1049. from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
  1050. where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
  1051. order by relname, attnum;
  1052. drop table p1, p2 cascade;
  1053. -- test attinhcount tracking with merged columns
  1054. create table depth0();
  1055. create table depth1(c text) inherits (depth0);
  1056. create table depth2() inherits (depth1);
  1057. alter table depth0 add c text;
  1058. select attrelid::regclass, attname, attinhcount, attislocal
  1059. from pg_attribute
  1060. where attnum > 0 and attrelid::regclass in ('depth0', 'depth1', 'depth2')
  1061. order by attrelid::regclass::text, attnum;
  1062. -- test renumbering of child-table columns in inherited operations
  1063. create table p1 (f1 int);
  1064. create table c1 (f2 text, f3 int) inherits (p1);
  1065. alter table p1 add column a1 int check (a1 > 0);
  1066. alter table p1 add column f2 text;
  1067. insert into p1 values (1,2,'abc');
  1068. insert into c1 values(11,'xyz',33,0); -- should fail
  1069. insert into c1 values(11,'xyz',33,22);
  1070. select * from p1;
  1071. update p1 set a1 = a1 + 1, f2 = upper(f2);
  1072. select * from p1;
  1073. drop table p1 cascade;
  1074. -- test that operations with a dropped column do not try to reference
  1075. -- its datatype
  1076. create domain mytype as text;
  1077. create temp table foo (f1 text, f2 mytype, f3 text);
  1078. insert into foo values('bb','cc','dd');
  1079. select * from foo;
  1080. drop domain mytype cascade;
  1081. select * from foo;
  1082. insert into foo values('qq','rr');
  1083. select * from foo;
  1084. update foo set f3 = 'zz';
  1085. select * from foo;
  1086. select f3,max(f1) from foo group by f3;
  1087. -- Simple tests for alter table column type
  1088. alter table foo alter f1 TYPE integer; -- fails
  1089. alter table foo alter f1 TYPE varchar(10);
  1090. create table anothertab (atcol1 serial8, atcol2 boolean,
  1091. constraint anothertab_chk check (atcol1 <= 3));
  1092. insert into anothertab (atcol1, atcol2) values (default, true);
  1093. insert into anothertab (atcol1, atcol2) values (default, false);
  1094. select * from anothertab;
  1095. alter table anothertab alter column atcol1 type boolean; -- fails
  1096. alter table anothertab alter column atcol1 type boolean using atcol1::int; -- fails
  1097. alter table anothertab alter column atcol1 type integer;
  1098. select * from anothertab;
  1099. insert into anothertab (atcol1, atcol2) values (45, null); -- fails
  1100. insert into anothertab (atcol1, atcol2) values (default, null);
  1101. select * from anothertab;
  1102. alter table anothertab alter column atcol2 type text
  1103. using case when atcol2 is true then 'IT WAS TRUE'
  1104. when atcol2 is false then 'IT WAS FALSE'
  1105. else 'IT WAS NULL!' end;
  1106. select * from anothertab;
  1107. alter table anothertab alter column atcol1 type boolean
  1108. using case when atcol1 % 2 = 0 then true else false end; -- fails
  1109. alter table anothertab alter column atcol1 drop default;
  1110. alter table anothertab alter column atcol1 type boolean
  1111. using case when atcol1 % 2 = 0 then true else false end; -- fails
  1112. alter table anothertab drop constraint anothertab_chk;
  1113. alter table anothertab drop constraint anothertab_chk; -- fails
  1114. alter table anothertab drop constraint IF EXISTS anothertab_chk; -- succeeds
  1115. alter table anothertab alter column atcol1 type boolean
  1116. using case when atcol1 % 2 = 0 then true else false end;
  1117. select * from anothertab;
  1118. drop table anothertab;
  1119. -- Test index handling in alter table column type (cf. bugs #15835, #15865)
  1120. create table anothertab(f1 int primary key, f2 int unique,
  1121. f3 int, f4 int, f5 int);
  1122. alter table anothertab
  1123. add exclude using btree (f3 with =);
  1124. alter table anothertab
  1125. add exclude using btree (f4 with =) where (f4 is not null);
  1126. alter table anothertab
  1127. add exclude using btree (f4 with =) where (f5 > 0);
  1128. alter table anothertab
  1129. add unique(f1,f4);
  1130. create index on anothertab(f2,f3);
  1131. create unique index on anothertab(f4);
  1132. \d anothertab
  1133. alter table anothertab alter column f1 type bigint;
  1134. alter table anothertab
  1135. alter column f2 type bigint,
  1136. alter column f3 type bigint,
  1137. alter column f4 type bigint;
  1138. alter table anothertab alter column f5 type bigint;
  1139. \d anothertab
  1140. drop table anothertab;
  1141. -- test that USING expressions are parsed before column alter type / drop steps
  1142. create table another (f1 int, f2 text, f3 text);
  1143. insert into another values(1, 'one', 'uno');
  1144. insert into another values(2, 'two', 'due');
  1145. insert into another values(3, 'three', 'tre');
  1146. select * from another;
  1147. alter table another
  1148. alter f1 type text using f2 || ' and ' || f3 || ' more',
  1149. alter f2 type bigint using f1 * 10,
  1150. drop column f3;
  1151. select * from another;
  1152. drop table another;
  1153. -- Create an index that skips WAL, then perform a SET DATA TYPE that skips
  1154. -- rewriting the index.
  1155. begin;
  1156. create table skip_wal_skip_rewrite_index (c varchar(10) primary key);
  1157. alter table skip_wal_skip_rewrite_index alter c type varchar(20);
  1158. commit;
  1159. -- We disallow changing table's row type if it's used for storage
  1160. create table at_tab1 (a int, b text);
  1161. create table at_tab2 (x int, y at_tab1);
  1162. alter table at_tab1 alter column b type varchar; -- fails
  1163. drop table at_tab2;
  1164. -- Use of row type in an expression is defended differently
  1165. create table at_tab2 (x int, y text, check((x,y)::at_tab1 = (1,'42')::at_tab1));
  1166. alter table at_tab1 alter column b type varchar; -- allowed, but ...
  1167. insert into at_tab2 values(1,'42'); -- ... this will fail
  1168. drop table at_tab1, at_tab2;
  1169. -- Check it for a partitioned table, too
  1170. create table at_tab1 (a int, b text) partition by list(a);
  1171. create table at_tab2 (x int, y at_tab1);
  1172. alter table at_tab1 alter column b type varchar; -- fails
  1173. drop table at_tab1, at_tab2;
  1174. -- Alter column type that's part of a partitioned index
  1175. create table at_partitioned (a int, b text) partition by range (a);
  1176. create table at_part_1 partition of at_partitioned for values from (0) to (1000);
  1177. insert into at_partitioned values (512, '0.123');
  1178. create table at_part_2 (b text, a int);
  1179. insert into at_part_2 values ('1.234', 1024);
  1180. create index on at_partitioned (b);
  1181. create index on at_partitioned (a);
  1182. \d at_part_1
  1183. \d at_part_2
  1184. alter table at_partitioned attach partition at_part_2 for values from (1000) to (2000);
  1185. \d at_part_2
  1186. alter table at_partitioned alter column b type numeric using b::numeric;
  1187. \d at_part_1
  1188. \d at_part_2
  1189. drop table at_partitioned;
  1190. -- Alter column type when no table rewrite is required
  1191. -- Also check that comments are preserved
  1192. create table at_partitioned(id int, name varchar(64), unique (id, name))
  1193. partition by hash(id);
  1194. comment on constraint at_partitioned_id_name_key on at_partitioned is 'parent constraint';
  1195. comment on index at_partitioned_id_name_key is 'parent index';
  1196. create table at_partitioned_0 partition of at_partitioned
  1197. for values with (modulus 2, remainder 0);
  1198. comment on constraint at_partitioned_0_id_name_key on at_partitioned_0 is 'child 0 constraint';
  1199. comment on index at_partitioned_0_id_name_key is 'child 0 index';
  1200. create table at_partitioned_1 partition of at_partitioned
  1201. for values with (modulus 2, remainder 1);
  1202. comment on constraint at_partitioned_1_id_name_key on at_partitioned_1 is 'child 1 constraint';
  1203. comment on index at_partitioned_1_id_name_key is 'child 1 index';
  1204. insert into at_partitioned values(1, 'foo');
  1205. insert into at_partitioned values(3, 'bar');
  1206. create temp table old_oids as
  1207. select relname, oid as oldoid, relfilenode as oldfilenode
  1208. from pg_class where relname like 'at_partitioned%';
  1209. select relname,
  1210. c.oid = oldoid as orig_oid,
  1211. case relfilenode
  1212. when 0 then 'none'
  1213. when c.oid then 'own'
  1214. when oldfilenode then 'orig'
  1215. else 'OTHER'
  1216. end as storage,
  1217. obj_description(c.oid, 'pg_class') as desc
  1218. from pg_class c left join old_oids using (relname)
  1219. where relname like 'at_partitioned%'
  1220. order by relname;
  1221. select conname, obj_description(oid, 'pg_constraint') as desc
  1222. from pg_constraint where conname like 'at_partitioned%'
  1223. order by conname;
  1224. alter table at_partitioned alter column name type varchar(127);
  1225. -- Note: these tests currently show the wrong behavior for comments :-(
  1226. select relname,
  1227. c.oid = oldoid as orig_oid,
  1228. case relfilenode
  1229. when 0 then 'none'
  1230. when c.oid then 'own'
  1231. when oldfilenode then 'orig'
  1232. else 'OTHER'
  1233. end as storage,
  1234. obj_description(c.oid, 'pg_class') as desc
  1235. from pg_class c left join old_oids using (relname)
  1236. where relname like 'at_partitioned%'
  1237. order by relname;
  1238. select conname, obj_description(oid, 'pg_constraint') as desc
  1239. from pg_constraint where conname like 'at_partitioned%'
  1240. order by conname;
  1241. -- Don't remove this DROP, it exposes bug #15672
  1242. drop table at_partitioned;
  1243. -- disallow recursive containment of row types
  1244. create temp table recur1 (f1 int);
  1245. alter table recur1 add column f2 recur1; -- fails
  1246. alter table recur1 add column f2 recur1[]; -- fails
  1247. create domain array_of_recur1 as recur1[];
  1248. alter table recur1 add column f2 array_of_recur1; -- fails
  1249. create temp table recur2 (f1 int, f2 recur1);
  1250. alter table recur1 add column f2 recur2; -- fails
  1251. alter table recur1 add column f2 int;
  1252. alter table recur1 alter column f2 type recur2; -- fails
  1253. -- SET STORAGE may need to add a TOAST table
  1254. create table test_storage (a text);
  1255. alter table test_storage alter a set storage plain;
  1256. alter table test_storage add b int default 0; -- rewrite table to remove its TOAST table
  1257. alter table test_storage alter a set storage extended; -- re-add TOAST table
  1258. select reltoastrelid <> 0 as has_toast_table
  1259. from pg_class
  1260. where oid = 'test_storage'::regclass;
  1261. -- test that SET STORAGE propagates to index correctly
  1262. create index test_storage_idx on test_storage (b, a);
  1263. alter table test_storage alter column a set storage external;
  1264. \d+ test_storage
  1265. \d+ test_storage_idx
  1266. -- ALTER COLUMN TYPE with a check constraint and a child table (bug #13779)
  1267. CREATE TABLE test_inh_check (a float check (a > 10.2), b float);
  1268. CREATE TABLE test_inh_check_child() INHERITS(test_inh_check);
  1269. \d test_inh_check
  1270. \d test_inh_check_child
  1271. select relname, conname, coninhcount, conislocal, connoinherit
  1272. from pg_constraint c, pg_class r
  1273. where relname like 'test_inh_check%' and c.conrelid = r.oid
  1274. order by 1, 2;
  1275. ALTER TABLE test_inh_check ALTER COLUMN a TYPE numeric;
  1276. \d test_inh_check
  1277. \d test_inh_check_child
  1278. select relname, conname, coninhcount, conislocal, connoinherit
  1279. from pg_constraint c, pg_class r
  1280. where relname like 'test_inh_check%' and c.conrelid = r.oid
  1281. order by 1, 2;
  1282. -- also try noinherit, local, and local+inherited cases
  1283. ALTER TABLE test_inh_check ADD CONSTRAINT bnoinherit CHECK (b > 100) NO INHERIT;
  1284. ALTER TABLE test_inh_check_child ADD CONSTRAINT blocal CHECK (b < 1000);
  1285. ALTER TABLE test_inh_check_child ADD CONSTRAINT bmerged CHECK (b > 1);
  1286. ALTER TABLE test_inh_check ADD CONSTRAINT bmerged CHECK (b > 1);
  1287. \d test_inh_check
  1288. \d test_inh_check_child
  1289. select relname, conname, coninhcount, conislocal, connoinherit
  1290. from pg_constraint c, pg_class r
  1291. where relname like 'test_inh_check%' and c.conrelid = r.oid
  1292. order by 1, 2;
  1293. ALTER TABLE test_inh_check ALTER COLUMN b TYPE numeric;
  1294. \d test_inh_check
  1295. \d test_inh_check_child
  1296. select relname, conname, coninhcount, conislocal, connoinherit
  1297. from pg_constraint c, pg_class r
  1298. where relname like 'test_inh_check%' and c.conrelid = r.oid
  1299. order by 1, 2;
  1300. -- ALTER COLUMN TYPE with different schema in children
  1301. -- Bug at https://postgr.es/m/20170102225618.GA10071@telsasoft.com
  1302. CREATE TABLE test_type_diff (f1 int);
  1303. CREATE TABLE test_type_diff_c (extra smallint) INHERITS (test_type_diff);
  1304. ALTER TABLE test_type_diff ADD COLUMN f2 int;
  1305. INSERT INTO test_type_diff_c VALUES (1, 2, 3);
  1306. ALTER TABLE test_type_diff ALTER COLUMN f2 TYPE bigint USING f2::bigint;
  1307. CREATE TABLE test_type_diff2 (int_two int2, int_four int4, int_eight int8);
  1308. CREATE TABLE test_type_diff2_c1 (int_four int4, int_eight int8, int_two int2);
  1309. CREATE TABLE test_type_diff2_c2 (int_eight int8, int_two int2, int_four int4);
  1310. CREATE TABLE test_type_diff2_c3 (int_two int2, int_four int4, int_eight int8);
  1311. ALTER TABLE test_type_diff2_c1 INHERIT test_type_diff2;
  1312. ALTER TABLE test_type_diff2_c2 INHERIT test_type_diff2;
  1313. ALTER TABLE test_type_diff2_c3 INHERIT test_type_diff2;
  1314. INSERT INTO test_type_diff2_c1 VALUES (1, 2, 3);
  1315. INSERT INTO test_type_diff2_c2 VALUES (4, 5, 6);
  1316. INSERT INTO test_type_diff2_c3 VALUES (7, 8, 9);
  1317. ALTER TABLE test_type_diff2 ALTER COLUMN int_four TYPE int8 USING int_four::int8;
  1318. -- whole-row references are disallowed
  1319. ALTER TABLE test_type_diff2 ALTER COLUMN int_four TYPE int4 USING (pg_column_size(test_type_diff2));
  1320. -- check for rollback of ANALYZE corrupting table property flags (bug #11638)
  1321. CREATE TABLE check_fk_presence_1 (id int PRIMARY KEY, t text);
  1322. CREATE TABLE check_fk_presence_2 (id int REFERENCES check_fk_presence_1, t text);
  1323. BEGIN;
  1324. ALTER TABLE check_fk_presence_2 DROP CONSTRAINT check_fk_presence_2_id_fkey;
  1325. ANALYZE check_fk_presence_2;
  1326. ROLLBACK;
  1327. \d check_fk_presence_2
  1328. DROP TABLE check_fk_presence_1, check_fk_presence_2;
  1329. -- check column addition within a view (bug #14876)
  1330. create table at_base_table(id int, stuff text);
  1331. insert into at_base_table values (23, 'skidoo');
  1332. create view at_view_1 as select * from at_base_table bt;
  1333. create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
  1334. \d+ at_view_1
  1335. \d+ at_view_2
  1336. explain (verbose, costs off) select * from at_view_2;
  1337. select * from at_view_2;
  1338. create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
  1339. \d+ at_view_1
  1340. \d+ at_view_2
  1341. explain (verbose, costs off) select * from at_view_2;
  1342. select * from at_view_2;
  1343. drop view at_view_2;
  1344. drop view at_view_1;
  1345. drop table at_base_table;
  1346. -- check adding a column not iself requiring a rewrite, together with
  1347. -- a column requiring a default (bug #16038)
  1348. -- ensure that rewrites aren't silently optimized away, removing the
  1349. -- value of the test
  1350. CREATE FUNCTION check_ddl_rewrite(p_tablename regclass, p_ddl text)
  1351. RETURNS boolean
  1352. LANGUAGE plpgsql AS $$
  1353. DECLARE
  1354. v_relfilenode oid;
  1355. BEGIN
  1356. v_relfilenode := relfilenode FROM pg_class WHERE oid = p_tablename;
  1357. EXECUTE p_ddl;
  1358. RETURN v_relfilenode <> (SELECT relfilenode FROM pg_class WHERE oid = p_tablename);
  1359. END;
  1360. $$;
  1361. CREATE TABLE rewrite_test(col text);
  1362. INSERT INTO rewrite_test VALUES ('something');
  1363. INSERT INTO rewrite_test VALUES (NULL);
  1364. -- empty[12] don't need rewrite, but notempty[12]_rewrite will force one
  1365. SELECT check_ddl_rewrite('rewrite_test', $$
  1366. ALTER TABLE rewrite_test
  1367. ADD COLUMN empty1 text,
  1368. ADD COLUMN notempty1_rewrite serial;
  1369. $$);
  1370. SELECT check_ddl_rewrite('rewrite_test', $$
  1371. ALTER TABLE rewrite_test
  1372. ADD COLUMN notempty2_rewrite serial,
  1373. ADD COLUMN empty2 text;
  1374. $$);
  1375. -- also check that fast defaults cause no problem, first without rewrite
  1376. SELECT check_ddl_rewrite('rewrite_test', $$
  1377. ALTER TABLE rewrite_test
  1378. ADD COLUMN empty3 text,
  1379. ADD COLUMN notempty3_norewrite int default 42;
  1380. $$);
  1381. SELECT check_ddl_rewrite('rewrite_test', $$
  1382. ALTER TABLE rewrite_test
  1383. ADD COLUMN notempty4_norewrite int default 42,
  1384. ADD COLUMN empty4 text;
  1385. $$);
  1386. -- then with rewrite
  1387. SELECT check_ddl_rewrite('rewrite_test', $$
  1388. ALTER TABLE rewrite_test
  1389. ADD COLUMN empty5 text,
  1390. ADD COLUMN notempty5_norewrite int default 42,
  1391. ADD COLUMN notempty5_rewrite serial;
  1392. $$);
  1393. SELECT check_ddl_rewrite('rewrite_test', $$
  1394. ALTER TABLE rewrite_test
  1395. ADD COLUMN notempty6_rewrite serial,
  1396. ADD COLUMN empty6 text,
  1397. ADD COLUMN notempty6_norewrite int default 42;
  1398. $$);
  1399. -- cleanup
  1400. DROP FUNCTION check_ddl_rewrite(regclass, text);
  1401. DROP TABLE rewrite_test;
  1402. --
  1403. -- lock levels
  1404. --
  1405. drop type lockmodes;
  1406. create type lockmodes as enum (
  1407. 'SIReadLock'
  1408. ,'AccessShareLock'
  1409. ,'RowShareLock'
  1410. ,'RowExclusiveLock'
  1411. ,'ShareUpdateExclusiveLock'
  1412. ,'ShareLock'
  1413. ,'ShareRowExclusiveLock'
  1414. ,'ExclusiveLock'
  1415. ,'AccessExclusiveLock'
  1416. );
  1417. drop view my_locks;
  1418. create or replace view my_locks as
  1419. select case when c.relname like 'pg_toast%' then 'pg_toast' else c.relname end, max(mode::lockmodes) as max_lockmode
  1420. from pg_locks l join pg_class c on l.relation = c.oid
  1421. where virtualtransaction = (
  1422. select virtualtransaction
  1423. from pg_locks
  1424. where transactionid = pg_current_xact_id()::xid)
  1425. and locktype = 'relation'
  1426. and relnamespace != (select oid from pg_namespace where nspname = 'pg_catalog')
  1427. and c.relname != 'my_locks'
  1428. group by c.relname;
  1429. create table alterlock (f1 int primary key, f2 text);
  1430. insert into alterlock values (1, 'foo');
  1431. create table alterlock2 (f3 int primary key, f1 int);
  1432. insert into alterlock2 values (1, 1);
  1433. begin; alter table alterlock alter column f2 set statistics 150;
  1434. select * from my_locks order by 1;
  1435. rollback;
  1436. begin; alter table alterlock cluster on alterlock_pkey;
  1437. select * from my_locks order by 1;
  1438. commit;
  1439. begin; alter table alterlock set without cluster;
  1440. select * from my_locks order by 1;
  1441. commit;
  1442. begin; alter table alterlock set (fillfactor = 100);
  1443. select * from my_locks order by 1;
  1444. commit;
  1445. begin; alter table alterlock reset (fillfactor);
  1446. select * from my_locks order by 1;
  1447. commit;
  1448. begin; alter table alterlock set (toast.autovacuum_enabled = off);
  1449. select * from my_locks order by 1;
  1450. commit;
  1451. begin; alter table alterlock set (autovacuum_enabled = off);
  1452. select * from my_locks order by 1;
  1453. commit;
  1454. begin; alter table alterlock alter column f2 set (n_distinct = 1);
  1455. select * from my_locks order by 1;
  1456. rollback;
  1457. -- test that mixing options with different lock levels works as expected
  1458. begin; alter table alterlock set (autovacuum_enabled = off, fillfactor = 80);
  1459. select * from my_locks order by 1;
  1460. commit;
  1461. begin; alter table alterlock alter column f2 set storage extended;
  1462. select * from my_locks order by 1;
  1463. rollback;
  1464. begin; alter table alterlock alter column f2 set default 'x';
  1465. select * from my_locks order by 1;
  1466. rollback;
  1467. begin;
  1468. create trigger ttdummy
  1469. before delete or update on alterlock
  1470. for each row
  1471. execute procedure
  1472. ttdummy (1, 1);
  1473. select * from my_locks order by 1;
  1474. rollback;
  1475. begin;
  1476. select * from my_locks order by 1;
  1477. alter table alterlock2 add foreign key (f1) references alterlock (f1);
  1478. select * from my_locks order by 1;
  1479. rollback;
  1480. begin;
  1481. alter table alterlock2
  1482. add constraint alterlock2nv foreign key (f1) references alterlock (f1) NOT VALID;
  1483. select * from my_locks order by 1;
  1484. commit;
  1485. begin;
  1486. alter table alterlock2 validate constraint alterlock2nv;
  1487. select * from my_locks order by 1;
  1488. rollback;
  1489. create or replace view my_locks as
  1490. select case when c.relname like 'pg_toast%' then 'pg_toast' else c.relname end, max(mode::lockmodes) as max_lockmode
  1491. from pg_locks l join pg_class c on l.relation = c.oid
  1492. where virtualtransaction = (
  1493. select virtualtransaction
  1494. from pg_locks
  1495. where transactionid = pg_current_xact_id()::xid)
  1496. and locktype = 'relation'
  1497. and relnamespace != (select oid from pg_namespace where nspname = 'pg_catalog')
  1498. and c.relname = 'my_locks'
  1499. group by c.relname;
  1500. -- raise exception
  1501. alter table my_locks set (autovacuum_enabled = false);
  1502. alter view my_locks set (autovacuum_enabled = false);
  1503. alter table my_locks reset (autovacuum_enabled);
  1504. alter view my_locks reset (autovacuum_enabled);
  1505. begin;
  1506. alter view my_locks set (security_barrier=off);
  1507. select * from my_locks order by 1;
  1508. alter view my_locks reset (security_barrier);
  1509. rollback;
  1510. -- this test intentionally applies the ALTER TABLE command against a view, but
  1511. -- uses a view option so we expect this to succeed. This form of SQL is
  1512. -- accepted for historical reasons, as shown in the docs for ALTER VIEW
  1513. begin;
  1514. alter table my_locks set (security_barrier=off);
  1515. select * from my_locks order by 1;
  1516. alter table my_locks reset (security_barrier);
  1517. rollback;
  1518. -- cleanup
  1519. drop table alterlock2;
  1520. drop table alterlock;
  1521. drop view my_locks;
  1522. drop type lockmodes;
  1523. --
  1524. -- alter function
  1525. --
  1526. create function test_strict(text) returns text as
  1527. 'select coalesce($1, ''got passed a null'');'
  1528. language sql returns null on null input;
  1529. select test_strict(NULL);
  1530. alter function test_strict(text) called on null input;
  1531. select test_strict(NULL);
  1532. create function non_strict(text) returns text as
  1533. 'select coalesce($1, ''got passed a null'');'
  1534. language sql called on null input;
  1535. select non_strict(NULL);
  1536. alter function non_strict(text) returns null on null input;
  1537. select non_strict(NULL);
  1538. --
  1539. -- alter object set schema
  1540. --
  1541. create schema alter1;
  1542. create schema alter2;
  1543. create table alter1.t1(f1 serial primary key, f2 int check (f2 > 0));
  1544. create view alter1.v1 as select * from alter1.t1;
  1545. create function alter1.plus1(int) returns int as 'select $1+1' language sql;
  1546. create domain alter1.posint integer check (value > 0);
  1547. create type alter1.ctype as (f1 int, f2 text);
  1548. create function alter1.same(alter1.ctype, alter1.ctype) returns boolean language sql
  1549. as 'select $1.f1 is not distinct from $2.f1 and $1.f2 is not distinct from $2.f2';
  1550. create operator alter1.=(procedure = alter1.same, leftarg = alter1.ctype, rightarg = alter1.ctype);
  1551. create operator class alter1.ctype_hash_ops default for type alter1.ctype using hash as
  1552. operator 1 alter1.=(alter1.ctype, alter1.ctype);
  1553. create conversion alter1.latin1_to_utf8 for 'latin1' to 'utf8' from iso8859_1_to_utf8;
  1554. create text search parser alter1.prs(start = prsd_start, gettoken = prsd_nexttoken, end = prsd_end, lextypes = prsd_lextype);
  1555. create text search configuration alter1.cfg(parser = alter1.prs);
  1556. create text search template alter1.tmpl(init = dsimple_init, lexize = dsimple_lexize);
  1557. create text search dictionary alter1.dict(template = alter1.tmpl);
  1558. insert into alter1.t1(f2) values(11);
  1559. insert into alter1.t1(f2) values(12);
  1560. alter table alter1.t1 set schema alter1; -- no-op, same schema
  1561. alter table alter1.t1 set schema alter2;
  1562. alter table alter1.v1 set schema alter2;
  1563. alter function alter1.plus1(int) set schema alter2;
  1564. alter domain alter1.posint set schema alter2;
  1565. alter operator class alter1.ctype_hash_ops using hash set schema alter2;
  1566. alter operator family alter1.ctype_hash_ops using hash set schema alter2;
  1567. alter operator alter1.=(alter1.ctype, alter1.ctype) set schema alter2;
  1568. alter function alter1.same(alter1.ctype, alter1.ctype) set schema alter2;
  1569. alter type alter1.ctype set schema alter1; -- no-op, same schema
  1570. alter type alter1.ctype set schema alter2;
  1571. alter conversion alter1.latin1_to_utf8 set schema alter2;
  1572. alter text search parser alter1.prs set schema alter2;
  1573. alter text search configuration alter1.cfg set schema alter2;
  1574. alter text search template alter1.tmpl set schema alter2;
  1575. alter text search dictionary alter1.dict set schema alter2;
  1576. -- this should succeed because nothing is left in alter1
  1577. drop schema alter1;
  1578. insert into alter2.t1(f2) values(13);
  1579. insert into alter2.t1(f2) values(14);
  1580. select * from alter2.t1;
  1581. select * from alter2.v1;
  1582. select alter2.plus1(41);
  1583. -- clean up
  1584. drop schema alter2 cascade;
  1585. --
  1586. -- composite types
  1587. --
  1588. CREATE TYPE test_type AS (a int);
  1589. \d test_type
  1590. ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails
  1591. ALTER TYPE test_type ADD ATTRIBUTE b text;
  1592. \d test_type
  1593. ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails
  1594. ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar;
  1595. \d test_type
  1596. ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer;
  1597. \d test_type
  1598. ALTER TYPE test_type DROP ATTRIBUTE b;
  1599. \d test_type
  1600. ALTER TYPE test_type DROP ATTRIBUTE c; -- fails
  1601. ALTER TYPE test_type DROP ATTRIBUTE IF EXISTS c;
  1602. ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean;
  1603. \d test_type
  1604. ALTER TYPE test_type RENAME ATTRIBUTE a TO aa;
  1605. ALTER TYPE test_type RENAME ATTRIBUTE d TO dd;
  1606. \d test_type
  1607. DROP TYPE test_type;
  1608. CREATE TYPE test_type1 AS (a int, b text);
  1609. CREATE TABLE test_tbl1 (x int, y test_type1);
  1610. ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
  1611. CREATE TYPE test_type2 AS (a int, b text);
  1612. CREATE TABLE test_tbl2 OF test_type2;
  1613. CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
  1614. \d test_type2
  1615. \d test_tbl2
  1616. ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
  1617. ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
  1618. \d test_type2
  1619. \d test_tbl2
  1620. ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
  1621. ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
  1622. \d test_type2
  1623. \d test_tbl2
  1624. ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
  1625. ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
  1626. \d test_type2
  1627. \d test_tbl2
  1628. ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
  1629. ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
  1630. \d test_type2
  1631. \d test_tbl2
  1632. \d test_tbl2_subclass
  1633. DROP TABLE test_tbl2_subclass;
  1634. CREATE TYPE test_typex AS (a int, b text);
  1635. CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
  1636. ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
  1637. ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
  1638. \d test_tblx
  1639. DROP TABLE test_tblx;
  1640. DROP TYPE test_typex;
  1641. -- This test isn't that interesting on its own, but the purpose is to leave
  1642. -- behind a table to test pg_upgrade with. The table has a composite type
  1643. -- column in it, and the composite type has a dropped attribute.
  1644. CREATE TYPE test_type3 AS (a int);
  1645. CREATE TABLE test_tbl3 (c) AS SELECT '(1)'::test_type3;
  1646. ALTER TYPE test_type3 DROP ATTRIBUTE a, ADD ATTRIBUTE b int;
  1647. CREATE TYPE test_type_empty AS ();
  1648. DROP TYPE test_type_empty;
  1649. --
  1650. -- typed tables: OF / NOT OF
  1651. --
  1652. CREATE TYPE tt_t0 AS (z inet, x int, y numeric(8,2));
  1653. ALTER TYPE tt_t0 DROP ATTRIBUTE z;
  1654. CREATE TABLE tt0 (x int NOT NULL, y numeric(8,2)); -- OK
  1655. CREATE TABLE tt1 (x int, y bigint); -- wrong base type
  1656. CREATE TABLE tt2 (x int, y numeric(9,2)); -- wrong typmod
  1657. CREATE TABLE tt3 (y numeric(8,2), x int); -- wrong column order
  1658. CREATE TABLE tt4 (x int); -- too few columns
  1659. CREATE TABLE tt5 (x int, y numeric(8,2), z int); -- too few columns
  1660. CREATE TABLE tt6 () INHERITS (tt0); -- can't have a parent
  1661. CREATE TABLE tt7 (x int, q text, y numeric(8,2));
  1662. ALTER TABLE tt7 DROP q; -- OK
  1663. ALTER TABLE tt0 OF tt_t0;
  1664. ALTER TABLE tt1 OF tt_t0;
  1665. ALTER TABLE tt2 OF tt_t0;
  1666. ALTER TABLE tt3 OF tt_t0;
  1667. ALTER TABLE tt4 OF tt_t0;
  1668. ALTER TABLE tt5 OF tt_t0;
  1669. ALTER TABLE tt6 OF tt_t0;
  1670. ALTER TABLE tt7 OF tt_t0;
  1671. CREATE TYPE tt_t1 AS (x int, y numeric(8,2));
  1672. ALTER TABLE tt7 OF tt_t1; -- reassign an already-typed table
  1673. ALTER TABLE tt7 NOT OF;
  1674. \d tt7
  1675. -- make sure we can drop a constraint on the parent but it remains on the child
  1676. CREATE TABLE test_drop_constr_parent (c text CHECK (c IS NOT NULL));
  1677. CREATE TABLE test_drop_constr_child () INHERITS (test_drop_constr_parent);
  1678. ALTER TABLE ONLY test_drop_constr_parent DROP CONSTRAINT "test_drop_constr_parent_c_check";
  1679. -- should fail
  1680. INSERT INTO test_drop_constr_child (c) VALUES (NULL);
  1681. DROP TABLE test_drop_constr_parent CASCADE;
  1682. --
  1683. -- IF EXISTS test
  1684. --
  1685. ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
  1686. ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
  1687. ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
  1688. ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
  1689. ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
  1690. ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
  1691. CREATE TABLE tt8(a int);
  1692. CREATE SCHEMA alter2;
  1693. ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
  1694. ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
  1695. ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
  1696. ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
  1697. ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
  1698. ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
  1699. \d alter2.tt8
  1700. DROP TABLE alter2.tt8;
  1701. DROP SCHEMA alter2;
  1702. --
  1703. -- Check conflicts between index and CHECK constraint names
  1704. --
  1705. CREATE TABLE tt9(c integer);
  1706. ALTER TABLE tt9 ADD CHECK(c > 1);
  1707. ALTER TABLE tt9 ADD CHECK(c > 2); -- picks nonconflicting name
  1708. ALTER TABLE tt9 ADD CONSTRAINT foo CHECK(c > 3);
  1709. ALTER TABLE tt9 ADD CONSTRAINT foo CHECK(c > 4); -- fail, dup name
  1710. ALTER TABLE tt9 ADD UNIQUE(c);
  1711. ALTER TABLE tt9 ADD UNIQUE(c); -- picks nonconflicting name
  1712. ALTER TABLE tt9 ADD CONSTRAINT tt9_c_key UNIQUE(c); -- fail, dup name
  1713. ALTER TABLE tt9 ADD CONSTRAINT foo UNIQUE(c); -- fail, dup name
  1714. ALTER TABLE tt9 ADD CONSTRAINT tt9_c_key CHECK(c > 5); -- fail, dup name
  1715. ALTER TABLE tt9 ADD CONSTRAINT tt9_c_key2 CHECK(c > 6);
  1716. ALTER TABLE tt9 ADD UNIQUE(c); -- picks nonconflicting name
  1717. \d tt9
  1718. DROP TABLE tt9;
  1719. -- Check that comments on constraints and indexes are not lost at ALTER TABLE.
  1720. CREATE TABLE comment_test (
  1721. id int,
  1722. positive_col int CHECK (positive_col > 0),
  1723. indexed_col int,
  1724. CONSTRAINT comment_test_pk PRIMARY KEY (id));
  1725. CREATE INDEX comment_test_index ON comment_test(indexed_col);
  1726. COMMENT ON COLUMN comment_test.id IS 'Column ''id'' on comment_test';
  1727. COMMENT ON INDEX comment_test_index IS 'Simple index on comment_test';
  1728. COMMENT ON CONSTRAINT comment_test_positive_col_check ON comment_test IS 'CHECK constraint on comment_test.positive_col';
  1729. COMMENT ON CONSTRAINT comment_test_pk ON comment_test IS 'PRIMARY KEY constraint of comment_test';
  1730. COMMENT ON INDEX comment_test_pk IS 'Index backing the PRIMARY KEY of comment_test';
  1731. SELECT col_description('comment_test'::regclass, 1) as comment;
  1732. SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test'::regclass ORDER BY 1, 2;
  1733. SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test'::regclass ORDER BY 1, 2;
  1734. -- Change the datatype of all the columns. ALTER TABLE is optimized to not
  1735. -- rebuild an index if the new data type is binary compatible with the old
  1736. -- one. Check do a dummy ALTER TABLE that doesn't change the datatype
  1737. -- first, to test that no-op codepath, and another one that does.
  1738. ALTER TABLE comment_test ALTER COLUMN indexed_col SET DATA TYPE int;
  1739. ALTER TABLE comment_test ALTER COLUMN indexed_col SET DATA TYPE text;
  1740. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int;
  1741. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
  1742. ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE int;
  1743. ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE bigint;
  1744. -- Check that the comments are intact.
  1745. SELECT col_description('comment_test'::regclass, 1) as comment;
  1746. SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test'::regclass ORDER BY 1, 2;
  1747. SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test'::regclass ORDER BY 1, 2;
  1748. -- Check compatibility for foreign keys and comments. This is done
  1749. -- separately as rebuilding the column type of the parent leads
  1750. -- to an error and would reduce the test scope.
  1751. CREATE TABLE comment_test_child (
  1752. id text CONSTRAINT comment_test_child_fk REFERENCES comment_test);
  1753. CREATE INDEX comment_test_child_fk ON comment_test_child(id);
  1754. COMMENT ON COLUMN comment_test_child.id IS 'Column ''id'' on comment_test_child';
  1755. COMMENT ON INDEX comment_test_child_fk IS 'Index backing the FOREIGN KEY of comment_test_child';
  1756. COMMENT ON CONSTRAINT comment_test_child_fk ON comment_test_child IS 'FOREIGN KEY constraint of comment_test_child';
  1757. -- Change column type of parent
  1758. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
  1759. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int USING id::integer;
  1760. -- Comments should be intact
  1761. SELECT col_description('comment_test_child'::regclass, 1) as comment;
  1762. SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test_child'::regclass ORDER BY 1, 2;
  1763. SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test_child'::regclass ORDER BY 1, 2;
  1764. -- Check that we map relation oids to filenodes and back correctly. Only
  1765. -- display bad mappings so the test output doesn't change all the time. A
  1766. -- filenode function call can return NULL for a relation dropped concurrently
  1767. -- with the call's surrounding query, so ignore a NULL mapped_oid for
  1768. -- relations that no longer exist after all calls finish.
  1769. CREATE TEMP TABLE filenode_mapping AS
  1770. SELECT
  1771. oid, mapped_oid, reltablespace, relfilenode, relname
  1772. FROM pg_class,
  1773. pg_filenode_relation(reltablespace, pg_relation_filenode(oid)) AS mapped_oid
  1774. WHERE relkind IN ('r', 'i', 'S', 't', 'm') AND mapped_oid IS DISTINCT FROM oid;
  1775. SELECT m.* FROM filenode_mapping m LEFT JOIN pg_class c ON c.oid = m.oid
  1776. WHERE c.oid IS NOT NULL OR m.mapped_oid IS NOT NULL;
  1777. -- Checks on creating and manipulation of user defined relations in
  1778. -- pg_catalog.
  1779. SHOW allow_system_table_mods;
  1780. -- disallowed because of search_path issues with pg_dump
  1781. CREATE TABLE pg_catalog.new_system_table();
  1782. -- instead create in public first, move to catalog
  1783. CREATE TABLE new_system_table(id serial primary key, othercol text);
  1784. ALTER TABLE new_system_table SET SCHEMA pg_catalog;
  1785. ALTER TABLE new_system_table SET SCHEMA public;
  1786. ALTER TABLE new_system_table SET SCHEMA pg_catalog;
  1787. -- will be ignored -- already there:
  1788. ALTER TABLE new_system_table SET SCHEMA pg_catalog;
  1789. ALTER TABLE new_system_table RENAME TO old_system_table;
  1790. CREATE INDEX old_system_table__othercol ON old_system_table (othercol);
  1791. INSERT INTO old_system_table(othercol) VALUES ('somedata'), ('otherdata');
  1792. UPDATE old_system_table SET id = -id;
  1793. DELETE FROM old_system_table WHERE othercol = 'somedata';
  1794. TRUNCATE old_system_table;
  1795. ALTER TABLE old_system_table DROP CONSTRAINT new_system_table_pkey;
  1796. ALTER TABLE old_system_table DROP COLUMN othercol;
  1797. DROP TABLE old_system_table;
  1798. -- set logged
  1799. CREATE UNLOGGED TABLE unlogged1(f1 SERIAL PRIMARY KEY, f2 TEXT);
  1800. -- check relpersistence of an unlogged table
  1801. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged1'
  1802. UNION ALL
  1803. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^unlogged1'
  1804. UNION ALL
  1805. SELECT 'toast index', ri.relkind, ri.relpersistence FROM pg_class r join pg_class t ON t.oid = r.reltoastrelid JOIN pg_index i ON i.indrelid = t.oid JOIN pg_class ri ON ri.oid = i.indexrelid WHERE r.relname ~ '^unlogged1'
  1806. ORDER BY relname;
  1807. CREATE UNLOGGED TABLE unlogged2(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES unlogged1); -- foreign key
  1808. CREATE UNLOGGED TABLE unlogged3(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES unlogged3); -- self-referencing foreign key
  1809. ALTER TABLE unlogged3 SET LOGGED; -- skip self-referencing foreign key
  1810. ALTER TABLE unlogged2 SET LOGGED; -- fails because a foreign key to an unlogged table exists
  1811. ALTER TABLE unlogged1 SET LOGGED;
  1812. -- check relpersistence of an unlogged table after changing to permanent
  1813. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged1'
  1814. UNION ALL
  1815. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^unlogged1'
  1816. UNION ALL
  1817. SELECT 'toast index', ri.relkind, ri.relpersistence FROM pg_class r join pg_class t ON t.oid = r.reltoastrelid JOIN pg_index i ON i.indrelid = t.oid JOIN pg_class ri ON ri.oid = i.indexrelid WHERE r.relname ~ '^unlogged1'
  1818. ORDER BY relname;
  1819. ALTER TABLE unlogged1 SET LOGGED; -- silently do nothing
  1820. DROP TABLE unlogged3;
  1821. DROP TABLE unlogged2;
  1822. DROP TABLE unlogged1;
  1823. -- set unlogged
  1824. CREATE TABLE logged1(f1 SERIAL PRIMARY KEY, f2 TEXT);
  1825. -- check relpersistence of a permanent table
  1826. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^logged1'
  1827. UNION ALL
  1828. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^logged1'
  1829. UNION ALL
  1830. SELECT 'toast index', ri.relkind, ri.relpersistence FROM pg_class r join pg_class t ON t.oid = r.reltoastrelid JOIN pg_index i ON i.indrelid = t.oid JOIN pg_class ri ON ri.oid = i.indexrelid WHERE r.relname ~ '^logged1'
  1831. ORDER BY relname;
  1832. CREATE TABLE logged2(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged1); -- foreign key
  1833. CREATE TABLE logged3(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged3); -- self-referencing foreign key
  1834. ALTER TABLE logged1 SET UNLOGGED; -- fails because a foreign key from a permanent table exists
  1835. ALTER TABLE logged3 SET UNLOGGED; -- skip self-referencing foreign key
  1836. ALTER TABLE logged2 SET UNLOGGED;
  1837. ALTER TABLE logged1 SET UNLOGGED;
  1838. -- check relpersistence of a permanent table after changing to unlogged
  1839. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^logged1'
  1840. UNION ALL
  1841. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^logged1'
  1842. UNION ALL
  1843. SELECT 'toast index', ri.relkind, ri.relpersistence FROM pg_class r join pg_class t ON t.oid = r.reltoastrelid JOIN pg_index i ON i.indrelid = t.oid JOIN pg_class ri ON ri.oid = i.indexrelid WHERE r.relname ~ '^logged1'
  1844. ORDER BY relname;
  1845. ALTER TABLE logged1 SET UNLOGGED; -- silently do nothing
  1846. DROP TABLE logged3;
  1847. DROP TABLE logged2;
  1848. DROP TABLE logged1;
  1849. -- test ADD COLUMN IF NOT EXISTS
  1850. CREATE TABLE test_add_column(c1 integer);
  1851. \d test_add_column
  1852. ALTER TABLE test_add_column
  1853. ADD COLUMN c2 integer;
  1854. \d test_add_column
  1855. ALTER TABLE test_add_column
  1856. ADD COLUMN c2 integer; -- fail because c2 already exists
  1857. ALTER TABLE ONLY test_add_column
  1858. ADD COLUMN c2 integer; -- fail because c2 already exists
  1859. \d test_add_column
  1860. ALTER TABLE test_add_column
  1861. ADD COLUMN IF NOT EXISTS c2 integer; -- skipping because c2 already exists
  1862. ALTER TABLE ONLY test_add_column
  1863. ADD COLUMN IF NOT EXISTS c2 integer; -- skipping because c2 already exists
  1864. \d test_add_column
  1865. ALTER TABLE test_add_column
  1866. ADD COLUMN c2 integer, -- fail because c2 already exists
  1867. ADD COLUMN c3 integer primary key;
  1868. \d test_add_column
  1869. ALTER TABLE test_add_column
  1870. ADD COLUMN IF NOT EXISTS c2 integer, -- skipping because c2 already exists
  1871. ADD COLUMN c3 integer primary key;
  1872. \d test_add_column
  1873. ALTER TABLE test_add_column
  1874. ADD COLUMN IF NOT EXISTS c2 integer, -- skipping because c2 already exists
  1875. ADD COLUMN IF NOT EXISTS c3 integer primary key; -- skipping because c3 already exists
  1876. \d test_add_column
  1877. ALTER TABLE test_add_column
  1878. ADD COLUMN IF NOT EXISTS c2 integer, -- skipping because c2 already exists
  1879. ADD COLUMN IF NOT EXISTS c3 integer, -- skipping because c3 already exists
  1880. ADD COLUMN c4 integer REFERENCES test_add_column;
  1881. \d test_add_column
  1882. ALTER TABLE test_add_column
  1883. ADD COLUMN IF NOT EXISTS c4 integer REFERENCES test_add_column;
  1884. \d test_add_column
  1885. ALTER TABLE test_add_column
  1886. ADD COLUMN IF NOT EXISTS c5 SERIAL CHECK (c5 > 8);
  1887. \d test_add_column
  1888. ALTER TABLE test_add_column
  1889. ADD COLUMN IF NOT EXISTS c5 SERIAL CHECK (c5 > 10);
  1890. \d test_add_column*
  1891. DROP TABLE test_add_column;
  1892. \d test_add_column*
  1893. -- assorted cases with multiple ALTER TABLE steps
  1894. CREATE TABLE ataddindex(f1 INT);
  1895. INSERT INTO ataddindex VALUES (42), (43);
  1896. CREATE UNIQUE INDEX ataddindexi0 ON ataddindex(f1);
  1897. ALTER TABLE ataddindex
  1898. ADD PRIMARY KEY USING INDEX ataddindexi0,
  1899. ALTER f1 TYPE BIGINT;
  1900. \d ataddindex
  1901. DROP TABLE ataddindex;
  1902. CREATE TABLE ataddindex(f1 VARCHAR(10));
  1903. INSERT INTO ataddindex(f1) VALUES ('foo'), ('a');
  1904. ALTER TABLE ataddindex
  1905. ALTER f1 SET DATA TYPE TEXT,
  1906. ADD EXCLUDE ((f1 LIKE 'a') WITH =);
  1907. \d ataddindex
  1908. DROP TABLE ataddindex;
  1909. CREATE TABLE ataddindex(id int, ref_id int);
  1910. ALTER TABLE ataddindex
  1911. ADD PRIMARY KEY (id),
  1912. ADD FOREIGN KEY (ref_id) REFERENCES ataddindex;
  1913. \d ataddindex
  1914. DROP TABLE ataddindex;
  1915. CREATE TABLE ataddindex(id int, ref_id int);
  1916. ALTER TABLE ataddindex
  1917. ADD UNIQUE (id),
  1918. ADD FOREIGN KEY (ref_id) REFERENCES ataddindex (id);
  1919. \d ataddindex
  1920. DROP TABLE ataddindex;
  1921. -- unsupported constraint types for partitioned tables
  1922. CREATE TABLE partitioned (
  1923. a int,
  1924. b int
  1925. ) PARTITION BY RANGE (a, (a+b+1));
  1926. ALTER TABLE partitioned ADD EXCLUDE USING gist (a WITH &&);
  1927. -- cannot drop column that is part of the partition key
  1928. ALTER TABLE partitioned DROP COLUMN a;
  1929. ALTER TABLE partitioned ALTER COLUMN a TYPE char(5);
  1930. ALTER TABLE partitioned DROP COLUMN b;
  1931. ALTER TABLE partitioned ALTER COLUMN b TYPE char(5);
  1932. -- partitioned table cannot participate in regular inheritance
  1933. CREATE TABLE nonpartitioned (
  1934. a int,
  1935. b int
  1936. );
  1937. ALTER TABLE partitioned INHERIT nonpartitioned;
  1938. ALTER TABLE nonpartitioned INHERIT partitioned;
  1939. -- cannot add NO INHERIT constraint to partitioned tables
  1940. ALTER TABLE partitioned ADD CONSTRAINT chk_a CHECK (a > 0) NO INHERIT;
  1941. DROP TABLE partitioned, nonpartitioned;
  1942. --
  1943. -- ATTACH PARTITION
  1944. --
  1945. -- check that target table is partitioned
  1946. CREATE TABLE unparted (
  1947. a int
  1948. );
  1949. CREATE TABLE fail_part (like unparted);
  1950. ALTER TABLE unparted ATTACH PARTITION fail_part FOR VALUES IN ('a');
  1951. DROP TABLE unparted, fail_part;
  1952. -- check that partition bound is compatible
  1953. CREATE TABLE list_parted (
  1954. a int NOT NULL,
  1955. b char(2) COLLATE "C",
  1956. CONSTRAINT check_a CHECK (a > 0)
  1957. ) PARTITION BY LIST (a);
  1958. CREATE TABLE fail_part (LIKE list_parted);
  1959. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES FROM (1) TO (10);
  1960. DROP TABLE fail_part;
  1961. -- check that the table being attached exists
  1962. ALTER TABLE list_parted ATTACH PARTITION nonexistent FOR VALUES IN (1);
  1963. -- check ownership of the source table
  1964. CREATE ROLE regress_test_me;
  1965. CREATE ROLE regress_test_not_me;
  1966. CREATE TABLE not_owned_by_me (LIKE list_parted);
  1967. ALTER TABLE not_owned_by_me OWNER TO regress_test_not_me;
  1968. SET SESSION AUTHORIZATION regress_test_me;
  1969. CREATE TABLE owned_by_me (
  1970. a int
  1971. ) PARTITION BY LIST (a);
  1972. ALTER TABLE owned_by_me ATTACH PARTITION not_owned_by_me FOR VALUES IN (1);
  1973. RESET SESSION AUTHORIZATION;
  1974. DROP TABLE owned_by_me, not_owned_by_me;
  1975. DROP ROLE regress_test_not_me;
  1976. DROP ROLE regress_test_me;
  1977. -- check that the table being attached is not part of regular inheritance
  1978. CREATE TABLE parent (LIKE list_parted);
  1979. CREATE TABLE child () INHERITS (parent);
  1980. ALTER TABLE list_parted ATTACH PARTITION child FOR VALUES IN (1);
  1981. ALTER TABLE list_parted ATTACH PARTITION parent FOR VALUES IN (1);
  1982. DROP TABLE parent CASCADE;
  1983. -- check any TEMP-ness
  1984. CREATE TEMP TABLE temp_parted (a int) PARTITION BY LIST (a);
  1985. CREATE TABLE perm_part (a int);
  1986. ALTER TABLE temp_parted ATTACH PARTITION perm_part FOR VALUES IN (1);
  1987. DROP TABLE temp_parted, perm_part;
  1988. -- check that the table being attached is not a typed table
  1989. CREATE TYPE mytype AS (a int);
  1990. CREATE TABLE fail_part OF mytype;
  1991. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  1992. DROP TYPE mytype CASCADE;
  1993. -- check that the table being attached has only columns present in the parent
  1994. CREATE TABLE fail_part (like list_parted, c int);
  1995. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  1996. DROP TABLE fail_part;
  1997. -- check that the table being attached has every column of the parent
  1998. CREATE TABLE fail_part (a int NOT NULL);
  1999. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  2000. DROP TABLE fail_part;
  2001. -- check that columns match in type, collation and NOT NULL status
  2002. CREATE TABLE fail_part (
  2003. b char(3),
  2004. a int NOT NULL
  2005. );
  2006. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  2007. ALTER TABLE fail_part ALTER b TYPE char (2) COLLATE "POSIX";
  2008. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  2009. DROP TABLE fail_part;
  2010. -- check that the table being attached has all constraints of the parent
  2011. CREATE TABLE fail_part (
  2012. b char(2) COLLATE "C",
  2013. a int NOT NULL
  2014. );
  2015. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  2016. -- check that the constraint matches in definition with parent's constraint
  2017. ALTER TABLE fail_part ADD CONSTRAINT check_a CHECK (a >= 0);
  2018. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  2019. DROP TABLE fail_part;
  2020. -- check the attributes and constraints after partition is attached
  2021. CREATE TABLE part_1 (
  2022. a int NOT NULL,
  2023. b char(2) COLLATE "C",
  2024. CONSTRAINT check_a CHECK (a > 0)
  2025. );
  2026. ALTER TABLE list_parted ATTACH PARTITION part_1 FOR VALUES IN (1);
  2027. -- attislocal and conislocal are always false for merged attributes and constraints respectively.
  2028. SELECT attislocal, attinhcount FROM pg_attribute WHERE attrelid = 'part_1'::regclass AND attnum > 0;
  2029. SELECT conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_1'::regclass AND conname = 'check_a';
  2030. -- check that the new partition won't overlap with an existing partition
  2031. CREATE TABLE fail_part (LIKE part_1 INCLUDING CONSTRAINTS);
  2032. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  2033. DROP TABLE fail_part;
  2034. -- check that an existing table can be attached as a default partition
  2035. CREATE TABLE def_part (LIKE list_parted INCLUDING CONSTRAINTS);
  2036. ALTER TABLE list_parted ATTACH PARTITION def_part DEFAULT;
  2037. -- check attaching default partition fails if a default partition already
  2038. -- exists
  2039. CREATE TABLE fail_def_part (LIKE part_1 INCLUDING CONSTRAINTS);
  2040. ALTER TABLE list_parted ATTACH PARTITION fail_def_part DEFAULT;
  2041. -- check validation when attaching list partitions
  2042. CREATE TABLE list_parted2 (
  2043. a int,
  2044. b char
  2045. ) PARTITION BY LIST (a);
  2046. -- check that violating rows are correctly reported
  2047. CREATE TABLE part_2 (LIKE list_parted2);
  2048. INSERT INTO part_2 VALUES (3, 'a');
  2049. ALTER TABLE list_parted2 ATTACH PARTITION part_2 FOR VALUES IN (2);
  2050. -- should be ok after deleting the bad row
  2051. DELETE FROM part_2;
  2052. ALTER TABLE list_parted2 ATTACH PARTITION part_2 FOR VALUES IN (2);
  2053. -- check partition cannot be attached if default has some row for its values
  2054. CREATE TABLE list_parted2_def PARTITION OF list_parted2 DEFAULT;
  2055. INSERT INTO list_parted2_def VALUES (11, 'z');
  2056. CREATE TABLE part_3 (LIKE list_parted2);
  2057. ALTER TABLE list_parted2 ATTACH PARTITION part_3 FOR VALUES IN (11);
  2058. -- should be ok after deleting the bad row
  2059. DELETE FROM list_parted2_def WHERE a = 11;
  2060. ALTER TABLE list_parted2 ATTACH PARTITION part_3 FOR VALUES IN (11);
  2061. -- adding constraints that describe the desired partition constraint
  2062. -- (or more restrictive) will help skip the validation scan
  2063. CREATE TABLE part_3_4 (
  2064. LIKE list_parted2,
  2065. CONSTRAINT check_a CHECK (a IN (3))
  2066. );
  2067. -- however, if a list partition does not accept nulls, there should be
  2068. -- an explicit NOT NULL constraint on the partition key column for the
  2069. -- validation scan to be skipped;
  2070. ALTER TABLE list_parted2 ATTACH PARTITION part_3_4 FOR VALUES IN (3, 4);
  2071. -- adding a NOT NULL constraint will cause the scan to be skipped
  2072. ALTER TABLE list_parted2 DETACH PARTITION part_3_4;
  2073. ALTER TABLE part_3_4 ALTER a SET NOT NULL;
  2074. ALTER TABLE list_parted2 ATTACH PARTITION part_3_4 FOR VALUES IN (3, 4);
  2075. -- check if default partition scan skipped
  2076. ALTER TABLE list_parted2_def ADD CONSTRAINT check_a CHECK (a IN (5, 6));
  2077. CREATE TABLE part_55_66 PARTITION OF list_parted2 FOR VALUES IN (55, 66);
  2078. -- check validation when attaching range partitions
  2079. CREATE TABLE range_parted (
  2080. a int,
  2081. b int
  2082. ) PARTITION BY RANGE (a, b);
  2083. -- check that violating rows are correctly reported
  2084. CREATE TABLE part1 (
  2085. a int NOT NULL CHECK (a = 1),
  2086. b int NOT NULL CHECK (b >= 1 AND b <= 10)
  2087. );
  2088. INSERT INTO part1 VALUES (1, 10);
  2089. -- Remember the TO bound is exclusive
  2090. ALTER TABLE range_parted ATTACH PARTITION part1 FOR VALUES FROM (1, 1) TO (1, 10);
  2091. -- should be ok after deleting the bad row
  2092. DELETE FROM part1;
  2093. ALTER TABLE range_parted ATTACH PARTITION part1 FOR VALUES FROM (1, 1) TO (1, 10);
  2094. -- adding constraints that describe the desired partition constraint
  2095. -- (or more restrictive) will help skip the validation scan
  2096. CREATE TABLE part2 (
  2097. a int NOT NULL CHECK (a = 1),
  2098. b int NOT NULL CHECK (b >= 10 AND b < 18)
  2099. );
  2100. ALTER TABLE range_parted ATTACH PARTITION part2 FOR VALUES FROM (1, 10) TO (1, 20);
  2101. -- Create default partition
  2102. CREATE TABLE partr_def1 PARTITION OF range_parted DEFAULT;
  2103. -- Only one default partition is allowed, hence, following should give error
  2104. CREATE TABLE partr_def2 (LIKE part1 INCLUDING CONSTRAINTS);
  2105. ALTER TABLE range_parted ATTACH PARTITION partr_def2 DEFAULT;
  2106. -- Overlapping partitions cannot be attached, hence, following should give error
  2107. INSERT INTO partr_def1 VALUES (2, 10);
  2108. CREATE TABLE part3 (LIKE range_parted);
  2109. ALTER TABLE range_parted ATTACH partition part3 FOR VALUES FROM (2, 10) TO (2, 20);
  2110. -- Attaching partitions should be successful when there are no overlapping rows
  2111. ALTER TABLE range_parted ATTACH partition part3 FOR VALUES FROM (3, 10) TO (3, 20);
  2112. -- check that leaf partitions are scanned when attaching a partitioned
  2113. -- table
  2114. CREATE TABLE part_5 (
  2115. LIKE list_parted2
  2116. ) PARTITION BY LIST (b);
  2117. -- check that violating rows are correctly reported
  2118. CREATE TABLE part_5_a PARTITION OF part_5 FOR VALUES IN ('a');
  2119. INSERT INTO part_5_a (a, b) VALUES (6, 'a');
  2120. ALTER TABLE list_parted2 ATTACH PARTITION part_5 FOR VALUES IN (5);
  2121. -- delete the faulting row and also add a constraint to skip the scan
  2122. DELETE FROM part_5_a WHERE a NOT IN (3);
  2123. ALTER TABLE part_5 ADD CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 5);
  2124. ALTER TABLE list_parted2 ATTACH PARTITION part_5 FOR VALUES IN (5);
  2125. ALTER TABLE list_parted2 DETACH PARTITION part_5;
  2126. ALTER TABLE part_5 DROP CONSTRAINT check_a;
  2127. -- scan should again be skipped, even though NOT NULL is now a column property
  2128. ALTER TABLE part_5 ADD CONSTRAINT check_a CHECK (a IN (5)), ALTER a SET NOT NULL;
  2129. ALTER TABLE list_parted2 ATTACH PARTITION part_5 FOR VALUES IN (5);
  2130. -- Check the case where attnos of the partitioning columns in the table being
  2131. -- attached differs from the parent. It should not affect the constraint-
  2132. -- checking logic that allows to skip the scan.
  2133. CREATE TABLE part_6 (
  2134. c int,
  2135. LIKE list_parted2,
  2136. CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 6)
  2137. );
  2138. ALTER TABLE part_6 DROP c;
  2139. ALTER TABLE list_parted2 ATTACH PARTITION part_6 FOR VALUES IN (6);
  2140. -- Similar to above, but the table being attached is a partitioned table
  2141. -- whose partition has still different attnos for the root partitioning
  2142. -- columns.
  2143. CREATE TABLE part_7 (
  2144. LIKE list_parted2,
  2145. CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 7)
  2146. ) PARTITION BY LIST (b);
  2147. CREATE TABLE part_7_a_null (
  2148. c int,
  2149. d int,
  2150. e int,
  2151. LIKE list_parted2, -- 'a' will have attnum = 4
  2152. CONSTRAINT check_b CHECK (b IS NULL OR b = 'a'),
  2153. CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 7)
  2154. );
  2155. ALTER TABLE part_7_a_null DROP c, DROP d, DROP e;
  2156. ALTER TABLE part_7 ATTACH PARTITION part_7_a_null FOR VALUES IN ('a', null);
  2157. ALTER TABLE list_parted2 ATTACH PARTITION part_7 FOR VALUES IN (7);
  2158. -- Same example, but check this time that the constraint correctly detects
  2159. -- violating rows
  2160. ALTER TABLE list_parted2 DETACH PARTITION part_7;
  2161. ALTER TABLE part_7 DROP CONSTRAINT check_a; -- thusly, scan won't be skipped
  2162. INSERT INTO part_7 (a, b) VALUES (8, null), (9, 'a');
  2163. SELECT tableoid::regclass, a, b FROM part_7 order by a;
  2164. ALTER TABLE list_parted2 ATTACH PARTITION part_7 FOR VALUES IN (7);
  2165. -- check that leaf partitions of default partition are scanned when
  2166. -- attaching a partitioned table.
  2167. ALTER TABLE part_5 DROP CONSTRAINT check_a;
  2168. CREATE TABLE part5_def PARTITION OF part_5 DEFAULT PARTITION BY LIST(a);
  2169. CREATE TABLE part5_def_p1 PARTITION OF part5_def FOR VALUES IN (5);
  2170. INSERT INTO part5_def_p1 VALUES (5, 'y');
  2171. CREATE TABLE part5_p1 (LIKE part_5);
  2172. ALTER TABLE part_5 ATTACH PARTITION part5_p1 FOR VALUES IN ('y');
  2173. -- should be ok after deleting the bad row
  2174. DELETE FROM part5_def_p1 WHERE b = 'y';
  2175. ALTER TABLE part_5 ATTACH PARTITION part5_p1 FOR VALUES IN ('y');
  2176. -- check that the table being attached is not already a partition
  2177. ALTER TABLE list_parted2 ATTACH PARTITION part_2 FOR VALUES IN (2);
  2178. -- check that circular inheritance is not allowed
  2179. ALTER TABLE part_5 ATTACH PARTITION list_parted2 FOR VALUES IN ('b');
  2180. ALTER TABLE list_parted2 ATTACH PARTITION list_parted2 FOR VALUES IN (0);
  2181. -- If a partitioned table being created or an existing table being attached
  2182. -- as a partition does not have a constraint that would allow validation scan
  2183. -- to be skipped, but an individual partition does, then the partition's
  2184. -- validation scan is skipped.
  2185. CREATE TABLE quuux (a int, b text) PARTITION BY LIST (a);
  2186. CREATE TABLE quuux_default PARTITION OF quuux DEFAULT PARTITION BY LIST (b);
  2187. CREATE TABLE quuux_default1 PARTITION OF quuux_default (
  2188. CONSTRAINT check_1 CHECK (a IS NOT NULL AND a = 1)
  2189. ) FOR VALUES IN ('b');
  2190. CREATE TABLE quuux1 (a int, b text);
  2191. ALTER TABLE quuux ATTACH PARTITION quuux1 FOR VALUES IN (1); -- validate!
  2192. CREATE TABLE quuux2 (a int, b text);
  2193. ALTER TABLE quuux ATTACH PARTITION quuux2 FOR VALUES IN (2); -- skip validation
  2194. DROP TABLE quuux1, quuux2;
  2195. -- should validate for quuux1, but not for quuux2
  2196. CREATE TABLE quuux1 PARTITION OF quuux FOR VALUES IN (1);
  2197. CREATE TABLE quuux2 PARTITION OF quuux FOR VALUES IN (2);
  2198. DROP TABLE quuux;
  2199. -- check validation when attaching hash partitions
  2200. -- Use hand-rolled hash functions and operator class to get predictable result
  2201. -- on different machines. part_test_int4_ops is defined in insert.sql.
  2202. -- check that the new partition won't overlap with an existing partition
  2203. CREATE TABLE hash_parted (
  2204. a int,
  2205. b int
  2206. ) PARTITION BY HASH (a part_test_int4_ops);
  2207. CREATE TABLE hpart_1 PARTITION OF hash_parted FOR VALUES WITH (MODULUS 4, REMAINDER 0);
  2208. CREATE TABLE fail_part (LIKE hpart_1);
  2209. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, REMAINDER 4);
  2210. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, REMAINDER 0);
  2211. DROP TABLE fail_part;
  2212. -- check validation when attaching hash partitions
  2213. -- check that violating rows are correctly reported
  2214. CREATE TABLE hpart_2 (LIKE hash_parted);
  2215. INSERT INTO hpart_2 VALUES (3, 0);
  2216. ALTER TABLE hash_parted ATTACH PARTITION hpart_2 FOR VALUES WITH (MODULUS 4, REMAINDER 1);
  2217. -- should be ok after deleting the bad row
  2218. DELETE FROM hpart_2;
  2219. ALTER TABLE hash_parted ATTACH PARTITION hpart_2 FOR VALUES WITH (MODULUS 4, REMAINDER 1);
  2220. -- check that leaf partitions are scanned when attaching a partitioned
  2221. -- table
  2222. CREATE TABLE hpart_5 (
  2223. LIKE hash_parted
  2224. ) PARTITION BY LIST (b);
  2225. -- check that violating rows are correctly reported
  2226. CREATE TABLE hpart_5_a PARTITION OF hpart_5 FOR VALUES IN ('1', '2', '3');
  2227. INSERT INTO hpart_5_a (a, b) VALUES (7, 1);
  2228. ALTER TABLE hash_parted ATTACH PARTITION hpart_5 FOR VALUES WITH (MODULUS 4, REMAINDER 2);
  2229. -- should be ok after deleting the bad row
  2230. DELETE FROM hpart_5_a;
  2231. ALTER TABLE hash_parted ATTACH PARTITION hpart_5 FOR VALUES WITH (MODULUS 4, REMAINDER 2);
  2232. -- check that the table being attach is with valid modulus and remainder value
  2233. CREATE TABLE fail_part(LIKE hash_parted);
  2234. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 0, REMAINDER 1);
  2235. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, REMAINDER 8);
  2236. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 3, REMAINDER 2);
  2237. DROP TABLE fail_part;
  2238. -- fails with incorrect object type
  2239. CREATE VIEW at_v1 AS SELECT 1 as a;
  2240. ALTER TABLE at_v1 ATTACH PARTITION dummy default;
  2241. DROP VIEW at_v1;
  2242. --
  2243. -- DETACH PARTITION
  2244. --
  2245. -- check that the table is partitioned at all
  2246. CREATE TABLE regular_table (a int);
  2247. ALTER TABLE regular_table DETACH PARTITION any_name;
  2248. DROP TABLE regular_table;
  2249. -- check that the partition being detached exists at all
  2250. ALTER TABLE list_parted2 DETACH PARTITION part_4;
  2251. ALTER TABLE hash_parted DETACH PARTITION hpart_4;
  2252. -- check that the partition being detached is actually a partition of the parent
  2253. CREATE TABLE not_a_part (a int);
  2254. ALTER TABLE list_parted2 DETACH PARTITION not_a_part;
  2255. ALTER TABLE list_parted2 DETACH PARTITION part_1;
  2256. ALTER TABLE hash_parted DETACH PARTITION not_a_part;
  2257. DROP TABLE not_a_part;
  2258. -- check that, after being detached, attinhcount/coninhcount is dropped to 0 and
  2259. -- attislocal/conislocal is set to true
  2260. ALTER TABLE list_parted2 DETACH PARTITION part_3_4;
  2261. SELECT attinhcount, attislocal FROM pg_attribute WHERE attrelid = 'part_3_4'::regclass AND attnum > 0;
  2262. SELECT coninhcount, conislocal FROM pg_constraint WHERE conrelid = 'part_3_4'::regclass AND conname = 'check_a';
  2263. DROP TABLE part_3_4;
  2264. -- check that a detached partition is not dropped on dropping a partitioned table
  2265. CREATE TABLE range_parted2 (
  2266. a int
  2267. ) PARTITION BY RANGE(a);
  2268. CREATE TABLE part_rp PARTITION OF range_parted2 FOR VALUES FROM (0) to (100);
  2269. ALTER TABLE range_parted2 DETACH PARTITION part_rp;
  2270. DROP TABLE range_parted2;
  2271. SELECT * from part_rp;
  2272. DROP TABLE part_rp;
  2273. -- concurrent detach
  2274. CREATE TABLE range_parted2 (
  2275. a int
  2276. ) PARTITION BY RANGE(a);
  2277. CREATE TABLE part_rp PARTITION OF range_parted2 FOR VALUES FROM (0) to (100);
  2278. BEGIN;
  2279. -- doesn't work in a partition block
  2280. ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
  2281. COMMIT;
  2282. CREATE TABLE part_rpd PARTITION OF range_parted2 DEFAULT;
  2283. -- doesn't work if there's a default partition
  2284. ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
  2285. -- doesn't work for the default partition
  2286. ALTER TABLE range_parted2 DETACH PARTITION part_rpd CONCURRENTLY;
  2287. DROP TABLE part_rpd;
  2288. -- works fine
  2289. ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
  2290. \d+ range_parted2
  2291. -- constraint should be created
  2292. \d part_rp
  2293. CREATE TABLE part_rp100 PARTITION OF range_parted2 (CHECK (a>=123 AND a<133 AND a IS NOT NULL)) FOR VALUES FROM (100) to (200);
  2294. ALTER TABLE range_parted2 DETACH PARTITION part_rp100 CONCURRENTLY;
  2295. -- redundant constraint should not be created
  2296. \d part_rp100
  2297. DROP TABLE range_parted2;
  2298. -- Check ALTER TABLE commands for partitioned tables and partitions
  2299. -- cannot add/drop column to/from *only* the parent
  2300. ALTER TABLE ONLY list_parted2 ADD COLUMN c int;
  2301. ALTER TABLE ONLY list_parted2 DROP COLUMN b;
  2302. -- cannot add a column to partition or drop an inherited one
  2303. ALTER TABLE part_2 ADD COLUMN c text;
  2304. ALTER TABLE part_2 DROP COLUMN b;
  2305. -- Nor rename, alter type
  2306. ALTER TABLE part_2 RENAME COLUMN b to c;
  2307. ALTER TABLE part_2 ALTER COLUMN b TYPE text;
  2308. -- cannot add/drop NOT NULL or check constraints to *only* the parent, when
  2309. -- partitions exist
  2310. ALTER TABLE ONLY list_parted2 ALTER b SET NOT NULL;
  2311. ALTER TABLE ONLY list_parted2 ADD CONSTRAINT check_b CHECK (b <> 'zz');
  2312. ALTER TABLE list_parted2 ALTER b SET NOT NULL;
  2313. ALTER TABLE ONLY list_parted2 ALTER b DROP NOT NULL;
  2314. ALTER TABLE list_parted2 ADD CONSTRAINT check_b CHECK (b <> 'zz');
  2315. ALTER TABLE ONLY list_parted2 DROP CONSTRAINT check_b;
  2316. -- It's alright though, if no partitions are yet created
  2317. CREATE TABLE parted_no_parts (a int) PARTITION BY LIST (a);
  2318. ALTER TABLE ONLY parted_no_parts ALTER a SET NOT NULL;
  2319. ALTER TABLE ONLY parted_no_parts ADD CONSTRAINT check_a CHECK (a > 0);
  2320. ALTER TABLE ONLY parted_no_parts ALTER a DROP NOT NULL;
  2321. ALTER TABLE ONLY parted_no_parts DROP CONSTRAINT check_a;
  2322. DROP TABLE parted_no_parts;
  2323. -- cannot drop inherited NOT NULL or check constraints from partition
  2324. ALTER TABLE list_parted2 ALTER b SET NOT NULL, ADD CONSTRAINT check_a2 CHECK (a > 0);
  2325. ALTER TABLE part_2 ALTER b DROP NOT NULL;
  2326. ALTER TABLE part_2 DROP CONSTRAINT check_a2;
  2327. -- Doesn't make sense to add NO INHERIT constraints on partitioned tables
  2328. ALTER TABLE list_parted2 add constraint check_b2 check (b <> 'zz') NO INHERIT;
  2329. -- check that a partition cannot participate in regular inheritance
  2330. CREATE TABLE inh_test () INHERITS (part_2);
  2331. CREATE TABLE inh_test (LIKE part_2);
  2332. ALTER TABLE inh_test INHERIT part_2;
  2333. ALTER TABLE part_2 INHERIT inh_test;
  2334. -- cannot drop or alter type of partition key columns of lower level
  2335. -- partitioned tables; for example, part_5, which is list_parted2's
  2336. -- partition, is partitioned on b;
  2337. ALTER TABLE list_parted2 DROP COLUMN b;
  2338. ALTER TABLE list_parted2 ALTER COLUMN b TYPE text;
  2339. -- dropping non-partition key columns should be allowed on the parent table.
  2340. ALTER TABLE list_parted DROP COLUMN b;
  2341. SELECT * FROM list_parted;
  2342. -- cleanup
  2343. DROP TABLE list_parted, list_parted2, range_parted;
  2344. DROP TABLE fail_def_part;
  2345. DROP TABLE hash_parted;
  2346. -- more tests for certain multi-level partitioning scenarios
  2347. create table p (a int, b int) partition by range (a, b);
  2348. create table p1 (b int, a int not null) partition by range (b);
  2349. create table p11 (like p1);
  2350. alter table p11 drop a;
  2351. alter table p11 add a int;
  2352. alter table p11 drop a;
  2353. alter table p11 add a int not null;
  2354. -- attnum for key attribute 'a' is different in p, p1, and p11
  2355. select attrelid::regclass, attname, attnum
  2356. from pg_attribute
  2357. where attname = 'a'
  2358. and (attrelid = 'p'::regclass
  2359. or attrelid = 'p1'::regclass
  2360. or attrelid = 'p11'::regclass)
  2361. order by attrelid::regclass::text;
  2362. alter table p1 attach partition p11 for values from (2) to (5);
  2363. insert into p1 (a, b) values (2, 3);
  2364. -- check that partition validation scan correctly detects violating rows
  2365. alter table p attach partition p1 for values from (1, 2) to (1, 10);
  2366. -- cleanup
  2367. drop table p;
  2368. drop table p1;
  2369. -- validate constraint on partitioned tables should only scan leaf partitions
  2370. create table parted_validate_test (a int) partition by list (a);
  2371. create table parted_validate_test_1 partition of parted_validate_test for values in (0, 1);
  2372. alter table parted_validate_test add constraint parted_validate_test_chka check (a > 0) not valid;
  2373. alter table parted_validate_test validate constraint parted_validate_test_chka;
  2374. drop table parted_validate_test;
  2375. -- test alter column options
  2376. CREATE TABLE attmp(i integer);
  2377. INSERT INTO attmp VALUES (1);
  2378. ALTER TABLE attmp ALTER COLUMN i SET (n_distinct = 1, n_distinct_inherited = 2);
  2379. ALTER TABLE attmp ALTER COLUMN i RESET (n_distinct_inherited);
  2380. ANALYZE attmp;
  2381. DROP TABLE attmp;
  2382. DROP USER regress_alter_table_user1;
  2383. -- check that violating rows are correctly reported when attaching as the
  2384. -- default partition
  2385. create table defpart_attach_test (a int) partition by list (a);
  2386. create table defpart_attach_test1 partition of defpart_attach_test for values in (1);
  2387. create table defpart_attach_test_d (b int, a int);
  2388. alter table defpart_attach_test_d drop b;
  2389. insert into defpart_attach_test_d values (1), (2);
  2390. -- error because its constraint as the default partition would be violated
  2391. -- by the row containing 1
  2392. alter table defpart_attach_test attach partition defpart_attach_test_d default;
  2393. delete from defpart_attach_test_d where a = 1;
  2394. alter table defpart_attach_test_d add check (a > 1);
  2395. -- should be attached successfully and without needing to be scanned
  2396. alter table defpart_attach_test attach partition defpart_attach_test_d default;
  2397. -- check that attaching a partition correctly reports any rows in the default
  2398. -- partition that should not be there for the new partition to be attached
  2399. -- successfully
  2400. create table defpart_attach_test_2 (like defpart_attach_test_d);
  2401. alter table defpart_attach_test attach partition defpart_attach_test_2 for values in (2);
  2402. drop table defpart_attach_test;
  2403. -- check combinations of temporary and permanent relations when attaching
  2404. -- partitions.
  2405. create table perm_part_parent (a int) partition by list (a);
  2406. create temp table temp_part_parent (a int) partition by list (a);
  2407. create table perm_part_child (a int);
  2408. create temp table temp_part_child (a int);
  2409. alter table temp_part_parent attach partition perm_part_child default; -- error
  2410. alter table perm_part_parent attach partition temp_part_child default; -- error
  2411. alter table temp_part_parent attach partition temp_part_child default; -- ok
  2412. drop table perm_part_parent cascade;
  2413. drop table temp_part_parent cascade;
  2414. -- check that attaching partitions to a table while it is being used is
  2415. -- prevented
  2416. create table tab_part_attach (a int) partition by list (a);
  2417. create or replace function func_part_attach() returns trigger
  2418. language plpgsql as $$
  2419. begin
  2420. execute 'create table tab_part_attach_1 (a int)';
  2421. execute 'alter table tab_part_attach attach partition tab_part_attach_1 for values in (1)';
  2422. return null;
  2423. end $$;
  2424. create trigger trig_part_attach before insert on tab_part_attach
  2425. for each statement execute procedure func_part_attach();
  2426. insert into tab_part_attach values (1);
  2427. drop table tab_part_attach;
  2428. drop function func_part_attach();
  2429. -- test case where the partitioning operator is a SQL function whose
  2430. -- evaluation results in the table's relcache being rebuilt partway through
  2431. -- the execution of an ATTACH PARTITION command
  2432. create function at_test_sql_partop (int4, int4) returns int language sql
  2433. as $$ select case when $1 = $2 then 0 when $1 > $2 then 1 else -1 end; $$;
  2434. create operator class at_test_sql_partop for type int4 using btree as
  2435. operator 1 < (int4, int4), operator 2 <= (int4, int4),
  2436. operator 3 = (int4, int4), operator 4 >= (int4, int4),
  2437. operator 5 > (int4, int4), function 1 at_test_sql_partop(int4, int4);
  2438. create table at_test_sql_partop (a int) partition by range (a at_test_sql_partop);
  2439. create table at_test_sql_partop_1 (a int);
  2440. alter table at_test_sql_partop attach partition at_test_sql_partop_1 for values from (0) to (10);
  2441. drop table at_test_sql_partop;
  2442. drop operator class at_test_sql_partop using btree;
  2443. drop function at_test_sql_partop;
  2444. /* Test case for bug #16242 */
  2445. -- We create a parent and child where the child has missing
  2446. -- non-null attribute values, and arrange to pass them through
  2447. -- tuple conversion from the child to the parent tupdesc
  2448. create table bar1 (a integer, b integer not null default 1)
  2449. partition by range (a);
  2450. create table bar2 (a integer);
  2451. insert into bar2 values (1);
  2452. alter table bar2 add column b integer not null default 1;
  2453. -- (at this point bar2 contains tuple with natts=1)
  2454. alter table bar1 attach partition bar2 default;
  2455. -- this works:
  2456. select * from bar1;
  2457. -- this exercises tuple conversion:
  2458. create function xtrig()
  2459. returns trigger language plpgsql
  2460. as $$
  2461. declare
  2462. r record;
  2463. begin
  2464. for r in select * from old loop
  2465. raise info 'a=%, b=%', r.a, r.b;
  2466. end loop;
  2467. return NULL;
  2468. end;
  2469. $$;
  2470. create trigger xtrig
  2471. after update on bar1
  2472. referencing old table as old
  2473. for each statement execute procedure xtrig();
  2474. update bar1 set a = a + 1;
  2475. /* End test case for bug #16242 */
  2476. /* Test case for bug #17409 */
  2477. create table attbl (p1 int constraint pk_attbl primary key);
  2478. create table atref (c1 int references attbl(p1));
  2479. cluster attbl using pk_attbl;
  2480. alter table attbl alter column p1 set data type bigint;
  2481. alter table atref alter column c1 set data type bigint;
  2482. drop table attbl, atref;
  2483. create table attbl (p1 int constraint pk_attbl primary key);
  2484. alter table attbl replica identity using index pk_attbl;
  2485. create table atref (c1 int references attbl(p1));
  2486. alter table attbl alter column p1 set data type bigint;
  2487. alter table atref alter column c1 set data type bigint;
  2488. drop table attbl, atref;
  2489. /* End test case for bug #17409 */
  2490. -- Test that ALTER TABLE rewrite preserves a clustered index
  2491. -- for normal indexes and indexes on constraints.
  2492. create table alttype_cluster (a int);
  2493. alter table alttype_cluster add primary key (a);
  2494. create index alttype_cluster_ind on alttype_cluster (a);
  2495. alter table alttype_cluster cluster on alttype_cluster_ind;
  2496. -- Normal index remains clustered.
  2497. select indexrelid::regclass, indisclustered from pg_index
  2498. where indrelid = 'alttype_cluster'::regclass
  2499. order by indexrelid::regclass::text;
  2500. alter table alttype_cluster alter a type bigint;
  2501. select indexrelid::regclass, indisclustered from pg_index
  2502. where indrelid = 'alttype_cluster'::regclass
  2503. order by indexrelid::regclass::text;
  2504. -- Constraint index remains clustered.
  2505. alter table alttype_cluster cluster on alttype_cluster_pkey;
  2506. select indexrelid::regclass, indisclustered from pg_index
  2507. where indrelid = 'alttype_cluster'::regclass
  2508. order by indexrelid::regclass::text;
  2509. alter table alttype_cluster alter a type int;
  2510. select indexrelid::regclass, indisclustered from pg_index
  2511. where indrelid = 'alttype_cluster'::regclass
  2512. order by indexrelid::regclass::text;
  2513. drop table alttype_cluster;
  2514. --
  2515. -- Check that attaching or detaching a partitioned partition correctly leads
  2516. -- to its partitions' constraint being updated to reflect the parent's
  2517. -- newly added/removed constraint
  2518. create table target_parted (a int, b int) partition by list (a);
  2519. create table attach_parted (a int, b int) partition by list (b);
  2520. create table attach_parted_part1 partition of attach_parted for values in (1);
  2521. -- insert a row directly into the leaf partition so that its partition
  2522. -- constraint is built and stored in the relcache
  2523. insert into attach_parted_part1 values (1, 1);
  2524. -- the following better invalidate the partition constraint of the leaf
  2525. -- partition too...
  2526. alter table target_parted attach partition attach_parted for values in (1);
  2527. -- ...such that the following insert fails
  2528. insert into attach_parted_part1 values (2, 1);
  2529. -- ...and doesn't when the partition is detached along with its own partition
  2530. alter table target_parted detach partition attach_parted;
  2531. insert into attach_parted_part1 values (2, 1);