Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eUuVd-0003jd-1k for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Dec 2017 13:15:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eUuVb-0004yn-Ry for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Dec 2017 13:15:35 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eUuVb-0004yd-0c for pgsql-hackers@lists.postgresql.org; Fri, 29 Dec 2017 13:15:35 +0000 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eUuVW-0003GO-09 for pgsql-hackers@postgresql.org; Fri, 29 Dec 2017 13:15:33 +0000 Received: by mail-wm0-x22f.google.com with SMTP id g75so48581019wme.0 for ; Fri, 29 Dec 2017 05:15:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant.it; s=google; h=from:subject:to:message-id:date:user-agent:mime-version; bh=4pUs6iIIeKJPYO7ginAOibIwQnT4Ge/hWTykZJdDsWg=; b=PJJ4Kg8cLX89unJzgKpLz7E5fVRYjcPi4MgmQIed9UtHLp9LU69DcKTT0P56Twn0z+ 4Uwf8rq7Wz9zyDTGN2b+LvnkWXoR/NP4e5phDxQzT8IryUiERIjJ0I/6YcrV2arXVCxA 3cTIUr19OvG+zxs9CsUWLj0M965syCbj/q2X8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version; bh=4pUs6iIIeKJPYO7ginAOibIwQnT4Ge/hWTykZJdDsWg=; b=magIR3F/HPKpTqqkZkXY+LDNULtfbTz2bOVXtrD155bFfP9+f4XFDbLifhhdiIxRBJ tVCOJBR2huI4hZ9xoo60ZkYn4A6lK/9yoBbqwtHBYtVWNWjfdnmiDJONRpRLwEjinluD d6M6qVjwqOVlDkiIjq/KBoNw3FUBgAWX7OrDr+3h/lR8xPmTgQS/6+69jmBNNhYZkZZ/ LHW01eX7/9mW3mWvvlBoD6KjI++MCfhMvT3mPOpVPNwv1+8pRSccGbWpX4fNwdUoaBZ+ lOVBVEy0aC+Tp5x6GdeQBCzxkRQPo+VuDCEF5jquLC84h0XtLopQIgulDnLvcDr/uycl 8hUA== X-Gm-Message-State: AKGB3mLl0lTodIwc5pm4b/l8Ho45w7Q87BEyrLzUOmPywoZqEz2uIvwe X35krbNIv8eYWm5EPKgLdh2zjehv82ffBsMdGWiksCwxBOlKBfS3t9VTcqmOkV7BOtDVx9ddizP 0S3jgcjJww2RjX3WrbY82xhjT4SQaya6gFF6s6B6m++jA7cs9hht881o1lTSvahEjTWoFmrPq73 iMBfVhffZ/mYG5ycI= X-Google-Smtp-Source: ACJfBou/oK903NhgVmgjAaLYztEXNsjk3DYEFHKDWOeI7s2NSh8lEYdMmy7XsyG6es9M2+fAi1r6lg== X-Received: by 10.80.245.237 with SMTP id x42mr44484076edm.80.1514553327601; Fri, 29 Dec 2017 05:15:27 -0800 (PST) Received: from titanium.homenet.telecomitalia.it (host41-61-dynamic.57-82-r.retail.telecomitalia.it. [82.57.61.41]) by smtp.googlemail.com with ESMTPSA id w2sm33980473edb.4.2017.12.29.05.15.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Dec 2017 05:15:26 -0800 (PST) From: Marco Nenciarini Subject: [PATCH] Logical decoding of TRUNCATE To: PostgreSQL-development Message-ID: <269bf0d2-6ac4-c032-6979-22aad6a9f5fb@2ndquadrant.it> Date: Fri, 29 Dec 2017 14:15:22 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="qQMRi61nzNcozNjmBIrzstZRUS9q3hiDE" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --qQMRi61nzNcozNjmBIrzstZRUS9q3hiDE Content-Type: multipart/mixed; boundary="Lztfbyk9FX1CaqVpp00GGHeTiY6FUb7uw"; protected-headers="v1" From: Marco Nenciarini To: PostgreSQL-development Message-ID: <269bf0d2-6ac4-c032-6979-22aad6a9f5fb@2ndquadrant.it> Subject: [PATCH] Logical decoding of TRUNCATE --Lztfbyk9FX1CaqVpp00GGHeTiY6FUb7uw Content-Type: multipart/mixed; boundary="------------7B74D3083442431138F96ED7" Content-Language: en-US This is a multi-part message in MIME format. --------------7B74D3083442431138F96ED7 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable Hi, This patch implements support for TRUNCATE statements in logical replication. The work has mainly done by Simon Riggs then finished by me. Tests are written by me. TRUNCATE is treated as a form of DELETE for the purpose of deciding whether to publish, or not. The "TRUNCATE behavior when session_replication_role =3D replica"[1] patc= h is required from this patch to work correctly with tables referenced by foreign keys. [1] https://commitfest.postgresql.org/16/1447/ Regards, Marco --=20 Marco Nenciarini - 2ndQuadrant Italy PostgreSQL Training, Services and Support marco.nenciarini@2ndQuadrant.it | www.2ndQuadrant.it --------------7B74D3083442431138F96ED7 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="logical_decode_truncate.v10.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="logical_decode_truncate.v10.patch" diff --git a/contrib/test_decoding/expected/ddl.out b/contrib/test_decodi= ng/expected/ddl.out index 1e22c1eefc..d1feea4909 100644 *** a/contrib/test_decoding/expected/ddl.out --- b/contrib/test_decoding/expected/ddl.out *************** *** 543,548 **** UPDATE table_with_unique_not_null SET data =3D 3 WHERE d= ata =3D 2; --- 543,550 ---- UPDATE table_with_unique_not_null SET id =3D -id; UPDATE table_with_unique_not_null SET id =3D -id; DELETE FROM table_with_unique_not_null WHERE data =3D 3; + TRUNCATE table_with_unique_not_null; + TRUNCATE table_with_unique_not_null, table_with_unique_not_null RESTART= IDENTITY CASCADE; -- check toast support BEGIN; CREATE SEQUENCE toasttable_rand_seq START 79 INCREMENT 1499; -- portabl= e "random" *************** *** 660,665 **** SELECT data FROM pg_logical_slot_get_changes('regression= _slot', NULL, NULL, 'inc --- 662,673 ---- table public.table_with_unique_not_null: DELETE: id[integer]:4 COMMIT BEGIN + table public.table_with_unique_not_null: TRUNCATE: (no-flags) + COMMIT + BEGIN + table public.table_with_unique_not_null: TRUNCATE: restart_seqs cascad= e + COMMIT + BEGIN table public.toasttable: INSERT: id[integer]:1 toasted_col1[text]:'123= 4567891011121314151617181920212223242526272829303132333435363738394041424= 3444546474849505152535455565758596061626364656667686970717273747576777879= 8081828384858687888990919293949596979899100101102103104105106107108109110= 1111121131141151161171181191201211221231241251261271281291301311321331341= 3513613713813914014114214314414514614714814915015115215315415515615715815= 9160161162163164165166167168169170171172173174175176177178179180181182183= 1841851861871881891901911921931941951961971981992002012022032042052062072= 0820921021121221321421521621721821922022122222322422522622722822923023123= 2233234235236237238239240241242243244245246247248249250251252253254255256= 2572582592602612622632642652662672682692702712722732742752762772782792802= 8128228328428528628728828929029129229329429529629729829930030130230330430= 5306307308309310311312313314315316317318319320321322323324325326327328329= 3303313323333343353363373383393403413423433443453463473483493503513523533= 5435535635735835936036136236336436536636736836937037137237337437537637737= 8379380381382383384385386387388389390391392393394395396397398399400401402= 4034044054064074084094104114124134144154164174184194204214224234244254264= 2742842943043143243343443543643743843944044144244344444544644744844945045= 1452453454455456457458459460461462463464465466467468469470471472473474475= 4764774784794804814824834844854864874884894904914924934944954964974984995= 0050150250350450550650750850951051151251351451551651751851952052152252352= 4525526527528529530531532533534535536537538539540541542543544545546547548= 5495505515525535545555565575585595605615625635645655665675685695705715725= 7357457557657757857958058158258358458558658758858959059159259359459559659= 7598599600601602603604605606607608609610611612613614615616617618619620621= 6226236246256266276286296306316326336346356366376386396406416426436446456= 4664764864965065165265365465565665765865966066166266366466566666766866967= 0671672673674675676677678679680681682683684685686687688689690691692693694= 6956966976986997007017027037047057067077087097107117127137147157167177187= 1972072172272372472572672772872973073173273373473573673773873974074174274= 3744745746747748749750751752753754755756757758759760761762763764765766767= 7687697707717727737747757767777787797807817827837847857867877887897907917= 9279379479579679779879980080180280380480580680780880981081181281381481581= 6817818819820821822823824825826827828829830831832833834835836837838839840= 8418428438448458468478488498508518528538548558568578588598608618628638648= 6586686786886987087187287387487587687787887988088188288388488588688788888= 9890891892893894895896897898899900901902903904905906907908909910911912913= 9149159169179189199209219229239249259269279289299309319329339349359369379= 3893994094194294394494594694794894995095195295395495595695795895996096196= 2963964965966967968969970971972973974975976977978979980981982983984985986= 9879889899909919929939949959969979989991000100110021003100410051006100710= 0810091010101110121013101410151016101710181019102010211022102310241025102= 6102710281029103010311032103310341035103610371038103910401041104210431044= 1045104610471048104910501051105210531054105510561057105810591060106110621= 0631064106510661067106810691070107110721073107410751076107710781079108010= 8110821083108410851086108710881089109010911092109310941095109610971098109= 9110011011102110311041105110611071108110911101111111211131114111511161117= 1118111911201121112211231124112511261127112811291130113111321133113411351= 1361137113811391140114111421143114411451146114711481149115011511152115311= 5411551156115711581159116011611162116311641165116611671168116911701171117= 2117311741175117611771178117911801181118211831184118511861187118811891190= 1191119211931194119511961197119811991200120112021203120412051206120712081= 2091210121112121213121412151216121712181219122012211222122312241225122612= 2712281229123012311232123312341235123612371238123912401241124212431244124= 5124612471248124912501251125212531254125512561257125812591260126112621263= 1264126512661267126812691270127112721273127412751276127712781279128012811= 2821283128412851286128712881289129012911292129312941295129612971298129913= 0013011302130313041305130613071308130913101311131213131314131513161317131= 8131913201321132213231324132513261327132813291330133113321333133413351336= 1337133813391340134113421343134413451346134713481349135013511352135313541= 3551356135713581359136013611362136313641365136613671368136913701371137213= 7313741375137613771378137913801381138213831384138513861387138813891390139= 1139213931394139513961397139813991400140114021403140414051406140714081409= 1410141114121413141414151416141714181419142014211422142314241425142614271= 4281429143014311432143314341435143614371438143914401441144214431444144514= 4614471448144914501451145214531454145514561457145814591460146114621463146= 4146514661467146814691470147114721473147414751476147714781479148014811482= 1483148414851486148714881489149014911492149314941495149614971498149915001= 5011502150315041505150615071508150915101511151215131514151515161517151815= 1915201521152215231524152515261527152815291530153115321533153415351536153= 7153815391540154115421543154415451546154715481549155015511552155315541555= 1556155715581559156015611562156315641565156615671568156915701571157215731= 5741575157615771578157915801581158215831584158515861587158815891590159115= 9215931594159515961597159815991600160116021603160416051606160716081609161= 0161116121613161416151616161716181619162016211622162316241625162616271628= 1629163016311632163316341635163616371638163916401641164216431644164516461= 6471648164916501651165216531654165516561657165816591660166116621663166416= 6516661667166816691670167116721673167416751676167716781679168016811682168= 3168416851686168716881689169016911692169316941695169616971698169917001701= 1702170317041705170617071708170917101711171217131714171517161717171817191= 7201721172217231724172517261727172817291730173117321733173417351736173717= 3817391740174117421743174417451746174717481749175017511752175317541755175= 6175717581759176017611762176317641765176617671768176917701771177217731774= 1775177617771778177917801781178217831784178517861787178817891790179117921= 7931794179517961797179817991800180118021803180418051806180718081809181018= 1118121813181418151816181718181819182018211822182318241825182618271828182= 9183018311832183318341835183618371838183918401841184218431844184518461847= 1848184918501851185218531854185518561857185818591860186118621863186418651= 8661867186818691870187118721873187418751876187718781879188018811882188318= 8418851886188718881889189018911892189318941895189618971898189919001901190= 2190319041905190619071908190919101911191219131914191519161917191819191920= 1921192219231924192519261927192819291930193119321933193419351936193719381= 9391940194119421943194419451946194719481949195019511952195319541955195619= 5719581959196019611962196319641965196619671968196919701971197219731974197= 5197619771978197919801981198219831984198519861987198819891990199119921993= 1994199519961997199819992000' rand1[double precision]:79 toasted_col2[tex= t]:null rand2[double precision]:1578 COMMIT BEGIN *************** *** 668,674 **** SELECT data FROM pg_logical_slot_get_changes('regression= _slot', NULL, NULL, 'inc BEGIN table public.toasttable: UPDATE: id[integer]:1 toasted_col1[text]:'123= 4567891011121314151617181920212223242526272829303132333435363738394041424= 3444546474849505152535455565758596061626364656667686970717273747576777879= 8081828384858687888990919293949596979899100101102103104105106107108109110= 1111121131141151161171181191201211221231241251261271281291301311321331341= 3513613713813914014114214314414514614714814915015115215315415515615715815= 9160161162163164165166167168169170171172173174175176177178179180181182183= 1841851861871881891901911921931941951961971981992002012022032042052062072= 0820921021121221321421521621721821922022122222322422522622722822923023123= 2233234235236237238239240241242243244245246247248249250251252253254255256= 2572582592602612622632642652662672682692702712722732742752762772782792802= 8128228328428528628728828929029129229329429529629729829930030130230330430= 5306307308309310311312313314315316317318319320321322323324325326327328329= 3303313323333343353363373383393403413423433443453463473483493503513523533= 5435535635735835936036136236336436536636736836937037137237337437537637737= 8379380381382383384385386387388389390391392393394395396397398399400401402= 4034044054064074084094104114124134144154164174184194204214224234244254264= 2742842943043143243343443543643743843944044144244344444544644744844945045= 1452453454455456457458459460461462463464465466467468469470471472473474475= 4764774784794804814824834844854864874884894904914924934944954964974984995= 0050150250350450550650750850951051151251351451551651751851952052152252352= 4525526527528529530531532533534535536537538539540541542543544545546547548= 5495505515525535545555565575585595605615625635645655665675685695705715725= 7357457557657757857958058158258358458558658758858959059159259359459559659= 7598599600601602603604605606607608609610611612613614615616617618619620621= 6226236246256266276286296306316326336346356366376386396406416426436446456= 4664764864965065165265365465565665765865966066166266366466566666766866967= 0671672673674675676677678679680681682683684685686687688689690691692693694= 6956966976986997007017027037047057067077087097107117127137147157167177187= 1972072172272372472572672772872973073173273373473573673773873974074174274= 3744745746747748749750751752753754755756757758759760761762763764765766767= 7687697707717727737747757767777787797807817827837847857867877887897907917= 9279379479579679779879980080180280380480580680780880981081181281381481581= 6817818819820821822823824825826827828829830831832833834835836837838839840= 8418428438448458468478488498508518528538548558568578588598608618628638648= 6586686786886987087187287387487587687787887988088188288388488588688788888= 9890891892893894895896897898899900901902903904905906907908909910911912913= 9149159169179189199209219229239249259269279289299309319329339349359369379= 3893994094194294394494594694794894995095195295395495595695795895996096196= 2963964965966967968969970971972973974975976977978979980981982983984985986= 9879889899909919929939949959969979989991000100110021003100410051006100710= 0810091010101110121013101410151016101710181019102010211022102310241025102= 6102710281029103010311032103310341035103610371038103910401041104210431044= 1045104610471048104910501051105210531054105510561057105810591060106110621= 0631064106510661067106810691070107110721073107410751076107710781079108010= 8110821083108410851086108710881089109010911092109310941095109610971098109= 9110011011102110311041105110611071108110911101111111211131114111511161117= 1118111911201121112211231124112511261127112811291130113111321133113411351= 1361137113811391140114111421143114411451146114711481149115011511152115311= 5411551156115711581159116011611162116311641165116611671168116911701171117= 2117311741175117611771178117911801181118211831184118511861187118811891190= 1191119211931194119511961197119811991200120112021203120412051206120712081= 2091210121112121213121412151216121712181219122012211222122312241225122612= 2712281229123012311232123312341235123612371238123912401241124212431244124= 5124612471248124912501251125212531254125512561257125812591260126112621263= 1264126512661267126812691270127112721273127412751276127712781279128012811= 2821283128412851286128712881289129012911292129312941295129612971298129913= 0013011302130313041305130613071308130913101311131213131314131513161317131= 8131913201321132213231324132513261327132813291330133113321333133413351336= 1337133813391340134113421343134413451346134713481349135013511352135313541= 3551356135713581359136013611362136313641365136613671368136913701371137213= 7313741375137613771378137913801381138213831384138513861387138813891390139= 1139213931394139513961397139813991400140114021403140414051406140714081409= 1410141114121413141414151416141714181419142014211422142314241425142614271= 4281429143014311432143314341435143614371438143914401441144214431444144514= 4614471448144914501451145214531454145514561457145814591460146114621463146= 4146514661467146814691470147114721473147414751476147714781479148014811482= 1483148414851486148714881489149014911492149314941495149614971498149915001= 5011502150315041505150615071508150915101511151215131514151515161517151815= 1915201521152215231524152515261527152815291530153115321533153415351536153= 7153815391540154115421543154415451546154715481549155015511552155315541555= 1556155715581559156015611562156315641565156615671568156915701571157215731= 5741575157615771578157915801581158215831584158515861587158815891590159115= 9215931594159515961597159815991600160116021603160416051606160716081609161= 0161116121613161416151616161716181619162016211622162316241625162616271628= 1629163016311632163316341635163616371638163916401641164216431644164516461= 6471648164916501651165216531654165516561657165816591660166116621663166416= 6516661667166816691670167116721673167416751676167716781679168016811682168= 3168416851686168716881689169016911692169316941695169616971698169917001701= 1702170317041705170617071708170917101711171217131714171517161717171817191= 7201721172217231724172517261727172817291730173117321733173417351736173717= 3817391740174117421743174417451746174717481749175017511752175317541755175= 6175717581759176017611762176317641765176617671768176917701771177217731774= 1775177617771778177917801781178217831784178517861787178817891790179117921= 7931794179517961797179817991800180118021803180418051806180718081809181018= 1118121813181418151816181718181819182018211822182318241825182618271828182= 9183018311832183318341835183618371838183918401841184218431844184518461847= 1848184918501851185218531854185518561857185818591860186118621863186418651= 8661867186818691870187118721873187418751876187718781879188018811882188318= 8418851886188718881889189018911892189318941895189618971898189919001901190= 2190319041905190619071908190919101911191219131914191519161917191819191920= 1921192219231924192519261927192819291930193119321933193419351936193719381= 9391940194119421943194419451946194719481949195019511952195319541955195619= 5719581959196019611962196319641965196619671968196919701971197219731974197= 5197619771978197919801981198219831984198519861987198819891990199119921993= 1994199519961997199819992000' rand1[double precision]:79 toasted_col2[tex= t]:null rand2[double precision]:1578 COMMIT ! (103 rows) =20 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') F= ROM generate_series(1, 2000) g(i); -- update of second column, first column unchanged --- 676,682 ---- BEGIN table public.toasttable: UPDATE: id[integer]:1 toasted_col1[text]:'123= 4567891011121314151617181920212223242526272829303132333435363738394041424= 3444546474849505152535455565758596061626364656667686970717273747576777879= 8081828384858687888990919293949596979899100101102103104105106107108109110= 1111121131141151161171181191201211221231241251261271281291301311321331341= 3513613713813914014114214314414514614714814915015115215315415515615715815= 9160161162163164165166167168169170171172173174175176177178179180181182183= 1841851861871881891901911921931941951961971981992002012022032042052062072= 0820921021121221321421521621721821922022122222322422522622722822923023123= 2233234235236237238239240241242243244245246247248249250251252253254255256= 2572582592602612622632642652662672682692702712722732742752762772782792802= 8128228328428528628728828929029129229329429529629729829930030130230330430= 5306307308309310311312313314315316317318319320321322323324325326327328329= 3303313323333343353363373383393403413423433443453463473483493503513523533= 5435535635735835936036136236336436536636736836937037137237337437537637737= 8379380381382383384385386387388389390391392393394395396397398399400401402= 4034044054064074084094104114124134144154164174184194204214224234244254264= 2742842943043143243343443543643743843944044144244344444544644744844945045= 1452453454455456457458459460461462463464465466467468469470471472473474475= 4764774784794804814824834844854864874884894904914924934944954964974984995= 0050150250350450550650750850951051151251351451551651751851952052152252352= 4525526527528529530531532533534535536537538539540541542543544545546547548= 5495505515525535545555565575585595605615625635645655665675685695705715725= 7357457557657757857958058158258358458558658758858959059159259359459559659= 7598599600601602603604605606607608609610611612613614615616617618619620621= 6226236246256266276286296306316326336346356366376386396406416426436446456= 4664764864965065165265365465565665765865966066166266366466566666766866967= 0671672673674675676677678679680681682683684685686687688689690691692693694= 6956966976986997007017027037047057067077087097107117127137147157167177187= 1972072172272372472572672772872973073173273373473573673773873974074174274= 3744745746747748749750751752753754755756757758759760761762763764765766767= 7687697707717727737747757767777787797807817827837847857867877887897907917= 9279379479579679779879980080180280380480580680780880981081181281381481581= 6817818819820821822823824825826827828829830831832833834835836837838839840= 8418428438448458468478488498508518528538548558568578588598608618628638648= 6586686786886987087187287387487587687787887988088188288388488588688788888= 9890891892893894895896897898899900901902903904905906907908909910911912913= 9149159169179189199209219229239249259269279289299309319329339349359369379= 3893994094194294394494594694794894995095195295395495595695795895996096196= 2963964965966967968969970971972973974975976977978979980981982983984985986= 9879889899909919929939949959969979989991000100110021003100410051006100710= 0810091010101110121013101410151016101710181019102010211022102310241025102= 6102710281029103010311032103310341035103610371038103910401041104210431044= 1045104610471048104910501051105210531054105510561057105810591060106110621= 0631064106510661067106810691070107110721073107410751076107710781079108010= 8110821083108410851086108710881089109010911092109310941095109610971098109= 9110011011102110311041105110611071108110911101111111211131114111511161117= 1118111911201121112211231124112511261127112811291130113111321133113411351= 1361137113811391140114111421143114411451146114711481149115011511152115311= 5411551156115711581159116011611162116311641165116611671168116911701171117= 2117311741175117611771178117911801181118211831184118511861187118811891190= 1191119211931194119511961197119811991200120112021203120412051206120712081= 2091210121112121213121412151216121712181219122012211222122312241225122612= 2712281229123012311232123312341235123612371238123912401241124212431244124= 5124612471248124912501251125212531254125512561257125812591260126112621263= 1264126512661267126812691270127112721273127412751276127712781279128012811= 2821283128412851286128712881289129012911292129312941295129612971298129913= 0013011302130313041305130613071308130913101311131213131314131513161317131= 8131913201321132213231324132513261327132813291330133113321333133413351336= 1337133813391340134113421343134413451346134713481349135013511352135313541= 3551356135713581359136013611362136313641365136613671368136913701371137213= 7313741375137613771378137913801381138213831384138513861387138813891390139= 1139213931394139513961397139813991400140114021403140414051406140714081409= 1410141114121413141414151416141714181419142014211422142314241425142614271= 4281429143014311432143314341435143614371438143914401441144214431444144514= 4614471448144914501451145214531454145514561457145814591460146114621463146= 4146514661467146814691470147114721473147414751476147714781479148014811482= 1483148414851486148714881489149014911492149314941495149614971498149915001= 5011502150315041505150615071508150915101511151215131514151515161517151815= 1915201521152215231524152515261527152815291530153115321533153415351536153= 7153815391540154115421543154415451546154715481549155015511552155315541555= 1556155715581559156015611562156315641565156615671568156915701571157215731= 5741575157615771578157915801581158215831584158515861587158815891590159115= 9215931594159515961597159815991600160116021603160416051606160716081609161= 0161116121613161416151616161716181619162016211622162316241625162616271628= 1629163016311632163316341635163616371638163916401641164216431644164516461= 6471648164916501651165216531654165516561657165816591660166116621663166416= 6516661667166816691670167116721673167416751676167716781679168016811682168= 3168416851686168716881689169016911692169316941695169616971698169917001701= 1702170317041705170617071708170917101711171217131714171517161717171817191= 7201721172217231724172517261727172817291730173117321733173417351736173717= 3817391740174117421743174417451746174717481749175017511752175317541755175= 6175717581759176017611762176317641765176617671768176917701771177217731774= 1775177617771778177917801781178217831784178517861787178817891790179117921= 7931794179517961797179817991800180118021803180418051806180718081809181018= 1118121813181418151816181718181819182018211822182318241825182618271828182= 9183018311832183318341835183618371838183918401841184218431844184518461847= 1848184918501851185218531854185518561857185818591860186118621863186418651= 8661867186818691870187118721873187418751876187718781879188018811882188318= 8418851886188718881889189018911892189318941895189618971898189919001901190= 2190319041905190619071908190919101911191219131914191519161917191819191920= 1921192219231924192519261927192819291930193119321933193419351936193719381= 9391940194119421943194419451946194719481949195019511952195319541955195619= 5719581959196019611962196319641965196619671968196919701971197219731974197= 5197619771978197919801981198219831984198519861987198819891990199119921993= 1994199519961997199819992000' rand1[double precision]:79 toasted_col2[tex= t]:null rand2[double precision]:1578 COMMIT ! (109 rows) =20 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') F= ROM generate_series(1, 2000) g(i); -- update of second column, first column unchanged diff --git a/contrib/test_decoding/sql/ddl.sqindex 057dae056b..0fb4102db8= 100644 *** a/contrib/test_decoding/sql/ddl.sql --- b/contrib/test_decoding/sql/ddl.sql *************** *** 333,338 **** UPDATE table_with_unique_not_null SET id =3D -id; --- 333,341 ---- UPDATE table_with_unique_not_null SET id =3D -id; DELETE FROM table_with_unique_not_null WHERE data =3D 3; =20 + TRUNCATE table_with_unique_not_null; + TRUNCATE table_with_unique_not_null, table_with_unique_not_null RESTART= IDENTITY CASCADE; +=20 -- check toast support BEGIN; CREATE SEQUENCE toasttable_rand_seq START 79 INCREMENT 1499; -- portabl= e "random" diff --git a/contrib/test_decoding/test_index 135b3b7638..f99f91b9b0 1006= 44 *** a/contrib/test_decoding/test_decoding.c --- b/contrib/test_decoding/test_decoding.c *************** *** 466,471 **** pg_decode_change(LogicalDecodingContext *ctx, ReorderBuf= ferTXN *txn, --- 466,485 ---- &change->data.tp.oldtuple->tuple, true); break; + case REORDER_BUFFER_CHANGE_TRUNCATE: + appendStringInfoString(ctx->out, " TRUNCATE:"); +=20 + if (change->data.truncate_msg.restart_seqs + || change->data.truncate_msg.cascade) + { + if (change->data.truncate_msg.restart_seqs) + appendStringInfo(ctx->out, " restart_seqs"); + if (change->data.truncate_msg.cascade) + appendStringInfo(ctx->out, " cascade"); + } + else + appendStringInfoString(ctx->out, " (no-flags)"); + break; default: Assert(false); } diff --git a/doc/src/sgml/logical-replicatioindex 75551d8ee1..6b64c8ef76 = 100644 *** a/doc/src/sgml/logical-replication.sgml --- b/doc/src/sgml/logical-replication.sgml *************** *** 111,116 **** --- 111,118 ---- any combination of INSERT, UPDATE, and DELETE, similar to how triggers are fired by particular event types. By default, all operation types are replica= ted. + TRUNCATE is treated as a form of DELETE<= /command> + for the purpose of deciding whether to publish, or not. =20 *************** *** 364,378 **** =20 - - - TRUNCATE commands are not replicated. This can= , of - course, be worked around by using DELETE instea= d. To - avoid accidental TRUNCATE invocations, you can = revoke - the TRUNCATE privilege from tables. - - -=20 Large objects (see ) are not repli= cated. --- 366,371 ---- diff --git a/doc/src/sgml/protocol.sgml b/doindex 8174e3defa..88cbecfbdd = 100644 *** a/doc/src/sgml/protocol.sgml --- b/doc/src/sgml/protocol.sgml *************** *** 6828,6833 **** TupleData --- 6828,6874 ---- =20 + + + Truncate + + + +=20 + + + + Byte1('T') + + + + Identifies the message as a truncate message. + + + + + + Int32 + + + + ID of the relation corresponding to the ID in the relat= ion + message. + + + + + + Int8 + + + + Option flags for truncate. Currently can be 0 for no fl= ags, + 1 for CASCADE, 2 for RESTART IDENTITY and 3 if both are= set. + + + +=20 diff --git a/doc/src/sgml/ref/creindex bfe12d5f41..c5032938a7 100644 *** a/doc/src/sgml/ref/create_publication.sgml --- b/doc/src/sgml/ref/create_publication.sgml *************** *** 111,116 **** CREATE PUBLICATION name= --- 111,121 ---- and so the default value for this option is 'insert, update, delete'. + + TRUNCATE is treated as a form of + DELETE for the purpose of deciding whethe= r + to publish, or not. + *************** *** 168,175 **** CREATE PUBLICATION name= =20 ! TRUNCATE and DDL operations ! are not published. =20 --- 173,179 ---- =20 ! DDL operations are not published. =20 diff --git a/src/backend/access/heap/heapam.c bindex 54f1100ffd..2e1cea37= 3d 100644 *** a/src/backend/access/heap/heapam.c --- b/src/backend/access/heap/heapam.c *************** *** 9134,9139 **** heap_redo(XLogReaderState *record) --- 9134,9146 ---- case XLOG_HEAP_UPDATE: heap_xlog_update(record, false); break; + case XLOG_HEAP_TRUNCATE: + /* + * TRUNCATE is a no-op because the actions are already logged + * as SMGR WAL records. TRUNCATE WAL record only exists to allow + * it to be logically decoded precisely. + */ + break; case XLOG_HEAP_HOT_UPDATE: heap_xlog_update(record, true); break; diff --git a/src/backend/access/rmgrdesindex 44d2d6333f..a44996379b 10064= 4 *** a/src/backend/access/rmgrdesc/heapdesc.c --- b/src/backend/access/rmgrdesc/heapdesc.c *************** *** 75,80 **** heap_desc(StringInfo buf, XLogReaderState *record) --- 75,93 ---- xlrec->new_offnum, xlrec->new_xmax); } + else if (info =3D=3D XLOG_HEAP_TRUNCATE) + { + xl_heap_truncate *xlrec =3D (xl_heap_truncate *) rec; +=20 + if (xlrec->flags & XLH_TRUNCATE_CASCADE) + appendStringInfo(buf, "cascade "); + if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS) + appendStringInfo(buf, "restart_seqs "); + appendStringInfo(buf, "nrelids %u nseqrelids %u", + xlrec->nrelids, + xlrec->nseqrelids); + /* Skip the list of relids and seqrelids */ + } else if (info =3D=3D XLOG_HEAP_CONFIRM) { xl_heap_confirm *xlrec =3D (xl_heap_confirm *) rec; *************** *** 186,191 **** heap_identify(uint8 info) --- 199,207 ---- case XLOG_HEAP_HOT_UPDATE | XLOG_HEAP_INIT_PAGE: id =3D "HOT_UPDATE+INIT"; break; + case XLOG_HEAP_TRUNCATE: + id =3D "TRUNCATE"; + break; case XLOG_HEAP_CONFIRM: id =3D "HEAP_CONFIRM"; break; diff --git a/src/backend/commands/tablecmds.cindex 296807849f..e5f7cb320f= 100644 *** a/src/backend/commands/tablecmds.c --- b/src/backend/commands/tablecmds.c *************** *** 16,21 **** --- 16,22 ---- =20 #include "access/genam.h" #include "access/heapam.h" + #include "access/heapam_xlog.h" #include "access/multixact.h" #include "access/reloptions.h" #include "access/relscan.h" *************** *** 1249,1259 **** ExecuteTruncate(TruncateStmt *stmt) { List *rels =3D NIL; List *relids =3D NIL; ! List *seq_relids =3D NIL; ! EState *estate; ! ResultRelInfo *resultRelInfos; ! ResultRelInfo *resultRelInfo; ! SubTransactionId mySubid; ListCell *cell; =20 /* --- 1250,1256 ---- { List *rels =3D NIL; List *relids =3D NIL; ! List *relids_logged =3D NIL; ListCell *cell; =20 /* *************** *** 1277,1282 **** ExecuteTruncate(TruncateStmt *stmt) --- 1274,1282 ---- truncate_check_rel(rel); rels =3D lappend(rels, rel); relids =3D lappend_oid(relids, myrelid); + /* Log this relation only if needed for logical decoding */ + if (RelationIsLogicallyLogged(rel)) + relids_logged =3D lappend_oid(relids_logged, myrelid); =20 if (recurse) { *************** *** 1297,1302 **** ExecuteTruncate(TruncateStmt *stmt) --- 1297,1305 ---- truncate_check_rel(rel); rels =3D lappend(rels, rel); relids =3D lappend_oid(relids, childrelid); + /* Log this relation only if needed for logical decoding */ + if (RelationIsLogicallyLogged(rel)) + relids_logged =3D lappend_oid(relids_logged, childrelid); } } else if (rel->rd_rel->relkind =3D=3D RELKIND_PARTITIONED_TABLE) *************** *** 1306,1312 **** ExecuteTruncate(TruncateStmt *stmt) --- 1309,1337 ---- errhint("Do not specify the ONLY keyword, or use truncate only on= the partitions directly."))); } =20 + ExecuteTruncateGuts(rels, relids, relids_logged, + stmt->behavior, stmt->restart_seqs); + } +=20 + void + ExecuteTruncateGuts(List *rels, List *relids, List *relids_logged, + DropBehavior behavior, bool restart_seqs) + { + List *seq_relids =3D NIL; + EState *estate; + ResultRelInfo *resultRelInfos; + ResultRelInfo *resultRelInfo; + SubTransactionId mySubid; + ListCell *cell; + List *seq_relids_logged =3D NIL; + uint32 nrelids =3D 0; + uint32 nseqrelids =3D 0; + uint32 maxrelids =3D 2; + Oid *logrelids =3D NULL; +=20 /* + * Open, exclusive-lock, and check all the explicitly-specified relati= ons + * * In CASCADE mode, suck in all referencing relations as well. This * requires multiple iterations to find indirectly-dependent relations= =2E At * each phase, we need to exclusive-lock new rels before looking for t= heir *************** *** 1314,1320 **** ExecuteTruncate(TruncateStmt *stmt) * soon as we open it, to avoid a faux pas such as holding lock for a = long * time on a rel we have no permissions for. */ ! if (stmt->behavior =3D=3D DROP_CASCADE) { for (;;) { --- 1339,1345 ---- * soon as we open it, to avoid a faux pas such as holding lock for a = long * time on a rel we have no permissions for. */ ! if (behavior =3D=3D DROP_CASCADE) { for (;;) { *************** *** 1336,1341 **** ExecuteTruncate(TruncateStmt *stmt) --- 1361,1369 ---- truncate_check_rel(rel); rels =3D lappend(rels, rel); relids =3D lappend_oid(relids, relid); + /* Log this relation only if needed for logical decoding */ + if (RelationIsLogicallyLogged(rel)) + relids_logged =3D lappend_oid(relids_logged, relid); } } } *************** *** 1355,1361 **** ExecuteTruncate(TruncateStmt *stmt) #ifdef USE_ASSERT_CHECKING heap_truncate_check_FKs(rels, false); #else ! if (stmt->behavior =3D=3D DROP_RESTRICT) heap_truncate_check_FKs(rels, false); #endif } --- 1383,1389 ---- #ifdef USE_ASSERT_CHECKING heap_truncate_check_FKs(rels, false); #else ! if (behavior =3D=3D DROP_RESTRICT) heap_truncate_check_FKs(rels, false); #endif } *************** *** 1366,1372 **** ExecuteTruncate(TruncateStmt *stmt) * We want to do this early since it's pointless to do all the truncat= ion * work only to fail on sequence permissions. */ ! if (stmt->restart_seqs) { foreach(cell, rels) { --- 1394,1400 ---- * We want to do this early since it's pointless to do all the truncat= ion * work only to fail on sequence permissions. */ ! if (restart_seqs) { foreach(cell, rels) { *************** *** 1387,1392 **** ExecuteTruncate(TruncateStmt *stmt) --- 1415,1424 ---- RelationGetRelationName(seq_rel)); =20 seq_relids =3D lappend_oid(seq_relids, seq_relid); + /* Log this relation only if needed for logical decoding */ + if (RelationIsLogicallyLogged(seq_rel)) + seq_relids_logged =3D lappend_oid(seq_relids_logged, + seq_relid); =20 relation_close(seq_rel, NoLock); } *************** *** 1521,1526 **** ExecuteTruncate(TruncateStmt *stmt) --- 1553,1612 ---- ResetSequence(seq_relid); } =20 + /* + * Write a WAL record to allow this set of actions to be logically dec= oded. + * We could optimize this away when !RelationIsLogicallyLogged(rel) + * but that doesn't save much space or time. + * + * Assemble an array of relids, then an array of seqrelids so we can w= rite + * a single WAL record for the whole action. + */ + logrelids =3D palloc(maxrelids * sizeof(Oid)); + foreach (cell, relids_logged) + { + nrelids++; + if (nrelids > maxrelids) + { + maxrelids *=3D 2; + logrelids =3D repalloc(logrelids, maxrelids * sizeof(Oid)); + } + logrelids[nrelids - 1] =3D lfirst_oid(cell); + } +=20 + foreach (cell, seq_relids_logged) + { + nseqrelids++; + if ((nrelids + nseqrelids) > maxrelids) + { + maxrelids *=3D 2; + logrelids =3D repalloc(logrelids, maxrelids * sizeof(Oid)); + } + logrelids[nrelids + nseqrelids - 1] =3D lfirst_oid(cell); + } +=20 + if ((nrelids + nseqrelids) > 0) + { + xl_heap_truncate xlrec; +=20 + xlrec.dbId =3D MyDatabaseId; + xlrec.nrelids =3D nrelids; + xlrec.nseqrelids =3D nseqrelids; + xlrec.flags =3D 0; + if (behavior =3D=3D DROP_CASCADE) + xlrec.flags |=3D XLH_TRUNCATE_CASCADE; + if (restart_seqs) + xlrec.flags |=3D XLH_TRUNCATE_RESTART_SEQS; +=20 + XLogBeginInsert(); + XLogRegisterData((char *) &xlrec, SizeOfHeapTruncate); + XLogRegisterData((char *) logrelids, + (nrelids + nseqrelids) * sizeof(Oid)); +=20 + XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); +=20 + (void) XLogInsert(RM_HEAP_ID, XLOG_HEAP_TRUNCATE); + } +=20 /* * Process all AFTER STATEMENT TRUNCATE triggers. */ diff --git a/src/backend/replication/loindex 486fd0c988..e06dcfe62d 10064= 4 *** a/src/backend/replication/logical/decode.c --- b/src/backend/replication/logical/decode.c *************** *** 65,70 **** static void DecodeLogicalMsgOp(LogicalDecodingContext *ctx= , XLogRecordBuffer *bu --- 65,71 ---- static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer = *buf); static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer = *buf); static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer = *buf); + static void DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffe= r *buf); static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBu= ffer *buf); static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBu= ffer *buf); =20 *************** *** 435,440 **** DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuff= er *buf) --- 436,446 ---- DecodeDelete(ctx, buf); break; =20 + case XLOG_HEAP_TRUNCATE: + if (SnapBuildProcessChange(builder, xid, buf->origptr)) + DecodeTruncate(ctx, buf); + break; +=20 case XLOG_HEAP_INPLACE: =20 /* *************** *** 805,810 **** DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuff= er *buf) --- 811,856 ---- ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,= change); } =20 + /* + * Parse XLOG_HEAP_TRUNCATE from wal + */ + static void + DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) + { + XLogReaderState *r =3D buf->record; + xl_heap_truncate *xlrec; + ReorderBufferChange *change; + int i; +=20 + xlrec =3D (xl_heap_truncate *) XLogRecGetData(r); +=20 + /* only interested in our database */ + if (xlrec->dbId !=3D ctx->slot->data.database) + return; +=20 + /* output plugin doesn't look for this origin, no need to queue */ + if (FilterByOrigin(ctx, XLogRecGetOrigin(r))) + return; +=20 + change =3D ReorderBufferGetChange(ctx->reorder); + change->action =3D REORDER_BUFFER_CHANGE_TRUNCATE; + change->origin_id =3D XLogRecGetOrigin(r); + if (xlrec->flags & XLH_TRUNCATE_CASCADE) + change->data.truncate_msg.cascade =3D true; + if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS) + change->data.truncate_msg.restart_seqs =3D true; +=20 + /* + * Queue up one change per relation, ignoring sequences for now + */ + for (i =3D 0; i < xlrec->nrelids; i++) + { + change->data.truncate_msg.relid =3D xlrec->relids[i]; + ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), + buf->origptr, change); + } + } +=20 /* * Decode XLOG_HEAP2_MULTI_INSERT_insert record into multiple tuplebufs= =2E * diff --git a/src/backend/replication/logical/prindex 9b126b2957..8ffa486e= 8c 100644 *** a/src/backend/replication/logical/proto.c --- b/src/backend/replication/logical/proto.c *************** *** 26,31 **** --- 26,34 ---- */ #define LOGICALREP_IS_REPLICA_IDENTITY 1 =20 + #define TRUNCATE_CASCADE (1<<0) + #define TRUNCATE_RESTART_SEQS (1<<1) +=20 static void logicalrep_write_attrs(StringInfo out, Relation rel); static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple); *************** *** 292,297 **** logicalrep_read_delete(StringInfo in, LogicalRepTupleDat= a *oldtup) --- 295,342 ---- return relid; } =20 + /* + * Write TRUNCATE to the output stream. + */ + void + logicalrep_write_truncate(StringInfo out, Relation rel, + bool cascade, bool restart_seqs) + { + uint8 flags =3D 0; +=20 + pq_sendbyte(out, 'T'); /* action TRUNCATE */ +=20 + /* use Oid as relation identifier */ + pq_sendint32(out, RelationGetRelid(rel)); +=20 + /* encode and send truncate flags */ + if (cascade) + flags |=3D TRUNCATE_CASCADE; + if (restart_seqs) + flags |=3D TRUNCATE_RESTART_SEQS; + pq_sendint8(out, flags); + } +=20 + /* + * Read TRUNCATE from stream. + */ + LogicalRepRelId + logicalrep_read_truncate(StringInfo in, bool *cascade, bool *restart_se= qs) + { + LogicalRepRelId relid; + uint8 flags; +=20 + /* read the relation id */ + relid =3D pq_getmsgint(in, 4); +=20 + /* read and decode truncate flags */ + flags =3D pq_getmsgint(in, 1); + *cascade =3D (flags & TRUNCATE_CASCADE) > 0; + *restart_seqs =3D (flags & TRUNCATE_RESTART_SEQS) > 0; +=20 + return relid; + } +=20 /* * Write relation description to the output stream. */ diff --git a/src/backend/replication/logical/rindex 5ac391dbda..4eefa5368= 1 100644 *** a/src/backend/replication/logical/reorderbuffer.c --- b/src/backend/replication/logical/reorderbuffer.c *************** *** 403,408 **** ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBuff= erChange *change) --- 403,410 ---- case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID: case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID: break; + case REORDER_BUFFER_CHANGE_TRUNCATE: + break; } =20 pfree(change); *************** *** 1342,1347 **** ReorderBufferCommit(ReorderBuffer *rb, TransactionId x= id, --- 1344,1356 ---- =20 switch (change->action) { + case REORDER_BUFFER_CHANGE_TRUNCATE: + reloid =3D change->data.truncate_msg.relid; + relation =3D RelationIdGetRelation(reloid); + rb->apply_change(rb, txn, relation, change); + RelationClose(relation); + break; +=20 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM: =20 /* *************** *** 2224,2229 **** ReorderBufferSerializeChange(ReorderBuffer *rb, Reorde= rBufferTXN *txn, --- 2233,2239 ---- } break; } + case REORDER_BUFFER_CHANGE_TRUNCATE: case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM: case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID: case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID: *************** *** 2509,2514 **** ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderB= ufferTXN *txn, --- 2519,2525 ---- break; } /* the base struct contains all the data, easy peasy */ + case REORDER_BUFFER_CHANGE_TRUNCATE: case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM: case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID: case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID: diff --git a/src/backend/replication/logical/worker.c index fa5d9bb120..1= 7e5b4b150 100644 *** a/src/backend/replication/logical/worker.c --- b/src/backend/replication/logical/worker.c *************** *** 869,874 **** apply_handle_delete(StringInfo s) --- 869,922 ---- CommandCounterIncrement(); } =20 + /* + * Handle TRUNCATE message. + * + * TODO: FDW support + */ + static void + apply_handle_truncate(StringInfo s) + { + LogicalRepRelMapEntry *rel; + LogicalRepRelId relid; + bool cascade =3D false; + bool restart_seqs =3D false; + List *rels =3D NIL; + List *relids =3D NIL; +=20 + ensure_transaction(); +=20 + relid =3D logicalrep_read_truncate(s, &cascade, &restart_seqs); + rel =3D logicalrep_rel_open(relid, RowExclusiveLock); + if (!should_apply_changes_for_rel(rel)) + { + /* + * The relation can't become interesting in the middle of the + * transaction so it's safe to unlock it. + */ + logicalrep_rel_close(rel, RowExclusiveLock); + return; + } +=20 + /* Check if we can do the truncate. */ + check_relation_updatable(rel); +=20 + rels =3D lappend(rels, rel->localrel); + relids =3D lappend_oid(relids, rel->localreloid); +=20 + ExecuteTruncateGuts(rels, relids, NULL, + cascade ? DROP_CASCADE : DROP_RESTRICT, + restart_seqs); +=20 + /* logicalrep_rel_close call not needed, because ExecuteTruncateGuts + * already closes the relations. Setting localrel to NULL in the map e= ntry + * is still needed. + */ + rel->localrel =3D NULL; +=20 + CommandCounterIncrement(); + } +=20 =20 /* * Logical replication protocol message dispatcher. *************** *** 900,905 **** apply_dispatch(StringInfo s) --- 948,957 ---- case 'D': apply_handle_delete(s); break; + /* TRUNCATE */ + case 'T': + apply_handle_truncate(s); + break; /* RELATION */ case 'R': apply_handle_relation(s); diff --git a/src/backend/replication/pgoutput/pindex 550b156e2d..5bd5af98= c5 100644 *** a/src/backend/replication/pgoutput/pgoutput.c --- b/src/backend/replication/pgoutput/pgoutput.c *************** *** 275,280 **** pgoutput_change(LogicalDecodingContext *ctx, ReorderBuff= erTXN *txn, --- 275,281 ---- if (!relentry->pubactions.pubupdate) return; break; + case REORDER_BUFFER_CHANGE_TRUNCATE: case REORDER_BUFFER_CHANGE_DELETE: if (!relentry->pubactions.pubdelete) return; *************** *** 352,357 **** pgoutput_change(LogicalDecodingContext *ctx, ReorderBuff= erTXN *txn, --- 353,367 ---- else elog(DEBUG1, "didn't send DELETE change because of missing oldtuple= "); break; + case REORDER_BUFFER_CHANGE_TRUNCATE: + { + OutputPluginPrepareWrite(ctx, true); + logicalrep_write_truncate(ctx->out, relation, + change->data.truncate_msg.cascade, + change->data.truncate_msg.restart_seqs); + OutputPluginWrite(ctx, true); + } + break; default: Assert(false); } diff --git a/src/include/access/heapam_xlog.h b/srindex 38f7f63984..88b57= 91e7c 100644 *** a/src/include/access/heapam_xlog.h --- b/src/include/access/heapam_xlog.h *************** *** 32,38 **** #define XLOG_HEAP_INSERT 0x00 #define XLOG_HEAP_DELETE 0x10 #define XLOG_HEAP_UPDATE 0x20 ! /* 0x030 is free, was XLOG_HEAP_MOVE */ #define XLOG_HEAP_HOT_UPDATE 0x40 #define XLOG_HEAP_CONFIRM 0x50 #define XLOG_HEAP_LOCK 0x60 --- 32,38 ---- #define XLOG_HEAP_INSERT 0x00 #define XLOG_HEAP_DELETE 0x10 #define XLOG_HEAP_UPDATE 0x20 ! #define XLOG_HEAP_TRUNCATE 0x30 #define XLOG_HEAP_HOT_UPDATE 0x40 #define XLOG_HEAP_CONFIRM 0x50 #define XLOG_HEAP_LOCK 0x60 *************** *** 109,114 **** typedef struct xl_heap_delete --- 109,136 ---- =20 #define SizeOfHeapDelete (offsetof(xl_heap_delete, flags) + sizeof(uint= 8)) =20 + /* + * xl_heap_delete flag values, 8 bits are available. + */ + #define XLH_TRUNCATE_CASCADE (1<<0) + #define XLH_TRUNCATE_RESTART_SEQS (1<<1) +=20 + /* + * For truncate we list all truncated relids in an array, followed by a= ll + * sequence relids that need to be restarted, if any. + * All rels are always within the same database, so we just list dbid o= nce. + */ + typedef struct xl_heap_truncate + { + Oid dbId; + uint32 nrelids; + uint32 nseqrelids; + uint8 flags; + Oid relids[FLEXIBLE_ARRAY_MEMBER]; + } xl_heap_truncate; +=20 + #define SizeOfHeapTruncate (offsetof(xl_heap_truncate, relids)) +=20 /* * We don't store the whole fixed part (HeapTupleHeaderData) of an inse= rted * or updated tuple in WAL; we can save a few bytes by reconstructing t= he diff --git a/src/include/executor/execuindex 2cc74da0ba..027def882e 10064= 4 *** a/src/include/executor/executor.h --- b/src/include/executor/executor.h *************** *** 553,556 **** extern void CheckCmdReplicaIdentity(Relation rel, CmdTyp= e cmd); --- 553,562 ---- extern void CheckSubscriptionRelkind(char relkind, const char *nspname,= const char *relname); =20 + /* + * prototype for tablecmds.c accessible for logical apply + */ + void ExecuteTruncateGuts(List *rels, List *relids, List *relids_logged,= + DropBehavior behavior, bool restart_seqs); +=20 #endif /* EXECUTOR_H */ diff --git a/src/include/replication/lindex a9736e1bf6..bd5e579980 100644= *** a/src/include/replication/logicalproto.h --- b/src/include/replication/logicalproto.h *************** *** 98,103 **** extern void logicalrep_write_delete(StringInfo out, Relat= ion rel, --- 98,107 ---- HeapTuple oldtuple); extern LogicalRepRelId logicalrep_read_delete(StringInfo in, LogicalRepTupleData *oldtup); + extern void logicalrep_write_truncate(StringInfo out, Relation rel, + bool cascade, bool restart_seqs); + extern LogicalRepRelId logicalrep_read_truncate(StringInfo in, + bool *cascade, bool *restart_seqs); extern void logicalrep_write_rel(StringInfo out, Relation rel); extern LogicalRepRelation *logicalrep_read_rel(StringInfo in); extern void logicalrep_write_typ(StringInfo out, Oid typoid); diff --git a/src/include/replication/reorderbindex b18ce5a9df..5a29e1ab5b= 100644 *** a/src/include/replication/reorderbuffer.h --- b/src/include/replication/reorderbuffer.h *************** *** 59,65 **** enum ReorderBufferChangeType REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID, REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT, ! REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM }; =20 /* --- 59,66 ---- REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID, REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT, ! REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM, ! REORDER_BUFFER_CHANGE_TRUNCATE }; =20 /* *************** *** 128,134 **** typedef struct ReorderBufferChange CommandId cmax; CommandId combocid; } tuplecid; ! } data; =20 /* * While in use this is how a change is linked into a transactions, --- 129,146 ---- CommandId cmax; CommandId combocid; } tuplecid; !=20 ! /* ! * Truncate data for REORDER_BUFFER_CHANGE_TRUNCATE representing ! * one relation to be truncated. ! */ ! struct ! { ! Oid relid; ! bool cascade; ! bool restart_seqs; ! } truncate_msg; ! } data; =20 /* * While in use this is how a change is linked into a transactions, diff --git a/src/test/subscription/t/001_rep_cindex 0136c79d4b..974e88821= c 100644 *** a/src/test/subscription/t/001_rep_changes.pl --- b/src/test/subscription/t/001_rep_changes.pl *************** *** 3,9 **** use strict; use warnings; use PostgresNode; use TestLib; ! use Test::More tests =3D> 16; =20 # Initialize publisher node my $node_publisher =3D get_new_node('publisher'); --- 3,9 ---- use warnings; use PostgresNode; use TestLib; ! use Test::More tests =3D> 24; =20 # Initialize publisher node my $node_publisher =3D get_new_node('publisher'); *************** *** 189,199 **** $result =3D $node_subscriber->safe_psql('postgres', is($result, qq(20|-20|-1), 'check changes skipped after subscription publication change'); =20 # check alter publication (relcache invalidation etc) $node_publisher->safe_psql('postgres', "ALTER PUBLICATION tap_pub_ins_only SET (publish =3D 'insert, delete')= "); $node_publisher->safe_psql('postgres', ! "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_full"); $node_publisher->safe_psql('postgres', "DELETE FROM tab_ins WHERE a > 0= "); $node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION WITH (copy_data =3D fa= lse)" --- 189,210 ---- is($result, qq(20|-20|-1), 'check changes skipped after subscription publication change'); =20 + # truncate should not be replicated + $node_publisher->safe_psql('postgres', "TRUNCATE tab_rep"); +=20 + $node_publisher->poll_query_until('postgres', $caughtup_query) + or die "Timed out while waiting for subscriber to catch up"; +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT count(*), min(a), max(a) FROM tab_rep"); + is($result, qq(20|-20|-1), + 'check changes skipped after subscription publication change'); +=20 # check alter publication (relcache invalidation etc) $node_publisher->safe_psql('postgres', "ALTER PUBLICATION tap_pub_ins_only SET (publish =3D 'insert, delete')= "); $node_publisher->safe_psql('postgres', ! "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_full, tab_rep"); $node_publisher->safe_psql('postgres', "DELETE FROM tab_ins WHERE a > 0= "); $node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION WITH (copy_data =3D fa= lse)" *************** *** 213,218 **** $result =3D $node_subscriber->safe_psql('postgres', --- 224,295 ---- "SELECT count(*), min(a), max(a) FROM tab_full"); is($result, qq(21|0|100), 'check replicated insert after alter publicat= ion'); =20 + # add a sequence and a foreign key to check cascade and restart + # identity truncate options + $node_subscriber->safe_psql('postgres', + "CREATE TABLE tab_notrep_fk (a int REFERENCES tab_rep(a))" + ); + $node_subscriber->safe_psql('postgres', "INSERT INTO tab_notrep_fk VALU= ES (-1)"); + $node_subscriber->safe_psql('postgres', + "CREATE SEQUENCE seq_notrep OWNED BY tab_rep.a" + ); + $node_subscriber->safe_psql('postgres', + "ALTER SEQUENCE seq_notrep START 101" + ); +=20 + # truncate should now be replicated + $node_publisher->safe_psql('postgres', "TRUNCATE tab_rep"); +=20 + $node_publisher->poll_query_until('postgres', $caughtup_query) + or die "Timed out while waiting for subscriber to catch up"; +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT count(*), min(a), max(a) FROM tab_rep"); + is($result, qq(0||), + 'check replicated truncate after alter publication'); +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT count(*), min(a), max(a) FROM tab_notrep_fk"); + is($result, qq(1|-1|-1), + 'check replicated truncate does not cascade'); +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT nextval('seq_notrep')"); + is($result, qq(1), + 'check replicated truncate does not restart identities'); +=20 + # should restart identity + $node_publisher->safe_psql('postgres', "TRUNCATE tab_rep RESTART IDENTI= TY"); +=20 + $node_publisher->poll_query_until('postgres', $caughtup_query) + or die "Timed out while waiting for subscriber to catch up"; +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT count(*), min(a), max(a) FROM tab_notrep_fk"); + is($result, qq(1|-1|-1), + 'check replicated truncate does not cascade'); +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT nextval('seq_notrep')"); + is($result, qq(101), + 'check replicated truncate restart identities'); +=20 + # should cascade + $node_publisher->safe_psql('postgres', "TRUNCATE tab_rep CASCADE"); +=20 + $node_publisher->poll_query_until('postgres', $caughtup_query) + or die "Timed out while waiting for subscriber to catch up"; +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT count(*), min(a), max(a) FROM tab_notrep_fk"); + is($result, qq(0||), + 'check replicated truncate cascade'); +=20 + $result =3D $node_subscriber->safe_psql('postgres', + "SELECT nextval('seq_notrep')"); + is($result, qq(102), + 'check replicated truncate does not restart identities'); +=20 # check restart on rename $oldpid =3D $node_publisher->safe_psql('postgres', "SELECT pid FROM pg_stat_replication WHERE application_name =3D '$appn= ame';" --------------7B74D3083442431138F96ED7-- --Lztfbyk9FX1CaqVpp00GGHeTiY6FUb7uw-- --qQMRi61nzNcozNjmBIrzstZRUS9q3hiDE Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEfCO4BD5l0pgKIbbiWJ8D8BulUDgFAlpGP+oACgkQWJ8D8Bul UDiB4BAAiEiW5y4kgbSu6pLn1qtmSztn5PR3GAnRsUE/JETaDSZyg+yaV8K2TLdM 0XH5gyt+nvaH/8PGBFJ5O+s2j4869Ffo6Kzm7dUk3b6hc7oXXStRvIcSu3aHYbj1 ACcfIauKtNVnfMZRzCG6FL9hsRenzPj/3xDaFrqoCAJFn/x2J1QcyqvYKkAsv3uo 4Ikq2xDOGgDFAoZ6xjA6P/VMJsICjFXAAOPeNnzX/eSXpw65nk8QL8+NvYhz0r3d fkNbKqhgbttSW6NABUy3X7Occ+YrXFR6BdrLO4DUjpdM1nS2KNEtsIU1kBGMP2pA YTCNgzobTQgsLIh9S/tkFFPaAypYgLU3O8SHLKhFe7m8x5xa9QPgpxUiLl6oVV2c vJUB9tKDzX+ZbD19QYIJlVOpaWirV4qYKA7ArRXKwFs/WNj1y0oWViga9O4ms/Wk trSu0uKf81vQ+JbC7l6K/YJ2vLSWQJpWz4dGnwSC7IAW+iBIonpUn/Zyp933KNMI al2g+3XQtenN+u36U7Y/WayJVzG05bgtgpxq0cQCztz0ultHY0PWn+ZLE9c4eg93 RIOfx8+ToKYRFVig6y0XMWoF9jDTSJKvoxFPMKVxPUEOgJNBkjo3RMI+aNrGItnq zXhqvkYTYpMd5myWqk0gu4zjhdaK9MnB4d/iIgsYBZ5lGVeYf+s= =k7YT -----END PGP SIGNATURE----- --qQMRi61nzNcozNjmBIrzstZRUS9q3hiDE--