%3CLINGO-SUB%20id%3D%22lingo-sub-1490128%22%20slang%3D%22en-US%22%3EPostgres%20Tips%3A%20How%20to%20convert%202%20Billion%20Rows%20to%20Bigint%20with%20Citus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1490128%22%20slang%3D%22en-US%22%3E%3CP%3EIt%E2%80%99s%20been%20a%20year%20since%20%3CA%20href%3D%22https%3A%2F%2Fblogs.microsoft.com%2Fblog%2F2019%2F01%2F24%2Fmicrosoft-acquires-citus-data-re-affirming-its-commitment-to-open-source-and-accelerating-azure-postgresql-performance-and-scale%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EMicrosoft%20acquired%20Citus%20Data%3C%2FA%3E%2C%20and%20while%20there%20have%20been%20some%20changes%20such%20as%20my%20team%20now%20officially%20being%20called%20%E2%80%9CNinjas%E2%80%9D%2C%20we%20still%20often%20get%20unusual%20and%20interesting%20Postgres%20%E2%80%9Cpuzzles%E2%80%9D%20to%20solve.%20If%20you%E2%80%99re%20not%20familiar%2C%20Citus%20is%20an%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eopen%20source%20Postgres%20extension%3C%2FA%3E%20that%20scales%20out%20Postgres%20horizontally%20that%20is%20now%20available%20as%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fquickstart-create-hyperscale-portal%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EHyperscale%20(Citus)%3C%2FA%3E%20as%20part%20of%20our%20managed%20Postgres%20service%20on%20Azure.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3ERecently%2C%20our%20friends%20at%20ConvertFlow%2C%20a%20%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fcustomers%2Fconvertflow%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elong-time%20Citus%20customer%3C%2FA%3E%26nbsp%3B%20approached%20our%20team%20with%20a%20particularly%20interesting%20Postgres%20challenge.%20ConvertFlow%20has%20what%20we%20call%20a%20%22HTAP%22%20workload%2C%20featuring%20an%20application%20that%20gives%20marketers%20a%20personalized%20way%20to%20guide%20website%20visitors%20to%20become%20leads%2C%20customers%2C%20and%20repeat%20buyers.%20For%20those%20of%20you%20that%20are%20curious%2C%20HTAP%20means%20that%20they%20have%20a%20mixed%20%3CA%20href%3D%22https%3A%2F%2Fdocs.citusdata.com%2Fen%2Fstable%2Fuse_cases%2Fmulti_tenant.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emulti-tenant%20SaaS%3C%2FA%3E%20and%20also%20%3CA%20href%3D%22https%3A%2F%2Fdocs.citusdata.com%2Fen%2Fstable%2Fuse_cases%2Frealtime_analytics.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ereal-time%20analytics%3C%2FA%3E%20workload%20multi-tenant%20SaaS.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1322202785%22%20id%3D%22toc-hId--1322202785%22%3EThe%20problem%3A%202%20billion%20rows%20%26amp%3B%20ongoing%20growth%20meant%20ConvertFlow%20would%20soon%20face%20an%20integer%20overflow%20issue%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConvertFlow%20has%20a%20pair%20of%20Postgres%20tracking%20tables%20sharded%20across%20multiple%20servers%20in%20a%20Citus%20database%20cluster%20at%20the%20heart%20of%20their%20application.%20A%20good%20chunk%20of%20their%20SaaS%20application%20workflow%20either%20updates%20or%20reads%20from%20these%20Postgres%20tables.%20ConvertFlow%E2%80%99s%20usage%20has%20shot%20up%20over%20the%20past%20few%20years%2C%20so%20as%20you%20can%20imagine%20these%20tables%20got%20really%20big%20over%20time.%20Today%2C%20there%20are%20two%20billion%20rows%20across%20the%20two%20tracking%20tables%2C%20those%20tables%20alone%20get%2040%2C000%20reads%20a%20minute%2C%20and%20the%20number%20of%20reads%20is%20climbing.%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%3CBR%20%2F%3EThis%20type%20of%20growth%2C%20unfortunately%2C%20often%20leads%20to%20a%20problem%3A%20integer%20overflow.%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%3CBR%20%2F%3EEach%20of%20ConvertFlow%E2%80%99s%20Postgres%20tracking%20tables%20had%20a%20bit%20over%201%20billion%20rows%2C%20and%20each%20table%20used%20an%20integer%20primary%20key%20that%20was%20set%20to%20autoincrement.%20This%20meant%20that%20each%20table%20had%20a%20row%20with%20an%20integer%20that%20was%20increasing%20with%20every%20row%20added.%20Unfortunately%2C%20Postgres%20limits%20the%20maximum%20size%20of%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fdatatype-numeric.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Einteger%20type%3C%2FA%3E%20to%202%2C147%2C483%2C647.%20This%20meant%20that%20the%20primary%20key%20columns%20were%20already%20roughly%20half%20full%2C%20and%20with%20ConvertFlow%E2%80%99s%20continued%20growth%2C%20integer%20overflow%20would%20soon%20become%20an%20issue.%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%3CBR%20%2F%3EFortunately%2C%20Postgres%20has%20another%20numeric%20data%20type%20called%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fdatatype-numeric.html%23DATATYPE-INT%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ebigint%3C%2FA%3E%2C%20with%20a%20limit%20of%209%2C223%2C372%2C036%2C854%2C775%2C807%2C%20or%20roughly%209%20quintillion.%20While%20Citus%20database%20clusters%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Farchitecting-petabyte-scale-analytics-by-scaling-out-postgres-on%2Fba-p%2F969685%22%20target%3D%22_self%22%3Ecan%20get%20extremely%20large%3C%2FA%3E%2C%20we%E2%80%99ve%20never%20encountered%20anything%20close%20to%20hitting%20the%20bigint%20limit.%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3EThis%20realization%20led%20us%20to%20our%20solution%3A%20if%20we%20switch%20from%20the%20integer%20data%20type%20to%20bigint%20in%20our%20customer%E2%80%99s%20Postgres%20tables%2C%20we%E2%80%99d%20be%20all%20set.%20Sounds%20easy%2C%20right%3F%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20data-unlink%3D%22true%22%20id%3D%22toc-hId-1165310048%22%20id%3D%22toc-hId-1165310048%22%3ENumeric%20Types%20available%20in%20Postgres%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20data-unlink%3D%22true%22%20id%3D%22toc-hId--642144415%22%20id%3D%22toc-hId--642144415%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Postgres-numeric-data-type-table.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F201249i4E72E78FC8BC94C5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Postgres-numeric-data-type-table.jpg%22%20alt%3D%22From%20the%20PostgreSQL%20documentation%20chapter%20on%20Data%20Types%2C%20the%20table%20of%20built-in%20Numeric%20Types%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFrom%20the%20PostgreSQL%20documentation%20chapter%20on%20Data%20Types%2C%20the%20table%20of%20built-in%20Numeric%20Types%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%20data-unlink%3D%22true%22%3E%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fdatatype-numeric.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ENumeric%20Types%20available%20in%20Postgres%3C%2FA%3E%3C%2FP%3E%0A%3CH2%20data-unlink%3D%22true%22%20id%3D%22toc-hId-1845368418%22%20id%3D%22toc-hId-1845368418%22%3E%3CBR%20%2F%3EMore%20challenges%3A%20aggressive%20Postgres%20locking%20might%20cause%20too%20much%20downtime%3C%2FH2%3E%0A%3CP%20data-unlink%3D%22true%22%3E%3CBR%20%2F%3EWell%2C%20one%20minor%20detail%20got%20in%20the%20way%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fblog%2F2018%2F02%2F15%2Fwhen-postgresql-blocks%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPostgres%20locks%20when%20changing%20the%20type%20of%20a%20column%3C%2FA%3E.%20Specifically%2C%20Postgres%20gets%20an%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fexplicit-locking.html%23LOCKING-TABLES%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EACCESS%20EXCLUSIVE%20lock%3C%2FA%3E%2C%20a%20very%20aggressive%20type%20of%20lock%2C%20when%20changing%20types.%20In%20Postgres%2C%20ACCESS%20EXCLUSIVE%20locks%20prevent%20updates%2C%20inserts%2C%20and%20even%20reads%20from%20going%20through%20for%20the%20duration%20of%20the%20update.%20As%20a%20result%2C%20for%20all%20practical%20purposes%20changing%20the%20Postgres%20data%20type%20would%20take%20the%20application%20down.%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%3CBR%20%2F%3ESome%20quick%20tests%20showed%20that%20there%20would%20be%20approximately%2010%20hours%20of%20downtime%20for%20the%20change%20to%20persist%20across%202%20billion%20rows.%20While%20this%20is%20relatively%20quick%20given%20the%20amount%20of%20data%2C%2010%20hours%20was%20definitely%20way%20too%20long%20of%20a%20maintenance%20window.%20So%2C%20we%20devised%20a%20plan%20to%20address%20this%20concern.%3C%2FP%3E%0A%3CH2%20data-unlink%3D%22true%22%20id%3D%22toc-hId-37913955%22%20id%3D%22toc-hId-37913955%22%3E%3CBR%20%2F%3EThe%20plan%3A%20make%20new%20BIGINT%20columns%2C%20copy%20the%20data%20to%20them%2C%20and%20switch%20over%20later%3C%2FH2%3E%0A%3CP%20data-unlink%3D%22true%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20plan%20started%20out%20simply%20enough.%20We%E2%80%99d%20make%20a%20new%20BIGINT%20column%20in%20each%20of%20the%20Postgres%20tables%20in%20the%20Citus%20cluster%2C%20mirror%20the%20data%20from%20the%20existing%20columns%20to%20the%20new%20ones%2C%20and%20switch%20over%20the%20application%20to%20use%20the%20new%20columns.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EThis%20seemed%20to%20be%20a%20great%20idea%E2%80%94we%20could%20set%20up%20a%20Postgres%20trigger%20for%20new%20or%20updated%20values%20and%20slowly%20do%20batch%20updates%20to%20copy%20over%20the%20existing%20values%20as%20they%20came%20in.%20To%20make%20it%20even%20better%2C%20we%20could%20schedule%20the%20batch%20updates%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fpg_cron%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eopen%20source%20pg_cron%20extension%3C%2FA%3E%2C%20which%20let%20us%20do%20those%20batch%20updates%20overnight%20without%20having%20to%20stay%20up%20late.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20only%20maintenance%20window%20would%20occur%20when%20we%20later%20switched%20over%20to%20using%20our%20new%20Postgres%20bigint%20columns.%20It%20would%20definitely%20be%20a%20bit%20slower%2C%20but%20we%E2%80%99d%20also%20have%20far%20less%20locking.%20In%20addition%2C%20we%20could%20avoid%20periods%20of%20high%20activity%20by%20doing%20batch%20updates%20of%20existing%20rows.%20We%20still%20had%20a%20few%20months%20of%20runway%20left%20before%20we%20ran%20out%20of%20usable%20integers%2C%20so%20the%20time%20to%20finish%20wasn%E2%80%99t%20an%20urgent%20factor%20yet.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1769540508%22%20id%3D%22toc-hId--1769540508%22%3E%3CBR%20%2F%3EThe%20twist%3A%20how%20to%20maintain%20existence%20of%20primary%20keys%20at%20all%20times%3F%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20quickly%20realized%20that%20we%E2%80%99d%20overlooked%20a%20surprising%20twist.%20The%20integer%20columns%20were%20part%20of%20the%20primary%20keys%20on%20the%20tables%20in%20question%2C%20and%20ConvertFlow%20uses%20a%20Rails%20app%2C%20so%20we%20had%20to%20maintain%20the%20existence%20of%20primary%20keys%20at%20all%20times%20to%20keep%20the%20app%20happy.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EWe%20could%20drop%20the%20primary%20key%20in%20a%20transaction%20and%20make%20a%20new%20one%2C%20but%20with%202%20billion%20rows%20in%20the%20distributed%20Postgres%20tables%2C%20that%20was%20still%20likely%20to%20take%20an%20hour%20or%20two%2C%20and%20we%20wanted%20a%20shorter%20maintenance%20window.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-717972325%22%20id%3D%22toc-hId-717972325%22%3E%3CBR%20%2F%3EThe%20fix%3A%20Postgres%E2%80%99s%20wonderful%20ADD%20table_constraint_using_index%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFortunately%2C%20Postgres%20has%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fsql-altertable.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eanother%20cool%20feature%20we%20could%20use%20in%20this%20case%3C%2FA%3E%3A%3CCODE%20style%3D%22color%3A%20black%3B%22%3EADD%20table_constraint_using_index%3C%2FCODE%3E.%20You%20can%20use%20this%20argument%20when%20creating%20a%20constraint%20to%20promote%20a%20unique%20index%20to%20a%20primary%20key%2C%20which%20lets%20Postgres%20know%20that%20the%20unique%20constraint%20is%20valid.%20That%20means%20that%20Postgres%20doesn%E2%80%99t%20need%20to%20do%20a%20full%20table%20scan%20to%20check%20validity%20of%20the%20primary%20key.%20As%20a%20result%2C%20we%20could%20drop%20the%20existing%20primary%20key%20and%20very%20quickly%20make%20a%20new%20one%20using%20that%20BIGINT%20column.%20While%20this%20would%20still%20involve%20a%20brief%20lock%20for%20the%20%3CCODE%20style%3D%22color%3A%20black%3B%22%3EALTER%20TABLE%3C%2FCODE%3E%20to%20complete%2C%20this%20approach%20would%20be%20much%20faster.%20We%20discussed%20the%20idea%20with%20ConvertFlow%2C%20wrote%20some%20Rails%20migrations%2C%20tested%20everything%2C%20and%20then%20scheduled%20a%20maintenance%20window.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20everything%20now%20figured%20out%2C%20we%20started.%20We%20ended%20up%20doing%20everything%20in%20a%20%3CA%20href%3D%22https%3A%2F%2Fguides.rubyonrails.org%2Factive_record_migrations.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ERails%20migration%3C%2FA%3E%20instead%20of%20raw%20SQL%2C%20but%20in%20the%20interest%20of%20making%20this%20more%20generally%20applicable%2C%20here%E2%80%99s%20the%20pure%20SQL%20version%20of%20what%20we%20ran%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E--%20First%2C%20make%20basic%20modifications%20to%20get%20the%20new%20column%20added.%20We've%20got%20a%20composite%20primary%20key%2C%20so%20the%20new%20constraint%20has%202%20columns%20in%20it%20to%20match%20the%20original.%20%0A%0AALTER%20TABLE%20target_table%20ADD%20COLUMN%20column_bigint%20BIGINT%3B%0AALTER%20TABLE%20target_table%20ADD%20CONSTRAINT%20future_primary_key%20UNIQUE%20(column_bigint%2C%20other_column)%3B%0A%0A--%20Next%2C%20ensure%20that%20all%20incoming%20updates%20and%20inserts%20populate%20that%20column%3A%0A%0ACREATE%20OR%20REPLACE%20FUNCTION%20populate_bigint%0ARETURNS%20trigger%20AS%0A%24BODY%24%0Anew.column_bigint%20%3A%3D%20new.original_column%3B%0A%24BODY%24%3B%0ACREATE%20TRIGGER%20populate_bigint_trigger%20BEFORE%20INSERT%20OR%20UPDATE%20ON%20target_table%0AFOR%20EACH%20ROW%0AEXECUTE%20populate_bigint()%3B%0A--%20Now%2C%20populate%20the%20column%20for%20all%20existing%20rows.%20We%E2%80%99re%20doing%20this%20in%20chunks%20so%20we%20can%20pause%20and%20resume%20more%20easily.%0A%0AUPDATE%20target_table%20SET%20column_bigint%20%3D%20original_column%20WHERE%20column_bigint%20IS%20NULL%20AND%20original_column%20BETWEEN%200%20AND%2010000%3B%0AUPDATE%20target_table%20SET%20column_bigint%20%3D%20original_column%20WHERE%20column_bigint%20IS%20NULL%20AND%20original_column%20BETWEEN%2010000%20AND%2020000%3B%0A--Repeat%20last%20line%20with%20changes%20as%20needed.%20We%20scheduled%20this%20using%20pg_cron%20so%20we%20didn't%20have%20to%20keep%20running%20different%20commands.%20%0A%0A--%20Now%20that%20everything%E2%80%99s%20caught%20up%2C%20take%20a%20few%20minutes%20of%20downtime%2C%20and%20switch%20over.%20We%E2%80%99re%20doing%20this%20in%20a%20transaction%2C%20just%20in%20case.%0A%0ABEGIN%3B%0AALTER%20TABLE%20target_table%20DROP%20CONSTRAINT%20original_pkey%3B%0AALTER%20TABLE%20target_table%20ADD%20CONSTRAINT%20new_pkey%20PRIMARY%20KEY%20USING%20INDEX%20future_primary_key%3B%0AEND%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1089482138%22%20id%3D%22toc-hId--1089482138%22%3Etl%3Bdr%3A%20Taking%20it%20slow%20meant%20only%2020%20minutes%20of%20downtime%20for%202%20billion%20rows.%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20ran%20this%2C%20and%20it%20worked!%20It%20ended%20up%20requiring%20about%2020%20minutes%20of%20downtime%2C%20most%20of%20which%20was%20waiting%20for%20long-running%20transactions%20to%20finish%20and%20doing%20testing%20to%20make%20sure%20we%20got%20it%20all%20right.%20This%20fit%20very%20nicely%20into%20the%20maintenance%20window%20the%20-ERR%3AREF-NOT-FOUND-ConvertFlow%20team%20had%20planned.%20What%20a%20fantastic%20improvement%20over%20the%2010%2B%20hours%20we%20had%20originally%20estimated%2C%20and%20at%20every%20single%20point%20in%20the%20process%20we%20had%20an%20easy%20way%20to%20back%20out%20if%20we%20had%20missed%20any%20details.%20Success!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20data-tid%3D%22messageBodyContainer%22%3E%0A%3CDIV%20data-tid%3D%22messageBodyContent%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%3EIf%20you're%20at%20the%26nbsp%3Bdatabase%20size%26nbsp%3Bwhere%20integer%20overflow%26nbsp%3Bis%20a%20problem%20for%20you%2C%20it's%20probably%20worth%20-ERR%3AREF-NOT-FOUND-checking%20out%20Citus%20as%20well.%20These%20Postgres%20tips%20work%20fine%20with%20and%20without%20the%26nbsp%3BCitus%20extension%2C%20but%20it%20never%20hurts%20to%20be%20ready%20to%20scale%20out.%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1490128%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22integer-bigint-graphic.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F201250i9019C24873AACED8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22integer-bigint-graphic.png%22%20alt%3D%22integer-bigint-graphic.png%22%20%2F%3E%3C%2FSPAN%3EThis%20is%20how%20we%20converted%202%20billion%20Postgres%20rows%20from%20INT%20to%20BIGINT%20that%20were%20part%20of%20a%20primary%20key%20with%2020%20minutes%20of%20downtime.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

It’s been a year since Microsoft acquired Citus Data, and while there have been some changes such as my team now officially being called “Ninjas”, we still often get unusual and interesting Postgres “puzzles” to solve. If you’re not familiar, Citus is an open source Postgres extension that scales out Postgres horizontally that is now available as Hyperscale (Citus) as part of our managed Postgres service on Azure.


Recently, our friends at ConvertFlow, a long-time Citus customer  approached our team with a particularly interesting Postgres challenge. ConvertFlow has what we call a "HTAP" workload, featuring an application that gives marketers a personalized way to guide website visitors to become leads, customers, and repeat buyers. For those of you that are curious, HTAP means that they have a mixed multi-tenant SaaS and also real-time analytics workload multi-tenant SaaS.

 

The problem: 2 billion rows & ongoing growth meant ConvertFlow would soon face an integer overflow issue

 

ConvertFlow has a pair of Postgres tracking tables sharded across multiple servers in a Citus database cluster at the heart of their application. A good chunk of their SaaS application workflow either updates or reads from these Postgres tables. ConvertFlow’s usage has shot up over the past few years, so as you can imagine these tables got really big over time. Today, there are two billion rows across the two tracking tables, those tables alone get 40,000 reads a minute, and the number of reads is climbing.


This type of growth, unfortunately, often leads to a problem: integer overflow.


Each of ConvertFlow’s Postgres tracking tables had a bit over 1 billion rows, and each table used an integer primary key that was set to autoincrement. This meant that each table had a row with an integer that was increasing with every row added. Unfortunately, Postgres limits the maximum size of the integer type to 2,147,483,647. This meant that the primary key columns were already roughly half full, and with ConvertFlow’s continued growth, integer overflow would soon become an issue.


Fortunately, Postgres has another numeric data type called bigint, with a limit of 9,223,372,036,854,775,807, or roughly 9 quintillion. While Citus database clusters can get extremely large, we’ve never encountered anything close to hitting the bigint limit.

 

This realization led us to our solution: if we switch from the integer data type to bigint in our customer’s Postgres tables, we’d be all set. Sounds easy, right?

 

Numeric Types available in Postgres

 

From the PostgreSQL documentation chapter on Data Types, the table of built-in Numeric TypesFrom the PostgreSQL documentation chapter on Data Types, the table of built-in Numeric Types

Numeric Types available in Postgres


More challenges: aggressive Postgres locking might cause too much downtime


Well, one minor detail got in the way here: Postgres locks when changing the type of a column. Specifically, Postgres gets an ACCESS EXCLUSIVE lock, a very aggressive type of lock, when changing types. In Postgres, ACCESS EXCLUSIVE locks prevent updates, inserts, and even reads from going through for the duration of the update. As a result, for all practical purposes changing the Postgres data type would take the application down.


Some quick tests showed that there would be approximately 10 hours of downtime for the change to persist across 2 billion rows. While this is relatively quick given the amount of data, 10 hours was definitely way too long of a maintenance window. So, we devised a plan to address this concern.


The plan: make new BIGINT columns, copy the data to them, and switch over later

 

The plan started out simply enough. We’d make a new BIGINT column in each of the Postgres tables in the Citus cluster, mirror the data from the existing columns to the new ones, and switch over the application to use the new columns.


This seemed to be a great idea—we could set up a Postgres trigger for new or updated values and slowly do batch updates to copy over the existing values as they came in. To make it even better, we could schedule the batch updates using the open source pg_cron extension, which let us do those batch updates overnight without having to stay up late.


The only maintenance window would occur when we later switched over to using our new Postgres bigint columns. It would definitely be a bit slower, but we’d also have far less locking. In addition, we could avoid periods of high activity by doing batch updates of existing rows. We still had a few months of runway left before we ran out of usable integers, so the time to finish wasn’t an urgent factor yet.


The twist: how to maintain existence of primary keys at all times?

 

We quickly realized that we’d overlooked a surprising twist. The integer columns were part of the primary keys on the tables in question, and ConvertFlow uses a Rails app, so we had to maintain the existence of primary keys at all times to keep the app happy.


We could drop the primary key in a transaction and make a new one, but with 2 billion rows in the distributed Postgres tables, that was still likely to take an hour or two, and we wanted a shorter maintenance window.


The fix: Postgres’s wonderful ADD table_constraint_using_index

 

Fortunately, Postgres has another cool feature we could use in this case: ADD table_constraint_using_index. You can use this argument when creating a constraint to promote a unique index to a primary key, which lets Postgres know that the unique constraint is valid. That means that Postgres doesn’t need to do a full table scan to check validity of the primary key. As a result, we could drop the existing primary key and very quickly make a new one using that BIGINT column. While this would still involve a brief lock for the ALTER TABLE to complete, this approach would be much faster. We discussed the idea with ConvertFlow, wrote some Rails migrations, tested everything, and then scheduled a maintenance window.

 

With everything now figured out, we started. We ended up doing everything in a Rails migration instead of raw SQL, but in the interest of making this more generally applicable, here’s the pure SQL version of what we ran:

 

 

 

 

 

 

-- First, make basic modifications to get the new column added. We've got a composite primary key, so the new constraint has 2 columns in it to match the original. 

ALTER TABLE target_table ADD COLUMN column_bigint BIGINT;
ALTER TABLE target_table ADD CONSTRAINT future_primary_key UNIQUE (column_bigint, other_column);

-- Next, ensure that all incoming updates and inserts populate that column:

CREATE OR REPLACE FUNCTION populate_bigint
RETURNS trigger AS
$BODY$
new.column_bigint := new.original_column;
$BODY$;
CREATE TRIGGER populate_bigint_trigger BEFORE INSERT OR UPDATE ON target_table
FOR EACH ROW
EXECUTE populate_bigint();
-- Now, populate the column for all existing rows. We’re doing this in chunks so we can pause and resume more easily.

UPDATE target_table SET column_bigint = original_column WHERE column_bigint IS NULL AND original_column BETWEEN 0 AND 10000;
UPDATE target_table SET column_bigint = original_column WHERE column_bigint IS NULL AND original_column BETWEEN 10000 AND 20000;
--Repeat last line with changes as needed. We scheduled this using pg_cron so we didn't have to keep running different commands. 

-- Now that everything’s caught up, take a few minutes of downtime, and switch over. We’re doing this in a transaction, just in case.

BEGIN;
ALTER TABLE target_table DROP CONSTRAINT original_pkey;
ALTER TABLE target_table ADD CONSTRAINT new_pkey PRIMARY KEY USING INDEX future_primary_key;
END;

 

 

 

 

 

 

tl;dr: Taking it slow meant only 20 minutes of downtime for 2 billion rows.

 

We ran this, and it worked! It ended up requiring about 20 minutes of downtime, most of which was waiting for long-running transactions to finish and doing testing to make sure we got it all right. This fit very nicely into the maintenance window the ConvertFlow team had planned. What a fantastic improvement over the 10+ hours we had originally estimated, and at every single point in the process we had an easy way to back out if we had missed any details. Success!

 

If you're at the database size where integer overflow is a problem for you, it's probably worth checking out Citus as well. These Postgres tips work fine with and without the Citus extension, but it never hurts to be ready to scale out.