alter_table.out 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593
  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. ERROR: relation "attmp_wrong" does not exist
  15. COMMENT ON TABLE attmp IS 'table comment';
  16. COMMENT ON TABLE attmp IS NULL;
  17. ALTER TABLE attmp ADD COLUMN xmin integer; -- fails
  18. ERROR: column name "xmin" conflicts with a system column name
  19. ALTER TABLE attmp ADD COLUMN a int4 default 3;
  20. ALTER TABLE attmp ADD COLUMN b name;
  21. ALTER TABLE attmp ADD COLUMN c text;
  22. ALTER TABLE attmp ADD COLUMN d float8;
  23. ALTER TABLE attmp ADD COLUMN e float4;
  24. ALTER TABLE attmp ADD COLUMN f int2;
  25. ALTER TABLE attmp ADD COLUMN g polygon;
  26. ALTER TABLE attmp ADD COLUMN i char;
  27. ALTER TABLE attmp ADD COLUMN k int4;
  28. ALTER TABLE attmp ADD COLUMN l tid;
  29. ALTER TABLE attmp ADD COLUMN m xid;
  30. ALTER TABLE attmp ADD COLUMN n oidvector;
  31. --ALTER TABLE attmp ADD COLUMN o lock;
  32. ALTER TABLE attmp ADD COLUMN p boolean;
  33. ALTER TABLE attmp ADD COLUMN q point;
  34. ALTER TABLE attmp ADD COLUMN r lseg;
  35. ALTER TABLE attmp ADD COLUMN s path;
  36. ALTER TABLE attmp ADD COLUMN t box;
  37. ALTER TABLE attmp ADD COLUMN v timestamp;
  38. ALTER TABLE attmp ADD COLUMN w interval;
  39. ALTER TABLE attmp ADD COLUMN x float8[];
  40. ALTER TABLE attmp ADD COLUMN y float4[];
  41. ALTER TABLE attmp ADD COLUMN z int2[];
  42. INSERT INTO attmp (a, b, c, d, e, f, g, i, k, l, m, n, p, q, r, s, t,
  43. v, w, x, y, z)
  44. VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
  45. 'c',
  46. 314159, '(1,1)', '512',
  47. '1 2 3 4 5 6 7 8', true, '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
  48. '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)',
  49. 'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
  50. SELECT * FROM attmp;
  51. initial | a | b | c | d | e | f | g | i | k | l | m | n | p | q | r | s | t | v | w | x | y | z
  52. ---------+---+------+------+-----+-----+---+-----------------------+---+--------+-------+-----+-----------------+---+-----------+-----------------------+-----------------------------+---------------------+--------------------------+------------------+-----------+-----------+-----------
  53. | 4 | name | text | 4.1 | 4.1 | 2 | ((4.1,4.1),(3.1,3.1)) | c | 314159 | (1,1) | 512 | 1 2 3 4 5 6 7 8 | t | (1.1,1.1) | [(4.1,4.1),(3.1,3.1)] | ((0,2),(4.1,4.1),(3.1,3.1)) | (4.1,4.1),(3.1,3.1) | Thu Jan 01 00:00:00 1970 | @ 1 hour 10 secs | {1,2,3,4} | {1,2,3,4} | {1,2,3,4}
  54. (1 row)
  55. DROP TABLE attmp;
  56. -- the wolf bug - schema mods caused inconsistent row descriptors
  57. CREATE TABLE attmp (
  58. initial int4
  59. );
  60. ALTER TABLE attmp ADD COLUMN a int4;
  61. ALTER TABLE attmp ADD COLUMN b name;
  62. ALTER TABLE attmp ADD COLUMN c text;
  63. ALTER TABLE attmp ADD COLUMN d float8;
  64. ALTER TABLE attmp ADD COLUMN e float4;
  65. ALTER TABLE attmp ADD COLUMN f int2;
  66. ALTER TABLE attmp ADD COLUMN g polygon;
  67. ALTER TABLE attmp ADD COLUMN i char;
  68. ALTER TABLE attmp ADD COLUMN k int4;
  69. ALTER TABLE attmp ADD COLUMN l tid;
  70. ALTER TABLE attmp ADD COLUMN m xid;
  71. ALTER TABLE attmp ADD COLUMN n oidvector;
  72. --ALTER TABLE attmp ADD COLUMN o lock;
  73. ALTER TABLE attmp ADD COLUMN p boolean;
  74. ALTER TABLE attmp ADD COLUMN q point;
  75. ALTER TABLE attmp ADD COLUMN r lseg;
  76. ALTER TABLE attmp ADD COLUMN s path;
  77. ALTER TABLE attmp ADD COLUMN t box;
  78. ALTER TABLE attmp ADD COLUMN v timestamp;
  79. ALTER TABLE attmp ADD COLUMN w interval;
  80. ALTER TABLE attmp ADD COLUMN x float8[];
  81. ALTER TABLE attmp ADD COLUMN y float4[];
  82. ALTER TABLE attmp ADD COLUMN z int2[];
  83. INSERT INTO attmp (a, b, c, d, e, f, g, i, k, l, m, n, p, q, r, s, t,
  84. v, w, x, y, z)
  85. VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
  86. 'c',
  87. 314159, '(1,1)', '512',
  88. '1 2 3 4 5 6 7 8', true, '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
  89. '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)',
  90. 'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
  91. SELECT * FROM attmp;
  92. initial | a | b | c | d | e | f | g | i | k | l | m | n | p | q | r | s | t | v | w | x | y | z
  93. ---------+---+------+------+-----+-----+---+-----------------------+---+--------+-------+-----+-----------------+---+-----------+-----------------------+-----------------------------+---------------------+--------------------------+------------------+-----------+-----------+-----------
  94. | 4 | name | text | 4.1 | 4.1 | 2 | ((4.1,4.1),(3.1,3.1)) | c | 314159 | (1,1) | 512 | 1 2 3 4 5 6 7 8 | t | (1.1,1.1) | [(4.1,4.1),(3.1,3.1)] | ((0,2),(4.1,4.1),(3.1,3.1)) | (4.1,4.1),(3.1,3.1) | Thu Jan 01 00:00:00 1970 | @ 1 hour 10 secs | {1,2,3,4} | {1,2,3,4} | {1,2,3,4}
  95. (1 row)
  96. CREATE INDEX attmp_idx ON attmp (a, (d + e), b);
  97. ALTER INDEX attmp_idx ALTER COLUMN 0 SET STATISTICS 1000;
  98. ERROR: column number must be in range from 1 to 32767
  99. LINE 1: ALTER INDEX attmp_idx ALTER COLUMN 0 SET STATISTICS 1000;
  100. ^
  101. ALTER INDEX attmp_idx ALTER COLUMN 1 SET STATISTICS 1000;
  102. ERROR: cannot alter statistics on non-expression column "a" of index "attmp_idx"
  103. HINT: Alter statistics on table column instead.
  104. ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS 1000;
  105. \d+ attmp_idx
  106. Index "public.attmp_idx"
  107. Column | Type | Key? | Definition | Storage | Stats target
  108. --------+------------------+------+------------+---------+--------------
  109. a | integer | yes | a | plain |
  110. expr | double precision | yes | (d + e) | plain | 1000
  111. b | cstring | yes | b | plain |
  112. btree, for table "public.attmp"
  113. ALTER INDEX attmp_idx ALTER COLUMN 3 SET STATISTICS 1000;
  114. ERROR: cannot alter statistics on non-expression column "b" of index "attmp_idx"
  115. HINT: Alter statistics on table column instead.
  116. ALTER INDEX attmp_idx ALTER COLUMN 4 SET STATISTICS 1000;
  117. ERROR: column number 4 of relation "attmp_idx" does not exist
  118. ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS -1;
  119. DROP TABLE attmp;
  120. -- fails with incorrect object type
  121. CREATE VIEW at_v1 AS SELECT 1 as a;
  122. ALTER TABLE at_v1 ALTER COLUMN a SET STATISTICS 0;
  123. ERROR: "at_v1" is not a table, materialized view, index, partitioned index, or foreign table
  124. DROP VIEW at_v1;
  125. --
  126. -- rename - check on both non-temp and temp tables
  127. --
  128. CREATE TABLE attmp (regtable int);
  129. CREATE TEMP TABLE attmp (attmptable int);
  130. ALTER TABLE attmp RENAME TO attmp_new;
  131. SELECT * FROM attmp;
  132. regtable
  133. ----------
  134. (0 rows)
  135. SELECT * FROM attmp_new;
  136. attmptable
  137. ------------
  138. (0 rows)
  139. ALTER TABLE attmp RENAME TO attmp_new2;
  140. SELECT * FROM attmp; -- should fail
  141. ERROR: relation "attmp" does not exist
  142. LINE 1: SELECT * FROM attmp;
  143. ^
  144. SELECT * FROM attmp_new;
  145. attmptable
  146. ------------
  147. (0 rows)
  148. SELECT * FROM attmp_new2;
  149. regtable
  150. ----------
  151. (0 rows)
  152. DROP TABLE attmp_new;
  153. DROP TABLE attmp_new2;
  154. -- check rename of partitioned tables and indexes also
  155. CREATE TABLE part_attmp (a int primary key) partition by range (a);
  156. CREATE TABLE part_attmp1 PARTITION OF part_attmp FOR VALUES FROM (0) TO (100);
  157. ALTER INDEX part_attmp_pkey RENAME TO part_attmp_index;
  158. ALTER INDEX part_attmp1_pkey RENAME TO part_attmp1_index;
  159. ALTER TABLE part_attmp RENAME TO part_at2tmp;
  160. ALTER TABLE part_attmp1 RENAME TO part_at2tmp1;
  161. SET ROLE regress_alter_table_user1;
  162. ALTER INDEX part_attmp_index RENAME TO fail;
  163. ERROR: must be owner of index part_attmp_index
  164. ALTER INDEX part_attmp1_index RENAME TO fail;
  165. ERROR: must be owner of index part_attmp1_index
  166. ALTER TABLE part_at2tmp RENAME TO fail;
  167. ERROR: must be owner of table part_at2tmp
  168. ALTER TABLE part_at2tmp1 RENAME TO fail;
  169. ERROR: must be owner of table part_at2tmp1
  170. RESET ROLE;
  171. DROP TABLE part_at2tmp;
  172. --
  173. -- check renaming to a table's array type's autogenerated name
  174. -- (the array type's name should get out of the way)
  175. --
  176. CREATE TABLE attmp_array (id int);
  177. CREATE TABLE attmp_array2 (id int);
  178. SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
  179. typname
  180. --------------
  181. _attmp_array
  182. (1 row)
  183. SELECT typname FROM pg_type WHERE oid = 'attmp_array2[]'::regtype;
  184. typname
  185. ---------------
  186. _attmp_array2
  187. (1 row)
  188. ALTER TABLE attmp_array2 RENAME TO _attmp_array;
  189. SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
  190. typname
  191. ---------------
  192. __attmp_array
  193. (1 row)
  194. SELECT typname FROM pg_type WHERE oid = '_attmp_array[]'::regtype;
  195. typname
  196. ----------------
  197. ___attmp_array
  198. (1 row)
  199. DROP TABLE _attmp_array;
  200. DROP TABLE attmp_array;
  201. -- renaming to table's own array type's name is an interesting corner case
  202. CREATE TABLE attmp_array (id int);
  203. SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
  204. typname
  205. --------------
  206. _attmp_array
  207. (1 row)
  208. ALTER TABLE attmp_array RENAME TO _attmp_array;
  209. SELECT typname FROM pg_type WHERE oid = '_attmp_array[]'::regtype;
  210. typname
  211. ---------------
  212. __attmp_array
  213. (1 row)
  214. DROP TABLE _attmp_array;
  215. -- ALTER TABLE ... RENAME on non-table relations
  216. -- renaming indexes (FIXME: this should probably test the index's functionality)
  217. ALTER INDEX IF EXISTS __onek_unique1 RENAME TO attmp_onek_unique1;
  218. NOTICE: relation "__onek_unique1" does not exist, skipping
  219. ALTER INDEX IF EXISTS __attmp_onek_unique1 RENAME TO onek_unique1;
  220. NOTICE: relation "__attmp_onek_unique1" does not exist, skipping
  221. ALTER INDEX onek_unique1 RENAME TO attmp_onek_unique1;
  222. ALTER INDEX attmp_onek_unique1 RENAME TO onek_unique1;
  223. SET ROLE regress_alter_table_user1;
  224. ALTER INDEX onek_unique1 RENAME TO fail; -- permission denied
  225. ERROR: must be owner of index onek_unique1
  226. RESET ROLE;
  227. -- rename statements with mismatching statement and object types
  228. CREATE TABLE alter_idx_rename_test (a INT);
  229. CREATE INDEX alter_idx_rename_test_idx ON alter_idx_rename_test (a);
  230. CREATE TABLE alter_idx_rename_test_parted (a INT) PARTITION BY LIST (a);
  231. CREATE INDEX alter_idx_rename_test_parted_idx ON alter_idx_rename_test_parted (a);
  232. BEGIN;
  233. ALTER INDEX alter_idx_rename_test RENAME TO alter_idx_rename_test_2;
  234. ALTER INDEX alter_idx_rename_test_parted RENAME TO alter_idx_rename_test_parted_2;
  235. SELECT relation::regclass, mode FROM pg_locks
  236. WHERE pid = pg_backend_pid() AND locktype = 'relation'
  237. AND relation::regclass::text LIKE 'alter\_idx%'
  238. ORDER BY relation::regclass::text COLLATE "C";
  239. relation | mode
  240. --------------------------------+---------------------
  241. alter_idx_rename_test_2 | AccessExclusiveLock
  242. alter_idx_rename_test_parted_2 | AccessExclusiveLock
  243. (2 rows)
  244. COMMIT;
  245. BEGIN;
  246. ALTER INDEX alter_idx_rename_test_idx RENAME TO alter_idx_rename_test_idx_2;
  247. ALTER INDEX alter_idx_rename_test_parted_idx RENAME TO alter_idx_rename_test_parted_idx_2;
  248. SELECT relation::regclass, mode FROM pg_locks
  249. WHERE pid = pg_backend_pid() AND locktype = 'relation'
  250. AND relation::regclass::text LIKE 'alter\_idx%'
  251. ORDER BY relation::regclass::text COLLATE "C";
  252. relation | mode
  253. ------------------------------------+--------------------------
  254. alter_idx_rename_test_idx_2 | ShareUpdateExclusiveLock
  255. alter_idx_rename_test_parted_idx_2 | ShareUpdateExclusiveLock
  256. (2 rows)
  257. COMMIT;
  258. BEGIN;
  259. ALTER TABLE alter_idx_rename_test_idx_2 RENAME TO alter_idx_rename_test_idx_3;
  260. ALTER TABLE alter_idx_rename_test_parted_idx_2 RENAME TO alter_idx_rename_test_parted_idx_3;
  261. SELECT relation::regclass, mode FROM pg_locks
  262. WHERE pid = pg_backend_pid() AND locktype = 'relation'
  263. AND relation::regclass::text LIKE 'alter\_idx%'
  264. ORDER BY relation::regclass::text COLLATE "C";
  265. relation | mode
  266. ------------------------------------+---------------------
  267. alter_idx_rename_test_idx_3 | AccessExclusiveLock
  268. alter_idx_rename_test_parted_idx_3 | AccessExclusiveLock
  269. (2 rows)
  270. COMMIT;
  271. DROP TABLE alter_idx_rename_test_2;
  272. -- renaming views
  273. CREATE VIEW attmp_view (unique1) AS SELECT unique1 FROM tenk1;
  274. ALTER TABLE attmp_view RENAME TO attmp_view_new;
  275. SET ROLE regress_alter_table_user1;
  276. ALTER VIEW attmp_view_new RENAME TO fail; -- permission denied
  277. ERROR: must be owner of view attmp_view_new
  278. RESET ROLE;
  279. -- hack to ensure we get an indexscan here
  280. set enable_seqscan to off;
  281. set enable_bitmapscan to off;
  282. -- 5 values, sorted
  283. SELECT unique1 FROM tenk1 WHERE unique1 < 5;
  284. unique1
  285. ---------
  286. 0
  287. 1
  288. 2
  289. 3
  290. 4
  291. (5 rows)
  292. reset enable_seqscan;
  293. reset enable_bitmapscan;
  294. DROP VIEW attmp_view_new;
  295. -- toast-like relation name
  296. alter table stud_emp rename to pg_toast_stud_emp;
  297. alter table pg_toast_stud_emp rename to stud_emp;
  298. -- renaming index should rename constraint as well
  299. ALTER TABLE onek ADD CONSTRAINT onek_unique1_constraint UNIQUE (unique1);
  300. ALTER INDEX onek_unique1_constraint RENAME TO onek_unique1_constraint_foo;
  301. ALTER TABLE onek DROP CONSTRAINT onek_unique1_constraint_foo;
  302. -- renaming constraint
  303. ALTER TABLE onek ADD CONSTRAINT onek_check_constraint CHECK (unique1 >= 0);
  304. ALTER TABLE onek RENAME CONSTRAINT onek_check_constraint TO onek_check_constraint_foo;
  305. ALTER TABLE onek DROP CONSTRAINT onek_check_constraint_foo;
  306. -- renaming constraint should rename index as well
  307. ALTER TABLE onek ADD CONSTRAINT onek_unique1_constraint UNIQUE (unique1);
  308. DROP INDEX onek_unique1_constraint; -- to see whether it's there
  309. ERROR: cannot drop index onek_unique1_constraint because constraint onek_unique1_constraint on table onek requires it
  310. HINT: You can drop constraint onek_unique1_constraint on table onek instead.
  311. ALTER TABLE onek RENAME CONSTRAINT onek_unique1_constraint TO onek_unique1_constraint_foo;
  312. DROP INDEX onek_unique1_constraint_foo; -- to see whether it's there
  313. ERROR: cannot drop index onek_unique1_constraint_foo because constraint onek_unique1_constraint_foo on table onek requires it
  314. HINT: You can drop constraint onek_unique1_constraint_foo on table onek instead.
  315. ALTER TABLE onek DROP CONSTRAINT onek_unique1_constraint_foo;
  316. -- renaming constraints vs. inheritance
  317. CREATE TABLE constraint_rename_test (a int CONSTRAINT con1 CHECK (a > 0), b int, c int);
  318. \d constraint_rename_test
  319. Table "public.constraint_rename_test"
  320. Column | Type | Collation | Nullable | Default
  321. --------+---------+-----------+----------+---------
  322. a | integer | | |
  323. b | integer | | |
  324. c | integer | | |
  325. Check constraints:
  326. "con1" CHECK (a > 0)
  327. CREATE TABLE constraint_rename_test2 (a int CONSTRAINT con1 CHECK (a > 0), d int) INHERITS (constraint_rename_test);
  328. NOTICE: merging column "a" with inherited definition
  329. NOTICE: merging constraint "con1" with inherited definition
  330. \d constraint_rename_test2
  331. Table "public.constraint_rename_test2"
  332. Column | Type | Collation | Nullable | Default
  333. --------+---------+-----------+----------+---------
  334. a | integer | | |
  335. b | integer | | |
  336. c | integer | | |
  337. d | integer | | |
  338. Check constraints:
  339. "con1" CHECK (a > 0)
  340. Inherits: constraint_rename_test
  341. ALTER TABLE constraint_rename_test2 RENAME CONSTRAINT con1 TO con1foo; -- fail
  342. ERROR: cannot rename inherited constraint "con1"
  343. ALTER TABLE ONLY constraint_rename_test RENAME CONSTRAINT con1 TO con1foo; -- fail
  344. ERROR: inherited constraint "con1" must be renamed in child tables too
  345. ALTER TABLE constraint_rename_test RENAME CONSTRAINT con1 TO con1foo; -- ok
  346. \d constraint_rename_test
  347. Table "public.constraint_rename_test"
  348. Column | Type | Collation | Nullable | Default
  349. --------+---------+-----------+----------+---------
  350. a | integer | | |
  351. b | integer | | |
  352. c | integer | | |
  353. Check constraints:
  354. "con1foo" CHECK (a > 0)
  355. Number of child tables: 1 (Use \d+ to list them.)
  356. \d constraint_rename_test2
  357. Table "public.constraint_rename_test2"
  358. Column | Type | Collation | Nullable | Default
  359. --------+---------+-----------+----------+---------
  360. a | integer | | |
  361. b | integer | | |
  362. c | integer | | |
  363. d | integer | | |
  364. Check constraints:
  365. "con1foo" CHECK (a > 0)
  366. Inherits: constraint_rename_test
  367. ALTER TABLE constraint_rename_test ADD CONSTRAINT con2 CHECK (b > 0) NO INHERIT;
  368. ALTER TABLE ONLY constraint_rename_test RENAME CONSTRAINT con2 TO con2foo; -- ok
  369. ALTER TABLE constraint_rename_test RENAME CONSTRAINT con2foo TO con2bar; -- ok
  370. \d constraint_rename_test
  371. Table "public.constraint_rename_test"
  372. Column | Type | Collation | Nullable | Default
  373. --------+---------+-----------+----------+---------
  374. a | integer | | |
  375. b | integer | | |
  376. c | integer | | |
  377. Check constraints:
  378. "con1foo" CHECK (a > 0)
  379. "con2bar" CHECK (b > 0) NO INHERIT
  380. Number of child tables: 1 (Use \d+ to list them.)
  381. \d constraint_rename_test2
  382. Table "public.constraint_rename_test2"
  383. Column | Type | Collation | Nullable | Default
  384. --------+---------+-----------+----------+---------
  385. a | integer | | |
  386. b | integer | | |
  387. c | integer | | |
  388. d | integer | | |
  389. Check constraints:
  390. "con1foo" CHECK (a > 0)
  391. Inherits: constraint_rename_test
  392. ALTER TABLE constraint_rename_test ADD CONSTRAINT con3 PRIMARY KEY (a);
  393. ALTER TABLE constraint_rename_test RENAME CONSTRAINT con3 TO con3foo; -- ok
  394. \d constraint_rename_test
  395. Table "public.constraint_rename_test"
  396. Column | Type | Collation | Nullable | Default
  397. --------+---------+-----------+----------+---------
  398. a | integer | | not null |
  399. b | integer | | |
  400. c | integer | | |
  401. Indexes:
  402. "con3foo" PRIMARY KEY, btree (a)
  403. Check constraints:
  404. "con1foo" CHECK (a > 0)
  405. "con2bar" CHECK (b > 0) NO INHERIT
  406. Number of child tables: 1 (Use \d+ to list them.)
  407. \d constraint_rename_test2
  408. Table "public.constraint_rename_test2"
  409. Column | Type | Collation | Nullable | Default
  410. --------+---------+-----------+----------+---------
  411. a | integer | | not null |
  412. b | integer | | |
  413. c | integer | | |
  414. d | integer | | |
  415. Check constraints:
  416. "con1foo" CHECK (a > 0)
  417. Inherits: constraint_rename_test
  418. DROP TABLE constraint_rename_test2;
  419. DROP TABLE constraint_rename_test;
  420. ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
  421. NOTICE: relation "constraint_not_exist" does not exist, skipping
  422. ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
  423. NOTICE: relation "constraint_rename_test" does not exist, skipping
  424. -- renaming constraints with cache reset of target relation
  425. CREATE TABLE constraint_rename_cache (a int,
  426. CONSTRAINT chk_a CHECK (a > 0),
  427. PRIMARY KEY (a));
  428. ALTER TABLE constraint_rename_cache
  429. RENAME CONSTRAINT chk_a TO chk_a_new;
  430. ALTER TABLE constraint_rename_cache
  431. RENAME CONSTRAINT constraint_rename_cache_pkey TO constraint_rename_pkey_new;
  432. CREATE TABLE like_constraint_rename_cache
  433. (LIKE constraint_rename_cache INCLUDING ALL);
  434. \d like_constraint_rename_cache
  435. Table "public.like_constraint_rename_cache"
  436. Column | Type | Collation | Nullable | Default
  437. --------+---------+-----------+----------+---------
  438. a | integer | | not null |
  439. Indexes:
  440. "like_constraint_rename_cache_pkey" PRIMARY KEY, btree (a)
  441. Check constraints:
  442. "chk_a_new" CHECK (a > 0)
  443. DROP TABLE constraint_rename_cache;
  444. DROP TABLE like_constraint_rename_cache;
  445. -- FOREIGN KEY CONSTRAINT adding TEST
  446. CREATE TABLE attmp2 (a int primary key);
  447. CREATE TABLE attmp3 (a int, b int);
  448. CREATE TABLE attmp4 (a int, b int, unique(a,b));
  449. CREATE TABLE attmp5 (a int, b int);
  450. -- Insert rows into attmp2 (pktable)
  451. INSERT INTO attmp2 values (1);
  452. INSERT INTO attmp2 values (2);
  453. INSERT INTO attmp2 values (3);
  454. INSERT INTO attmp2 values (4);
  455. -- Insert rows into attmp3
  456. INSERT INTO attmp3 values (1,10);
  457. INSERT INTO attmp3 values (1,20);
  458. INSERT INTO attmp3 values (5,50);
  459. -- Try (and fail) to add constraint due to invalid source columns
  460. ALTER TABLE attmp3 add constraint attmpconstr foreign key(c) references attmp2 match full;
  461. ERROR: column "c" referenced in foreign key constraint does not exist
  462. -- Try (and fail) to add constraint due to invalid destination columns explicitly given
  463. ALTER TABLE attmp3 add constraint attmpconstr foreign key(a) references attmp2(b) match full;
  464. ERROR: column "b" referenced in foreign key constraint does not exist
  465. -- Try (and fail) to add constraint due to invalid data
  466. ALTER TABLE attmp3 add constraint attmpconstr foreign key (a) references attmp2 match full;
  467. ERROR: insert or update on table "attmp3" violates foreign key constraint "attmpconstr"
  468. DETAIL: Key (a)=(5) is not present in table "attmp2".
  469. -- Delete failing row
  470. DELETE FROM attmp3 where a=5;
  471. -- Try (and succeed)
  472. ALTER TABLE attmp3 add constraint attmpconstr foreign key (a) references attmp2 match full;
  473. ALTER TABLE attmp3 drop constraint attmpconstr;
  474. INSERT INTO attmp3 values (5,50);
  475. -- Try NOT VALID and then VALIDATE CONSTRAINT, but fails. Delete failure then re-validate
  476. ALTER TABLE attmp3 add constraint attmpconstr foreign key (a) references attmp2 match full NOT VALID;
  477. ALTER TABLE attmp3 validate constraint attmpconstr;
  478. ERROR: insert or update on table "attmp3" violates foreign key constraint "attmpconstr"
  479. DETAIL: Key (a)=(5) is not present in table "attmp2".
  480. -- Delete failing row
  481. DELETE FROM attmp3 where a=5;
  482. -- Try (and succeed) and repeat to show it works on already valid constraint
  483. ALTER TABLE attmp3 validate constraint attmpconstr;
  484. ALTER TABLE attmp3 validate constraint attmpconstr;
  485. -- Try a non-verified CHECK constraint
  486. ALTER TABLE attmp3 ADD CONSTRAINT b_greater_than_ten CHECK (b > 10); -- fail
  487. ERROR: check constraint "b_greater_than_ten" of relation "attmp3" is violated by some row
  488. ALTER TABLE attmp3 ADD CONSTRAINT b_greater_than_ten CHECK (b > 10) NOT VALID; -- succeeds
  489. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- fails
  490. ERROR: check constraint "b_greater_than_ten" of relation "attmp3" is violated by some row
  491. DELETE FROM attmp3 WHERE NOT b > 10;
  492. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- succeeds
  493. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- succeeds
  494. -- Test inherited NOT VALID CHECK constraints
  495. select * from attmp3;
  496. a | b
  497. ---+----
  498. 1 | 20
  499. (1 row)
  500. CREATE TABLE attmp6 () INHERITS (attmp3);
  501. CREATE TABLE attmp7 () INHERITS (attmp3);
  502. INSERT INTO attmp6 VALUES (6, 30), (7, 16);
  503. ALTER TABLE attmp3 ADD CONSTRAINT b_le_20 CHECK (b <= 20) NOT VALID;
  504. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_le_20; -- fails
  505. ERROR: check constraint "b_le_20" of relation "attmp6" is violated by some row
  506. DELETE FROM attmp6 WHERE b > 20;
  507. ALTER TABLE attmp3 VALIDATE CONSTRAINT b_le_20; -- succeeds
  508. -- An already validated constraint must not be revalidated
  509. CREATE FUNCTION boo(int) RETURNS int IMMUTABLE STRICT LANGUAGE plpgsql AS $$ BEGIN RAISE NOTICE 'boo: %', $1; RETURN $1; END; $$;
  510. INSERT INTO attmp7 VALUES (8, 18);
  511. ALTER TABLE attmp7 ADD CONSTRAINT identity CHECK (b = boo(b));
  512. NOTICE: boo: 18
  513. ALTER TABLE attmp3 ADD CONSTRAINT IDENTITY check (b = boo(b)) NOT VALID;
  514. NOTICE: merging constraint "identity" with inherited definition
  515. ALTER TABLE attmp3 VALIDATE CONSTRAINT identity;
  516. NOTICE: boo: 20
  517. NOTICE: boo: 16
  518. -- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
  519. create table parent_noinh_convalid (a int);
  520. create table child_noinh_convalid () inherits (parent_noinh_convalid);
  521. insert into parent_noinh_convalid values (1);
  522. insert into child_noinh_convalid values (1);
  523. alter table parent_noinh_convalid add constraint check_a_is_2 check (a = 2) no inherit not valid;
  524. -- fail, because of the row in parent
  525. alter table parent_noinh_convalid validate constraint check_a_is_2;
  526. ERROR: check constraint "check_a_is_2" of relation "parent_noinh_convalid" is violated by some row
  527. delete from only parent_noinh_convalid;
  528. -- ok (parent itself contains no violating rows)
  529. alter table parent_noinh_convalid validate constraint check_a_is_2;
  530. select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid'::regclass and conname = 'check_a_is_2';
  531. convalidated
  532. --------------
  533. t
  534. (1 row)
  535. -- cleanup
  536. drop table parent_noinh_convalid, child_noinh_convalid;
  537. -- Try (and fail) to create constraint from attmp5(a) to attmp4(a) - unique constraint on
  538. -- attmp4 is a,b
  539. ALTER TABLE attmp5 add constraint attmpconstr foreign key(a) references attmp4(a) match full;
  540. ERROR: there is no unique constraint matching given keys for referenced table "attmp4"
  541. DROP TABLE attmp7;
  542. DROP TABLE attmp6;
  543. DROP TABLE attmp5;
  544. DROP TABLE attmp4;
  545. DROP TABLE attmp3;
  546. DROP TABLE attmp2;
  547. -- NOT VALID with plan invalidation -- ensure we don't use a constraint for
  548. -- exclusion until validated
  549. set constraint_exclusion TO 'partition';
  550. create table nv_parent (d date, check (false) no inherit not valid);
  551. -- not valid constraint added at creation time should automatically become valid
  552. \d nv_parent
  553. Table "public.nv_parent"
  554. Column | Type | Collation | Nullable | Default
  555. --------+------+-----------+----------+---------
  556. d | date | | |
  557. Check constraints:
  558. "nv_parent_check" CHECK (false) NO INHERIT
  559. create table nv_child_2010 () inherits (nv_parent);
  560. create table nv_child_2011 () inherits (nv_parent);
  561. alter table nv_child_2010 add check (d between '2010-01-01'::date and '2010-12-31'::date) not valid;
  562. alter table nv_child_2011 add check (d between '2011-01-01'::date and '2011-12-31'::date) not valid;
  563. explain (costs off) select * from nv_parent where d between '2011-08-01' and '2011-08-31';
  564. QUERY PLAN
  565. ---------------------------------------------------------------------------
  566. Append
  567. -> Seq Scan on nv_parent nv_parent_1
  568. Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
  569. -> Seq Scan on nv_child_2010 nv_parent_2
  570. Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
  571. -> Seq Scan on nv_child_2011 nv_parent_3
  572. Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
  573. (7 rows)
  574. create table nv_child_2009 (check (d between '2009-01-01'::date and '2009-12-31'::date)) inherits (nv_parent);
  575. explain (costs off) select * from nv_parent where d between '2011-08-01'::date and '2011-08-31'::date;
  576. QUERY PLAN
  577. ---------------------------------------------------------------------------
  578. Append
  579. -> Seq Scan on nv_parent nv_parent_1
  580. Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
  581. -> Seq Scan on nv_child_2010 nv_parent_2
  582. Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
  583. -> Seq Scan on nv_child_2011 nv_parent_3
  584. Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
  585. (7 rows)
  586. explain (costs off) select * from nv_parent where d between '2009-08-01'::date and '2009-08-31'::date;
  587. QUERY PLAN
  588. ---------------------------------------------------------------------------
  589. Append
  590. -> Seq Scan on nv_parent nv_parent_1
  591. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  592. -> Seq Scan on nv_child_2010 nv_parent_2
  593. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  594. -> Seq Scan on nv_child_2011 nv_parent_3
  595. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  596. -> Seq Scan on nv_child_2009 nv_parent_4
  597. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  598. (9 rows)
  599. -- after validation, the constraint should be used
  600. alter table nv_child_2011 VALIDATE CONSTRAINT nv_child_2011_d_check;
  601. explain (costs off) select * from nv_parent where d between '2009-08-01'::date and '2009-08-31'::date;
  602. QUERY PLAN
  603. ---------------------------------------------------------------------------
  604. Append
  605. -> Seq Scan on nv_parent nv_parent_1
  606. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  607. -> Seq Scan on nv_child_2010 nv_parent_2
  608. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  609. -> Seq Scan on nv_child_2009 nv_parent_3
  610. Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
  611. (7 rows)
  612. -- add an inherited NOT VALID constraint
  613. alter table nv_parent add check (d between '2001-01-01'::date and '2099-12-31'::date) not valid;
  614. \d nv_child_2009
  615. Table "public.nv_child_2009"
  616. Column | Type | Collation | Nullable | Default
  617. --------+------+-----------+----------+---------
  618. d | date | | |
  619. Check constraints:
  620. "nv_child_2009_d_check" CHECK (d >= '01-01-2009'::date AND d <= '12-31-2009'::date)
  621. "nv_parent_d_check" CHECK (d >= '01-01-2001'::date AND d <= '12-31-2099'::date) NOT VALID
  622. Inherits: nv_parent
  623. -- we leave nv_parent and children around to help test pg_dump logic
  624. -- Foreign key adding test with mixed types
  625. -- Note: these tables are TEMP to avoid name conflicts when this test
  626. -- is run in parallel with foreign_key.sql.
  627. CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
  628. INSERT INTO PKTABLE VALUES(42);
  629. CREATE TEMP TABLE FKTABLE (ftest1 inet);
  630. -- This next should fail, because int=inet does not exist
  631. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  632. ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implemented
  633. DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
  634. -- This should also fail for the same reason, but here we
  635. -- give the column name
  636. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
  637. ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implemented
  638. DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
  639. DROP TABLE FKTABLE;
  640. -- This should succeed, even though they are different types,
  641. -- because int=int8 exists and is a member of the integer opfamily
  642. CREATE TEMP TABLE FKTABLE (ftest1 int8);
  643. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  644. -- Check it actually works
  645. INSERT INTO FKTABLE VALUES(42); -- should succeed
  646. INSERT INTO FKTABLE VALUES(43); -- should fail
  647. ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
  648. DETAIL: Key (ftest1)=(43) is not present in table "pktable".
  649. DROP TABLE FKTABLE;
  650. -- This should fail, because we'd have to cast numeric to int which is
  651. -- not an implicit coercion (or use numeric=numeric, but that's not part
  652. -- of the integer opfamily)
  653. CREATE TEMP TABLE FKTABLE (ftest1 numeric);
  654. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  655. ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implemented
  656. DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: numeric and integer.
  657. DROP TABLE FKTABLE;
  658. DROP TABLE PKTABLE;
  659. -- On the other hand, this should work because int implicitly promotes to
  660. -- numeric, and we allow promotion on the FK side
  661. CREATE TEMP TABLE PKTABLE (ptest1 numeric PRIMARY KEY);
  662. INSERT INTO PKTABLE VALUES(42);
  663. CREATE TEMP TABLE FKTABLE (ftest1 int);
  664. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
  665. -- Check it actually works
  666. INSERT INTO FKTABLE VALUES(42); -- should succeed
  667. INSERT INTO FKTABLE VALUES(43); -- should fail
  668. ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
  669. DETAIL: Key (ftest1)=(43) is not present in table "pktable".
  670. DROP TABLE FKTABLE;
  671. DROP TABLE PKTABLE;
  672. CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
  673. PRIMARY KEY(ptest1, ptest2));
  674. -- This should fail, because we just chose really odd types
  675. CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
  676. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
  677. ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
  678. DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
  679. DROP TABLE FKTABLE;
  680. -- Again, so should this...
  681. CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
  682. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
  683. references pktable(ptest1, ptest2);
  684. ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
  685. DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
  686. DROP TABLE FKTABLE;
  687. -- This fails because we mixed up the column ordering
  688. CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
  689. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
  690. references pktable(ptest2, ptest1);
  691. ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
  692. DETAIL: Key columns "ftest1" and "ptest2" are of incompatible types: integer and inet.
  693. -- As does this...
  694. ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
  695. references pktable(ptest1, ptest2);
  696. ERROR: foreign key constraint "fktable_ftest2_ftest1_fkey" cannot be implemented
  697. DETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
  698. DROP TABLE FKTABLE;
  699. DROP TABLE PKTABLE;
  700. -- Test that ALTER CONSTRAINT updates trigger deferrability properly
  701. CREATE TEMP TABLE PKTABLE (ptest1 int primary key);
  702. CREATE TEMP TABLE FKTABLE (ftest1 int);
  703. ALTER TABLE FKTABLE ADD CONSTRAINT fknd FOREIGN KEY(ftest1) REFERENCES pktable
  704. ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE;
  705. ALTER TABLE FKTABLE ADD CONSTRAINT fkdd FOREIGN KEY(ftest1) REFERENCES pktable
  706. ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED;
  707. ALTER TABLE FKTABLE ADD CONSTRAINT fkdi FOREIGN KEY(ftest1) REFERENCES pktable
  708. ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE;
  709. ALTER TABLE FKTABLE ADD CONSTRAINT fknd2 FOREIGN KEY(ftest1) REFERENCES pktable
  710. ON DELETE CASCADE ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED;
  711. ALTER TABLE FKTABLE ALTER CONSTRAINT fknd2 NOT DEFERRABLE;
  712. ALTER TABLE FKTABLE ADD CONSTRAINT fkdd2 FOREIGN KEY(ftest1) REFERENCES pktable
  713. ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE;
  714. ALTER TABLE FKTABLE ALTER CONSTRAINT fkdd2 DEFERRABLE INITIALLY DEFERRED;
  715. ALTER TABLE FKTABLE ADD CONSTRAINT fkdi2 FOREIGN KEY(ftest1) REFERENCES pktable
  716. ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE;
  717. ALTER TABLE FKTABLE ALTER CONSTRAINT fkdi2 DEFERRABLE INITIALLY IMMEDIATE;
  718. SELECT conname, tgfoid::regproc, tgtype, tgdeferrable, tginitdeferred
  719. FROM pg_trigger JOIN pg_constraint con ON con.oid = tgconstraint
  720. WHERE tgrelid = 'pktable'::regclass
  721. ORDER BY 1,2,3;
  722. conname | tgfoid | tgtype | tgdeferrable | tginitdeferred
  723. ---------+------------------------+--------+--------------+----------------
  724. fkdd | "RI_FKey_cascade_del" | 9 | f | f
  725. fkdd | "RI_FKey_noaction_upd" | 17 | t | t
  726. fkdd2 | "RI_FKey_cascade_del" | 9 | f | f
  727. fkdd2 | "RI_FKey_noaction_upd" | 17 | t | t
  728. fkdi | "RI_FKey_cascade_del" | 9 | f | f
  729. fkdi | "RI_FKey_noaction_upd" | 17 | t | f
  730. fkdi2 | "RI_FKey_cascade_del" | 9 | f | f
  731. fkdi2 | "RI_FKey_noaction_upd" | 17 | t | f
  732. fknd | "RI_FKey_cascade_del" | 9 | f | f
  733. fknd | "RI_FKey_noaction_upd" | 17 | f | f
  734. fknd2 | "RI_FKey_cascade_del" | 9 | f | f
  735. fknd2 | "RI_FKey_noaction_upd" | 17 | f | f
  736. (12 rows)
  737. SELECT conname, tgfoid::regproc, tgtype, tgdeferrable, tginitdeferred
  738. FROM pg_trigger JOIN pg_constraint con ON con.oid = tgconstraint
  739. WHERE tgrelid = 'fktable'::regclass
  740. ORDER BY 1,2,3;
  741. conname | tgfoid | tgtype | tgdeferrable | tginitdeferred
  742. ---------+---------------------+--------+--------------+----------------
  743. fkdd | "RI_FKey_check_ins" | 5 | t | t
  744. fkdd | "RI_FKey_check_upd" | 17 | t | t
  745. fkdd2 | "RI_FKey_check_ins" | 5 | t | t
  746. fkdd2 | "RI_FKey_check_upd" | 17 | t | t
  747. fkdi | "RI_FKey_check_ins" | 5 | t | f
  748. fkdi | "RI_FKey_check_upd" | 17 | t | f
  749. fkdi2 | "RI_FKey_check_ins" | 5 | t | f
  750. fkdi2 | "RI_FKey_check_upd" | 17 | t | f
  751. fknd | "RI_FKey_check_ins" | 5 | f | f
  752. fknd | "RI_FKey_check_upd" | 17 | f | f
  753. fknd2 | "RI_FKey_check_ins" | 5 | f | f
  754. fknd2 | "RI_FKey_check_upd" | 17 | f | f
  755. (12 rows)
  756. -- temp tables should go away by themselves, need not drop them.
  757. -- test check constraint adding
  758. create table atacc1 ( test int );
  759. -- add a check constraint
  760. alter table atacc1 add constraint atacc_test1 check (test>3);
  761. -- should fail
  762. insert into atacc1 (test) values (2);
  763. ERROR: new row for relation "atacc1" violates check constraint "atacc_test1"
  764. DETAIL: Failing row contains (2).
  765. -- should succeed
  766. insert into atacc1 (test) values (4);
  767. drop table atacc1;
  768. -- let's do one where the check fails when added
  769. create table atacc1 ( test int );
  770. -- insert a soon to be failing row
  771. insert into atacc1 (test) values (2);
  772. -- add a check constraint (fails)
  773. alter table atacc1 add constraint atacc_test1 check (test>3);
  774. ERROR: check constraint "atacc_test1" of relation "atacc1" is violated by some row
  775. insert into atacc1 (test) values (4);
  776. drop table atacc1;
  777. -- let's do one where the check fails because the column doesn't exist
  778. create table atacc1 ( test int );
  779. -- add a check constraint (fails)
  780. alter table atacc1 add constraint atacc_test1 check (test1>3);
  781. ERROR: column "test1" does not exist
  782. HINT: Perhaps you meant to reference the column "atacc1.test".
  783. drop table atacc1;
  784. -- something a little more complicated
  785. create table atacc1 ( test int, test2 int, test3 int);
  786. -- add a check constraint (fails)
  787. alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
  788. -- should fail
  789. insert into atacc1 (test,test2,test3) values (4,4,2);
  790. ERROR: new row for relation "atacc1" violates check constraint "atacc_test1"
  791. DETAIL: Failing row contains (4, 4, 2).
  792. -- should succeed
  793. insert into atacc1 (test,test2,test3) values (4,4,5);
  794. drop table atacc1;
  795. -- lets do some naming tests
  796. create table atacc1 (test int check (test>3), test2 int);
  797. alter table atacc1 add check (test2>test);
  798. -- should fail for $2
  799. insert into atacc1 (test2, test) values (3, 4);
  800. ERROR: new row for relation "atacc1" violates check constraint "atacc1_check"
  801. DETAIL: Failing row contains (4, 3).
  802. drop table atacc1;
  803. -- inheritance related tests
  804. create table atacc1 (test int);
  805. create table atacc2 (test2 int);
  806. create table atacc3 (test3 int) inherits (atacc1, atacc2);
  807. alter table atacc2 add constraint foo check (test2>0);
  808. -- fail and then succeed on atacc2
  809. insert into atacc2 (test2) values (-3);
  810. ERROR: new row for relation "atacc2" violates check constraint "foo"
  811. DETAIL: Failing row contains (-3).
  812. insert into atacc2 (test2) values (3);
  813. -- fail and then succeed on atacc3
  814. insert into atacc3 (test2) values (-3);
  815. ERROR: new row for relation "atacc3" violates check constraint "foo"
  816. DETAIL: Failing row contains (null, -3, null).
  817. insert into atacc3 (test2) values (3);
  818. drop table atacc3;
  819. drop table atacc2;
  820. drop table atacc1;
  821. -- same things with one created with INHERIT
  822. create table atacc1 (test int);
  823. create table atacc2 (test2 int);
  824. create table atacc3 (test3 int) inherits (atacc1, atacc2);
  825. alter table atacc3 no inherit atacc2;
  826. -- fail
  827. alter table atacc3 no inherit atacc2;
  828. ERROR: relation "atacc2" is not a parent of relation "atacc3"
  829. -- make sure it really isn't a child
  830. insert into atacc3 (test2) values (3);
  831. select test2 from atacc2;
  832. test2
  833. -------
  834. (0 rows)
  835. -- fail due to missing constraint
  836. alter table atacc2 add constraint foo check (test2>0);
  837. alter table atacc3 inherit atacc2;
  838. ERROR: child table is missing constraint "foo"
  839. -- fail due to missing column
  840. alter table atacc3 rename test2 to testx;
  841. alter table atacc3 inherit atacc2;
  842. ERROR: child table is missing column "test2"
  843. -- fail due to mismatched data type
  844. alter table atacc3 add test2 bool;
  845. alter table atacc3 inherit atacc2;
  846. ERROR: child table "atacc3" has different type for column "test2"
  847. alter table atacc3 drop test2;
  848. -- succeed
  849. alter table atacc3 add test2 int;
  850. update atacc3 set test2 = 4 where test2 is null;
  851. alter table atacc3 add constraint foo check (test2>0);
  852. alter table atacc3 inherit atacc2;
  853. -- fail due to duplicates and circular inheritance
  854. alter table atacc3 inherit atacc2;
  855. ERROR: relation "atacc2" would be inherited from more than once
  856. alter table atacc2 inherit atacc3;
  857. ERROR: circular inheritance not allowed
  858. DETAIL: "atacc3" is already a child of "atacc2".
  859. alter table atacc2 inherit atacc2;
  860. ERROR: circular inheritance not allowed
  861. DETAIL: "atacc2" is already a child of "atacc2".
  862. -- test that we really are a child now (should see 4 not 3 and cascade should go through)
  863. select test2 from atacc2;
  864. test2
  865. -------
  866. 4
  867. (1 row)
  868. drop table atacc2 cascade;
  869. NOTICE: drop cascades to table atacc3
  870. drop table atacc1;
  871. -- adding only to a parent is allowed as of 9.2
  872. create table atacc1 (test int);
  873. create table atacc2 (test2 int) inherits (atacc1);
  874. -- ok:
  875. alter table atacc1 add constraint foo check (test>0) no inherit;
  876. -- check constraint is not there on child
  877. insert into atacc2 (test) values (-3);
  878. -- check constraint is there on parent
  879. insert into atacc1 (test) values (-3);
  880. ERROR: new row for relation "atacc1" violates check constraint "foo"
  881. DETAIL: Failing row contains (-3).
  882. insert into atacc1 (test) values (3);
  883. -- fail, violating row:
  884. alter table atacc2 add constraint foo check (test>0) no inherit;
  885. ERROR: check constraint "foo" of relation "atacc2" is violated by some row
  886. drop table atacc2;
  887. drop table atacc1;
  888. -- test unique constraint adding
  889. create table atacc1 ( test int ) ;
  890. -- add a unique constraint
  891. alter table atacc1 add constraint atacc_test1 unique (test);
  892. -- insert first value
  893. insert into atacc1 (test) values (2);
  894. -- should fail
  895. insert into atacc1 (test) values (2);
  896. ERROR: duplicate key value violates unique constraint "atacc_test1"
  897. DETAIL: Key (test)=(2) already exists.
  898. -- should succeed
  899. insert into atacc1 (test) values (4);
  900. -- try to create duplicates via alter table using - should fail
  901. alter table atacc1 alter column test type integer using 0;
  902. ERROR: could not create unique index "atacc_test1"
  903. DETAIL: Key (test)=(0) is duplicated.
  904. drop table atacc1;
  905. -- let's do one where the unique constraint fails when added
  906. create table atacc1 ( test int );
  907. -- insert soon to be failing rows
  908. insert into atacc1 (test) values (2);
  909. insert into atacc1 (test) values (2);
  910. -- add a unique constraint (fails)
  911. alter table atacc1 add constraint atacc_test1 unique (test);
  912. ERROR: could not create unique index "atacc_test1"
  913. DETAIL: Key (test)=(2) is duplicated.
  914. insert into atacc1 (test) values (3);
  915. drop table atacc1;
  916. -- let's do one where the unique constraint fails
  917. -- because the column doesn't exist
  918. create table atacc1 ( test int );
  919. -- add a unique constraint (fails)
  920. alter table atacc1 add constraint atacc_test1 unique (test1);
  921. ERROR: column "test1" named in key does not exist
  922. drop table atacc1;
  923. -- something a little more complicated
  924. create table atacc1 ( test int, test2 int);
  925. -- add a unique constraint
  926. alter table atacc1 add constraint atacc_test1 unique (test, test2);
  927. -- insert initial value
  928. insert into atacc1 (test,test2) values (4,4);
  929. -- should fail
  930. insert into atacc1 (test,test2) values (4,4);
  931. ERROR: duplicate key value violates unique constraint "atacc_test1"
  932. DETAIL: Key (test, test2)=(4, 4) already exists.
  933. -- should all succeed
  934. insert into atacc1 (test,test2) values (4,5);
  935. insert into atacc1 (test,test2) values (5,4);
  936. insert into atacc1 (test,test2) values (5,5);
  937. drop table atacc1;
  938. -- lets do some naming tests
  939. create table atacc1 (test int, test2 int, unique(test));
  940. alter table atacc1 add unique (test2);
  941. -- should fail for @@ second one @@
  942. insert into atacc1 (test2, test) values (3, 3);
  943. insert into atacc1 (test2, test) values (2, 3);
  944. ERROR: duplicate key value violates unique constraint "atacc1_test_key"
  945. DETAIL: Key (test)=(3) already exists.
  946. drop table atacc1;
  947. -- test primary key constraint adding
  948. create table atacc1 ( id serial, test int) ;
  949. -- add a primary key constraint
  950. alter table atacc1 add constraint atacc_test1 primary key (test);
  951. -- insert first value
  952. insert into atacc1 (test) values (2);
  953. -- should fail
  954. insert into atacc1 (test) values (2);
  955. ERROR: duplicate key value violates unique constraint "atacc_test1"
  956. DETAIL: Key (test)=(2) already exists.
  957. -- should succeed
  958. insert into atacc1 (test) values (4);
  959. -- inserting NULL should fail
  960. insert into atacc1 (test) values(NULL);
  961. ERROR: null value in column "test" of relation "atacc1" violates not-null constraint
  962. DETAIL: Failing row contains (4, null).
  963. -- try adding a second primary key (should fail)
  964. alter table atacc1 add constraint atacc_oid1 primary key(id);
  965. ERROR: multiple primary keys for table "atacc1" are not allowed
  966. -- drop first primary key constraint
  967. alter table atacc1 drop constraint atacc_test1 restrict;
  968. -- try adding a primary key on oid (should succeed)
  969. alter table atacc1 add constraint atacc_oid1 primary key(id);
  970. drop table atacc1;
  971. -- let's do one where the primary key constraint fails when added
  972. create table atacc1 ( test int );
  973. -- insert soon to be failing rows
  974. insert into atacc1 (test) values (2);
  975. insert into atacc1 (test) values (2);
  976. -- add a primary key (fails)
  977. alter table atacc1 add constraint atacc_test1 primary key (test);
  978. ERROR: could not create unique index "atacc_test1"
  979. DETAIL: Key (test)=(2) is duplicated.
  980. insert into atacc1 (test) values (3);
  981. drop table atacc1;
  982. -- let's do another one where the primary key constraint fails when added
  983. create table atacc1 ( test int );
  984. -- insert soon to be failing row
  985. insert into atacc1 (test) values (NULL);
  986. -- add a primary key (fails)
  987. alter table atacc1 add constraint atacc_test1 primary key (test);
  988. ERROR: column "test" of relation "atacc1" contains null values
  989. insert into atacc1 (test) values (3);
  990. drop table atacc1;
  991. -- let's do one where the primary key constraint fails
  992. -- because the column doesn't exist
  993. create table atacc1 ( test int );
  994. -- add a primary key constraint (fails)
  995. alter table atacc1 add constraint atacc_test1 primary key (test1);
  996. ERROR: column "test1" of relation "atacc1" does not exist
  997. drop table atacc1;
  998. -- adding a new column as primary key to a non-empty table.
  999. -- should fail unless the column has a non-null default value.
  1000. create table atacc1 ( test int );
  1001. insert into atacc1 (test) values (0);
  1002. -- add a primary key column without a default (fails).
  1003. alter table atacc1 add column test2 int primary key;
  1004. ERROR: column "test2" of relation "atacc1" contains null values
  1005. -- now add a primary key column with a default (succeeds).
  1006. alter table atacc1 add column test2 int default 0 primary key;
  1007. drop table atacc1;
  1008. -- this combination used to have order-of-execution problems (bug #15580)
  1009. create table atacc1 (a int);
  1010. insert into atacc1 values(1);
  1011. alter table atacc1
  1012. add column b float8 not null default random(),
  1013. add primary key(a);
  1014. drop table atacc1;
  1015. -- additionally, we've seen issues with foreign key validation not being
  1016. -- properly delayed until after a table rewrite. Check that works ok.
  1017. create table atacc1 (a int primary key);
  1018. alter table atacc1 add constraint atacc1_fkey foreign key (a) references atacc1 (a) not valid;
  1019. alter table atacc1 validate constraint atacc1_fkey, alter a type bigint;
  1020. drop table atacc1;
  1021. -- we've also seen issues with check constraints being validated at the wrong
  1022. -- time when there's a pending table rewrite.
  1023. create table atacc1 (a bigint, b int);
  1024. insert into atacc1 values(1,1);
  1025. alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
  1026. alter table atacc1 validate constraint atacc1_chk, alter a type int;
  1027. drop table atacc1;
  1028. -- same as above, but ensure the constraint violation is detected
  1029. create table atacc1 (a bigint, b int);
  1030. insert into atacc1 values(1,2);
  1031. alter table atacc1 add constraint atacc1_chk check(b = 1) not valid;
  1032. alter table atacc1 validate constraint atacc1_chk, alter a type int;
  1033. ERROR: check constraint "atacc1_chk" of relation "atacc1" is violated by some row
  1034. drop table atacc1;
  1035. -- something a little more complicated
  1036. create table atacc1 ( test int, test2 int);
  1037. -- add a primary key constraint
  1038. alter table atacc1 add constraint atacc_test1 primary key (test, test2);
  1039. -- try adding a second primary key - should fail
  1040. alter table atacc1 add constraint atacc_test2 primary key (test);
  1041. ERROR: multiple primary keys for table "atacc1" are not allowed
  1042. -- insert initial value
  1043. insert into atacc1 (test,test2) values (4,4);
  1044. -- should fail
  1045. insert into atacc1 (test,test2) values (4,4);
  1046. ERROR: duplicate key value violates unique constraint "atacc_test1"
  1047. DETAIL: Key (test, test2)=(4, 4) already exists.
  1048. insert into atacc1 (test,test2) values (NULL,3);
  1049. ERROR: null value in column "test" of relation "atacc1" violates not-null constraint
  1050. DETAIL: Failing row contains (null, 3).
  1051. insert into atacc1 (test,test2) values (3, NULL);
  1052. ERROR: null value in column "test2" of relation "atacc1" violates not-null constraint
  1053. DETAIL: Failing row contains (3, null).
  1054. insert into atacc1 (test,test2) values (NULL,NULL);
  1055. ERROR: null value in column "test" of relation "atacc1" violates not-null constraint
  1056. DETAIL: Failing row contains (null, null).
  1057. -- should all succeed
  1058. insert into atacc1 (test,test2) values (4,5);
  1059. insert into atacc1 (test,test2) values (5,4);
  1060. insert into atacc1 (test,test2) values (5,5);
  1061. drop table atacc1;
  1062. -- lets do some naming tests
  1063. create table atacc1 (test int, test2 int, primary key(test));
  1064. -- only first should succeed
  1065. insert into atacc1 (test2, test) values (3, 3);
  1066. insert into atacc1 (test2, test) values (2, 3);
  1067. ERROR: duplicate key value violates unique constraint "atacc1_pkey"
  1068. DETAIL: Key (test)=(3) already exists.
  1069. insert into atacc1 (test2, test) values (1, NULL);
  1070. ERROR: null value in column "test" of relation "atacc1" violates not-null constraint
  1071. DETAIL: Failing row contains (null, 1).
  1072. drop table atacc1;
  1073. -- alter table / alter column [set/drop] not null tests
  1074. -- try altering system catalogs, should fail
  1075. alter table pg_class alter column relname drop not null;
  1076. ERROR: permission denied: "pg_class" is a system catalog
  1077. alter table pg_class alter relname set not null;
  1078. ERROR: permission denied: "pg_class" is a system catalog
  1079. -- try altering non-existent table, should fail
  1080. alter table non_existent alter column bar set not null;
  1081. ERROR: relation "non_existent" does not exist
  1082. alter table non_existent alter column bar drop not null;
  1083. ERROR: relation "non_existent" does not exist
  1084. -- test setting columns to null and not null and vice versa
  1085. -- test checking for null values and primary key
  1086. create table atacc1 (test int not null);
  1087. alter table atacc1 add constraint "atacc1_pkey" primary key (test);
  1088. alter table atacc1 alter column test drop not null;
  1089. ERROR: column "test" is in a primary key
  1090. alter table atacc1 drop constraint "atacc1_pkey";
  1091. alter table atacc1 alter column test drop not null;
  1092. insert into atacc1 values (null);
  1093. alter table atacc1 alter test set not null;
  1094. ERROR: column "test" of relation "atacc1" contains null values
  1095. delete from atacc1;
  1096. alter table atacc1 alter test set not null;
  1097. -- try altering a non-existent column, should fail
  1098. alter table atacc1 alter bar set not null;
  1099. ERROR: column "bar" of relation "atacc1" does not exist
  1100. alter table atacc1 alter bar drop not null;
  1101. ERROR: column "bar" of relation "atacc1" does not exist
  1102. -- try creating a view and altering that, should fail
  1103. create view myview as select * from atacc1;
  1104. alter table myview alter column test drop not null;
  1105. ERROR: "myview" is not a table or foreign table
  1106. alter table myview alter column test set not null;
  1107. ERROR: "myview" is not a table or foreign table
  1108. drop view myview;
  1109. drop table atacc1;
  1110. -- set not null verified by constraints
  1111. create table atacc1 (test_a int, test_b int);
  1112. insert into atacc1 values (null, 1);
  1113. -- constraint not cover all values, should fail
  1114. alter table atacc1 add constraint atacc1_constr_or check(test_a is not null or test_b < 10);
  1115. alter table atacc1 alter test_a set not null;
  1116. ERROR: column "test_a" of relation "atacc1" contains null values
  1117. alter table atacc1 drop constraint atacc1_constr_or;
  1118. -- not valid constraint, should fail
  1119. alter table atacc1 add constraint atacc1_constr_invalid check(test_a is not null) not valid;
  1120. alter table atacc1 alter test_a set not null;
  1121. ERROR: column "test_a" of relation "atacc1" contains null values
  1122. alter table atacc1 drop constraint atacc1_constr_invalid;
  1123. -- with valid constraint
  1124. update atacc1 set test_a = 1;
  1125. alter table atacc1 add constraint atacc1_constr_a_valid check(test_a is not null);
  1126. alter table atacc1 alter test_a set not null;
  1127. delete from atacc1;
  1128. insert into atacc1 values (2, null);
  1129. alter table atacc1 alter test_a drop not null;
  1130. -- test multiple set not null at same time
  1131. -- test_a checked by atacc1_constr_a_valid, test_b should fail by table scan
  1132. alter table atacc1 alter test_a set not null, alter test_b set not null;
  1133. ERROR: column "test_b" of relation "atacc1" contains null values
  1134. -- commands order has no importance
  1135. alter table atacc1 alter test_b set not null, alter test_a set not null;
  1136. ERROR: column "test_b" of relation "atacc1" contains null values
  1137. -- valid one by table scan, one by check constraints
  1138. update atacc1 set test_b = 1;
  1139. alter table atacc1 alter test_b set not null, alter test_a set not null;
  1140. alter table atacc1 alter test_a drop not null, alter test_b drop not null;
  1141. -- both column has check constraints
  1142. alter table atacc1 add constraint atacc1_constr_b_valid check(test_b is not null);
  1143. alter table atacc1 alter test_b set not null, alter test_a set not null;
  1144. drop table atacc1;
  1145. -- test inheritance
  1146. create table parent (a int);
  1147. create table child (b varchar(255)) inherits (parent);
  1148. alter table parent alter a set not null;
  1149. insert into parent values (NULL);
  1150. ERROR: null value in column "a" of relation "parent" violates not-null constraint
  1151. DETAIL: Failing row contains (null).
  1152. insert into child (a, b) values (NULL, 'foo');
  1153. ERROR: null value in column "a" of relation "child" violates not-null constraint
  1154. DETAIL: Failing row contains (null, foo).
  1155. alter table parent alter a drop not null;
  1156. insert into parent values (NULL);
  1157. insert into child (a, b) values (NULL, 'foo');
  1158. alter table only parent alter a set not null;
  1159. ERROR: column "a" of relation "parent" contains null values
  1160. alter table child alter a set not null;
  1161. ERROR: column "a" of relation "child" contains null values
  1162. delete from parent;
  1163. alter table only parent alter a set not null;
  1164. insert into parent values (NULL);
  1165. ERROR: null value in column "a" of relation "parent" violates not-null constraint
  1166. DETAIL: Failing row contains (null).
  1167. alter table child alter a set not null;
  1168. insert into child (a, b) values (NULL, 'foo');
  1169. ERROR: null value in column "a" of relation "child" violates not-null constraint
  1170. DETAIL: Failing row contains (null, foo).
  1171. delete from child;
  1172. alter table child alter a set not null;
  1173. insert into child (a, b) values (NULL, 'foo');
  1174. ERROR: null value in column "a" of relation "child" violates not-null constraint
  1175. DETAIL: Failing row contains (null, foo).
  1176. drop table child;
  1177. drop table parent;
  1178. -- test setting and removing default values
  1179. create table def_test (
  1180. c1 int4 default 5,
  1181. c2 text default 'initial_default'
  1182. );
  1183. insert into def_test default values;
  1184. alter table def_test alter column c1 drop default;
  1185. insert into def_test default values;
  1186. alter table def_test alter column c2 drop default;
  1187. insert into def_test default values;
  1188. alter table def_test alter column c1 set default 10;
  1189. alter table def_test alter column c2 set default 'new_default';
  1190. insert into def_test default values;
  1191. select * from def_test;
  1192. c1 | c2
  1193. ----+-----------------
  1194. 5 | initial_default
  1195. | initial_default
  1196. |
  1197. 10 | new_default
  1198. (4 rows)
  1199. -- set defaults to an incorrect type: this should fail
  1200. alter table def_test alter column c1 set default 'wrong_datatype';
  1201. ERROR: invalid input syntax for type integer: "wrong_datatype"
  1202. alter table def_test alter column c2 set default 20;
  1203. -- set defaults on a non-existent column: this should fail
  1204. alter table def_test alter column c3 set default 30;
  1205. ERROR: column "c3" of relation "def_test" does not exist
  1206. -- set defaults on views: we need to create a view, add a rule
  1207. -- to allow insertions into it, and then alter the view to add
  1208. -- a default
  1209. create view def_view_test as select * from def_test;
  1210. create rule def_view_test_ins as
  1211. on insert to def_view_test
  1212. do instead insert into def_test select new.*;
  1213. insert into def_view_test default values;
  1214. alter table def_view_test alter column c1 set default 45;
  1215. insert into def_view_test default values;
  1216. alter table def_view_test alter column c2 set default 'view_default';
  1217. insert into def_view_test default values;
  1218. select * from def_view_test;
  1219. c1 | c2
  1220. ----+-----------------
  1221. 5 | initial_default
  1222. | initial_default
  1223. |
  1224. 10 | new_default
  1225. |
  1226. 45 |
  1227. 45 | view_default
  1228. (7 rows)
  1229. drop rule def_view_test_ins on def_view_test;
  1230. drop view def_view_test;
  1231. drop table def_test;
  1232. -- alter table / drop column tests
  1233. -- try altering system catalogs, should fail
  1234. alter table pg_class drop column relname;
  1235. ERROR: permission denied: "pg_class" is a system catalog
  1236. -- try altering non-existent table, should fail
  1237. alter table nosuchtable drop column bar;
  1238. ERROR: relation "nosuchtable" does not exist
  1239. -- test dropping columns
  1240. create table atacc1 (a int4 not null, b int4, c int4 not null, d int4);
  1241. insert into atacc1 values (1, 2, 3, 4);
  1242. alter table atacc1 drop a;
  1243. alter table atacc1 drop a;
  1244. ERROR: column "a" of relation "atacc1" does not exist
  1245. -- SELECTs
  1246. select * from atacc1;
  1247. b | c | d
  1248. ---+---+---
  1249. 2 | 3 | 4
  1250. (1 row)
  1251. select * from atacc1 order by a;
  1252. ERROR: column "a" does not exist
  1253. LINE 1: select * from atacc1 order by a;
  1254. ^
  1255. select * from atacc1 order by "........pg.dropped.1........";
  1256. ERROR: column "........pg.dropped.1........" does not exist
  1257. LINE 1: select * from atacc1 order by "........pg.dropped.1........"...
  1258. ^
  1259. select * from atacc1 group by a;
  1260. ERROR: column "a" does not exist
  1261. LINE 1: select * from atacc1 group by a;
  1262. ^
  1263. select * from atacc1 group by "........pg.dropped.1........";
  1264. ERROR: column "........pg.dropped.1........" does not exist
  1265. LINE 1: select * from atacc1 group by "........pg.dropped.1........"...
  1266. ^
  1267. select atacc1.* from atacc1;
  1268. b | c | d
  1269. ---+---+---
  1270. 2 | 3 | 4
  1271. (1 row)
  1272. select a from atacc1;
  1273. ERROR: column "a" does not exist
  1274. LINE 1: select a from atacc1;
  1275. ^
  1276. select atacc1.a from atacc1;
  1277. ERROR: column atacc1.a does not exist
  1278. LINE 1: select atacc1.a from atacc1;
  1279. ^
  1280. select b,c,d from atacc1;
  1281. b | c | d
  1282. ---+---+---
  1283. 2 | 3 | 4
  1284. (1 row)
  1285. select a,b,c,d from atacc1;
  1286. ERROR: column "a" does not exist
  1287. LINE 1: select a,b,c,d from atacc1;
  1288. ^
  1289. select * from atacc1 where a = 1;
  1290. ERROR: column "a" does not exist
  1291. LINE 1: select * from atacc1 where a = 1;
  1292. ^
  1293. select "........pg.dropped.1........" from atacc1;
  1294. ERROR: column "........pg.dropped.1........" does not exist
  1295. LINE 1: select "........pg.dropped.1........" from atacc1;
  1296. ^
  1297. select atacc1."........pg.dropped.1........" from atacc1;
  1298. ERROR: column atacc1.........pg.dropped.1........ does not exist
  1299. LINE 1: select atacc1."........pg.dropped.1........" from atacc1;
  1300. ^
  1301. select "........pg.dropped.1........",b,c,d from atacc1;
  1302. ERROR: column "........pg.dropped.1........" does not exist
  1303. LINE 1: select "........pg.dropped.1........",b,c,d from atacc1;
  1304. ^
  1305. select * from atacc1 where "........pg.dropped.1........" = 1;
  1306. ERROR: column "........pg.dropped.1........" does not exist
  1307. LINE 1: select * from atacc1 where "........pg.dropped.1........" = ...
  1308. ^
  1309. -- UPDATEs
  1310. update atacc1 set a = 3;
  1311. ERROR: column "a" of relation "atacc1" does not exist
  1312. LINE 1: update atacc1 set a = 3;
  1313. ^
  1314. update atacc1 set b = 2 where a = 3;
  1315. ERROR: column "a" does not exist
  1316. LINE 1: update atacc1 set b = 2 where a = 3;
  1317. ^
  1318. update atacc1 set "........pg.dropped.1........" = 3;
  1319. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1320. LINE 1: update atacc1 set "........pg.dropped.1........" = 3;
  1321. ^
  1322. update atacc1 set b = 2 where "........pg.dropped.1........" = 3;
  1323. ERROR: column "........pg.dropped.1........" does not exist
  1324. LINE 1: update atacc1 set b = 2 where "........pg.dropped.1........"...
  1325. ^
  1326. -- INSERTs
  1327. insert into atacc1 values (10, 11, 12, 13);
  1328. ERROR: INSERT has more expressions than target columns
  1329. LINE 1: insert into atacc1 values (10, 11, 12, 13);
  1330. ^
  1331. insert into atacc1 values (default, 11, 12, 13);
  1332. ERROR: INSERT has more expressions than target columns
  1333. LINE 1: insert into atacc1 values (default, 11, 12, 13);
  1334. ^
  1335. insert into atacc1 values (11, 12, 13);
  1336. insert into atacc1 (a) values (10);
  1337. ERROR: column "a" of relation "atacc1" does not exist
  1338. LINE 1: insert into atacc1 (a) values (10);
  1339. ^
  1340. insert into atacc1 (a) values (default);
  1341. ERROR: column "a" of relation "atacc1" does not exist
  1342. LINE 1: insert into atacc1 (a) values (default);
  1343. ^
  1344. insert into atacc1 (a,b,c,d) values (10,11,12,13);
  1345. ERROR: column "a" of relation "atacc1" does not exist
  1346. LINE 1: insert into atacc1 (a,b,c,d) values (10,11,12,13);
  1347. ^
  1348. insert into atacc1 (a,b,c,d) values (default,11,12,13);
  1349. ERROR: column "a" of relation "atacc1" does not exist
  1350. LINE 1: insert into atacc1 (a,b,c,d) values (default,11,12,13);
  1351. ^
  1352. insert into atacc1 (b,c,d) values (11,12,13);
  1353. insert into atacc1 ("........pg.dropped.1........") values (10);
  1354. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1355. LINE 1: insert into atacc1 ("........pg.dropped.1........") values (...
  1356. ^
  1357. insert into atacc1 ("........pg.dropped.1........") values (default);
  1358. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1359. LINE 1: insert into atacc1 ("........pg.dropped.1........") values (...
  1360. ^
  1361. insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);
  1362. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1363. LINE 1: insert into atacc1 ("........pg.dropped.1........",b,c,d) va...
  1364. ^
  1365. insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);
  1366. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1367. LINE 1: insert into atacc1 ("........pg.dropped.1........",b,c,d) va...
  1368. ^
  1369. -- DELETEs
  1370. delete from atacc1 where a = 3;
  1371. ERROR: column "a" does not exist
  1372. LINE 1: delete from atacc1 where a = 3;
  1373. ^
  1374. delete from atacc1 where "........pg.dropped.1........" = 3;
  1375. ERROR: column "........pg.dropped.1........" does not exist
  1376. LINE 1: delete from atacc1 where "........pg.dropped.1........" = 3;
  1377. ^
  1378. delete from atacc1;
  1379. -- try dropping a non-existent column, should fail
  1380. alter table atacc1 drop bar;
  1381. ERROR: column "bar" of relation "atacc1" does not exist
  1382. -- try removing an oid column, should succeed (as it's nonexistent)
  1383. alter table atacc1 SET WITHOUT OIDS;
  1384. -- try adding an oid column, should fail (not supported)
  1385. alter table atacc1 SET WITH OIDS;
  1386. ERROR: syntax error at or near "WITH"
  1387. LINE 1: alter table atacc1 SET WITH OIDS;
  1388. ^
  1389. -- try dropping the xmin column, should fail
  1390. alter table atacc1 drop xmin;
  1391. ERROR: cannot drop system column "xmin"
  1392. -- try creating a view and altering that, should fail
  1393. create view myview as select * from atacc1;
  1394. select * from myview;
  1395. b | c | d
  1396. ---+---+---
  1397. (0 rows)
  1398. alter table myview drop d;
  1399. ERROR: "myview" is not a table, composite type, or foreign table
  1400. drop view myview;
  1401. -- test some commands to make sure they fail on the dropped column
  1402. analyze atacc1(a);
  1403. ERROR: column "a" of relation "atacc1" does not exist
  1404. analyze atacc1("........pg.dropped.1........");
  1405. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1406. vacuum analyze atacc1(a);
  1407. ERROR: column "a" of relation "atacc1" does not exist
  1408. vacuum analyze atacc1("........pg.dropped.1........");
  1409. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1410. comment on column atacc1.a is 'testing';
  1411. ERROR: column "a" of relation "atacc1" does not exist
  1412. comment on column atacc1."........pg.dropped.1........" is 'testing';
  1413. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1414. alter table atacc1 alter a set storage plain;
  1415. ERROR: column "a" of relation "atacc1" does not exist
  1416. alter table atacc1 alter "........pg.dropped.1........" set storage plain;
  1417. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1418. alter table atacc1 alter a set statistics 0;
  1419. ERROR: column "a" of relation "atacc1" does not exist
  1420. alter table atacc1 alter "........pg.dropped.1........" set statistics 0;
  1421. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1422. alter table atacc1 alter a set default 3;
  1423. ERROR: column "a" of relation "atacc1" does not exist
  1424. alter table atacc1 alter "........pg.dropped.1........" set default 3;
  1425. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1426. alter table atacc1 alter a drop default;
  1427. ERROR: column "a" of relation "atacc1" does not exist
  1428. alter table atacc1 alter "........pg.dropped.1........" drop default;
  1429. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1430. alter table atacc1 alter a set not null;
  1431. ERROR: column "a" of relation "atacc1" does not exist
  1432. alter table atacc1 alter "........pg.dropped.1........" set not null;
  1433. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1434. alter table atacc1 alter a drop not null;
  1435. ERROR: column "a" of relation "atacc1" does not exist
  1436. alter table atacc1 alter "........pg.dropped.1........" drop not null;
  1437. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1438. alter table atacc1 rename a to x;
  1439. ERROR: column "a" does not exist
  1440. alter table atacc1 rename "........pg.dropped.1........" to x;
  1441. ERROR: column "........pg.dropped.1........" does not exist
  1442. alter table atacc1 add primary key(a);
  1443. ERROR: column "a" of relation "atacc1" does not exist
  1444. alter table atacc1 add primary key("........pg.dropped.1........");
  1445. ERROR: column "........pg.dropped.1........" of relation "atacc1" does not exist
  1446. alter table atacc1 add unique(a);
  1447. ERROR: column "a" named in key does not exist
  1448. alter table atacc1 add unique("........pg.dropped.1........");
  1449. ERROR: column "........pg.dropped.1........" named in key does not exist
  1450. alter table atacc1 add check (a > 3);
  1451. ERROR: column "a" does not exist
  1452. alter table atacc1 add check ("........pg.dropped.1........" > 3);
  1453. ERROR: column "........pg.dropped.1........" does not exist
  1454. create table atacc2 (id int4 unique);
  1455. alter table atacc1 add foreign key (a) references atacc2(id);
  1456. ERROR: column "a" referenced in foreign key constraint does not exist
  1457. alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);
  1458. ERROR: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
  1459. alter table atacc2 add foreign key (id) references atacc1(a);
  1460. ERROR: column "a" referenced in foreign key constraint does not exist
  1461. alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");
  1462. ERROR: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
  1463. drop table atacc2;
  1464. create index "testing_idx" on atacc1(a);
  1465. ERROR: column "a" does not exist
  1466. create index "testing_idx" on atacc1("........pg.dropped.1........");
  1467. ERROR: column "........pg.dropped.1........" does not exist
  1468. -- test create as and select into
  1469. insert into atacc1 values (21, 22, 23);
  1470. create table attest1 as select * from atacc1;
  1471. select * from attest1;
  1472. b | c | d
  1473. ----+----+----
  1474. 21 | 22 | 23
  1475. (1 row)
  1476. drop table attest1;
  1477. select * into attest2 from atacc1;
  1478. select * from attest2;
  1479. b | c | d
  1480. ----+----+----
  1481. 21 | 22 | 23
  1482. (1 row)
  1483. drop table attest2;
  1484. -- try dropping all columns
  1485. alter table atacc1 drop c;
  1486. alter table atacc1 drop d;
  1487. alter table atacc1 drop b;
  1488. select * from atacc1;
  1489. --
  1490. (1 row)
  1491. drop table atacc1;
  1492. -- test constraint error reporting in presence of dropped columns
  1493. create table atacc1 (id serial primary key, value int check (value < 10));
  1494. insert into atacc1(value) values (100);
  1495. ERROR: new row for relation "atacc1" violates check constraint "atacc1_value_check"
  1496. DETAIL: Failing row contains (1, 100).
  1497. alter table atacc1 drop column value;
  1498. alter table atacc1 add column value int check (value < 10);
  1499. insert into atacc1(value) values (100);
  1500. ERROR: new row for relation "atacc1" violates check constraint "atacc1_value_check"
  1501. DETAIL: Failing row contains (2, 100).
  1502. insert into atacc1(id, value) values (null, 0);
  1503. ERROR: null value in column "id" of relation "atacc1" violates not-null constraint
  1504. DETAIL: Failing row contains (null, 0).
  1505. drop table atacc1;
  1506. -- test inheritance
  1507. create table parent (a int, b int, c int);
  1508. insert into parent values (1, 2, 3);
  1509. alter table parent drop a;
  1510. create table child (d varchar(255)) inherits (parent);
  1511. insert into child values (12, 13, 'testing');
  1512. select * from parent;
  1513. b | c
  1514. ----+----
  1515. 2 | 3
  1516. 12 | 13
  1517. (2 rows)
  1518. select * from child;
  1519. b | c | d
  1520. ----+----+---------
  1521. 12 | 13 | testing
  1522. (1 row)
  1523. alter table parent drop c;
  1524. select * from parent;
  1525. b
  1526. ----
  1527. 2
  1528. 12
  1529. (2 rows)
  1530. select * from child;
  1531. b | d
  1532. ----+---------
  1533. 12 | testing
  1534. (1 row)
  1535. drop table child;
  1536. drop table parent;
  1537. -- check error cases for inheritance column merging
  1538. create table parent (a float8, b numeric(10,4), c text collate "C");
  1539. create table child (a float4) inherits (parent); -- fail
  1540. NOTICE: merging column "a" with inherited definition
  1541. ERROR: column "a" has a type conflict
  1542. DETAIL: double precision versus real
  1543. create table child (b decimal(10,7)) inherits (parent); -- fail
  1544. NOTICE: moving and merging column "b" with inherited definition
  1545. DETAIL: User-specified column moved to the position of the inherited column.
  1546. ERROR: column "b" has a type conflict
  1547. DETAIL: numeric(10,4) versus numeric(10,7)
  1548. create table child (c text collate "POSIX") inherits (parent); -- fail
  1549. NOTICE: moving and merging column "c" with inherited definition
  1550. DETAIL: User-specified column moved to the position of the inherited column.
  1551. ERROR: column "c" has a collation conflict
  1552. DETAIL: "C" versus "POSIX"
  1553. create table child (a double precision, b decimal(10,4)) inherits (parent);
  1554. NOTICE: merging column "a" with inherited definition
  1555. NOTICE: merging column "b" with inherited definition
  1556. drop table child;
  1557. drop table parent;
  1558. -- test copy in/out
  1559. create table attest (a int4, b int4, c int4);
  1560. insert into attest values (1,2,3);
  1561. alter table attest drop a;
  1562. copy attest to stdout;
  1563. 2 3
  1564. copy attest(a) to stdout;
  1565. ERROR: column "a" of relation "attest" does not exist
  1566. copy attest("........pg.dropped.1........") to stdout;
  1567. ERROR: column "........pg.dropped.1........" of relation "attest" does not exist
  1568. copy attest from stdin;
  1569. ERROR: extra data after last expected column
  1570. CONTEXT: COPY attest, line 1: "10 11 12"
  1571. select * from attest;
  1572. b | c
  1573. ---+---
  1574. 2 | 3
  1575. (1 row)
  1576. copy attest from stdin;
  1577. select * from attest;
  1578. b | c
  1579. ----+----
  1580. 2 | 3
  1581. 21 | 22
  1582. (2 rows)
  1583. copy attest(a) from stdin;
  1584. ERROR: column "a" of relation "attest" does not exist
  1585. copy attest("........pg.dropped.1........") from stdin;
  1586. ERROR: column "........pg.dropped.1........" of relation "attest" does not exist
  1587. copy attest(b,c) from stdin;
  1588. select * from attest;
  1589. b | c
  1590. ----+----
  1591. 2 | 3
  1592. 21 | 22
  1593. 31 | 32
  1594. (3 rows)
  1595. drop table attest;
  1596. -- test inheritance
  1597. create table dropColumn (a int, b int, e int);
  1598. create table dropColumnChild (c int) inherits (dropColumn);
  1599. create table dropColumnAnother (d int) inherits (dropColumnChild);
  1600. -- these two should fail
  1601. alter table dropColumnchild drop column a;
  1602. ERROR: cannot drop inherited column "a"
  1603. alter table only dropColumnChild drop column b;
  1604. ERROR: cannot drop inherited column "b"
  1605. -- these three should work
  1606. alter table only dropColumn drop column e;
  1607. alter table dropColumnChild drop column c;
  1608. alter table dropColumn drop column a;
  1609. create table renameColumn (a int);
  1610. create table renameColumnChild (b int) inherits (renameColumn);
  1611. create table renameColumnAnother (c int) inherits (renameColumnChild);
  1612. -- these three should fail
  1613. alter table renameColumnChild rename column a to d;
  1614. ERROR: cannot rename inherited column "a"
  1615. alter table only renameColumnChild rename column a to d;
  1616. ERROR: inherited column "a" must be renamed in child tables too
  1617. alter table only renameColumn rename column a to d;
  1618. ERROR: inherited column "a" must be renamed in child tables too
  1619. -- these should work
  1620. alter table renameColumn rename column a to d;
  1621. alter table renameColumnChild rename column b to a;
  1622. -- these should work
  1623. alter table if exists doesnt_exist_tab rename column a to d;
  1624. NOTICE: relation "doesnt_exist_tab" does not exist, skipping
  1625. alter table if exists doesnt_exist_tab rename column b to a;
  1626. NOTICE: relation "doesnt_exist_tab" does not exist, skipping
  1627. -- this should work
  1628. alter table renameColumn add column w int;
  1629. -- this should fail
  1630. alter table only renameColumn add column x int;
  1631. ERROR: column must be added to child tables too
  1632. -- Test corner cases in dropping of inherited columns
  1633. create table p1 (f1 int, f2 int);
  1634. create table c1 (f1 int not null) inherits(p1);
  1635. NOTICE: merging column "f1" with inherited definition
  1636. -- should be rejected since c1.f1 is inherited
  1637. alter table c1 drop column f1;
  1638. ERROR: cannot drop inherited column "f1"
  1639. -- should work
  1640. alter table p1 drop column f1;
  1641. -- c1.f1 is still there, but no longer inherited
  1642. select f1 from c1;
  1643. f1
  1644. ----
  1645. (0 rows)
  1646. alter table c1 drop column f1;
  1647. select f1 from c1;
  1648. ERROR: column "f1" does not exist
  1649. LINE 1: select f1 from c1;
  1650. ^
  1651. HINT: Perhaps you meant to reference the column "c1.f2".
  1652. drop table p1 cascade;
  1653. NOTICE: drop cascades to table c1
  1654. create table p1 (f1 int, f2 int);
  1655. create table c1 () inherits(p1);
  1656. -- should be rejected since c1.f1 is inherited
  1657. alter table c1 drop column f1;
  1658. ERROR: cannot drop inherited column "f1"
  1659. alter table p1 drop column f1;
  1660. -- c1.f1 is dropped now, since there is no local definition for it
  1661. select f1 from c1;
  1662. ERROR: column "f1" does not exist
  1663. LINE 1: select f1 from c1;
  1664. ^
  1665. HINT: Perhaps you meant to reference the column "c1.f2".
  1666. drop table p1 cascade;
  1667. NOTICE: drop cascades to table c1
  1668. create table p1 (f1 int, f2 int);
  1669. create table c1 () inherits(p1);
  1670. -- should be rejected since c1.f1 is inherited
  1671. alter table c1 drop column f1;
  1672. ERROR: cannot drop inherited column "f1"
  1673. alter table only p1 drop column f1;
  1674. -- c1.f1 is NOT dropped, but must now be considered non-inherited
  1675. alter table c1 drop column f1;
  1676. drop table p1 cascade;
  1677. NOTICE: drop cascades to table c1
  1678. create table p1 (f1 int, f2 int);
  1679. create table c1 (f1 int not null) inherits(p1);
  1680. NOTICE: merging column "f1" with inherited definition
  1681. -- should be rejected since c1.f1 is inherited
  1682. alter table c1 drop column f1;
  1683. ERROR: cannot drop inherited column "f1"
  1684. alter table only p1 drop column f1;
  1685. -- c1.f1 is still there, but no longer inherited
  1686. alter table c1 drop column f1;
  1687. drop table p1 cascade;
  1688. NOTICE: drop cascades to table c1
  1689. create table p1(id int, name text);
  1690. create table p2(id2 int, name text, height int);
  1691. create table c1(age int) inherits(p1,p2);
  1692. NOTICE: merging multiple inherited definitions of column "name"
  1693. create table gc1() inherits (c1);
  1694. select relname, attname, attinhcount, attislocal
  1695. from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
  1696. where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
  1697. order by relname, attnum;
  1698. relname | attname | attinhcount | attislocal
  1699. ---------+---------+-------------+------------
  1700. c1 | id | 1 | f
  1701. c1 | name | 2 | f
  1702. c1 | id2 | 1 | f
  1703. c1 | height | 1 | f
  1704. c1 | age | 0 | t
  1705. gc1 | id | 1 | f
  1706. gc1 | name | 1 | f
  1707. gc1 | id2 | 1 | f
  1708. gc1 | height | 1 | f
  1709. gc1 | age | 1 | f
  1710. p1 | id | 0 | t
  1711. p1 | name | 0 | t
  1712. p2 | id2 | 0 | t
  1713. p2 | name | 0 | t
  1714. p2 | height | 0 | t
  1715. (15 rows)
  1716. -- should work
  1717. alter table only p1 drop column name;
  1718. -- should work. Now c1.name is local and inhcount is 0.
  1719. alter table p2 drop column name;
  1720. -- should be rejected since its inherited
  1721. alter table gc1 drop column name;
  1722. ERROR: cannot drop inherited column "name"
  1723. -- should work, and drop gc1.name along
  1724. alter table c1 drop column name;
  1725. -- should fail: column does not exist
  1726. alter table gc1 drop column name;
  1727. ERROR: column "name" of relation "gc1" does not exist
  1728. -- should work and drop the attribute in all tables
  1729. alter table p2 drop column height;
  1730. -- IF EXISTS test
  1731. create table dropColumnExists ();
  1732. alter table dropColumnExists drop column non_existing; --fail
  1733. ERROR: column "non_existing" of relation "dropcolumnexists" does not exist
  1734. alter table dropColumnExists drop column if exists non_existing; --succeed
  1735. NOTICE: column "non_existing" of relation "dropcolumnexists" does not exist, skipping
  1736. select relname, attname, attinhcount, attislocal
  1737. from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
  1738. where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
  1739. order by relname, attnum;
  1740. relname | attname | attinhcount | attislocal
  1741. ---------+---------+-------------+------------
  1742. c1 | id | 1 | f
  1743. c1 | id2 | 1 | f
  1744. c1 | age | 0 | t
  1745. gc1 | id | 1 | f
  1746. gc1 | id2 | 1 | f
  1747. gc1 | age | 1 | f
  1748. p1 | id | 0 | t
  1749. p2 | id2 | 0 | t
  1750. (8 rows)
  1751. drop table p1, p2 cascade;
  1752. NOTICE: drop cascades to 2 other objects
  1753. DETAIL: drop cascades to table c1
  1754. drop cascades to table gc1
  1755. -- test attinhcount tracking with merged columns
  1756. create table depth0();
  1757. create table depth1(c text) inherits (depth0);
  1758. create table depth2() inherits (depth1);
  1759. alter table depth0 add c text;
  1760. NOTICE: merging definition of column "c" for child "depth1"
  1761. select attrelid::regclass, attname, attinhcount, attislocal
  1762. from pg_attribute
  1763. where attnum > 0 and attrelid::regclass in ('depth0', 'depth1', 'depth2')
  1764. order by attrelid::regclass::text, attnum;
  1765. attrelid | attname | attinhcount | attislocal
  1766. ----------+---------+-------------+------------
  1767. depth0 | c | 0 | t
  1768. depth1 | c | 1 | t
  1769. depth2 | c | 1 | f
  1770. (3 rows)
  1771. -- test renumbering of child-table columns in inherited operations
  1772. create table p1 (f1 int);
  1773. create table c1 (f2 text, f3 int) inherits (p1);
  1774. alter table p1 add column a1 int check (a1 > 0);
  1775. alter table p1 add column f2 text;
  1776. NOTICE: merging definition of column "f2" for child "c1"
  1777. insert into p1 values (1,2,'abc');
  1778. insert into c1 values(11,'xyz',33,0); -- should fail
  1779. ERROR: new row for relation "c1" violates check constraint "p1_a1_check"
  1780. DETAIL: Failing row contains (11, xyz, 33, 0).
  1781. insert into c1 values(11,'xyz',33,22);
  1782. select * from p1;
  1783. f1 | a1 | f2
  1784. ----+----+-----
  1785. 1 | 2 | abc
  1786. 11 | 22 | xyz
  1787. (2 rows)
  1788. update p1 set a1 = a1 + 1, f2 = upper(f2);
  1789. select * from p1;
  1790. f1 | a1 | f2
  1791. ----+----+-----
  1792. 1 | 3 | ABC
  1793. 11 | 23 | XYZ
  1794. (2 rows)
  1795. drop table p1 cascade;
  1796. NOTICE: drop cascades to table c1
  1797. -- test that operations with a dropped column do not try to reference
  1798. -- its datatype
  1799. create domain mytype as text;
  1800. create temp table foo (f1 text, f2 mytype, f3 text);
  1801. insert into foo values('bb','cc','dd');
  1802. select * from foo;
  1803. f1 | f2 | f3
  1804. ----+----+----
  1805. bb | cc | dd
  1806. (1 row)
  1807. drop domain mytype cascade;
  1808. NOTICE: drop cascades to column f2 of table foo
  1809. select * from foo;
  1810. f1 | f3
  1811. ----+----
  1812. bb | dd
  1813. (1 row)
  1814. insert into foo values('qq','rr');
  1815. select * from foo;
  1816. f1 | f3
  1817. ----+----
  1818. bb | dd
  1819. qq | rr
  1820. (2 rows)
  1821. update foo set f3 = 'zz';
  1822. select * from foo;
  1823. f1 | f3
  1824. ----+----
  1825. bb | zz
  1826. qq | zz
  1827. (2 rows)
  1828. select f3,max(f1) from foo group by f3;
  1829. f3 | max
  1830. ----+-----
  1831. zz | qq
  1832. (1 row)
  1833. -- Simple tests for alter table column type
  1834. alter table foo alter f1 TYPE integer; -- fails
  1835. ERROR: column "f1" cannot be cast automatically to type integer
  1836. HINT: You might need to specify "USING f1::integer".
  1837. alter table foo alter f1 TYPE varchar(10);
  1838. create table anothertab (atcol1 serial8, atcol2 boolean,
  1839. constraint anothertab_chk check (atcol1 <= 3));
  1840. insert into anothertab (atcol1, atcol2) values (default, true);
  1841. insert into anothertab (atcol1, atcol2) values (default, false);
  1842. select * from anothertab;
  1843. atcol1 | atcol2
  1844. --------+--------
  1845. 1 | t
  1846. 2 | f
  1847. (2 rows)
  1848. alter table anothertab alter column atcol1 type boolean; -- fails
  1849. ERROR: column "atcol1" cannot be cast automatically to type boolean
  1850. HINT: You might need to specify "USING atcol1::boolean".
  1851. alter table anothertab alter column atcol1 type boolean using atcol1::int; -- fails
  1852. ERROR: result of USING clause for column "atcol1" cannot be cast automatically to type boolean
  1853. HINT: You might need to add an explicit cast.
  1854. alter table anothertab alter column atcol1 type integer;
  1855. select * from anothertab;
  1856. atcol1 | atcol2
  1857. --------+--------
  1858. 1 | t
  1859. 2 | f
  1860. (2 rows)
  1861. insert into anothertab (atcol1, atcol2) values (45, null); -- fails
  1862. ERROR: new row for relation "anothertab" violates check constraint "anothertab_chk"
  1863. DETAIL: Failing row contains (45, null).
  1864. insert into anothertab (atcol1, atcol2) values (default, null);
  1865. select * from anothertab;
  1866. atcol1 | atcol2
  1867. --------+--------
  1868. 1 | t
  1869. 2 | f
  1870. 3 |
  1871. (3 rows)
  1872. alter table anothertab alter column atcol2 type text
  1873. using case when atcol2 is true then 'IT WAS TRUE'
  1874. when atcol2 is false then 'IT WAS FALSE'
  1875. else 'IT WAS NULL!' end;
  1876. select * from anothertab;
  1877. atcol1 | atcol2
  1878. --------+--------------
  1879. 1 | IT WAS TRUE
  1880. 2 | IT WAS FALSE
  1881. 3 | IT WAS NULL!
  1882. (3 rows)
  1883. alter table anothertab alter column atcol1 type boolean
  1884. using case when atcol1 % 2 = 0 then true else false end; -- fails
  1885. ERROR: default for column "atcol1" cannot be cast automatically to type boolean
  1886. alter table anothertab alter column atcol1 drop default;
  1887. alter table anothertab alter column atcol1 type boolean
  1888. using case when atcol1 % 2 = 0 then true else false end; -- fails
  1889. ERROR: operator does not exist: boolean <= integer
  1890. HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
  1891. alter table anothertab drop constraint anothertab_chk;
  1892. alter table anothertab drop constraint anothertab_chk; -- fails
  1893. ERROR: constraint "anothertab_chk" of relation "anothertab" does not exist
  1894. alter table anothertab drop constraint IF EXISTS anothertab_chk; -- succeeds
  1895. NOTICE: constraint "anothertab_chk" of relation "anothertab" does not exist, skipping
  1896. alter table anothertab alter column atcol1 type boolean
  1897. using case when atcol1 % 2 = 0 then true else false end;
  1898. select * from anothertab;
  1899. atcol1 | atcol2
  1900. --------+--------------
  1901. f | IT WAS TRUE
  1902. t | IT WAS FALSE
  1903. f | IT WAS NULL!
  1904. (3 rows)
  1905. drop table anothertab;
  1906. -- Test index handling in alter table column type (cf. bugs #15835, #15865)
  1907. create table anothertab(f1 int primary key, f2 int unique,
  1908. f3 int, f4 int, f5 int);
  1909. alter table anothertab
  1910. add exclude using btree (f3 with =);
  1911. alter table anothertab
  1912. add exclude using btree (f4 with =) where (f4 is not null);
  1913. alter table anothertab
  1914. add exclude using btree (f4 with =) where (f5 > 0);
  1915. alter table anothertab
  1916. add unique(f1,f4);
  1917. create index on anothertab(f2,f3);
  1918. create unique index on anothertab(f4);
  1919. \d anothertab
  1920. Table "public.anothertab"
  1921. Column | Type | Collation | Nullable | Default
  1922. --------+---------+-----------+----------+---------
  1923. f1 | integer | | not null |
  1924. f2 | integer | | |
  1925. f3 | integer | | |
  1926. f4 | integer | | |
  1927. f5 | integer | | |
  1928. Indexes:
  1929. "anothertab_pkey" PRIMARY KEY, btree (f1)
  1930. "anothertab_f1_f4_key" UNIQUE CONSTRAINT, btree (f1, f4)
  1931. "anothertab_f2_f3_idx" btree (f2, f3)
  1932. "anothertab_f2_key" UNIQUE CONSTRAINT, btree (f2)
  1933. "anothertab_f3_excl" EXCLUDE USING btree (f3 WITH =)
  1934. "anothertab_f4_excl" EXCLUDE USING btree (f4 WITH =) WHERE (f4 IS NOT NULL)
  1935. "anothertab_f4_excl1" EXCLUDE USING btree (f4 WITH =) WHERE (f5 > 0)
  1936. "anothertab_f4_idx" UNIQUE, btree (f4)
  1937. alter table anothertab alter column f1 type bigint;
  1938. alter table anothertab
  1939. alter column f2 type bigint,
  1940. alter column f3 type bigint,
  1941. alter column f4 type bigint;
  1942. alter table anothertab alter column f5 type bigint;
  1943. \d anothertab
  1944. Table "public.anothertab"
  1945. Column | Type | Collation | Nullable | Default
  1946. --------+--------+-----------+----------+---------
  1947. f1 | bigint | | not null |
  1948. f2 | bigint | | |
  1949. f3 | bigint | | |
  1950. f4 | bigint | | |
  1951. f5 | bigint | | |
  1952. Indexes:
  1953. "anothertab_pkey" PRIMARY KEY, btree (f1)
  1954. "anothertab_f1_f4_key" UNIQUE CONSTRAINT, btree (f1, f4)
  1955. "anothertab_f2_f3_idx" btree (f2, f3)
  1956. "anothertab_f2_key" UNIQUE CONSTRAINT, btree (f2)
  1957. "anothertab_f3_excl" EXCLUDE USING btree (f3 WITH =)
  1958. "anothertab_f4_excl" EXCLUDE USING btree (f4 WITH =) WHERE (f4 IS NOT NULL)
  1959. "anothertab_f4_excl1" EXCLUDE USING btree (f4 WITH =) WHERE (f5 > 0)
  1960. "anothertab_f4_idx" UNIQUE, btree (f4)
  1961. drop table anothertab;
  1962. -- test that USING expressions are parsed before column alter type / drop steps
  1963. create table another (f1 int, f2 text, f3 text);
  1964. insert into another values(1, 'one', 'uno');
  1965. insert into another values(2, 'two', 'due');
  1966. insert into another values(3, 'three', 'tre');
  1967. select * from another;
  1968. f1 | f2 | f3
  1969. ----+-------+-----
  1970. 1 | one | uno
  1971. 2 | two | due
  1972. 3 | three | tre
  1973. (3 rows)
  1974. alter table another
  1975. alter f1 type text using f2 || ' and ' || f3 || ' more',
  1976. alter f2 type bigint using f1 * 10,
  1977. drop column f3;
  1978. select * from another;
  1979. f1 | f2
  1980. --------------------+----
  1981. one and uno more | 10
  1982. two and due more | 20
  1983. three and tre more | 30
  1984. (3 rows)
  1985. drop table another;
  1986. -- Create an index that skips WAL, then perform a SET DATA TYPE that skips
  1987. -- rewriting the index.
  1988. begin;
  1989. create table skip_wal_skip_rewrite_index (c varchar(10) primary key);
  1990. alter table skip_wal_skip_rewrite_index alter c type varchar(20);
  1991. commit;
  1992. -- We disallow changing table's row type if it's used for storage
  1993. create table at_tab1 (a int, b text);
  1994. create table at_tab2 (x int, y at_tab1);
  1995. alter table at_tab1 alter column b type varchar; -- fails
  1996. ERROR: cannot alter table "at_tab1" because column "at_tab2.y" uses its row type
  1997. drop table at_tab2;
  1998. -- Use of row type in an expression is defended differently
  1999. create table at_tab2 (x int, y text, check((x,y)::at_tab1 = (1,'42')::at_tab1));
  2000. alter table at_tab1 alter column b type varchar; -- allowed, but ...
  2001. insert into at_tab2 values(1,'42'); -- ... this will fail
  2002. ERROR: ROW() column has type text instead of type character varying
  2003. drop table at_tab1, at_tab2;
  2004. -- Check it for a partitioned table, too
  2005. create table at_tab1 (a int, b text) partition by list(a);
  2006. create table at_tab2 (x int, y at_tab1);
  2007. alter table at_tab1 alter column b type varchar; -- fails
  2008. ERROR: cannot alter table "at_tab1" because column "at_tab2.y" uses its row type
  2009. drop table at_tab1, at_tab2;
  2010. -- Alter column type that's part of a partitioned index
  2011. create table at_partitioned (a int, b text) partition by range (a);
  2012. create table at_part_1 partition of at_partitioned for values from (0) to (1000);
  2013. insert into at_partitioned values (512, '0.123');
  2014. create table at_part_2 (b text, a int);
  2015. insert into at_part_2 values ('1.234', 1024);
  2016. create index on at_partitioned (b);
  2017. create index on at_partitioned (a);
  2018. \d at_part_1
  2019. Table "public.at_part_1"
  2020. Column | Type | Collation | Nullable | Default
  2021. --------+---------+-----------+----------+---------
  2022. a | integer | | |
  2023. b | text | | |
  2024. Partition of: at_partitioned FOR VALUES FROM (0) TO (1000)
  2025. Indexes:
  2026. "at_part_1_a_idx" btree (a)
  2027. "at_part_1_b_idx" btree (b)
  2028. \d at_part_2
  2029. Table "public.at_part_2"
  2030. Column | Type | Collation | Nullable | Default
  2031. --------+---------+-----------+----------+---------
  2032. b | text | | |
  2033. a | integer | | |
  2034. alter table at_partitioned attach partition at_part_2 for values from (1000) to (2000);
  2035. \d at_part_2
  2036. Table "public.at_part_2"
  2037. Column | Type | Collation | Nullable | Default
  2038. --------+---------+-----------+----------+---------
  2039. b | text | | |
  2040. a | integer | | |
  2041. Partition of: at_partitioned FOR VALUES FROM (1000) TO (2000)
  2042. Indexes:
  2043. "at_part_2_a_idx" btree (a)
  2044. "at_part_2_b_idx" btree (b)
  2045. alter table at_partitioned alter column b type numeric using b::numeric;
  2046. \d at_part_1
  2047. Table "public.at_part_1"
  2048. Column | Type | Collation | Nullable | Default
  2049. --------+---------+-----------+----------+---------
  2050. a | integer | | |
  2051. b | numeric | | |
  2052. Partition of: at_partitioned FOR VALUES FROM (0) TO (1000)
  2053. Indexes:
  2054. "at_part_1_a_idx" btree (a)
  2055. "at_part_1_b_idx" btree (b)
  2056. \d at_part_2
  2057. Table "public.at_part_2"
  2058. Column | Type | Collation | Nullable | Default
  2059. --------+---------+-----------+----------+---------
  2060. b | numeric | | |
  2061. a | integer | | |
  2062. Partition of: at_partitioned FOR VALUES FROM (1000) TO (2000)
  2063. Indexes:
  2064. "at_part_2_a_idx" btree (a)
  2065. "at_part_2_b_idx" btree (b)
  2066. drop table at_partitioned;
  2067. -- Alter column type when no table rewrite is required
  2068. -- Also check that comments are preserved
  2069. create table at_partitioned(id int, name varchar(64), unique (id, name))
  2070. partition by hash(id);
  2071. comment on constraint at_partitioned_id_name_key on at_partitioned is 'parent constraint';
  2072. comment on index at_partitioned_id_name_key is 'parent index';
  2073. create table at_partitioned_0 partition of at_partitioned
  2074. for values with (modulus 2, remainder 0);
  2075. comment on constraint at_partitioned_0_id_name_key on at_partitioned_0 is 'child 0 constraint';
  2076. comment on index at_partitioned_0_id_name_key is 'child 0 index';
  2077. create table at_partitioned_1 partition of at_partitioned
  2078. for values with (modulus 2, remainder 1);
  2079. comment on constraint at_partitioned_1_id_name_key on at_partitioned_1 is 'child 1 constraint';
  2080. comment on index at_partitioned_1_id_name_key is 'child 1 index';
  2081. insert into at_partitioned values(1, 'foo');
  2082. insert into at_partitioned values(3, 'bar');
  2083. create temp table old_oids as
  2084. select relname, oid as oldoid, relfilenode as oldfilenode
  2085. from pg_class where relname like 'at_partitioned%';
  2086. select relname,
  2087. c.oid = oldoid as orig_oid,
  2088. case relfilenode
  2089. when 0 then 'none'
  2090. when c.oid then 'own'
  2091. when oldfilenode then 'orig'
  2092. else 'OTHER'
  2093. end as storage,
  2094. obj_description(c.oid, 'pg_class') as desc
  2095. from pg_class c left join old_oids using (relname)
  2096. where relname like 'at_partitioned%'
  2097. order by relname;
  2098. relname | orig_oid | storage | desc
  2099. ------------------------------+----------+---------+---------------
  2100. at_partitioned | t | none |
  2101. at_partitioned_0 | t | own |
  2102. at_partitioned_0_id_name_key | t | own | child 0 index
  2103. at_partitioned_1 | t | own |
  2104. at_partitioned_1_id_name_key | t | own | child 1 index
  2105. at_partitioned_id_name_key | t | none | parent index
  2106. (6 rows)
  2107. select conname, obj_description(oid, 'pg_constraint') as desc
  2108. from pg_constraint where conname like 'at_partitioned%'
  2109. order by conname;
  2110. conname | desc
  2111. ------------------------------+--------------------
  2112. at_partitioned_0_id_name_key | child 0 constraint
  2113. at_partitioned_1_id_name_key | child 1 constraint
  2114. at_partitioned_id_name_key | parent constraint
  2115. (3 rows)
  2116. alter table at_partitioned alter column name type varchar(127);
  2117. -- Note: these tests currently show the wrong behavior for comments :-(
  2118. select relname,
  2119. c.oid = oldoid as orig_oid,
  2120. case relfilenode
  2121. when 0 then 'none'
  2122. when c.oid then 'own'
  2123. when oldfilenode then 'orig'
  2124. else 'OTHER'
  2125. end as storage,
  2126. obj_description(c.oid, 'pg_class') as desc
  2127. from pg_class c left join old_oids using (relname)
  2128. where relname like 'at_partitioned%'
  2129. order by relname;
  2130. relname | orig_oid | storage | desc
  2131. ------------------------------+----------+---------+--------------
  2132. at_partitioned | t | none |
  2133. at_partitioned_0 | t | own |
  2134. at_partitioned_0_id_name_key | f | own | parent index
  2135. at_partitioned_1 | t | own |
  2136. at_partitioned_1_id_name_key | f | own | parent index
  2137. at_partitioned_id_name_key | f | none | parent index
  2138. (6 rows)
  2139. select conname, obj_description(oid, 'pg_constraint') as desc
  2140. from pg_constraint where conname like 'at_partitioned%'
  2141. order by conname;
  2142. conname | desc
  2143. ------------------------------+-------------------
  2144. at_partitioned_0_id_name_key |
  2145. at_partitioned_1_id_name_key |
  2146. at_partitioned_id_name_key | parent constraint
  2147. (3 rows)
  2148. -- Don't remove this DROP, it exposes bug #15672
  2149. drop table at_partitioned;
  2150. -- disallow recursive containment of row types
  2151. create temp table recur1 (f1 int);
  2152. alter table recur1 add column f2 recur1; -- fails
  2153. ERROR: composite type recur1 cannot be made a member of itself
  2154. alter table recur1 add column f2 recur1[]; -- fails
  2155. ERROR: composite type recur1 cannot be made a member of itself
  2156. create domain array_of_recur1 as recur1[];
  2157. alter table recur1 add column f2 array_of_recur1; -- fails
  2158. ERROR: composite type recur1 cannot be made a member of itself
  2159. create temp table recur2 (f1 int, f2 recur1);
  2160. alter table recur1 add column f2 recur2; -- fails
  2161. ERROR: composite type recur1 cannot be made a member of itself
  2162. alter table recur1 add column f2 int;
  2163. alter table recur1 alter column f2 type recur2; -- fails
  2164. ERROR: composite type recur1 cannot be made a member of itself
  2165. -- SET STORAGE may need to add a TOAST table
  2166. create table test_storage (a text);
  2167. alter table test_storage alter a set storage plain;
  2168. alter table test_storage add b int default 0; -- rewrite table to remove its TOAST table
  2169. alter table test_storage alter a set storage extended; -- re-add TOAST table
  2170. select reltoastrelid <> 0 as has_toast_table
  2171. from pg_class
  2172. where oid = 'test_storage'::regclass;
  2173. has_toast_table
  2174. -----------------
  2175. t
  2176. (1 row)
  2177. -- test that SET STORAGE propagates to index correctly
  2178. create index test_storage_idx on test_storage (b, a);
  2179. alter table test_storage alter column a set storage external;
  2180. \d+ test_storage
  2181. Table "public.test_storage"
  2182. Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
  2183. --------+---------+-----------+----------+---------+----------+--------------+-------------
  2184. a | text | | | | external | |
  2185. b | integer | | | 0 | plain | |
  2186. Indexes:
  2187. "test_storage_idx" btree (b, a)
  2188. \d+ test_storage_idx
  2189. Index "public.test_storage_idx"
  2190. Column | Type | Key? | Definition | Storage | Stats target
  2191. --------+---------+------+------------+----------+--------------
  2192. b | integer | yes | b | plain |
  2193. a | text | yes | a | external |
  2194. btree, for table "public.test_storage"
  2195. -- ALTER COLUMN TYPE with a check constraint and a child table (bug #13779)
  2196. CREATE TABLE test_inh_check (a float check (a > 10.2), b float);
  2197. CREATE TABLE test_inh_check_child() INHERITS(test_inh_check);
  2198. \d test_inh_check
  2199. Table "public.test_inh_check"
  2200. Column | Type | Collation | Nullable | Default
  2201. --------+------------------+-----------+----------+---------
  2202. a | double precision | | |
  2203. b | double precision | | |
  2204. Check constraints:
  2205. "test_inh_check_a_check" CHECK (a > 10.2::double precision)
  2206. Number of child tables: 1 (Use \d+ to list them.)
  2207. \d test_inh_check_child
  2208. Table "public.test_inh_check_child"
  2209. Column | Type | Collation | Nullable | Default
  2210. --------+------------------+-----------+----------+---------
  2211. a | double precision | | |
  2212. b | double precision | | |
  2213. Check constraints:
  2214. "test_inh_check_a_check" CHECK (a > 10.2::double precision)
  2215. Inherits: test_inh_check
  2216. select relname, conname, coninhcount, conislocal, connoinherit
  2217. from pg_constraint c, pg_class r
  2218. where relname like 'test_inh_check%' and c.conrelid = r.oid
  2219. order by 1, 2;
  2220. relname | conname | coninhcount | conislocal | connoinherit
  2221. ----------------------+------------------------+-------------+------------+--------------
  2222. test_inh_check | test_inh_check_a_check | 0 | t | f
  2223. test_inh_check_child | test_inh_check_a_check | 1 | f | f
  2224. (2 rows)
  2225. ALTER TABLE test_inh_check ALTER COLUMN a TYPE numeric;
  2226. \d test_inh_check
  2227. Table "public.test_inh_check"
  2228. Column | Type | Collation | Nullable | Default
  2229. --------+------------------+-----------+----------+---------
  2230. a | numeric | | |
  2231. b | double precision | | |
  2232. Check constraints:
  2233. "test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
  2234. Number of child tables: 1 (Use \d+ to list them.)
  2235. \d test_inh_check_child
  2236. Table "public.test_inh_check_child"
  2237. Column | Type | Collation | Nullable | Default
  2238. --------+------------------+-----------+----------+---------
  2239. a | numeric | | |
  2240. b | double precision | | |
  2241. Check constraints:
  2242. "test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
  2243. Inherits: test_inh_check
  2244. select relname, conname, coninhcount, conislocal, connoinherit
  2245. from pg_constraint c, pg_class r
  2246. where relname like 'test_inh_check%' and c.conrelid = r.oid
  2247. order by 1, 2;
  2248. relname | conname | coninhcount | conislocal | connoinherit
  2249. ----------------------+------------------------+-------------+------------+--------------
  2250. test_inh_check | test_inh_check_a_check | 0 | t | f
  2251. test_inh_check_child | test_inh_check_a_check | 1 | f | f
  2252. (2 rows)
  2253. -- also try noinherit, local, and local+inherited cases
  2254. ALTER TABLE test_inh_check ADD CONSTRAINT bnoinherit CHECK (b > 100) NO INHERIT;
  2255. ALTER TABLE test_inh_check_child ADD CONSTRAINT blocal CHECK (b < 1000);
  2256. ALTER TABLE test_inh_check_child ADD CONSTRAINT bmerged CHECK (b > 1);
  2257. ALTER TABLE test_inh_check ADD CONSTRAINT bmerged CHECK (b > 1);
  2258. NOTICE: merging constraint "bmerged" with inherited definition
  2259. \d test_inh_check
  2260. Table "public.test_inh_check"
  2261. Column | Type | Collation | Nullable | Default
  2262. --------+------------------+-----------+----------+---------
  2263. a | numeric | | |
  2264. b | double precision | | |
  2265. Check constraints:
  2266. "bmerged" CHECK (b > 1::double precision)
  2267. "bnoinherit" CHECK (b > 100::double precision) NO INHERIT
  2268. "test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
  2269. Number of child tables: 1 (Use \d+ to list them.)
  2270. \d test_inh_check_child
  2271. Table "public.test_inh_check_child"
  2272. Column | Type | Collation | Nullable | Default
  2273. --------+------------------+-----------+----------+---------
  2274. a | numeric | | |
  2275. b | double precision | | |
  2276. Check constraints:
  2277. "blocal" CHECK (b < 1000::double precision)
  2278. "bmerged" CHECK (b > 1::double precision)
  2279. "test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
  2280. Inherits: test_inh_check
  2281. select relname, conname, coninhcount, conislocal, connoinherit
  2282. from pg_constraint c, pg_class r
  2283. where relname like 'test_inh_check%' and c.conrelid = r.oid
  2284. order by 1, 2;
  2285. relname | conname | coninhcount | conislocal | connoinherit
  2286. ----------------------+------------------------+-------------+------------+--------------
  2287. test_inh_check | bmerged | 0 | t | f
  2288. test_inh_check | bnoinherit | 0 | t | t
  2289. test_inh_check | test_inh_check_a_check | 0 | t | f
  2290. test_inh_check_child | blocal | 0 | t | f
  2291. test_inh_check_child | bmerged | 1 | t | f
  2292. test_inh_check_child | test_inh_check_a_check | 1 | f | f
  2293. (6 rows)
  2294. ALTER TABLE test_inh_check ALTER COLUMN b TYPE numeric;
  2295. NOTICE: merging constraint "bmerged" with inherited definition
  2296. \d test_inh_check
  2297. Table "public.test_inh_check"
  2298. Column | Type | Collation | Nullable | Default
  2299. --------+---------+-----------+----------+---------
  2300. a | numeric | | |
  2301. b | numeric | | |
  2302. Check constraints:
  2303. "bmerged" CHECK (b::double precision > 1::double precision)
  2304. "bnoinherit" CHECK (b::double precision > 100::double precision) NO INHERIT
  2305. "test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
  2306. Number of child tables: 1 (Use \d+ to list them.)
  2307. \d test_inh_check_child
  2308. Table "public.test_inh_check_child"
  2309. Column | Type | Collation | Nullable | Default
  2310. --------+---------+-----------+----------+---------
  2311. a | numeric | | |
  2312. b | numeric | | |
  2313. Check constraints:
  2314. "blocal" CHECK (b::double precision < 1000::double precision)
  2315. "bmerged" CHECK (b::double precision > 1::double precision)
  2316. "test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
  2317. Inherits: test_inh_check
  2318. select relname, conname, coninhcount, conislocal, connoinherit
  2319. from pg_constraint c, pg_class r
  2320. where relname like 'test_inh_check%' and c.conrelid = r.oid
  2321. order by 1, 2;
  2322. relname | conname | coninhcount | conislocal | connoinherit
  2323. ----------------------+------------------------+-------------+------------+--------------
  2324. test_inh_check | bmerged | 0 | t | f
  2325. test_inh_check | bnoinherit | 0 | t | t
  2326. test_inh_check | test_inh_check_a_check | 0 | t | f
  2327. test_inh_check_child | blocal | 0 | t | f
  2328. test_inh_check_child | bmerged | 1 | t | f
  2329. test_inh_check_child | test_inh_check_a_check | 1 | f | f
  2330. (6 rows)
  2331. -- ALTER COLUMN TYPE with different schema in children
  2332. -- Bug at https://postgr.es/m/20170102225618.GA10071@telsasoft.com
  2333. CREATE TABLE test_type_diff (f1 int);
  2334. CREATE TABLE test_type_diff_c (extra smallint) INHERITS (test_type_diff);
  2335. ALTER TABLE test_type_diff ADD COLUMN f2 int;
  2336. INSERT INTO test_type_diff_c VALUES (1, 2, 3);
  2337. ALTER TABLE test_type_diff ALTER COLUMN f2 TYPE bigint USING f2::bigint;
  2338. CREATE TABLE test_type_diff2 (int_two int2, int_four int4, int_eight int8);
  2339. CREATE TABLE test_type_diff2_c1 (int_four int4, int_eight int8, int_two int2);
  2340. CREATE TABLE test_type_diff2_c2 (int_eight int8, int_two int2, int_four int4);
  2341. CREATE TABLE test_type_diff2_c3 (int_two int2, int_four int4, int_eight int8);
  2342. ALTER TABLE test_type_diff2_c1 INHERIT test_type_diff2;
  2343. ALTER TABLE test_type_diff2_c2 INHERIT test_type_diff2;
  2344. ALTER TABLE test_type_diff2_c3 INHERIT test_type_diff2;
  2345. INSERT INTO test_type_diff2_c1 VALUES (1, 2, 3);
  2346. INSERT INTO test_type_diff2_c2 VALUES (4, 5, 6);
  2347. INSERT INTO test_type_diff2_c3 VALUES (7, 8, 9);
  2348. ALTER TABLE test_type_diff2 ALTER COLUMN int_four TYPE int8 USING int_four::int8;
  2349. -- whole-row references are disallowed
  2350. ALTER TABLE test_type_diff2 ALTER COLUMN int_four TYPE int4 USING (pg_column_size(test_type_diff2));
  2351. ERROR: cannot convert whole-row table reference
  2352. DETAIL: USING expression contains a whole-row table reference.
  2353. -- check for rollback of ANALYZE corrupting table property flags (bug #11638)
  2354. CREATE TABLE check_fk_presence_1 (id int PRIMARY KEY, t text);
  2355. CREATE TABLE check_fk_presence_2 (id int REFERENCES check_fk_presence_1, t text);
  2356. BEGIN;
  2357. ALTER TABLE check_fk_presence_2 DROP CONSTRAINT check_fk_presence_2_id_fkey;
  2358. ANALYZE check_fk_presence_2;
  2359. ROLLBACK;
  2360. \d check_fk_presence_2
  2361. Table "public.check_fk_presence_2"
  2362. Column | Type | Collation | Nullable | Default
  2363. --------+---------+-----------+----------+---------
  2364. id | integer | | |
  2365. t | text | | |
  2366. Foreign-key constraints:
  2367. "check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
  2368. DROP TABLE check_fk_presence_1, check_fk_presence_2;
  2369. -- check column addition within a view (bug #14876)
  2370. create table at_base_table(id int, stuff text);
  2371. insert into at_base_table values (23, 'skidoo');
  2372. create view at_view_1 as select * from at_base_table bt;
  2373. create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
  2374. \d+ at_view_1
  2375. View "public.at_view_1"
  2376. Column | Type | Collation | Nullable | Default | Storage | Description
  2377. --------+---------+-----------+----------+---------+----------+-------------
  2378. id | integer | | | | plain |
  2379. stuff | text | | | | extended |
  2380. View definition:
  2381. SELECT bt.id,
  2382. bt.stuff
  2383. FROM at_base_table bt;
  2384. \d+ at_view_2
  2385. View "public.at_view_2"
  2386. Column | Type | Collation | Nullable | Default | Storage | Description
  2387. --------+---------+-----------+----------+---------+----------+-------------
  2388. id | integer | | | | plain |
  2389. stuff | text | | | | extended |
  2390. j | json | | | | extended |
  2391. View definition:
  2392. SELECT v1.id,
  2393. v1.stuff,
  2394. to_json(v1.*) AS j
  2395. FROM at_view_1 v1;
  2396. explain (verbose, costs off) select * from at_view_2;
  2397. QUERY PLAN
  2398. ----------------------------------------------------------
  2399. Seq Scan on public.at_base_table bt
  2400. Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff))
  2401. (2 rows)
  2402. select * from at_view_2;
  2403. id | stuff | j
  2404. ----+--------+----------------------------
  2405. 23 | skidoo | {"id":23,"stuff":"skidoo"}
  2406. (1 row)
  2407. create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
  2408. \d+ at_view_1
  2409. View "public.at_view_1"
  2410. Column | Type | Collation | Nullable | Default | Storage | Description
  2411. --------+---------+-----------+----------+---------+----------+-------------
  2412. id | integer | | | | plain |
  2413. stuff | text | | | | extended |
  2414. more | integer | | | | plain |
  2415. View definition:
  2416. SELECT bt.id,
  2417. bt.stuff,
  2418. 2 + 2 AS more
  2419. FROM at_base_table bt;
  2420. \d+ at_view_2
  2421. View "public.at_view_2"
  2422. Column | Type | Collation | Nullable | Default | Storage | Description
  2423. --------+---------+-----------+----------+---------+----------+-------------
  2424. id | integer | | | | plain |
  2425. stuff | text | | | | extended |
  2426. j | json | | | | extended |
  2427. View definition:
  2428. SELECT v1.id,
  2429. v1.stuff,
  2430. to_json(v1.*) AS j
  2431. FROM at_view_1 v1;
  2432. explain (verbose, costs off) select * from at_view_2;
  2433. QUERY PLAN
  2434. ----------------------------------------------------------------
  2435. Seq Scan on public.at_base_table bt
  2436. Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
  2437. (2 rows)
  2438. select * from at_view_2;
  2439. id | stuff | j
  2440. ----+--------+----------------------------------------
  2441. 23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
  2442. (1 row)
  2443. drop view at_view_2;
  2444. drop view at_view_1;
  2445. drop table at_base_table;
  2446. -- check adding a column not iself requiring a rewrite, together with
  2447. -- a column requiring a default (bug #16038)
  2448. -- ensure that rewrites aren't silently optimized away, removing the
  2449. -- value of the test
  2450. CREATE FUNCTION check_ddl_rewrite(p_tablename regclass, p_ddl text)
  2451. RETURNS boolean
  2452. LANGUAGE plpgsql AS $$
  2453. DECLARE
  2454. v_relfilenode oid;
  2455. BEGIN
  2456. v_relfilenode := relfilenode FROM pg_class WHERE oid = p_tablename;
  2457. EXECUTE p_ddl;
  2458. RETURN v_relfilenode <> (SELECT relfilenode FROM pg_class WHERE oid = p_tablename);
  2459. END;
  2460. $$;
  2461. CREATE TABLE rewrite_test(col text);
  2462. INSERT INTO rewrite_test VALUES ('something');
  2463. INSERT INTO rewrite_test VALUES (NULL);
  2464. -- empty[12] don't need rewrite, but notempty[12]_rewrite will force one
  2465. SELECT check_ddl_rewrite('rewrite_test', $$
  2466. ALTER TABLE rewrite_test
  2467. ADD COLUMN empty1 text,
  2468. ADD COLUMN notempty1_rewrite serial;
  2469. $$);
  2470. check_ddl_rewrite
  2471. -------------------
  2472. t
  2473. (1 row)
  2474. SELECT check_ddl_rewrite('rewrite_test', $$
  2475. ALTER TABLE rewrite_test
  2476. ADD COLUMN notempty2_rewrite serial,
  2477. ADD COLUMN empty2 text;
  2478. $$);
  2479. check_ddl_rewrite
  2480. -------------------
  2481. t
  2482. (1 row)
  2483. -- also check that fast defaults cause no problem, first without rewrite
  2484. SELECT check_ddl_rewrite('rewrite_test', $$
  2485. ALTER TABLE rewrite_test
  2486. ADD COLUMN empty3 text,
  2487. ADD COLUMN notempty3_norewrite int default 42;
  2488. $$);
  2489. check_ddl_rewrite
  2490. -------------------
  2491. f
  2492. (1 row)
  2493. SELECT check_ddl_rewrite('rewrite_test', $$
  2494. ALTER TABLE rewrite_test
  2495. ADD COLUMN notempty4_norewrite int default 42,
  2496. ADD COLUMN empty4 text;
  2497. $$);
  2498. check_ddl_rewrite
  2499. -------------------
  2500. f
  2501. (1 row)
  2502. -- then with rewrite
  2503. SELECT check_ddl_rewrite('rewrite_test', $$
  2504. ALTER TABLE rewrite_test
  2505. ADD COLUMN empty5 text,
  2506. ADD COLUMN notempty5_norewrite int default 42,
  2507. ADD COLUMN notempty5_rewrite serial;
  2508. $$);
  2509. check_ddl_rewrite
  2510. -------------------
  2511. t
  2512. (1 row)
  2513. SELECT check_ddl_rewrite('rewrite_test', $$
  2514. ALTER TABLE rewrite_test
  2515. ADD COLUMN notempty6_rewrite serial,
  2516. ADD COLUMN empty6 text,
  2517. ADD COLUMN notempty6_norewrite int default 42;
  2518. $$);
  2519. check_ddl_rewrite
  2520. -------------------
  2521. t
  2522. (1 row)
  2523. -- cleanup
  2524. DROP FUNCTION check_ddl_rewrite(regclass, text);
  2525. DROP TABLE rewrite_test;
  2526. --
  2527. -- lock levels
  2528. --
  2529. drop type lockmodes;
  2530. ERROR: type "lockmodes" does not exist
  2531. create type lockmodes as enum (
  2532. 'SIReadLock'
  2533. ,'AccessShareLock'
  2534. ,'RowShareLock'
  2535. ,'RowExclusiveLock'
  2536. ,'ShareUpdateExclusiveLock'
  2537. ,'ShareLock'
  2538. ,'ShareRowExclusiveLock'
  2539. ,'ExclusiveLock'
  2540. ,'AccessExclusiveLock'
  2541. );
  2542. drop view my_locks;
  2543. ERROR: view "my_locks" does not exist
  2544. create or replace view my_locks as
  2545. select case when c.relname like 'pg_toast%' then 'pg_toast' else c.relname end, max(mode::lockmodes) as max_lockmode
  2546. from pg_locks l join pg_class c on l.relation = c.oid
  2547. where virtualtransaction = (
  2548. select virtualtransaction
  2549. from pg_locks
  2550. where transactionid = pg_current_xact_id()::xid)
  2551. and locktype = 'relation'
  2552. and relnamespace != (select oid from pg_namespace where nspname = 'pg_catalog')
  2553. and c.relname != 'my_locks'
  2554. group by c.relname;
  2555. create table alterlock (f1 int primary key, f2 text);
  2556. insert into alterlock values (1, 'foo');
  2557. create table alterlock2 (f3 int primary key, f1 int);
  2558. insert into alterlock2 values (1, 1);
  2559. begin; alter table alterlock alter column f2 set statistics 150;
  2560. select * from my_locks order by 1;
  2561. relname | max_lockmode
  2562. -----------+--------------------------
  2563. alterlock | ShareUpdateExclusiveLock
  2564. (1 row)
  2565. rollback;
  2566. begin; alter table alterlock cluster on alterlock_pkey;
  2567. select * from my_locks order by 1;
  2568. relname | max_lockmode
  2569. ----------------+--------------------------
  2570. alterlock | ShareUpdateExclusiveLock
  2571. alterlock_pkey | ShareUpdateExclusiveLock
  2572. (2 rows)
  2573. commit;
  2574. begin; alter table alterlock set without cluster;
  2575. select * from my_locks order by 1;
  2576. relname | max_lockmode
  2577. -----------+--------------------------
  2578. alterlock | ShareUpdateExclusiveLock
  2579. (1 row)
  2580. commit;
  2581. begin; alter table alterlock set (fillfactor = 100);
  2582. select * from my_locks order by 1;
  2583. relname | max_lockmode
  2584. -----------+--------------------------
  2585. alterlock | ShareUpdateExclusiveLock
  2586. pg_toast | ShareUpdateExclusiveLock
  2587. (2 rows)
  2588. commit;
  2589. begin; alter table alterlock reset (fillfactor);
  2590. select * from my_locks order by 1;
  2591. relname | max_lockmode
  2592. -----------+--------------------------
  2593. alterlock | ShareUpdateExclusiveLock
  2594. pg_toast | ShareUpdateExclusiveLock
  2595. (2 rows)
  2596. commit;
  2597. begin; alter table alterlock set (toast.autovacuum_enabled = off);
  2598. select * from my_locks order by 1;
  2599. relname | max_lockmode
  2600. -----------+--------------------------
  2601. alterlock | ShareUpdateExclusiveLock
  2602. pg_toast | ShareUpdateExclusiveLock
  2603. (2 rows)
  2604. commit;
  2605. begin; alter table alterlock set (autovacuum_enabled = off);
  2606. select * from my_locks order by 1;
  2607. relname | max_lockmode
  2608. -----------+--------------------------
  2609. alterlock | ShareUpdateExclusiveLock
  2610. pg_toast | ShareUpdateExclusiveLock
  2611. (2 rows)
  2612. commit;
  2613. begin; alter table alterlock alter column f2 set (n_distinct = 1);
  2614. select * from my_locks order by 1;
  2615. relname | max_lockmode
  2616. -----------+--------------------------
  2617. alterlock | ShareUpdateExclusiveLock
  2618. (1 row)
  2619. rollback;
  2620. -- test that mixing options with different lock levels works as expected
  2621. begin; alter table alterlock set (autovacuum_enabled = off, fillfactor = 80);
  2622. select * from my_locks order by 1;
  2623. relname | max_lockmode
  2624. -----------+--------------------------
  2625. alterlock | ShareUpdateExclusiveLock
  2626. pg_toast | ShareUpdateExclusiveLock
  2627. (2 rows)
  2628. commit;
  2629. begin; alter table alterlock alter column f2 set storage extended;
  2630. select * from my_locks order by 1;
  2631. relname | max_lockmode
  2632. -----------+---------------------
  2633. alterlock | AccessExclusiveLock
  2634. (1 row)
  2635. rollback;
  2636. begin; alter table alterlock alter column f2 set default 'x';
  2637. select * from my_locks order by 1;
  2638. relname | max_lockmode
  2639. -----------+---------------------
  2640. alterlock | AccessExclusiveLock
  2641. (1 row)
  2642. rollback;
  2643. begin;
  2644. create trigger ttdummy
  2645. before delete or update on alterlock
  2646. for each row
  2647. execute procedure
  2648. ttdummy (1, 1);
  2649. select * from my_locks order by 1;
  2650. relname | max_lockmode
  2651. -----------+-----------------------
  2652. alterlock | ShareRowExclusiveLock
  2653. (1 row)
  2654. rollback;
  2655. begin;
  2656. select * from my_locks order by 1;
  2657. relname | max_lockmode
  2658. ---------+--------------
  2659. (0 rows)
  2660. alter table alterlock2 add foreign key (f1) references alterlock (f1);
  2661. select * from my_locks order by 1;
  2662. relname | max_lockmode
  2663. -----------------+-----------------------
  2664. alterlock | ShareRowExclusiveLock
  2665. alterlock2 | ShareRowExclusiveLock
  2666. alterlock2_pkey | AccessShareLock
  2667. alterlock_pkey | AccessShareLock
  2668. (4 rows)
  2669. rollback;
  2670. begin;
  2671. alter table alterlock2
  2672. add constraint alterlock2nv foreign key (f1) references alterlock (f1) NOT VALID;
  2673. select * from my_locks order by 1;
  2674. relname | max_lockmode
  2675. ------------+-----------------------
  2676. alterlock | ShareRowExclusiveLock
  2677. alterlock2 | ShareRowExclusiveLock
  2678. (2 rows)
  2679. commit;
  2680. begin;
  2681. alter table alterlock2 validate constraint alterlock2nv;
  2682. select * from my_locks order by 1;
  2683. relname | max_lockmode
  2684. -----------------+--------------------------
  2685. alterlock | RowShareLock
  2686. alterlock2 | ShareUpdateExclusiveLock
  2687. alterlock2_pkey | AccessShareLock
  2688. alterlock_pkey | AccessShareLock
  2689. (4 rows)
  2690. rollback;
  2691. create or replace view my_locks as
  2692. select case when c.relname like 'pg_toast%' then 'pg_toast' else c.relname end, max(mode::lockmodes) as max_lockmode
  2693. from pg_locks l join pg_class c on l.relation = c.oid
  2694. where virtualtransaction = (
  2695. select virtualtransaction
  2696. from pg_locks
  2697. where transactionid = pg_current_xact_id()::xid)
  2698. and locktype = 'relation'
  2699. and relnamespace != (select oid from pg_namespace where nspname = 'pg_catalog')
  2700. and c.relname = 'my_locks'
  2701. group by c.relname;
  2702. -- raise exception
  2703. alter table my_locks set (autovacuum_enabled = false);
  2704. ERROR: unrecognized parameter "autovacuum_enabled"
  2705. alter view my_locks set (autovacuum_enabled = false);
  2706. ERROR: unrecognized parameter "autovacuum_enabled"
  2707. alter table my_locks reset (autovacuum_enabled);
  2708. alter view my_locks reset (autovacuum_enabled);
  2709. begin;
  2710. alter view my_locks set (security_barrier=off);
  2711. select * from my_locks order by 1;
  2712. relname | max_lockmode
  2713. ----------+---------------------
  2714. my_locks | AccessExclusiveLock
  2715. (1 row)
  2716. alter view my_locks reset (security_barrier);
  2717. rollback;
  2718. -- this test intentionally applies the ALTER TABLE command against a view, but
  2719. -- uses a view option so we expect this to succeed. This form of SQL is
  2720. -- accepted for historical reasons, as shown in the docs for ALTER VIEW
  2721. begin;
  2722. alter table my_locks set (security_barrier=off);
  2723. select * from my_locks order by 1;
  2724. relname | max_lockmode
  2725. ----------+---------------------
  2726. my_locks | AccessExclusiveLock
  2727. (1 row)
  2728. alter table my_locks reset (security_barrier);
  2729. rollback;
  2730. -- cleanup
  2731. drop table alterlock2;
  2732. drop table alterlock;
  2733. drop view my_locks;
  2734. drop type lockmodes;
  2735. --
  2736. -- alter function
  2737. --
  2738. create function test_strict(text) returns text as
  2739. 'select coalesce($1, ''got passed a null'');'
  2740. language sql returns null on null input;
  2741. select test_strict(NULL);
  2742. test_strict
  2743. -------------
  2744. (1 row)
  2745. alter function test_strict(text) called on null input;
  2746. select test_strict(NULL);
  2747. test_strict
  2748. -------------------
  2749. got passed a null
  2750. (1 row)
  2751. create function non_strict(text) returns text as
  2752. 'select coalesce($1, ''got passed a null'');'
  2753. language sql called on null input;
  2754. select non_strict(NULL);
  2755. non_strict
  2756. -------------------
  2757. got passed a null
  2758. (1 row)
  2759. alter function non_strict(text) returns null on null input;
  2760. select non_strict(NULL);
  2761. non_strict
  2762. ------------
  2763. (1 row)
  2764. --
  2765. -- alter object set schema
  2766. --
  2767. create schema alter1;
  2768. create schema alter2;
  2769. create table alter1.t1(f1 serial primary key, f2 int check (f2 > 0));
  2770. create view alter1.v1 as select * from alter1.t1;
  2771. create function alter1.plus1(int) returns int as 'select $1+1' language sql;
  2772. create domain alter1.posint integer check (value > 0);
  2773. create type alter1.ctype as (f1 int, f2 text);
  2774. create function alter1.same(alter1.ctype, alter1.ctype) returns boolean language sql
  2775. as 'select $1.f1 is not distinct from $2.f1 and $1.f2 is not distinct from $2.f2';
  2776. create operator alter1.=(procedure = alter1.same, leftarg = alter1.ctype, rightarg = alter1.ctype);
  2777. create operator class alter1.ctype_hash_ops default for type alter1.ctype using hash as
  2778. operator 1 alter1.=(alter1.ctype, alter1.ctype);
  2779. create conversion alter1.latin1_to_utf8 for 'latin1' to 'utf8' from iso8859_1_to_utf8;
  2780. create text search parser alter1.prs(start = prsd_start, gettoken = prsd_nexttoken, end = prsd_end, lextypes = prsd_lextype);
  2781. create text search configuration alter1.cfg(parser = alter1.prs);
  2782. create text search template alter1.tmpl(init = dsimple_init, lexize = dsimple_lexize);
  2783. create text search dictionary alter1.dict(template = alter1.tmpl);
  2784. insert into alter1.t1(f2) values(11);
  2785. insert into alter1.t1(f2) values(12);
  2786. alter table alter1.t1 set schema alter1; -- no-op, same schema
  2787. alter table alter1.t1 set schema alter2;
  2788. alter table alter1.v1 set schema alter2;
  2789. alter function alter1.plus1(int) set schema alter2;
  2790. alter domain alter1.posint set schema alter2;
  2791. alter operator class alter1.ctype_hash_ops using hash set schema alter2;
  2792. alter operator family alter1.ctype_hash_ops using hash set schema alter2;
  2793. alter operator alter1.=(alter1.ctype, alter1.ctype) set schema alter2;
  2794. alter function alter1.same(alter1.ctype, alter1.ctype) set schema alter2;
  2795. alter type alter1.ctype set schema alter1; -- no-op, same schema
  2796. alter type alter1.ctype set schema alter2;
  2797. alter conversion alter1.latin1_to_utf8 set schema alter2;
  2798. alter text search parser alter1.prs set schema alter2;
  2799. alter text search configuration alter1.cfg set schema alter2;
  2800. alter text search template alter1.tmpl set schema alter2;
  2801. alter text search dictionary alter1.dict set schema alter2;
  2802. -- this should succeed because nothing is left in alter1
  2803. drop schema alter1;
  2804. insert into alter2.t1(f2) values(13);
  2805. insert into alter2.t1(f2) values(14);
  2806. select * from alter2.t1;
  2807. f1 | f2
  2808. ----+----
  2809. 1 | 11
  2810. 2 | 12
  2811. 3 | 13
  2812. 4 | 14
  2813. (4 rows)
  2814. select * from alter2.v1;
  2815. f1 | f2
  2816. ----+----
  2817. 1 | 11
  2818. 2 | 12
  2819. 3 | 13
  2820. 4 | 14
  2821. (4 rows)
  2822. select alter2.plus1(41);
  2823. plus1
  2824. -------
  2825. 42
  2826. (1 row)
  2827. -- clean up
  2828. drop schema alter2 cascade;
  2829. NOTICE: drop cascades to 13 other objects
  2830. DETAIL: drop cascades to table alter2.t1
  2831. drop cascades to view alter2.v1
  2832. drop cascades to function alter2.plus1(integer)
  2833. drop cascades to type alter2.posint
  2834. drop cascades to type alter2.ctype
  2835. drop cascades to function alter2.same(alter2.ctype,alter2.ctype)
  2836. drop cascades to operator alter2.=(alter2.ctype,alter2.ctype)
  2837. drop cascades to operator family alter2.ctype_hash_ops for access method hash
  2838. drop cascades to conversion alter2.latin1_to_utf8
  2839. drop cascades to text search parser alter2.prs
  2840. drop cascades to text search configuration alter2.cfg
  2841. drop cascades to text search template alter2.tmpl
  2842. drop cascades to text search dictionary alter2.dict
  2843. --
  2844. -- composite types
  2845. --
  2846. CREATE TYPE test_type AS (a int);
  2847. \d test_type
  2848. Composite type "public.test_type"
  2849. Column | Type | Collation | Nullable | Default
  2850. --------+---------+-----------+----------+---------
  2851. a | integer | | |
  2852. ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails
  2853. ERROR: relation "nosuchtype" does not exist
  2854. ALTER TYPE test_type ADD ATTRIBUTE b text;
  2855. \d test_type
  2856. Composite type "public.test_type"
  2857. Column | Type | Collation | Nullable | Default
  2858. --------+---------+-----------+----------+---------
  2859. a | integer | | |
  2860. b | text | | |
  2861. ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails
  2862. ERROR: column "b" of relation "test_type" already exists
  2863. ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar;
  2864. \d test_type
  2865. Composite type "public.test_type"
  2866. Column | Type | Collation | Nullable | Default
  2867. --------+-------------------+-----------+----------+---------
  2868. a | integer | | |
  2869. b | character varying | | |
  2870. ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer;
  2871. \d test_type
  2872. Composite type "public.test_type"
  2873. Column | Type | Collation | Nullable | Default
  2874. --------+---------+-----------+----------+---------
  2875. a | integer | | |
  2876. b | integer | | |
  2877. ALTER TYPE test_type DROP ATTRIBUTE b;
  2878. \d test_type
  2879. Composite type "public.test_type"
  2880. Column | Type | Collation | Nullable | Default
  2881. --------+---------+-----------+----------+---------
  2882. a | integer | | |
  2883. ALTER TYPE test_type DROP ATTRIBUTE c; -- fails
  2884. ERROR: column "c" of relation "test_type" does not exist
  2885. ALTER TYPE test_type DROP ATTRIBUTE IF EXISTS c;
  2886. NOTICE: column "c" of relation "test_type" does not exist, skipping
  2887. ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean;
  2888. \d test_type
  2889. Composite type "public.test_type"
  2890. Column | Type | Collation | Nullable | Default
  2891. --------+---------+-----------+----------+---------
  2892. d | boolean | | |
  2893. ALTER TYPE test_type RENAME ATTRIBUTE a TO aa;
  2894. ERROR: column "a" does not exist
  2895. ALTER TYPE test_type RENAME ATTRIBUTE d TO dd;
  2896. \d test_type
  2897. Composite type "public.test_type"
  2898. Column | Type | Collation | Nullable | Default
  2899. --------+---------+-----------+----------+---------
  2900. dd | boolean | | |
  2901. DROP TYPE test_type;
  2902. CREATE TYPE test_type1 AS (a int, b text);
  2903. CREATE TABLE test_tbl1 (x int, y test_type1);
  2904. ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
  2905. ERROR: cannot alter type "test_type1" because column "test_tbl1.y" uses it
  2906. CREATE TYPE test_type2 AS (a int, b text);
  2907. CREATE TABLE test_tbl2 OF test_type2;
  2908. CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
  2909. \d test_type2
  2910. Composite type "public.test_type2"
  2911. Column | Type | Collation | Nullable | Default
  2912. --------+---------+-----------+----------+---------
  2913. a | integer | | |
  2914. b | text | | |
  2915. \d test_tbl2
  2916. Table "public.test_tbl2"
  2917. Column | Type | Collation | Nullable | Default
  2918. --------+---------+-----------+----------+---------
  2919. a | integer | | |
  2920. b | text | | |
  2921. Number of child tables: 1 (Use \d+ to list them.)
  2922. Typed table of type: test_type2
  2923. ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
  2924. ERROR: cannot alter type "test_type2" because it is the type of a typed table
  2925. HINT: Use ALTER ... CASCADE to alter the typed tables too.
  2926. ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
  2927. \d test_type2
  2928. Composite type "public.test_type2"
  2929. Column | Type | Collation | Nullable | Default
  2930. --------+---------+-----------+----------+---------
  2931. a | integer | | |
  2932. b | text | | |
  2933. c | text | | |
  2934. \d test_tbl2
  2935. Table "public.test_tbl2"
  2936. Column | Type | Collation | Nullable | Default
  2937. --------+---------+-----------+----------+---------
  2938. a | integer | | |
  2939. b | text | | |
  2940. c | text | | |
  2941. Number of child tables: 1 (Use \d+ to list them.)
  2942. Typed table of type: test_type2
  2943. ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
  2944. ERROR: cannot alter type "test_type2" because it is the type of a typed table
  2945. HINT: Use ALTER ... CASCADE to alter the typed tables too.
  2946. ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
  2947. \d test_type2
  2948. Composite type "public.test_type2"
  2949. Column | Type | Collation | Nullable | Default
  2950. --------+-------------------+-----------+----------+---------
  2951. a | integer | | |
  2952. b | character varying | | |
  2953. c | text | | |
  2954. \d test_tbl2
  2955. Table "public.test_tbl2"
  2956. Column | Type | Collation | Nullable | Default
  2957. --------+-------------------+-----------+----------+---------
  2958. a | integer | | |
  2959. b | character varying | | |
  2960. c | text | | |
  2961. Number of child tables: 1 (Use \d+ to list them.)
  2962. Typed table of type: test_type2
  2963. ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
  2964. ERROR: cannot alter type "test_type2" because it is the type of a typed table
  2965. HINT: Use ALTER ... CASCADE to alter the typed tables too.
  2966. ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
  2967. \d test_type2
  2968. Composite type "public.test_type2"
  2969. Column | Type | Collation | Nullable | Default
  2970. --------+---------+-----------+----------+---------
  2971. a | integer | | |
  2972. c | text | | |
  2973. \d test_tbl2
  2974. Table "public.test_tbl2"
  2975. Column | Type | Collation | Nullable | Default
  2976. --------+---------+-----------+----------+---------
  2977. a | integer | | |
  2978. c | text | | |
  2979. Number of child tables: 1 (Use \d+ to list them.)
  2980. Typed table of type: test_type2
  2981. ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
  2982. ERROR: cannot alter type "test_type2" because it is the type of a typed table
  2983. HINT: Use ALTER ... CASCADE to alter the typed tables too.
  2984. ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
  2985. \d test_type2
  2986. Composite type "public.test_type2"
  2987. Column | Type | Collation | Nullable | Default
  2988. --------+---------+-----------+----------+---------
  2989. aa | integer | | |
  2990. c | text | | |
  2991. \d test_tbl2
  2992. Table "public.test_tbl2"
  2993. Column | Type | Collation | Nullable | Default
  2994. --------+---------+-----------+----------+---------
  2995. aa | integer | | |
  2996. c | text | | |
  2997. Number of child tables: 1 (Use \d+ to list them.)
  2998. Typed table of type: test_type2
  2999. \d test_tbl2_subclass
  3000. Table "public.test_tbl2_subclass"
  3001. Column | Type | Collation | Nullable | Default
  3002. --------+---------+-----------+----------+---------
  3003. aa | integer | | |
  3004. c | text | | |
  3005. Inherits: test_tbl2
  3006. DROP TABLE test_tbl2_subclass;
  3007. CREATE TYPE test_typex AS (a int, b text);
  3008. CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
  3009. ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
  3010. ERROR: cannot drop column a of composite type test_typex because other objects depend on it
  3011. DETAIL: constraint test_tblx_y_check on table test_tblx depends on column a of composite type test_typex
  3012. HINT: Use DROP ... CASCADE to drop the dependent objects too.
  3013. ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
  3014. NOTICE: drop cascades to constraint test_tblx_y_check on table test_tblx
  3015. \d test_tblx
  3016. Table "public.test_tblx"
  3017. Column | Type | Collation | Nullable | Default
  3018. --------+------------+-----------+----------+---------
  3019. x | integer | | |
  3020. y | test_typex | | |
  3021. DROP TABLE test_tblx;
  3022. DROP TYPE test_typex;
  3023. -- This test isn't that interesting on its own, but the purpose is to leave
  3024. -- behind a table to test pg_upgrade with. The table has a composite type
  3025. -- column in it, and the composite type has a dropped attribute.
  3026. CREATE TYPE test_type3 AS (a int);
  3027. CREATE TABLE test_tbl3 (c) AS SELECT '(1)'::test_type3;
  3028. ALTER TYPE test_type3 DROP ATTRIBUTE a, ADD ATTRIBUTE b int;
  3029. CREATE TYPE test_type_empty AS ();
  3030. DROP TYPE test_type_empty;
  3031. --
  3032. -- typed tables: OF / NOT OF
  3033. --
  3034. CREATE TYPE tt_t0 AS (z inet, x int, y numeric(8,2));
  3035. ALTER TYPE tt_t0 DROP ATTRIBUTE z;
  3036. CREATE TABLE tt0 (x int NOT NULL, y numeric(8,2)); -- OK
  3037. CREATE TABLE tt1 (x int, y bigint); -- wrong base type
  3038. CREATE TABLE tt2 (x int, y numeric(9,2)); -- wrong typmod
  3039. CREATE TABLE tt3 (y numeric(8,2), x int); -- wrong column order
  3040. CREATE TABLE tt4 (x int); -- too few columns
  3041. CREATE TABLE tt5 (x int, y numeric(8,2), z int); -- too few columns
  3042. CREATE TABLE tt6 () INHERITS (tt0); -- can't have a parent
  3043. CREATE TABLE tt7 (x int, q text, y numeric(8,2));
  3044. ALTER TABLE tt7 DROP q; -- OK
  3045. ALTER TABLE tt0 OF tt_t0;
  3046. ALTER TABLE tt1 OF tt_t0;
  3047. ERROR: table "tt1" has different type for column "y"
  3048. ALTER TABLE tt2 OF tt_t0;
  3049. ERROR: table "tt2" has different type for column "y"
  3050. ALTER TABLE tt3 OF tt_t0;
  3051. ERROR: table has column "y" where type requires "x"
  3052. ALTER TABLE tt4 OF tt_t0;
  3053. ERROR: table is missing column "y"
  3054. ALTER TABLE tt5 OF tt_t0;
  3055. ERROR: table has extra column "z"
  3056. ALTER TABLE tt6 OF tt_t0;
  3057. ERROR: typed tables cannot inherit
  3058. ALTER TABLE tt7 OF tt_t0;
  3059. CREATE TYPE tt_t1 AS (x int, y numeric(8,2));
  3060. ALTER TABLE tt7 OF tt_t1; -- reassign an already-typed table
  3061. ALTER TABLE tt7 NOT OF;
  3062. \d tt7
  3063. Table "public.tt7"
  3064. Column | Type | Collation | Nullable | Default
  3065. --------+--------------+-----------+----------+---------
  3066. x | integer | | |
  3067. y | numeric(8,2) | | |
  3068. -- make sure we can drop a constraint on the parent but it remains on the child
  3069. CREATE TABLE test_drop_constr_parent (c text CHECK (c IS NOT NULL));
  3070. CREATE TABLE test_drop_constr_child () INHERITS (test_drop_constr_parent);
  3071. ALTER TABLE ONLY test_drop_constr_parent DROP CONSTRAINT "test_drop_constr_parent_c_check";
  3072. -- should fail
  3073. INSERT INTO test_drop_constr_child (c) VALUES (NULL);
  3074. ERROR: new row for relation "test_drop_constr_child" violates check constraint "test_drop_constr_parent_c_check"
  3075. DETAIL: Failing row contains (null).
  3076. DROP TABLE test_drop_constr_parent CASCADE;
  3077. NOTICE: drop cascades to table test_drop_constr_child
  3078. --
  3079. -- IF EXISTS test
  3080. --
  3081. ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
  3082. NOTICE: relation "tt8" does not exist, skipping
  3083. ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
  3084. NOTICE: relation "tt8" does not exist, skipping
  3085. ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
  3086. NOTICE: relation "tt8" does not exist, skipping
  3087. ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
  3088. NOTICE: relation "tt8" does not exist, skipping
  3089. ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
  3090. NOTICE: relation "tt8" does not exist, skipping
  3091. ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
  3092. NOTICE: relation "tt8" does not exist, skipping
  3093. CREATE TABLE tt8(a int);
  3094. CREATE SCHEMA alter2;
  3095. ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
  3096. ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
  3097. ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
  3098. ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
  3099. ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
  3100. ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
  3101. \d alter2.tt8
  3102. Table "alter2.tt8"
  3103. Column | Type | Collation | Nullable | Default
  3104. --------+---------+-----------+----------+---------
  3105. a | integer | | |
  3106. f1 | integer | | not null | 0
  3107. Indexes:
  3108. "xxx" PRIMARY KEY, btree (f1)
  3109. Check constraints:
  3110. "tt8_f_check" CHECK (f1 >= 0 AND f1 <= 10)
  3111. DROP TABLE alter2.tt8;
  3112. DROP SCHEMA alter2;
  3113. --
  3114. -- Check conflicts between index and CHECK constraint names
  3115. --
  3116. CREATE TABLE tt9(c integer);
  3117. ALTER TABLE tt9 ADD CHECK(c > 1);
  3118. ALTER TABLE tt9 ADD CHECK(c > 2); -- picks nonconflicting name
  3119. ALTER TABLE tt9 ADD CONSTRAINT foo CHECK(c > 3);
  3120. ALTER TABLE tt9 ADD CONSTRAINT foo CHECK(c > 4); -- fail, dup name
  3121. ERROR: constraint "foo" for relation "tt9" already exists
  3122. ALTER TABLE tt9 ADD UNIQUE(c);
  3123. ALTER TABLE tt9 ADD UNIQUE(c); -- picks nonconflicting name
  3124. ALTER TABLE tt9 ADD CONSTRAINT tt9_c_key UNIQUE(c); -- fail, dup name
  3125. ERROR: relation "tt9_c_key" already exists
  3126. ALTER TABLE tt9 ADD CONSTRAINT foo UNIQUE(c); -- fail, dup name
  3127. ERROR: constraint "foo" for relation "tt9" already exists
  3128. ALTER TABLE tt9 ADD CONSTRAINT tt9_c_key CHECK(c > 5); -- fail, dup name
  3129. ERROR: constraint "tt9_c_key" for relation "tt9" already exists
  3130. ALTER TABLE tt9 ADD CONSTRAINT tt9_c_key2 CHECK(c > 6);
  3131. ALTER TABLE tt9 ADD UNIQUE(c); -- picks nonconflicting name
  3132. \d tt9
  3133. Table "public.tt9"
  3134. Column | Type | Collation | Nullable | Default
  3135. --------+---------+-----------+----------+---------
  3136. c | integer | | |
  3137. Indexes:
  3138. "tt9_c_key" UNIQUE CONSTRAINT, btree (c)
  3139. "tt9_c_key1" UNIQUE CONSTRAINT, btree (c)
  3140. "tt9_c_key3" UNIQUE CONSTRAINT, btree (c)
  3141. Check constraints:
  3142. "foo" CHECK (c > 3)
  3143. "tt9_c_check" CHECK (c > 1)
  3144. "tt9_c_check1" CHECK (c > 2)
  3145. "tt9_c_key2" CHECK (c > 6)
  3146. DROP TABLE tt9;
  3147. -- Check that comments on constraints and indexes are not lost at ALTER TABLE.
  3148. CREATE TABLE comment_test (
  3149. id int,
  3150. positive_col int CHECK (positive_col > 0),
  3151. indexed_col int,
  3152. CONSTRAINT comment_test_pk PRIMARY KEY (id));
  3153. CREATE INDEX comment_test_index ON comment_test(indexed_col);
  3154. COMMENT ON COLUMN comment_test.id IS 'Column ''id'' on comment_test';
  3155. COMMENT ON INDEX comment_test_index IS 'Simple index on comment_test';
  3156. COMMENT ON CONSTRAINT comment_test_positive_col_check ON comment_test IS 'CHECK constraint on comment_test.positive_col';
  3157. COMMENT ON CONSTRAINT comment_test_pk ON comment_test IS 'PRIMARY KEY constraint of comment_test';
  3158. COMMENT ON INDEX comment_test_pk IS 'Index backing the PRIMARY KEY of comment_test';
  3159. SELECT col_description('comment_test'::regclass, 1) as comment;
  3160. comment
  3161. -----------------------------
  3162. Column 'id' on comment_test
  3163. (1 row)
  3164. 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;
  3165. index | comment
  3166. --------------------+-----------------------------------------------
  3167. comment_test_index | Simple index on comment_test
  3168. comment_test_pk | Index backing the PRIMARY KEY of comment_test
  3169. (2 rows)
  3170. SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test'::regclass ORDER BY 1, 2;
  3171. constraint | comment
  3172. ---------------------------------+-----------------------------------------------
  3173. comment_test_pk | PRIMARY KEY constraint of comment_test
  3174. comment_test_positive_col_check | CHECK constraint on comment_test.positive_col
  3175. (2 rows)
  3176. -- Change the datatype of all the columns. ALTER TABLE is optimized to not
  3177. -- rebuild an index if the new data type is binary compatible with the old
  3178. -- one. Check do a dummy ALTER TABLE that doesn't change the datatype
  3179. -- first, to test that no-op codepath, and another one that does.
  3180. ALTER TABLE comment_test ALTER COLUMN indexed_col SET DATA TYPE int;
  3181. ALTER TABLE comment_test ALTER COLUMN indexed_col SET DATA TYPE text;
  3182. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int;
  3183. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
  3184. ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE int;
  3185. ALTER TABLE comment_test ALTER COLUMN positive_col SET DATA TYPE bigint;
  3186. -- Check that the comments are intact.
  3187. SELECT col_description('comment_test'::regclass, 1) as comment;
  3188. comment
  3189. -----------------------------
  3190. Column 'id' on comment_test
  3191. (1 row)
  3192. 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;
  3193. index | comment
  3194. --------------------+-----------------------------------------------
  3195. comment_test_index | Simple index on comment_test
  3196. comment_test_pk | Index backing the PRIMARY KEY of comment_test
  3197. (2 rows)
  3198. SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test'::regclass ORDER BY 1, 2;
  3199. constraint | comment
  3200. ---------------------------------+-----------------------------------------------
  3201. comment_test_pk | PRIMARY KEY constraint of comment_test
  3202. comment_test_positive_col_check | CHECK constraint on comment_test.positive_col
  3203. (2 rows)
  3204. -- Check compatibility for foreign keys and comments. This is done
  3205. -- separately as rebuilding the column type of the parent leads
  3206. -- to an error and would reduce the test scope.
  3207. CREATE TABLE comment_test_child (
  3208. id text CONSTRAINT comment_test_child_fk REFERENCES comment_test);
  3209. CREATE INDEX comment_test_child_fk ON comment_test_child(id);
  3210. COMMENT ON COLUMN comment_test_child.id IS 'Column ''id'' on comment_test_child';
  3211. COMMENT ON INDEX comment_test_child_fk IS 'Index backing the FOREIGN KEY of comment_test_child';
  3212. COMMENT ON CONSTRAINT comment_test_child_fk ON comment_test_child IS 'FOREIGN KEY constraint of comment_test_child';
  3213. -- Change column type of parent
  3214. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
  3215. ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int USING id::integer;
  3216. ERROR: foreign key constraint "comment_test_child_fk" cannot be implemented
  3217. DETAIL: Key columns "id" and "id" are of incompatible types: text and integer.
  3218. -- Comments should be intact
  3219. SELECT col_description('comment_test_child'::regclass, 1) as comment;
  3220. comment
  3221. -----------------------------------
  3222. Column 'id' on comment_test_child
  3223. (1 row)
  3224. 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;
  3225. index | comment
  3226. -----------------------+-----------------------------------------------------
  3227. comment_test_child_fk | Index backing the FOREIGN KEY of comment_test_child
  3228. (1 row)
  3229. SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test_child'::regclass ORDER BY 1, 2;
  3230. constraint | comment
  3231. -----------------------+----------------------------------------------
  3232. comment_test_child_fk | FOREIGN KEY constraint of comment_test_child
  3233. (1 row)
  3234. -- Check that we map relation oids to filenodes and back correctly. Only
  3235. -- display bad mappings so the test output doesn't change all the time. A
  3236. -- filenode function call can return NULL for a relation dropped concurrently
  3237. -- with the call's surrounding query, so ignore a NULL mapped_oid for
  3238. -- relations that no longer exist after all calls finish.
  3239. CREATE TEMP TABLE filenode_mapping AS
  3240. SELECT
  3241. oid, mapped_oid, reltablespace, relfilenode, relname
  3242. FROM pg_class,
  3243. pg_filenode_relation(reltablespace, pg_relation_filenode(oid)) AS mapped_oid
  3244. WHERE relkind IN ('r', 'i', 'S', 't', 'm') AND mapped_oid IS DISTINCT FROM oid;
  3245. SELECT m.* FROM filenode_mapping m LEFT JOIN pg_class c ON c.oid = m.oid
  3246. WHERE c.oid IS NOT NULL OR m.mapped_oid IS NOT NULL;
  3247. oid | mapped_oid | reltablespace | relfilenode | relname
  3248. -----+------------+---------------+-------------+---------
  3249. (0 rows)
  3250. -- Checks on creating and manipulation of user defined relations in
  3251. -- pg_catalog.
  3252. SHOW allow_system_table_mods;
  3253. allow_system_table_mods
  3254. -------------------------
  3255. off
  3256. (1 row)
  3257. -- disallowed because of search_path issues with pg_dump
  3258. CREATE TABLE pg_catalog.new_system_table();
  3259. ERROR: permission denied to create "pg_catalog.new_system_table"
  3260. DETAIL: System catalog modifications are currently disallowed.
  3261. -- instead create in public first, move to catalog
  3262. CREATE TABLE new_system_table(id serial primary key, othercol text);
  3263. ALTER TABLE new_system_table SET SCHEMA pg_catalog;
  3264. ALTER TABLE new_system_table SET SCHEMA public;
  3265. ALTER TABLE new_system_table SET SCHEMA pg_catalog;
  3266. -- will be ignored -- already there:
  3267. ALTER TABLE new_system_table SET SCHEMA pg_catalog;
  3268. ALTER TABLE new_system_table RENAME TO old_system_table;
  3269. CREATE INDEX old_system_table__othercol ON old_system_table (othercol);
  3270. INSERT INTO old_system_table(othercol) VALUES ('somedata'), ('otherdata');
  3271. UPDATE old_system_table SET id = -id;
  3272. DELETE FROM old_system_table WHERE othercol = 'somedata';
  3273. TRUNCATE old_system_table;
  3274. ALTER TABLE old_system_table DROP CONSTRAINT new_system_table_pkey;
  3275. ALTER TABLE old_system_table DROP COLUMN othercol;
  3276. DROP TABLE old_system_table;
  3277. -- set logged
  3278. CREATE UNLOGGED TABLE unlogged1(f1 SERIAL PRIMARY KEY, f2 TEXT);
  3279. -- check relpersistence of an unlogged table
  3280. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged1'
  3281. UNION ALL
  3282. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^unlogged1'
  3283. UNION ALL
  3284. 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'
  3285. ORDER BY relname;
  3286. relname | relkind | relpersistence
  3287. ------------------+---------+----------------
  3288. toast index | i | u
  3289. toast table | t | u
  3290. unlogged1 | r | u
  3291. unlogged1_f1_seq | S | p
  3292. unlogged1_pkey | i | u
  3293. (5 rows)
  3294. CREATE UNLOGGED TABLE unlogged2(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES unlogged1); -- foreign key
  3295. CREATE UNLOGGED TABLE unlogged3(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES unlogged3); -- self-referencing foreign key
  3296. ALTER TABLE unlogged3 SET LOGGED; -- skip self-referencing foreign key
  3297. ALTER TABLE unlogged2 SET LOGGED; -- fails because a foreign key to an unlogged table exists
  3298. ERROR: could not change table "unlogged2" to logged because it references unlogged table "unlogged1"
  3299. ALTER TABLE unlogged1 SET LOGGED;
  3300. -- check relpersistence of an unlogged table after changing to permanent
  3301. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged1'
  3302. UNION ALL
  3303. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^unlogged1'
  3304. UNION ALL
  3305. 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'
  3306. ORDER BY relname;
  3307. relname | relkind | relpersistence
  3308. ------------------+---------+----------------
  3309. toast index | i | p
  3310. toast table | t | p
  3311. unlogged1 | r | p
  3312. unlogged1_f1_seq | S | p
  3313. unlogged1_pkey | i | p
  3314. (5 rows)
  3315. ALTER TABLE unlogged1 SET LOGGED; -- silently do nothing
  3316. DROP TABLE unlogged3;
  3317. DROP TABLE unlogged2;
  3318. DROP TABLE unlogged1;
  3319. -- set unlogged
  3320. CREATE TABLE logged1(f1 SERIAL PRIMARY KEY, f2 TEXT);
  3321. -- check relpersistence of a permanent table
  3322. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^logged1'
  3323. UNION ALL
  3324. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^logged1'
  3325. UNION ALL
  3326. 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'
  3327. ORDER BY relname;
  3328. relname | relkind | relpersistence
  3329. ----------------+---------+----------------
  3330. logged1 | r | p
  3331. logged1_f1_seq | S | p
  3332. logged1_pkey | i | p
  3333. toast index | i | p
  3334. toast table | t | p
  3335. (5 rows)
  3336. CREATE TABLE logged2(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged1); -- foreign key
  3337. CREATE TABLE logged3(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged3); -- self-referencing foreign key
  3338. ALTER TABLE logged1 SET UNLOGGED; -- fails because a foreign key from a permanent table exists
  3339. ERROR: could not change table "logged1" to unlogged because it references logged table "logged2"
  3340. ALTER TABLE logged3 SET UNLOGGED; -- skip self-referencing foreign key
  3341. ALTER TABLE logged2 SET UNLOGGED;
  3342. ALTER TABLE logged1 SET UNLOGGED;
  3343. -- check relpersistence of a permanent table after changing to unlogged
  3344. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^logged1'
  3345. UNION ALL
  3346. SELECT 'toast table', t.relkind, t.relpersistence FROM pg_class r JOIN pg_class t ON t.oid = r.reltoastrelid WHERE r.relname ~ '^logged1'
  3347. UNION ALL
  3348. 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'
  3349. ORDER BY relname;
  3350. relname | relkind | relpersistence
  3351. ----------------+---------+----------------
  3352. logged1 | r | u
  3353. logged1_f1_seq | S | p
  3354. logged1_pkey | i | u
  3355. toast index | i | u
  3356. toast table | t | u
  3357. (5 rows)
  3358. ALTER TABLE logged1 SET UNLOGGED; -- silently do nothing
  3359. DROP TABLE logged3;
  3360. DROP TABLE logged2;
  3361. DROP TABLE logged1;
  3362. -- test ADD COLUMN IF NOT EXISTS
  3363. CREATE TABLE test_add_column(c1 integer);
  3364. \d test_add_column
  3365. Table "public.test_add_column"
  3366. Column | Type | Collation | Nullable | Default
  3367. --------+---------+-----------+----------+---------
  3368. c1 | integer | | |
  3369. ALTER TABLE test_add_column
  3370. ADD COLUMN c2 integer;
  3371. \d test_add_column
  3372. Table "public.test_add_column"
  3373. Column | Type | Collation | Nullable | Default
  3374. --------+---------+-----------+----------+---------
  3375. c1 | integer | | |
  3376. c2 | integer | | |
  3377. ALTER TABLE test_add_column
  3378. ADD COLUMN c2 integer; -- fail because c2 already exists
  3379. ERROR: column "c2" of relation "test_add_column" already exists
  3380. ALTER TABLE ONLY test_add_column
  3381. ADD COLUMN c2 integer; -- fail because c2 already exists
  3382. ERROR: column "c2" of relation "test_add_column" already exists
  3383. \d test_add_column
  3384. Table "public.test_add_column"
  3385. Column | Type | Collation | Nullable | Default
  3386. --------+---------+-----------+----------+---------
  3387. c1 | integer | | |
  3388. c2 | integer | | |
  3389. ALTER TABLE test_add_column
  3390. ADD COLUMN IF NOT EXISTS c2 integer; -- skipping because c2 already exists
  3391. NOTICE: column "c2" of relation "test_add_column" already exists, skipping
  3392. ALTER TABLE ONLY test_add_column
  3393. ADD COLUMN IF NOT EXISTS c2 integer; -- skipping because c2 already exists
  3394. NOTICE: column "c2" of relation "test_add_column" already exists, skipping
  3395. \d test_add_column
  3396. Table "public.test_add_column"
  3397. Column | Type | Collation | Nullable | Default
  3398. --------+---------+-----------+----------+---------
  3399. c1 | integer | | |
  3400. c2 | integer | | |
  3401. ALTER TABLE test_add_column
  3402. ADD COLUMN c2 integer, -- fail because c2 already exists
  3403. ADD COLUMN c3 integer primary key;
  3404. ERROR: column "c2" of relation "test_add_column" already exists
  3405. \d test_add_column
  3406. Table "public.test_add_column"
  3407. Column | Type | Collation | Nullable | Default
  3408. --------+---------+-----------+----------+---------
  3409. c1 | integer | | |
  3410. c2 | integer | | |
  3411. ALTER TABLE test_add_column
  3412. ADD COLUMN IF NOT EXISTS c2 integer, -- skipping because c2 already exists
  3413. ADD COLUMN c3 integer primary key;
  3414. NOTICE: column "c2" of relation "test_add_column" already exists, skipping
  3415. \d test_add_column
  3416. Table "public.test_add_column"
  3417. Column | Type | Collation | Nullable | Default
  3418. --------+---------+-----------+----------+---------
  3419. c1 | integer | | |
  3420. c2 | integer | | |
  3421. c3 | integer | | not null |
  3422. Indexes:
  3423. "test_add_column_pkey" PRIMARY KEY, btree (c3)
  3424. ALTER TABLE test_add_column
  3425. ADD COLUMN IF NOT EXISTS c2 integer, -- skipping because c2 already exists
  3426. ADD COLUMN IF NOT EXISTS c3 integer primary key; -- skipping because c3 already exists
  3427. NOTICE: column "c2" of relation "test_add_column" already exists, skipping
  3428. NOTICE: column "c3" of relation "test_add_column" already exists, skipping
  3429. \d test_add_column
  3430. Table "public.test_add_column"
  3431. Column | Type | Collation | Nullable | Default
  3432. --------+---------+-----------+----------+---------
  3433. c1 | integer | | |
  3434. c2 | integer | | |
  3435. c3 | integer | | not null |
  3436. Indexes:
  3437. "test_add_column_pkey" PRIMARY KEY, btree (c3)
  3438. ALTER TABLE test_add_column
  3439. ADD COLUMN IF NOT EXISTS c2 integer, -- skipping because c2 already exists
  3440. ADD COLUMN IF NOT EXISTS c3 integer, -- skipping because c3 already exists
  3441. ADD COLUMN c4 integer REFERENCES test_add_column;
  3442. NOTICE: column "c2" of relation "test_add_column" already exists, skipping
  3443. NOTICE: column "c3" of relation "test_add_column" already exists, skipping
  3444. \d test_add_column
  3445. Table "public.test_add_column"
  3446. Column | Type | Collation | Nullable | Default
  3447. --------+---------+-----------+----------+---------
  3448. c1 | integer | | |
  3449. c2 | integer | | |
  3450. c3 | integer | | not null |
  3451. c4 | integer | | |
  3452. Indexes:
  3453. "test_add_column_pkey" PRIMARY KEY, btree (c3)
  3454. Foreign-key constraints:
  3455. "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3456. Referenced by:
  3457. TABLE "test_add_column" CONSTRAINT "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3458. ALTER TABLE test_add_column
  3459. ADD COLUMN IF NOT EXISTS c4 integer REFERENCES test_add_column;
  3460. NOTICE: column "c4" of relation "test_add_column" already exists, skipping
  3461. \d test_add_column
  3462. Table "public.test_add_column"
  3463. Column | Type | Collation | Nullable | Default
  3464. --------+---------+-----------+----------+---------
  3465. c1 | integer | | |
  3466. c2 | integer | | |
  3467. c3 | integer | | not null |
  3468. c4 | integer | | |
  3469. Indexes:
  3470. "test_add_column_pkey" PRIMARY KEY, btree (c3)
  3471. Foreign-key constraints:
  3472. "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3473. Referenced by:
  3474. TABLE "test_add_column" CONSTRAINT "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3475. ALTER TABLE test_add_column
  3476. ADD COLUMN IF NOT EXISTS c5 SERIAL CHECK (c5 > 8);
  3477. \d test_add_column
  3478. Table "public.test_add_column"
  3479. Column | Type | Collation | Nullable | Default
  3480. --------+---------+-----------+----------+---------------------------------------------
  3481. c1 | integer | | |
  3482. c2 | integer | | |
  3483. c3 | integer | | not null |
  3484. c4 | integer | | |
  3485. c5 | integer | | not null | nextval('test_add_column_c5_seq'::regclass)
  3486. Indexes:
  3487. "test_add_column_pkey" PRIMARY KEY, btree (c3)
  3488. Check constraints:
  3489. "test_add_column_c5_check" CHECK (c5 > 8)
  3490. Foreign-key constraints:
  3491. "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3492. Referenced by:
  3493. TABLE "test_add_column" CONSTRAINT "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3494. ALTER TABLE test_add_column
  3495. ADD COLUMN IF NOT EXISTS c5 SERIAL CHECK (c5 > 10);
  3496. NOTICE: column "c5" of relation "test_add_column" already exists, skipping
  3497. \d test_add_column*
  3498. Table "public.test_add_column"
  3499. Column | Type | Collation | Nullable | Default
  3500. --------+---------+-----------+----------+---------------------------------------------
  3501. c1 | integer | | |
  3502. c2 | integer | | |
  3503. c3 | integer | | not null |
  3504. c4 | integer | | |
  3505. c5 | integer | | not null | nextval('test_add_column_c5_seq'::regclass)
  3506. Indexes:
  3507. "test_add_column_pkey" PRIMARY KEY, btree (c3)
  3508. Check constraints:
  3509. "test_add_column_c5_check" CHECK (c5 > 8)
  3510. Foreign-key constraints:
  3511. "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3512. Referenced by:
  3513. TABLE "test_add_column" CONSTRAINT "test_add_column_c4_fkey" FOREIGN KEY (c4) REFERENCES test_add_column(c3)
  3514. Sequence "public.test_add_column_c5_seq"
  3515. Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
  3516. ---------+-------+---------+------------+-----------+---------+-------
  3517. integer | 1 | 1 | 2147483647 | 1 | no | 1
  3518. Owned by: public.test_add_column.c5
  3519. Index "public.test_add_column_pkey"
  3520. Column | Type | Key? | Definition
  3521. --------+---------+------+------------
  3522. c3 | integer | yes | c3
  3523. primary key, btree, for table "public.test_add_column"
  3524. DROP TABLE test_add_column;
  3525. \d test_add_column*
  3526. -- assorted cases with multiple ALTER TABLE steps
  3527. CREATE TABLE ataddindex(f1 INT);
  3528. INSERT INTO ataddindex VALUES (42), (43);
  3529. CREATE UNIQUE INDEX ataddindexi0 ON ataddindex(f1);
  3530. ALTER TABLE ataddindex
  3531. ADD PRIMARY KEY USING INDEX ataddindexi0,
  3532. ALTER f1 TYPE BIGINT;
  3533. \d ataddindex
  3534. Table "public.ataddindex"
  3535. Column | Type | Collation | Nullable | Default
  3536. --------+--------+-----------+----------+---------
  3537. f1 | bigint | | not null |
  3538. Indexes:
  3539. "ataddindexi0" PRIMARY KEY, btree (f1)
  3540. DROP TABLE ataddindex;
  3541. CREATE TABLE ataddindex(f1 VARCHAR(10));
  3542. INSERT INTO ataddindex(f1) VALUES ('foo'), ('a');
  3543. ALTER TABLE ataddindex
  3544. ALTER f1 SET DATA TYPE TEXT,
  3545. ADD EXCLUDE ((f1 LIKE 'a') WITH =);
  3546. \d ataddindex
  3547. Table "public.ataddindex"
  3548. Column | Type | Collation | Nullable | Default
  3549. --------+------+-----------+----------+---------
  3550. f1 | text | | |
  3551. Indexes:
  3552. "ataddindex_expr_excl" EXCLUDE USING btree ((f1 ~~ 'a'::text) WITH =)
  3553. DROP TABLE ataddindex;
  3554. CREATE TABLE ataddindex(id int, ref_id int);
  3555. ALTER TABLE ataddindex
  3556. ADD PRIMARY KEY (id),
  3557. ADD FOREIGN KEY (ref_id) REFERENCES ataddindex;
  3558. \d ataddindex
  3559. Table "public.ataddindex"
  3560. Column | Type | Collation | Nullable | Default
  3561. --------+---------+-----------+----------+---------
  3562. id | integer | | not null |
  3563. ref_id | integer | | |
  3564. Indexes:
  3565. "ataddindex_pkey" PRIMARY KEY, btree (id)
  3566. Foreign-key constraints:
  3567. "ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
  3568. Referenced by:
  3569. TABLE "ataddindex" CONSTRAINT "ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
  3570. DROP TABLE ataddindex;
  3571. CREATE TABLE ataddindex(id int, ref_id int);
  3572. ALTER TABLE ataddindex
  3573. ADD UNIQUE (id),
  3574. ADD FOREIGN KEY (ref_id) REFERENCES ataddindex (id);
  3575. \d ataddindex
  3576. Table "public.ataddindex"
  3577. Column | Type | Collation | Nullable | Default
  3578. --------+---------+-----------+----------+---------
  3579. id | integer | | |
  3580. ref_id | integer | | |
  3581. Indexes:
  3582. "ataddindex_id_key" UNIQUE CONSTRAINT, btree (id)
  3583. Foreign-key constraints:
  3584. "ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
  3585. Referenced by:
  3586. TABLE "ataddindex" CONSTRAINT "ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
  3587. DROP TABLE ataddindex;
  3588. -- unsupported constraint types for partitioned tables
  3589. CREATE TABLE partitioned (
  3590. a int,
  3591. b int
  3592. ) PARTITION BY RANGE (a, (a+b+1));
  3593. ALTER TABLE partitioned ADD EXCLUDE USING gist (a WITH &&);
  3594. ERROR: exclusion constraints are not supported on partitioned tables
  3595. LINE 1: ALTER TABLE partitioned ADD EXCLUDE USING gist (a WITH &&);
  3596. ^
  3597. -- cannot drop column that is part of the partition key
  3598. ALTER TABLE partitioned DROP COLUMN a;
  3599. ERROR: cannot drop column "a" because it is part of the partition key of relation "partitioned"
  3600. ALTER TABLE partitioned ALTER COLUMN a TYPE char(5);
  3601. ERROR: cannot alter column "a" because it is part of the partition key of relation "partitioned"
  3602. ALTER TABLE partitioned DROP COLUMN b;
  3603. ERROR: cannot drop column "b" because it is part of the partition key of relation "partitioned"
  3604. ALTER TABLE partitioned ALTER COLUMN b TYPE char(5);
  3605. ERROR: cannot alter column "b" because it is part of the partition key of relation "partitioned"
  3606. -- partitioned table cannot participate in regular inheritance
  3607. CREATE TABLE nonpartitioned (
  3608. a int,
  3609. b int
  3610. );
  3611. ALTER TABLE partitioned INHERIT nonpartitioned;
  3612. ERROR: cannot change inheritance of partitioned table
  3613. ALTER TABLE nonpartitioned INHERIT partitioned;
  3614. ERROR: cannot inherit from partitioned table "partitioned"
  3615. -- cannot add NO INHERIT constraint to partitioned tables
  3616. ALTER TABLE partitioned ADD CONSTRAINT chk_a CHECK (a > 0) NO INHERIT;
  3617. ERROR: cannot add NO INHERIT constraint to partitioned table "partitioned"
  3618. DROP TABLE partitioned, nonpartitioned;
  3619. --
  3620. -- ATTACH PARTITION
  3621. --
  3622. -- check that target table is partitioned
  3623. CREATE TABLE unparted (
  3624. a int
  3625. );
  3626. CREATE TABLE fail_part (like unparted);
  3627. ALTER TABLE unparted ATTACH PARTITION fail_part FOR VALUES IN ('a');
  3628. ERROR: table "unparted" is not partitioned
  3629. DROP TABLE unparted, fail_part;
  3630. -- check that partition bound is compatible
  3631. CREATE TABLE list_parted (
  3632. a int NOT NULL,
  3633. b char(2) COLLATE "C",
  3634. CONSTRAINT check_a CHECK (a > 0)
  3635. ) PARTITION BY LIST (a);
  3636. CREATE TABLE fail_part (LIKE list_parted);
  3637. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES FROM (1) TO (10);
  3638. ERROR: invalid bound specification for a list partition
  3639. LINE 1: ...list_parted ATTACH PARTITION fail_part FOR VALUES FROM (1) T...
  3640. ^
  3641. DROP TABLE fail_part;
  3642. -- check that the table being attached exists
  3643. ALTER TABLE list_parted ATTACH PARTITION nonexistent FOR VALUES IN (1);
  3644. ERROR: relation "nonexistent" does not exist
  3645. -- check ownership of the source table
  3646. CREATE ROLE regress_test_me;
  3647. CREATE ROLE regress_test_not_me;
  3648. CREATE TABLE not_owned_by_me (LIKE list_parted);
  3649. ALTER TABLE not_owned_by_me OWNER TO regress_test_not_me;
  3650. SET SESSION AUTHORIZATION regress_test_me;
  3651. CREATE TABLE owned_by_me (
  3652. a int
  3653. ) PARTITION BY LIST (a);
  3654. ALTER TABLE owned_by_me ATTACH PARTITION not_owned_by_me FOR VALUES IN (1);
  3655. ERROR: must be owner of table not_owned_by_me
  3656. RESET SESSION AUTHORIZATION;
  3657. DROP TABLE owned_by_me, not_owned_by_me;
  3658. DROP ROLE regress_test_not_me;
  3659. DROP ROLE regress_test_me;
  3660. -- check that the table being attached is not part of regular inheritance
  3661. CREATE TABLE parent (LIKE list_parted);
  3662. CREATE TABLE child () INHERITS (parent);
  3663. ALTER TABLE list_parted ATTACH PARTITION child FOR VALUES IN (1);
  3664. ERROR: cannot attach inheritance child as partition
  3665. ALTER TABLE list_parted ATTACH PARTITION parent FOR VALUES IN (1);
  3666. ERROR: cannot attach inheritance parent as partition
  3667. DROP TABLE parent CASCADE;
  3668. NOTICE: drop cascades to table child
  3669. -- check any TEMP-ness
  3670. CREATE TEMP TABLE temp_parted (a int) PARTITION BY LIST (a);
  3671. CREATE TABLE perm_part (a int);
  3672. ALTER TABLE temp_parted ATTACH PARTITION perm_part FOR VALUES IN (1);
  3673. ERROR: cannot attach a permanent relation as partition of temporary relation "temp_parted"
  3674. DROP TABLE temp_parted, perm_part;
  3675. -- check that the table being attached is not a typed table
  3676. CREATE TYPE mytype AS (a int);
  3677. CREATE TABLE fail_part OF mytype;
  3678. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3679. ERROR: cannot attach a typed table as partition
  3680. DROP TYPE mytype CASCADE;
  3681. NOTICE: drop cascades to table fail_part
  3682. -- check that the table being attached has only columns present in the parent
  3683. CREATE TABLE fail_part (like list_parted, c int);
  3684. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3685. ERROR: table "fail_part" contains column "c" not found in parent "list_parted"
  3686. DETAIL: The new partition may contain only the columns present in parent.
  3687. DROP TABLE fail_part;
  3688. -- check that the table being attached has every column of the parent
  3689. CREATE TABLE fail_part (a int NOT NULL);
  3690. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3691. ERROR: child table is missing column "b"
  3692. DROP TABLE fail_part;
  3693. -- check that columns match in type, collation and NOT NULL status
  3694. CREATE TABLE fail_part (
  3695. b char(3),
  3696. a int NOT NULL
  3697. );
  3698. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3699. ERROR: child table "fail_part" has different type for column "b"
  3700. ALTER TABLE fail_part ALTER b TYPE char (2) COLLATE "POSIX";
  3701. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3702. ERROR: child table "fail_part" has different collation for column "b"
  3703. DROP TABLE fail_part;
  3704. -- check that the table being attached has all constraints of the parent
  3705. CREATE TABLE fail_part (
  3706. b char(2) COLLATE "C",
  3707. a int NOT NULL
  3708. );
  3709. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3710. ERROR: child table is missing constraint "check_a"
  3711. -- check that the constraint matches in definition with parent's constraint
  3712. ALTER TABLE fail_part ADD CONSTRAINT check_a CHECK (a >= 0);
  3713. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3714. ERROR: child table "fail_part" has different definition for check constraint "check_a"
  3715. DROP TABLE fail_part;
  3716. -- check the attributes and constraints after partition is attached
  3717. CREATE TABLE part_1 (
  3718. a int NOT NULL,
  3719. b char(2) COLLATE "C",
  3720. CONSTRAINT check_a CHECK (a > 0)
  3721. );
  3722. ALTER TABLE list_parted ATTACH PARTITION part_1 FOR VALUES IN (1);
  3723. -- attislocal and conislocal are always false for merged attributes and constraints respectively.
  3724. SELECT attislocal, attinhcount FROM pg_attribute WHERE attrelid = 'part_1'::regclass AND attnum > 0;
  3725. attislocal | attinhcount
  3726. ------------+-------------
  3727. f | 1
  3728. f | 1
  3729. (2 rows)
  3730. SELECT conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_1'::regclass AND conname = 'check_a';
  3731. conislocal | coninhcount
  3732. ------------+-------------
  3733. f | 1
  3734. (1 row)
  3735. -- check that the new partition won't overlap with an existing partition
  3736. CREATE TABLE fail_part (LIKE part_1 INCLUDING CONSTRAINTS);
  3737. ALTER TABLE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3738. ERROR: partition "fail_part" would overlap partition "part_1"
  3739. LINE 1: ...LE list_parted ATTACH PARTITION fail_part FOR VALUES IN (1);
  3740. ^
  3741. DROP TABLE fail_part;
  3742. -- check that an existing table can be attached as a default partition
  3743. CREATE TABLE def_part (LIKE list_parted INCLUDING CONSTRAINTS);
  3744. ALTER TABLE list_parted ATTACH PARTITION def_part DEFAULT;
  3745. -- check attaching default partition fails if a default partition already
  3746. -- exists
  3747. CREATE TABLE fail_def_part (LIKE part_1 INCLUDING CONSTRAINTS);
  3748. ALTER TABLE list_parted ATTACH PARTITION fail_def_part DEFAULT;
  3749. ERROR: partition "fail_def_part" conflicts with existing default partition "def_part"
  3750. LINE 1: ...ER TABLE list_parted ATTACH PARTITION fail_def_part DEFAULT;
  3751. ^
  3752. -- check validation when attaching list partitions
  3753. CREATE TABLE list_parted2 (
  3754. a int,
  3755. b char
  3756. ) PARTITION BY LIST (a);
  3757. -- check that violating rows are correctly reported
  3758. CREATE TABLE part_2 (LIKE list_parted2);
  3759. INSERT INTO part_2 VALUES (3, 'a');
  3760. ALTER TABLE list_parted2 ATTACH PARTITION part_2 FOR VALUES IN (2);
  3761. ERROR: partition constraint of relation "part_2" is violated by some row
  3762. -- should be ok after deleting the bad row
  3763. DELETE FROM part_2;
  3764. ALTER TABLE list_parted2 ATTACH PARTITION part_2 FOR VALUES IN (2);
  3765. -- check partition cannot be attached if default has some row for its values
  3766. CREATE TABLE list_parted2_def PARTITION OF list_parted2 DEFAULT;
  3767. INSERT INTO list_parted2_def VALUES (11, 'z');
  3768. CREATE TABLE part_3 (LIKE list_parted2);
  3769. ALTER TABLE list_parted2 ATTACH PARTITION part_3 FOR VALUES IN (11);
  3770. ERROR: updated partition constraint for default partition "list_parted2_def" would be violated by some row
  3771. -- should be ok after deleting the bad row
  3772. DELETE FROM list_parted2_def WHERE a = 11;
  3773. ALTER TABLE list_parted2 ATTACH PARTITION part_3 FOR VALUES IN (11);
  3774. -- adding constraints that describe the desired partition constraint
  3775. -- (or more restrictive) will help skip the validation scan
  3776. CREATE TABLE part_3_4 (
  3777. LIKE list_parted2,
  3778. CONSTRAINT check_a CHECK (a IN (3))
  3779. );
  3780. -- however, if a list partition does not accept nulls, there should be
  3781. -- an explicit NOT NULL constraint on the partition key column for the
  3782. -- validation scan to be skipped;
  3783. ALTER TABLE list_parted2 ATTACH PARTITION part_3_4 FOR VALUES IN (3, 4);
  3784. -- adding a NOT NULL constraint will cause the scan to be skipped
  3785. ALTER TABLE list_parted2 DETACH PARTITION part_3_4;
  3786. ALTER TABLE part_3_4 ALTER a SET NOT NULL;
  3787. ALTER TABLE list_parted2 ATTACH PARTITION part_3_4 FOR VALUES IN (3, 4);
  3788. -- check if default partition scan skipped
  3789. ALTER TABLE list_parted2_def ADD CONSTRAINT check_a CHECK (a IN (5, 6));
  3790. CREATE TABLE part_55_66 PARTITION OF list_parted2 FOR VALUES IN (55, 66);
  3791. -- check validation when attaching range partitions
  3792. CREATE TABLE range_parted (
  3793. a int,
  3794. b int
  3795. ) PARTITION BY RANGE (a, b);
  3796. -- check that violating rows are correctly reported
  3797. CREATE TABLE part1 (
  3798. a int NOT NULL CHECK (a = 1),
  3799. b int NOT NULL CHECK (b >= 1 AND b <= 10)
  3800. );
  3801. INSERT INTO part1 VALUES (1, 10);
  3802. -- Remember the TO bound is exclusive
  3803. ALTER TABLE range_parted ATTACH PARTITION part1 FOR VALUES FROM (1, 1) TO (1, 10);
  3804. ERROR: partition constraint of relation "part1" is violated by some row
  3805. -- should be ok after deleting the bad row
  3806. DELETE FROM part1;
  3807. ALTER TABLE range_parted ATTACH PARTITION part1 FOR VALUES FROM (1, 1) TO (1, 10);
  3808. -- adding constraints that describe the desired partition constraint
  3809. -- (or more restrictive) will help skip the validation scan
  3810. CREATE TABLE part2 (
  3811. a int NOT NULL CHECK (a = 1),
  3812. b int NOT NULL CHECK (b >= 10 AND b < 18)
  3813. );
  3814. ALTER TABLE range_parted ATTACH PARTITION part2 FOR VALUES FROM (1, 10) TO (1, 20);
  3815. -- Create default partition
  3816. CREATE TABLE partr_def1 PARTITION OF range_parted DEFAULT;
  3817. -- Only one default partition is allowed, hence, following should give error
  3818. CREATE TABLE partr_def2 (LIKE part1 INCLUDING CONSTRAINTS);
  3819. ALTER TABLE range_parted ATTACH PARTITION partr_def2 DEFAULT;
  3820. ERROR: partition "partr_def2" conflicts with existing default partition "partr_def1"
  3821. LINE 1: ...LTER TABLE range_parted ATTACH PARTITION partr_def2 DEFAULT;
  3822. ^
  3823. -- Overlapping partitions cannot be attached, hence, following should give error
  3824. INSERT INTO partr_def1 VALUES (2, 10);
  3825. CREATE TABLE part3 (LIKE range_parted);
  3826. ALTER TABLE range_parted ATTACH partition part3 FOR VALUES FROM (2, 10) TO (2, 20);
  3827. ERROR: updated partition constraint for default partition "partr_def1" would be violated by some row
  3828. -- Attaching partitions should be successful when there are no overlapping rows
  3829. ALTER TABLE range_parted ATTACH partition part3 FOR VALUES FROM (3, 10) TO (3, 20);
  3830. -- check that leaf partitions are scanned when attaching a partitioned
  3831. -- table
  3832. CREATE TABLE part_5 (
  3833. LIKE list_parted2
  3834. ) PARTITION BY LIST (b);
  3835. -- check that violating rows are correctly reported
  3836. CREATE TABLE part_5_a PARTITION OF part_5 FOR VALUES IN ('a');
  3837. INSERT INTO part_5_a (a, b) VALUES (6, 'a');
  3838. ALTER TABLE list_parted2 ATTACH PARTITION part_5 FOR VALUES IN (5);
  3839. ERROR: partition constraint of relation "part_5_a" is violated by some row
  3840. -- delete the faulting row and also add a constraint to skip the scan
  3841. DELETE FROM part_5_a WHERE a NOT IN (3);
  3842. ALTER TABLE part_5 ADD CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 5);
  3843. ALTER TABLE list_parted2 ATTACH PARTITION part_5 FOR VALUES IN (5);
  3844. ALTER TABLE list_parted2 DETACH PARTITION part_5;
  3845. ALTER TABLE part_5 DROP CONSTRAINT check_a;
  3846. -- scan should again be skipped, even though NOT NULL is now a column property
  3847. ALTER TABLE part_5 ADD CONSTRAINT check_a CHECK (a IN (5)), ALTER a SET NOT NULL;
  3848. ALTER TABLE list_parted2 ATTACH PARTITION part_5 FOR VALUES IN (5);
  3849. -- Check the case where attnos of the partitioning columns in the table being
  3850. -- attached differs from the parent. It should not affect the constraint-
  3851. -- checking logic that allows to skip the scan.
  3852. CREATE TABLE part_6 (
  3853. c int,
  3854. LIKE list_parted2,
  3855. CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 6)
  3856. );
  3857. ALTER TABLE part_6 DROP c;
  3858. ALTER TABLE list_parted2 ATTACH PARTITION part_6 FOR VALUES IN (6);
  3859. -- Similar to above, but the table being attached is a partitioned table
  3860. -- whose partition has still different attnos for the root partitioning
  3861. -- columns.
  3862. CREATE TABLE part_7 (
  3863. LIKE list_parted2,
  3864. CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 7)
  3865. ) PARTITION BY LIST (b);
  3866. CREATE TABLE part_7_a_null (
  3867. c int,
  3868. d int,
  3869. e int,
  3870. LIKE list_parted2, -- 'a' will have attnum = 4
  3871. CONSTRAINT check_b CHECK (b IS NULL OR b = 'a'),
  3872. CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 7)
  3873. );
  3874. ALTER TABLE part_7_a_null DROP c, DROP d, DROP e;
  3875. ALTER TABLE part_7 ATTACH PARTITION part_7_a_null FOR VALUES IN ('a', null);
  3876. ALTER TABLE list_parted2 ATTACH PARTITION part_7 FOR VALUES IN (7);
  3877. -- Same example, but check this time that the constraint correctly detects
  3878. -- violating rows
  3879. ALTER TABLE list_parted2 DETACH PARTITION part_7;
  3880. ALTER TABLE part_7 DROP CONSTRAINT check_a; -- thusly, scan won't be skipped
  3881. INSERT INTO part_7 (a, b) VALUES (8, null), (9, 'a');
  3882. SELECT tableoid::regclass, a, b FROM part_7 order by a;
  3883. tableoid | a | b
  3884. ---------------+---+---
  3885. part_7_a_null | 8 |
  3886. part_7_a_null | 9 | a
  3887. (2 rows)
  3888. ALTER TABLE list_parted2 ATTACH PARTITION part_7 FOR VALUES IN (7);
  3889. ERROR: partition constraint of relation "part_7_a_null" is violated by some row
  3890. -- check that leaf partitions of default partition are scanned when
  3891. -- attaching a partitioned table.
  3892. ALTER TABLE part_5 DROP CONSTRAINT check_a;
  3893. CREATE TABLE part5_def PARTITION OF part_5 DEFAULT PARTITION BY LIST(a);
  3894. CREATE TABLE part5_def_p1 PARTITION OF part5_def FOR VALUES IN (5);
  3895. INSERT INTO part5_def_p1 VALUES (5, 'y');
  3896. CREATE TABLE part5_p1 (LIKE part_5);
  3897. ALTER TABLE part_5 ATTACH PARTITION part5_p1 FOR VALUES IN ('y');
  3898. ERROR: updated partition constraint for default partition "part5_def_p1" would be violated by some row
  3899. -- should be ok after deleting the bad row
  3900. DELETE FROM part5_def_p1 WHERE b = 'y';
  3901. ALTER TABLE part_5 ATTACH PARTITION part5_p1 FOR VALUES IN ('y');
  3902. -- check that the table being attached is not already a partition
  3903. ALTER TABLE list_parted2 ATTACH PARTITION part_2 FOR VALUES IN (2);
  3904. ERROR: "part_2" is already a partition
  3905. -- check that circular inheritance is not allowed
  3906. ALTER TABLE part_5 ATTACH PARTITION list_parted2 FOR VALUES IN ('b');
  3907. ERROR: circular inheritance not allowed
  3908. DETAIL: "part_5" is already a child of "list_parted2".
  3909. ALTER TABLE list_parted2 ATTACH PARTITION list_parted2 FOR VALUES IN (0);
  3910. ERROR: circular inheritance not allowed
  3911. DETAIL: "list_parted2" is already a child of "list_parted2".
  3912. -- If a partitioned table being created or an existing table being attached
  3913. -- as a partition does not have a constraint that would allow validation scan
  3914. -- to be skipped, but an individual partition does, then the partition's
  3915. -- validation scan is skipped.
  3916. CREATE TABLE quuux (a int, b text) PARTITION BY LIST (a);
  3917. CREATE TABLE quuux_default PARTITION OF quuux DEFAULT PARTITION BY LIST (b);
  3918. CREATE TABLE quuux_default1 PARTITION OF quuux_default (
  3919. CONSTRAINT check_1 CHECK (a IS NOT NULL AND a = 1)
  3920. ) FOR VALUES IN ('b');
  3921. CREATE TABLE quuux1 (a int, b text);
  3922. ALTER TABLE quuux ATTACH PARTITION quuux1 FOR VALUES IN (1); -- validate!
  3923. CREATE TABLE quuux2 (a int, b text);
  3924. ALTER TABLE quuux ATTACH PARTITION quuux2 FOR VALUES IN (2); -- skip validation
  3925. DROP TABLE quuux1, quuux2;
  3926. -- should validate for quuux1, but not for quuux2
  3927. CREATE TABLE quuux1 PARTITION OF quuux FOR VALUES IN (1);
  3928. CREATE TABLE quuux2 PARTITION OF quuux FOR VALUES IN (2);
  3929. DROP TABLE quuux;
  3930. -- check validation when attaching hash partitions
  3931. -- Use hand-rolled hash functions and operator class to get predictable result
  3932. -- on different machines. part_test_int4_ops is defined in insert.sql.
  3933. -- check that the new partition won't overlap with an existing partition
  3934. CREATE TABLE hash_parted (
  3935. a int,
  3936. b int
  3937. ) PARTITION BY HASH (a part_test_int4_ops);
  3938. CREATE TABLE hpart_1 PARTITION OF hash_parted FOR VALUES WITH (MODULUS 4, REMAINDER 0);
  3939. CREATE TABLE fail_part (LIKE hpart_1);
  3940. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, REMAINDER 4);
  3941. ERROR: partition "fail_part" would overlap partition "hpart_1"
  3942. LINE 1: ...hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODU...
  3943. ^
  3944. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, REMAINDER 0);
  3945. ERROR: partition "fail_part" would overlap partition "hpart_1"
  3946. LINE 1: ...hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODU...
  3947. ^
  3948. DROP TABLE fail_part;
  3949. -- check validation when attaching hash partitions
  3950. -- check that violating rows are correctly reported
  3951. CREATE TABLE hpart_2 (LIKE hash_parted);
  3952. INSERT INTO hpart_2 VALUES (3, 0);
  3953. ALTER TABLE hash_parted ATTACH PARTITION hpart_2 FOR VALUES WITH (MODULUS 4, REMAINDER 1);
  3954. ERROR: partition constraint of relation "hpart_2" is violated by some row
  3955. -- should be ok after deleting the bad row
  3956. DELETE FROM hpart_2;
  3957. ALTER TABLE hash_parted ATTACH PARTITION hpart_2 FOR VALUES WITH (MODULUS 4, REMAINDER 1);
  3958. -- check that leaf partitions are scanned when attaching a partitioned
  3959. -- table
  3960. CREATE TABLE hpart_5 (
  3961. LIKE hash_parted
  3962. ) PARTITION BY LIST (b);
  3963. -- check that violating rows are correctly reported
  3964. CREATE TABLE hpart_5_a PARTITION OF hpart_5 FOR VALUES IN ('1', '2', '3');
  3965. INSERT INTO hpart_5_a (a, b) VALUES (7, 1);
  3966. ALTER TABLE hash_parted ATTACH PARTITION hpart_5 FOR VALUES WITH (MODULUS 4, REMAINDER 2);
  3967. ERROR: partition constraint of relation "hpart_5_a" is violated by some row
  3968. -- should be ok after deleting the bad row
  3969. DELETE FROM hpart_5_a;
  3970. ALTER TABLE hash_parted ATTACH PARTITION hpart_5 FOR VALUES WITH (MODULUS 4, REMAINDER 2);
  3971. -- check that the table being attach is with valid modulus and remainder value
  3972. CREATE TABLE fail_part(LIKE hash_parted);
  3973. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 0, REMAINDER 1);
  3974. ERROR: modulus for hash partition must be an integer value greater than zero
  3975. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, REMAINDER 8);
  3976. ERROR: remainder for hash partition must be less than modulus
  3977. ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 3, REMAINDER 2);
  3978. ERROR: every hash partition modulus must be a factor of the next larger modulus
  3979. DETAIL: The new modulus 3 is not a factor of 4, the modulus of existing partition "hpart_1".
  3980. DROP TABLE fail_part;
  3981. -- fails with incorrect object type
  3982. CREATE VIEW at_v1 AS SELECT 1 as a;
  3983. ALTER TABLE at_v1 ATTACH PARTITION dummy default;
  3984. ERROR: "at_v1" is not a table or partitioned index
  3985. DROP VIEW at_v1;
  3986. --
  3987. -- DETACH PARTITION
  3988. --
  3989. -- check that the table is partitioned at all
  3990. CREATE TABLE regular_table (a int);
  3991. ALTER TABLE regular_table DETACH PARTITION any_name;
  3992. ERROR: table "regular_table" is not partitioned
  3993. DROP TABLE regular_table;
  3994. -- check that the partition being detached exists at all
  3995. ALTER TABLE list_parted2 DETACH PARTITION part_4;
  3996. ERROR: relation "part_4" does not exist
  3997. ALTER TABLE hash_parted DETACH PARTITION hpart_4;
  3998. ERROR: relation "hpart_4" does not exist
  3999. -- check that the partition being detached is actually a partition of the parent
  4000. CREATE TABLE not_a_part (a int);
  4001. ALTER TABLE list_parted2 DETACH PARTITION not_a_part;
  4002. ERROR: relation "not_a_part" is not a partition of relation "list_parted2"
  4003. ALTER TABLE list_parted2 DETACH PARTITION part_1;
  4004. ERROR: relation "part_1" is not a partition of relation "list_parted2"
  4005. ALTER TABLE hash_parted DETACH PARTITION not_a_part;
  4006. ERROR: relation "not_a_part" is not a partition of relation "hash_parted"
  4007. DROP TABLE not_a_part;
  4008. -- check that, after being detached, attinhcount/coninhcount is dropped to 0 and
  4009. -- attislocal/conislocal is set to true
  4010. ALTER TABLE list_parted2 DETACH PARTITION part_3_4;
  4011. SELECT attinhcount, attislocal FROM pg_attribute WHERE attrelid = 'part_3_4'::regclass AND attnum > 0;
  4012. attinhcount | attislocal
  4013. -------------+------------
  4014. 0 | t
  4015. 0 | t
  4016. (2 rows)
  4017. SELECT coninhcount, conislocal FROM pg_constraint WHERE conrelid = 'part_3_4'::regclass AND conname = 'check_a';
  4018. coninhcount | conislocal
  4019. -------------+------------
  4020. 0 | t
  4021. (1 row)
  4022. DROP TABLE part_3_4;
  4023. -- check that a detached partition is not dropped on dropping a partitioned table
  4024. CREATE TABLE range_parted2 (
  4025. a int
  4026. ) PARTITION BY RANGE(a);
  4027. CREATE TABLE part_rp PARTITION OF range_parted2 FOR VALUES FROM (0) to (100);
  4028. ALTER TABLE range_parted2 DETACH PARTITION part_rp;
  4029. DROP TABLE range_parted2;
  4030. SELECT * from part_rp;
  4031. a
  4032. ---
  4033. (0 rows)
  4034. DROP TABLE part_rp;
  4035. -- concurrent detach
  4036. CREATE TABLE range_parted2 (
  4037. a int
  4038. ) PARTITION BY RANGE(a);
  4039. CREATE TABLE part_rp PARTITION OF range_parted2 FOR VALUES FROM (0) to (100);
  4040. BEGIN;
  4041. -- doesn't work in a partition block
  4042. ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
  4043. ERROR: ALTER TABLE ... DETACH CONCURRENTLY cannot run inside a transaction block
  4044. COMMIT;
  4045. CREATE TABLE part_rpd PARTITION OF range_parted2 DEFAULT;
  4046. -- doesn't work if there's a default partition
  4047. ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
  4048. ERROR: cannot detach partitions concurrently when a default partition exists
  4049. -- doesn't work for the default partition
  4050. ALTER TABLE range_parted2 DETACH PARTITION part_rpd CONCURRENTLY;
  4051. ERROR: cannot detach partitions concurrently when a default partition exists
  4052. DROP TABLE part_rpd;
  4053. -- works fine
  4054. ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
  4055. \d+ range_parted2
  4056. Partitioned table "public.range_parted2"
  4057. Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
  4058. --------+---------+-----------+----------+---------+---------+--------------+-------------
  4059. a | integer | | | | plain | |
  4060. Partition key: RANGE (a)
  4061. Number of partitions: 0
  4062. -- constraint should be created
  4063. \d part_rp
  4064. Table "public.part_rp"
  4065. Column | Type | Collation | Nullable | Default
  4066. --------+---------+-----------+----------+---------
  4067. a | integer | | |
  4068. Check constraints:
  4069. "part_rp_a_check" CHECK (a IS NOT NULL AND a >= 0 AND a < 100)
  4070. 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);
  4071. ALTER TABLE range_parted2 DETACH PARTITION part_rp100 CONCURRENTLY;
  4072. -- redundant constraint should not be created
  4073. \d part_rp100
  4074. Table "public.part_rp100"
  4075. Column | Type | Collation | Nullable | Default
  4076. --------+---------+-----------+----------+---------
  4077. a | integer | | |
  4078. Check constraints:
  4079. "part_rp100_a_check" CHECK (a >= 123 AND a < 133 AND a IS NOT NULL)
  4080. DROP TABLE range_parted2;
  4081. -- Check ALTER TABLE commands for partitioned tables and partitions
  4082. -- cannot add/drop column to/from *only* the parent
  4083. ALTER TABLE ONLY list_parted2 ADD COLUMN c int;
  4084. ERROR: column must be added to child tables too
  4085. ALTER TABLE ONLY list_parted2 DROP COLUMN b;
  4086. ERROR: cannot drop column from only the partitioned table when partitions exist
  4087. HINT: Do not specify the ONLY keyword.
  4088. -- cannot add a column to partition or drop an inherited one
  4089. ALTER TABLE part_2 ADD COLUMN c text;
  4090. ERROR: cannot add column to a partition
  4091. ALTER TABLE part_2 DROP COLUMN b;
  4092. ERROR: cannot drop inherited column "b"
  4093. -- Nor rename, alter type
  4094. ALTER TABLE part_2 RENAME COLUMN b to c;
  4095. ERROR: cannot rename inherited column "b"
  4096. ALTER TABLE part_2 ALTER COLUMN b TYPE text;
  4097. ERROR: cannot alter inherited column "b"
  4098. -- cannot add/drop NOT NULL or check constraints to *only* the parent, when
  4099. -- partitions exist
  4100. ALTER TABLE ONLY list_parted2 ALTER b SET NOT NULL;
  4101. ERROR: constraint must be added to child tables too
  4102. DETAIL: Column "b" of relation "part_2" is not already NOT NULL.
  4103. HINT: Do not specify the ONLY keyword.
  4104. ALTER TABLE ONLY list_parted2 ADD CONSTRAINT check_b CHECK (b <> 'zz');
  4105. ERROR: constraint must be added to child tables too
  4106. ALTER TABLE list_parted2 ALTER b SET NOT NULL;
  4107. ALTER TABLE ONLY list_parted2 ALTER b DROP NOT NULL;
  4108. ERROR: cannot remove constraint from only the partitioned table when partitions exist
  4109. HINT: Do not specify the ONLY keyword.
  4110. ALTER TABLE list_parted2 ADD CONSTRAINT check_b CHECK (b <> 'zz');
  4111. ALTER TABLE ONLY list_parted2 DROP CONSTRAINT check_b;
  4112. ERROR: cannot remove constraint from only the partitioned table when partitions exist
  4113. HINT: Do not specify the ONLY keyword.
  4114. -- It's alright though, if no partitions are yet created
  4115. CREATE TABLE parted_no_parts (a int) PARTITION BY LIST (a);
  4116. ALTER TABLE ONLY parted_no_parts ALTER a SET NOT NULL;
  4117. ALTER TABLE ONLY parted_no_parts ADD CONSTRAINT check_a CHECK (a > 0);
  4118. ALTER TABLE ONLY parted_no_parts ALTER a DROP NOT NULL;
  4119. ALTER TABLE ONLY parted_no_parts DROP CONSTRAINT check_a;
  4120. DROP TABLE parted_no_parts;
  4121. -- cannot drop inherited NOT NULL or check constraints from partition
  4122. ALTER TABLE list_parted2 ALTER b SET NOT NULL, ADD CONSTRAINT check_a2 CHECK (a > 0);
  4123. ALTER TABLE part_2 ALTER b DROP NOT NULL;
  4124. ERROR: column "b" is marked NOT NULL in parent table
  4125. ALTER TABLE part_2 DROP CONSTRAINT check_a2;
  4126. ERROR: cannot drop inherited constraint "check_a2" of relation "part_2"
  4127. -- Doesn't make sense to add NO INHERIT constraints on partitioned tables
  4128. ALTER TABLE list_parted2 add constraint check_b2 check (b <> 'zz') NO INHERIT;
  4129. ERROR: cannot add NO INHERIT constraint to partitioned table "list_parted2"
  4130. -- check that a partition cannot participate in regular inheritance
  4131. CREATE TABLE inh_test () INHERITS (part_2);
  4132. ERROR: cannot inherit from partition "part_2"
  4133. CREATE TABLE inh_test (LIKE part_2);
  4134. ALTER TABLE inh_test INHERIT part_2;
  4135. ERROR: cannot inherit from a partition
  4136. ALTER TABLE part_2 INHERIT inh_test;
  4137. ERROR: cannot change inheritance of a partition
  4138. -- cannot drop or alter type of partition key columns of lower level
  4139. -- partitioned tables; for example, part_5, which is list_parted2's
  4140. -- partition, is partitioned on b;
  4141. ALTER TABLE list_parted2 DROP COLUMN b;
  4142. ERROR: cannot drop column "b" because it is part of the partition key of relation "part_5"
  4143. ALTER TABLE list_parted2 ALTER COLUMN b TYPE text;
  4144. ERROR: cannot alter column "b" because it is part of the partition key of relation "part_5"
  4145. -- dropping non-partition key columns should be allowed on the parent table.
  4146. ALTER TABLE list_parted DROP COLUMN b;
  4147. SELECT * FROM list_parted;
  4148. a
  4149. ---
  4150. (0 rows)
  4151. -- cleanup
  4152. DROP TABLE list_parted, list_parted2, range_parted;
  4153. DROP TABLE fail_def_part;
  4154. DROP TABLE hash_parted;
  4155. -- more tests for certain multi-level partitioning scenarios
  4156. create table p (a int, b int) partition by range (a, b);
  4157. create table p1 (b int, a int not null) partition by range (b);
  4158. create table p11 (like p1);
  4159. alter table p11 drop a;
  4160. alter table p11 add a int;
  4161. alter table p11 drop a;
  4162. alter table p11 add a int not null;
  4163. -- attnum for key attribute 'a' is different in p, p1, and p11
  4164. select attrelid::regclass, attname, attnum
  4165. from pg_attribute
  4166. where attname = 'a'
  4167. and (attrelid = 'p'::regclass
  4168. or attrelid = 'p1'::regclass
  4169. or attrelid = 'p11'::regclass)
  4170. order by attrelid::regclass::text;
  4171. attrelid | attname | attnum
  4172. ----------+---------+--------
  4173. p | a | 1
  4174. p1 | a | 2
  4175. p11 | a | 4
  4176. (3 rows)
  4177. alter table p1 attach partition p11 for values from (2) to (5);
  4178. insert into p1 (a, b) values (2, 3);
  4179. -- check that partition validation scan correctly detects violating rows
  4180. alter table p attach partition p1 for values from (1, 2) to (1, 10);
  4181. ERROR: partition constraint of relation "p11" is violated by some row
  4182. -- cleanup
  4183. drop table p;
  4184. drop table p1;
  4185. -- validate constraint on partitioned tables should only scan leaf partitions
  4186. create table parted_validate_test (a int) partition by list (a);
  4187. create table parted_validate_test_1 partition of parted_validate_test for values in (0, 1);
  4188. alter table parted_validate_test add constraint parted_validate_test_chka check (a > 0) not valid;
  4189. alter table parted_validate_test validate constraint parted_validate_test_chka;
  4190. drop table parted_validate_test;
  4191. -- test alter column options
  4192. CREATE TABLE attmp(i integer);
  4193. INSERT INTO attmp VALUES (1);
  4194. ALTER TABLE attmp ALTER COLUMN i SET (n_distinct = 1, n_distinct_inherited = 2);
  4195. ALTER TABLE attmp ALTER COLUMN i RESET (n_distinct_inherited);
  4196. ANALYZE attmp;
  4197. DROP TABLE attmp;
  4198. DROP USER regress_alter_table_user1;
  4199. -- check that violating rows are correctly reported when attaching as the
  4200. -- default partition
  4201. create table defpart_attach_test (a int) partition by list (a);
  4202. create table defpart_attach_test1 partition of defpart_attach_test for values in (1);
  4203. create table defpart_attach_test_d (b int, a int);
  4204. alter table defpart_attach_test_d drop b;
  4205. insert into defpart_attach_test_d values (1), (2);
  4206. -- error because its constraint as the default partition would be violated
  4207. -- by the row containing 1
  4208. alter table defpart_attach_test attach partition defpart_attach_test_d default;
  4209. ERROR: partition constraint of relation "defpart_attach_test_d" is violated by some row
  4210. delete from defpart_attach_test_d where a = 1;
  4211. alter table defpart_attach_test_d add check (a > 1);
  4212. -- should be attached successfully and without needing to be scanned
  4213. alter table defpart_attach_test attach partition defpart_attach_test_d default;
  4214. -- check that attaching a partition correctly reports any rows in the default
  4215. -- partition that should not be there for the new partition to be attached
  4216. -- successfully
  4217. create table defpart_attach_test_2 (like defpart_attach_test_d);
  4218. alter table defpart_attach_test attach partition defpart_attach_test_2 for values in (2);
  4219. ERROR: updated partition constraint for default partition "defpart_attach_test_d" would be violated by some row
  4220. drop table defpart_attach_test;
  4221. -- check combinations of temporary and permanent relations when attaching
  4222. -- partitions.
  4223. create table perm_part_parent (a int) partition by list (a);
  4224. create temp table temp_part_parent (a int) partition by list (a);
  4225. create table perm_part_child (a int);
  4226. create temp table temp_part_child (a int);
  4227. alter table temp_part_parent attach partition perm_part_child default; -- error
  4228. ERROR: cannot attach a permanent relation as partition of temporary relation "temp_part_parent"
  4229. alter table perm_part_parent attach partition temp_part_child default; -- error
  4230. ERROR: cannot attach a temporary relation as partition of permanent relation "perm_part_parent"
  4231. alter table temp_part_parent attach partition temp_part_child default; -- ok
  4232. drop table perm_part_parent cascade;
  4233. drop table temp_part_parent cascade;
  4234. -- check that attaching partitions to a table while it is being used is
  4235. -- prevented
  4236. create table tab_part_attach (a int) partition by list (a);
  4237. create or replace function func_part_attach() returns trigger
  4238. language plpgsql as $$
  4239. begin
  4240. execute 'create table tab_part_attach_1 (a int)';
  4241. execute 'alter table tab_part_attach attach partition tab_part_attach_1 for values in (1)';
  4242. return null;
  4243. end $$;
  4244. create trigger trig_part_attach before insert on tab_part_attach
  4245. for each statement execute procedure func_part_attach();
  4246. insert into tab_part_attach values (1);
  4247. ERROR: cannot ALTER TABLE "tab_part_attach" because it is being used by active queries in this session
  4248. CONTEXT: SQL statement "alter table tab_part_attach attach partition tab_part_attach_1 for values in (1)"
  4249. PL/pgSQL function func_part_attach() line 4 at EXECUTE
  4250. drop table tab_part_attach;
  4251. drop function func_part_attach();
  4252. -- test case where the partitioning operator is a SQL function whose
  4253. -- evaluation results in the table's relcache being rebuilt partway through
  4254. -- the execution of an ATTACH PARTITION command
  4255. create function at_test_sql_partop (int4, int4) returns int language sql
  4256. as $$ select case when $1 = $2 then 0 when $1 > $2 then 1 else -1 end; $$;
  4257. create operator class at_test_sql_partop for type int4 using btree as
  4258. operator 1 < (int4, int4), operator 2 <= (int4, int4),
  4259. operator 3 = (int4, int4), operator 4 >= (int4, int4),
  4260. operator 5 > (int4, int4), function 1 at_test_sql_partop(int4, int4);
  4261. create table at_test_sql_partop (a int) partition by range (a at_test_sql_partop);
  4262. create table at_test_sql_partop_1 (a int);
  4263. alter table at_test_sql_partop attach partition at_test_sql_partop_1 for values from (0) to (10);
  4264. drop table at_test_sql_partop;
  4265. drop operator class at_test_sql_partop using btree;
  4266. drop function at_test_sql_partop;
  4267. /* Test case for bug #16242 */
  4268. -- We create a parent and child where the child has missing
  4269. -- non-null attribute values, and arrange to pass them through
  4270. -- tuple conversion from the child to the parent tupdesc
  4271. create table bar1 (a integer, b integer not null default 1)
  4272. partition by range (a);
  4273. create table bar2 (a integer);
  4274. insert into bar2 values (1);
  4275. alter table bar2 add column b integer not null default 1;
  4276. -- (at this point bar2 contains tuple with natts=1)
  4277. alter table bar1 attach partition bar2 default;
  4278. -- this works:
  4279. select * from bar1;
  4280. a | b
  4281. ---+---
  4282. 1 | 1
  4283. (1 row)
  4284. -- this exercises tuple conversion:
  4285. create function xtrig()
  4286. returns trigger language plpgsql
  4287. as $$
  4288. declare
  4289. r record;
  4290. begin
  4291. for r in select * from old loop
  4292. raise info 'a=%, b=%', r.a, r.b;
  4293. end loop;
  4294. return NULL;
  4295. end;
  4296. $$;
  4297. create trigger xtrig
  4298. after update on bar1
  4299. referencing old table as old
  4300. for each statement execute procedure xtrig();
  4301. update bar1 set a = a + 1;
  4302. INFO: a=1, b=1
  4303. /* End test case for bug #16242 */
  4304. /* Test case for bug #17409 */
  4305. create table attbl (p1 int constraint pk_attbl primary key);
  4306. create table atref (c1 int references attbl(p1));
  4307. cluster attbl using pk_attbl;
  4308. alter table attbl alter column p1 set data type bigint;
  4309. alter table atref alter column c1 set data type bigint;
  4310. drop table attbl, atref;
  4311. create table attbl (p1 int constraint pk_attbl primary key);
  4312. alter table attbl replica identity using index pk_attbl;
  4313. create table atref (c1 int references attbl(p1));
  4314. alter table attbl alter column p1 set data type bigint;
  4315. alter table atref alter column c1 set data type bigint;
  4316. drop table attbl, atref;
  4317. /* End test case for bug #17409 */
  4318. -- Test that ALTER TABLE rewrite preserves a clustered index
  4319. -- for normal indexes and indexes on constraints.
  4320. create table alttype_cluster (a int);
  4321. alter table alttype_cluster add primary key (a);
  4322. create index alttype_cluster_ind on alttype_cluster (a);
  4323. alter table alttype_cluster cluster on alttype_cluster_ind;
  4324. -- Normal index remains clustered.
  4325. select indexrelid::regclass, indisclustered from pg_index
  4326. where indrelid = 'alttype_cluster'::regclass
  4327. order by indexrelid::regclass::text;
  4328. indexrelid | indisclustered
  4329. ----------------------+----------------
  4330. alttype_cluster_ind | t
  4331. alttype_cluster_pkey | f
  4332. (2 rows)
  4333. alter table alttype_cluster alter a type bigint;
  4334. select indexrelid::regclass, indisclustered from pg_index
  4335. where indrelid = 'alttype_cluster'::regclass
  4336. order by indexrelid::regclass::text;
  4337. indexrelid | indisclustered
  4338. ----------------------+----------------
  4339. alttype_cluster_ind | t
  4340. alttype_cluster_pkey | f
  4341. (2 rows)
  4342. -- Constraint index remains clustered.
  4343. alter table alttype_cluster cluster on alttype_cluster_pkey;
  4344. select indexrelid::regclass, indisclustered from pg_index
  4345. where indrelid = 'alttype_cluster'::regclass
  4346. order by indexrelid::regclass::text;
  4347. indexrelid | indisclustered
  4348. ----------------------+----------------
  4349. alttype_cluster_ind | f
  4350. alttype_cluster_pkey | t
  4351. (2 rows)
  4352. alter table alttype_cluster alter a type int;
  4353. select indexrelid::regclass, indisclustered from pg_index
  4354. where indrelid = 'alttype_cluster'::regclass
  4355. order by indexrelid::regclass::text;
  4356. indexrelid | indisclustered
  4357. ----------------------+----------------
  4358. alttype_cluster_ind | f
  4359. alttype_cluster_pkey | t
  4360. (2 rows)
  4361. drop table alttype_cluster;
  4362. --
  4363. -- Check that attaching or detaching a partitioned partition correctly leads
  4364. -- to its partitions' constraint being updated to reflect the parent's
  4365. -- newly added/removed constraint
  4366. create table target_parted (a int, b int) partition by list (a);
  4367. create table attach_parted (a int, b int) partition by list (b);
  4368. create table attach_parted_part1 partition of attach_parted for values in (1);
  4369. -- insert a row directly into the leaf partition so that its partition
  4370. -- constraint is built and stored in the relcache
  4371. insert into attach_parted_part1 values (1, 1);
  4372. -- the following better invalidate the partition constraint of the leaf
  4373. -- partition too...
  4374. alter table target_parted attach partition attach_parted for values in (1);
  4375. -- ...such that the following insert fails
  4376. insert into attach_parted_part1 values (2, 1);
  4377. ERROR: new row for relation "attach_parted_part1" violates partition constraint
  4378. DETAIL: Failing row contains (2, 1).
  4379. -- ...and doesn't when the partition is detached along with its own partition
  4380. alter table target_parted detach partition attach_parted;
  4381. insert into attach_parted_part1 values (2, 1);