{"title":"Data change feed","description":"Query inserts, updates, and deletes between catalog snapshots using table_changes and related functions.","order":660,"askSuggestions":["When should I use the change feed instead of time travel?","How do I read only rows changed since my last sync?","Why does table_deletions miss rows I know were deleted?"],"navigation":true,"routeId":"lakehouseDataChangeFeed","body":{"code":"var Component=(()=>{var u=Object.create;var r=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,f=Object.prototype.hasOwnProperty;var w=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),_=(t,e)=>{for(var s in e)r(t,s,{get:e[s],enumerable:!0})},l=(t,e,s,a)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let d of g(e))!f.call(t,d)&&d!==s&&r(t,d,{get:()=>e[d],enumerable:!(a=m(e,d))||a.enumerable});return t};var k=(t,e,s)=>(s=t!=null?u(b(t)):{},l(e||!t||!t.__esModule?r(s,\"default\",{value:t,enumerable:!0}):s,t)),y=t=>l(r({},\"__esModule\",{value:!0}),t);var c=w((x,i)=>{i.exports=_jsx_runtime});var T={};_(T,{default:()=>p,frontmatter:()=>v});var n=k(c()),v={title:\"Data change feed\",routeId:\"lakehouseDataChangeFeed\",description:\"Query inserts, updates, and deletes between catalog snapshots using table_changes and related functions.\",order:660,askSuggestions:[\"When should I use the change feed instead of time travel?\",\"How do I read only rows changed since my last sync?\",\"Why does table_deletions miss rows I know were deleted?\"]};function o(t){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",h3:\"h3\",li:\"li\",p:\"p\",pre:\"pre\",strong:\"strong\",table:\"table\",tbody:\"tbody\",td:\"td\",th:\"th\",thead:\"thead\",tr:\"tr\",ul:\"ul\",...t.components},{DocsPreviewDocument:s,DocsPreviewSection:a,DocsTable:d}=e;return s||h(\"DocsPreviewDocument\",!0),a||h(\"DocsPreviewSection\",!0),d||h(\"DocsTable\",!0),(0,n.jsxs)(s,{anchors:\"data-change-feed time-travel-vs-change-feed finding-snapshot-bounds example-workflow changes-from-a-single-snapshot changes-across-multiple-snapshots changes-over-a-time-range table_changes table_deletions table_insertions compaction-and-retention learn-more\",children:[(0,n.jsxs)(a,{sectionId:\"data-change-feed\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,n.jsx)(e.h1,{id:\"data-change-feed\",children:\"Data Change Feed\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"Snapshots record every committed change to an Altertable catalog. \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel\",children:\"Time travel\"}),\" reads \",(0,n.jsx)(e.strong,{children:\"state\"}),\" \\u2014 what a table looked like at a snapshot or timestamp. The data change feed reads \",(0,n.jsx)(e.strong,{children:\"deltas\"}),\" \\u2014 which rows were inserted, updated, or deleted between two bounds \\u2014 for change data capture (CDC).\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Both features share the same snapshot timeline. Each committed \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/transactions\",children:\"transaction\"}),\" creates one snapshot, and bounds can be snapshot IDs or timestamps in either API.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Run the statements below through Altertable's \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"}),\" against an \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/catalogs\",children:\"Altertable catalog\"}),\" you own.\"]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"time-travel-vs-change-feed\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,n.jsx)(e.h2,{id:\"time-travel-vs-change-feed\",children:\"Time travel vs change feed\"}),`\n`,(0,n.jsx)(e.p,{children:\"Use time travel when you need a full table image at a point in time:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`SELECT sku, label\nFROM my_catalog.main.product_skus AT (VERSION => 2);\n`})}),`\n`,(0,n.jsx)(e.p,{children:\"Use the change feed when you need the row-level mutations that happened in or between snapshots:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`FROM my_catalog.table_changes('product_skus', 2, 4)\nORDER BY snapshot_id;\n`})}),`\n`,(0,n.jsxs)(e.table,{children:[(0,n.jsx)(e.thead,{children:(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.th,{children:\"Goal\"}),(0,n.jsx)(e.th,{children:\"Use\"})]})}),(0,n.jsxs)(e.tbody,{children:[(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"Reproduce a report as it ran last Tuesday\"}),(0,n.jsxs)(e.td,{children:[(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel\",children:\"Time travel\"}),\" with \",(0,n.jsx)(e.code,{children:\"AT (TIMESTAMP => \\u2026)\"})]})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"Sync only new and changed rows to a warehouse\"}),(0,n.jsxs)(e.td,{children:[(0,n.jsx)(e.code,{children:\"table_insertions\"}),\" / \",(0,n.jsx)(e.code,{children:\"table_changes\"}),\" between snapshot bounds\"]})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"Investigate who removed a SKU and when\"}),(0,n.jsxs)(e.td,{children:[(0,n.jsx)(e.code,{children:\"table_deletions\"}),\" or \",(0,n.jsx)(e.code,{children:\"table_changes\"}),\" for the suspect snapshot range\"]})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"Compare catalog state before and after a deploy\"}),(0,n.jsx)(e.td,{children:\"Time travel at two snapshot versions\"})]})]})]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"finding-snapshot-bounds\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,n.jsx)(e.h2,{id:\"finding-snapshot-bounds\",children:\"Finding snapshot bounds\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"Pass snapshot IDs or timestamps to \",(0,n.jsx)(e.code,{children:\"table_changes\"}),\" and its helpers. List available snapshots with \",(0,n.jsx)(e.code,{children:\"snapshots()\"}),\", or read the latest ID with \",(0,n.jsx)(e.code,{children:\"current_snapshot()\"}),\" \\u2014 the same helpers described in \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel#snapshots\",children:\"Time travel\"}),\".\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`SELECT snapshot_id, snapshot_time, changes, author, commit_message\nFROM my_catalog.snapshots()\nORDER BY snapshot_id;\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"Commit messages from \",(0,n.jsx)(e.code,{children:\"set_commit_message\"}),\" are especially useful here: they label snapshots in the change feed the same way they appear in the snapshot history.\"]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"example-workflow\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,n.jsx)(e.h2,{id:\"example-workflow\",children:\"Example workflow\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"The workflow below uses the same \",(0,n.jsx)(e.code,{children:\"product_skus\"}),\" table referenced in the time travel docs. Each comment marks the snapshot created by that statement:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`-- Snapshot 1\nCREATE TABLE my_catalog.main.product_skus (sku VARCHAR, label VARCHAR);\n\n-- Snapshot 2\nINSERT INTO my_catalog.main.product_skus VALUES ('SKU-001', 'Oak shelf'), ('SKU-002', 'Maple bench');\n\n-- Snapshot 3\nDELETE FROM my_catalog.main.product_skus WHERE sku = 'SKU-001';\n\n-- Snapshot 4\nUPDATE my_catalog.main.product_skus SET label = concat(label, ' (v2)');\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"At snapshot 2 the table contains two rows \\u2014 the same result as \",(0,n.jsx)(e.code,{children:\"AT (VERSION => 2)\"}),\" in \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel\",children:\"Time travel\"}),\". Snapshots 3 and 4 each add one kind of delta.\"]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"changes-from-a-single-snapshot\",anchors:\"\",ancestors:\"example-workflow\",index:\"4\",children:[(0,n.jsx)(e.h3,{id:\"changes-from-a-single-snapshot\",children:\"Changes from a single snapshot\"}),`\n`,(0,n.jsx)(e.p,{children:\"Inspect everything that happened in snapshot 2:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`FROM my_catalog.table_changes('product_skus', 2, 2);\n`})}),`\n`,(0,n.jsxs)(e.table,{children:[(0,n.jsx)(e.thead,{children:(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.th,{children:\"snapshot_id\"}),(0,n.jsx)(e.th,{children:\"rowid\"}),(0,n.jsx)(e.th,{children:\"change_type\"}),(0,n.jsx)(e.th,{children:\"sku\"}),(0,n.jsx)(e.th,{children:\"label\"})]})}),(0,n.jsxs)(e.tbody,{children:[(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"2\"}),(0,n.jsx)(e.td,{children:\"0\"}),(0,n.jsx)(e.td,{children:\"insert\"}),(0,n.jsx)(e.td,{children:\"SKU-001\"}),(0,n.jsx)(e.td,{children:\"Oak shelf\"})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"2\"}),(0,n.jsx)(e.td,{children:\"1\"}),(0,n.jsx)(e.td,{children:\"insert\"}),(0,n.jsx)(e.td,{children:\"SKU-002\"}),(0,n.jsx)(e.td,{children:\"Maple bench\"})]})]})]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"changes-across-multiple-snapshots\",anchors:\"\",ancestors:\"example-workflow\",index:\"5\",children:[(0,n.jsx)(e.h3,{id:\"changes-across-multiple-snapshots\",children:\"Changes across multiple snapshots\"}),`\n`,(0,n.jsx)(e.p,{children:\"Follow the delete in snapshot 3 and the update in snapshot 4:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`FROM my_catalog.table_changes('product_skus', 3, 4)\nORDER BY snapshot_id;\n`})}),`\n`,(0,n.jsxs)(e.table,{children:[(0,n.jsx)(e.thead,{children:(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.th,{children:\"snapshot_id\"}),(0,n.jsx)(e.th,{children:\"rowid\"}),(0,n.jsx)(e.th,{children:\"change_type\"}),(0,n.jsx)(e.th,{children:\"sku\"}),(0,n.jsx)(e.th,{children:\"label\"})]})}),(0,n.jsxs)(e.tbody,{children:[(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"3\"}),(0,n.jsx)(e.td,{children:\"0\"}),(0,n.jsx)(e.td,{children:\"delete\"}),(0,n.jsx)(e.td,{children:\"SKU-001\"}),(0,n.jsx)(e.td,{children:\"Oak shelf\"})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"4\"}),(0,n.jsx)(e.td,{children:\"1\"}),(0,n.jsx)(e.td,{children:\"update_preimage\"}),(0,n.jsx)(e.td,{children:\"SKU-002\"}),(0,n.jsx)(e.td,{children:\"Maple bench\"})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"4\"}),(0,n.jsx)(e.td,{children:\"1\"}),(0,n.jsx)(e.td,{children:\"update_postimage\"}),(0,n.jsx)(e.td,{children:\"SKU-002\"}),(0,n.jsx)(e.td,{children:\"Maple bench (v2)\"})]})]})]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"changes-over-a-time-range\",anchors:\"\",ancestors:\"example-workflow\",index:\"6\",children:[(0,n.jsx)(e.h3,{id:\"changes-over-a-time-range\",children:\"Changes over a time range\"}),`\n`,(0,n.jsx)(e.p,{children:\"Bounds accept timestamps as well as snapshot IDs:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`FROM my_catalog.table_changes('product_skus', now() - INTERVAL '1 week', now());\n`})}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"table_changes\",anchors:\"\",ancestors:\"\",index:\"7\",children:[(0,n.jsx)(e.h2,{id:\"table_changes\",children:\"table_changes\"}),`\n`,(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.code,{children:\"table_changes\"}),\" takes a table name and two inclusive bounds: a start snapshot and an end snapshot. Bounds can be snapshot IDs or timestamps.\"]}),`\n`,(0,n.jsx)(e.p,{children:\"The result uses the table schema as of the end snapshot, plus three extra columns:\"}),`\n`,(0,n.jsx)(d,{columns:[\"Column\",\"Description\"],data:[{Column:\"`snapshot_id`\",Description:\"The snapshot that made the change\"},{Column:\"`rowid`\",Description:\"The row identifier of the changed row\"},{Column:\"`change_type`\",Description:\"`insert`, `update_preimage`, `update_postimage`, or `delete`\"}]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Updates appear as two rows: \",(0,n.jsx)(e.code,{children:\"update_preimage\"}),\" is the row before the update; \",(0,n.jsx)(e.code,{children:\"update_postimage\"}),\" is the row after the update.\"]}),`\n`,(0,n.jsx)(e.p,{children:\"When the table schema changes between the start and end bounds, results use the schema as of the end snapshot. If a column is dropped, it is omitted from the entire result. If a column is added, changes before the addition show the column's default value.\"}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"table_deletions\",anchors:\"\",ancestors:\"\",index:\"8\",children:[(0,n.jsx)(e.h2,{id:\"table_deletions\",children:\"table_deletions\"}),`\n`,(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.code,{children:\"table_deletions\"}),\" returns only rows deleted between two snapshots. It takes the same arguments as \",(0,n.jsx)(e.code,{children:\"table_changes\"}),\":\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`FROM my_catalog.table_deletions('product_skus', 3, 3);\n`})}),`\n`,(0,n.jsxs)(e.table,{children:[(0,n.jsx)(e.thead,{children:(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.th,{children:\"snapshot_id\"}),(0,n.jsx)(e.th,{children:\"rowid\"}),(0,n.jsx)(e.th,{children:\"sku\"}),(0,n.jsx)(e.th,{children:\"label\"})]})}),(0,n.jsx)(e.tbody,{children:(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"3\"}),(0,n.jsx)(e.td,{children:\"0\"}),(0,n.jsx)(e.td,{children:\"SKU-001\"}),(0,n.jsx)(e.td,{children:\"Oak shelf\"})]})})]}),`\n`,(0,n.jsxs)(e.p,{children:[\"The result includes \",(0,n.jsx)(e.code,{children:\"snapshot_id\"}),\" and \",(0,n.jsx)(e.code,{children:\"rowid\"}),\", followed by the table's data columns. There is no \",(0,n.jsx)(e.code,{children:\"change_type\"}),\" column because every row is a deletion.\"]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"table_insertions\",anchors:\"\",ancestors:\"\",index:\"9\",children:[(0,n.jsx)(e.h2,{id:\"table_insertions\",children:\"table_insertions\"}),`\n`,(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.code,{children:\"table_insertions\"}),\" returns only rows inserted between two snapshots:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-sql\",children:`FROM my_catalog.table_insertions('product_skus', 2, 2);\n`})}),`\n`,(0,n.jsxs)(e.table,{children:[(0,n.jsx)(e.thead,{children:(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.th,{children:\"snapshot_id\"}),(0,n.jsx)(e.th,{children:\"rowid\"}),(0,n.jsx)(e.th,{children:\"sku\"}),(0,n.jsx)(e.th,{children:\"label\"})]})}),(0,n.jsxs)(e.tbody,{children:[(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"2\"}),(0,n.jsx)(e.td,{children:\"0\"}),(0,n.jsx)(e.td,{children:\"SKU-001\"}),(0,n.jsx)(e.td,{children:\"Oak shelf\"})]}),(0,n.jsxs)(e.tr,{children:[(0,n.jsx)(e.td,{children:\"2\"}),(0,n.jsx)(e.td,{children:\"1\"}),(0,n.jsx)(e.td,{children:\"SKU-002\"}),(0,n.jsx)(e.td,{children:\"Maple bench\"})]})]})]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"compaction-and-retention\",anchors:\"\",ancestors:\"\",index:\"10\",children:[(0,n.jsx)(e.h2,{id:\"compaction-and-retention\",children:\"Compaction and retention\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"Compaction that expires snapshots limits both time travel and the change feed. If deleted rows are removed during compaction, \",(0,n.jsx)(e.code,{children:\"table_deletions\"}),\" can no longer return them even though you know they existed.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Set \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel#snapshot-retention\",children:\"snapshot retention\"}),\" with care when downstream pipelines depend on historical change feeds or long-running \",(0,n.jsx)(e.code,{children:\"AT\"}),\" queries.\"]}),`\n`]}),(0,n.jsxs)(a,{sectionId:\"learn-more\",anchors:\"\",ancestors:\"\",index:\"11\",children:[(0,n.jsx)(e.h2,{id:\"learn-more\",children:\"Learn more\"}),`\n`,(0,n.jsxs)(e.ul,{children:[`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel\",children:\"Time Travel\"})}),\": Query table state at a snapshot and list snapshot history\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:(0,n.jsx)(e.a,{href:\"/docs/lakehouse/features/transactions\",children:\"Transactions\"})}),\": Each committed transaction creates one snapshot\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:(0,n.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL Engine\"})}),\": DuckDB dialect and federated queries\"]}),`\n`]})]})]})}function p(t={}){let{wrapper:e}=t.components||{};return e?(0,n.jsx)(e,{...t,children:(0,n.jsx)(o,{...t})}):o(t)}function h(t,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+t+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return y(T);})();\n;return Component;"},"_id":"docs/lakehouse/features/data-change-feed.mdx","_raw":{"sourceFilePath":"docs/lakehouse/features/data-change-feed.mdx","sourceFileName":"data-change-feed.mdx","sourceFileDir":"docs/lakehouse/features","contentType":"mdx","flattenedPath":"docs/lakehouse/features/data-change-feed"},"type":"Docs","path":"/lakehouse/features/data-change-feed","level":3,"parent":"lakehouse/features"}