{"id":36798,"date":"2025-01-22T10:38:22","date_gmt":"2025-01-22T09:38:22","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=36798"},"modified":"2025-01-22T10:40:17","modified_gmt":"2025-01-22T09:40:17","slug":"postgresql-18-add-old-new-support-to-returning-in-dml-queries","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/","title":{"rendered":"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">While doing some research on <a href=\"https:\/\/www.openstack.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">OpenStack<\/a> (<a href=\"https:\/\/www.dbi-services.com\/blog\/creating-your-private-cloud-using-openstack-1-introduction\/\" target=\"_blank\" rel=\"noreferrer noopener\">see here for the introduction post<\/a>), I thought blogging about a new feature for the next version of PostgreSQL might free my head a bit from all those OpenStack services, so here we go \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What you can do in PostgreSQL since a very long time, is to return the values of an insert\/update or delete statement like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1,3,10,18]; title: ; notranslate\" title=\"\">\npostgres=# create table t ( a int, b text );\nCREATE TABLE\npostgres=# insert into t values (1,&#039;aaa&#039;) returning *;\n a |  b  \n---+-----\n 1 | aaa\n(1 row)\n\nINSERT 0 1\npostgres=# update t set b = &#039;bbb&#039; returning *;\n a |  b  \n---+-----\n 1 | bbb\n(1 row)\n\nUPDATE 1\n\npostgres=# delete from t where b = &#039;bbb&#039; returning *;\n a |  b  \n---+-----\n 1 | bbb\n(1 row)\n\nDELETE 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This is especially useful for generated or default values which might not be known from the beginning:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1,3,4,5,7]; title: ; notranslate\" title=\"\">\npostgres=# drop table t;\nDROP TABLE\npostgres=# create table t ( a int primary key generated always as identity\n                          , b text\n                          , c timestamptz default now() );\nCREATE TABLE\npostgres=# insert into t (b) values (&#039;aaa&#039;) returning a,c;\n a |               c               \n---+-------------------------------\n 1 | 2025-01-22 10:22:13.738709+01\n(1 row)\n\nINSERT 0 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What you can&#8217;t do as of today is to reference (or return) the old and new values for the affected rows a the same time. With this <a href=\"https:\/\/git.postgresql.org\/gitweb\/?p=postgresql.git;a=commitdiff;h=80feb727c869cc0b2e12bd1543bafa449be9c8e2\" target=\"_blank\" rel=\"noreferrer noopener\">patch<\/a> this changes, and you can return both: old and new:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1]; title: ; notranslate\" title=\"\">\npostgres=# update t set b = &#039;xxx&#039; returning old.b as old, new.b as new;\n old | new \n-----+-----\n aaa | xxx\n(1 row)\n\nUPDATE 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">For an insert statement old values will be null, for a delete statement the new values will be null:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1,8]; title: ; notranslate\" title=\"\">\npostgres=# insert into t (b) values (&#039;yyy&#039;) returning old.b as old, new.b as new;\nold | new\n----+-----\n    | yyy\n(1 row)\n\nINSERT 0 1\npostgres=# delete from t where b = &#039;yyy&#039; returning old.b as old, new.b as new;\n old | new \n-----+-----\n yyy | \n(1 row)\n\nDELETE 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This works (and did before) for multiple rows as well:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1,3]; title: ; notranslate\" title=\"\">\npostgres=# insert into t (b) select md5(i::text) from generate_series(1,10) i;\nINSERT 0 10\npostgres=# update t set b = &#039;qqq&#039; returning old.b as old, new.b as new;\n               old                | new \n----------------------------------+-----\n xxx                              | qqq\n c4ca4238a0b923820dcc509a6f75849b | qqq\n c81e728d9d4c2f636f067f89cc14862c | qqq\n eccbc87e4b5ce2fe28308fd9f2a7baf3 | qqq\n a87ff679a2f3e71d9181a67b7542122c | qqq\n e4da3b7fbbce2345d7772b0674a318d5 | qqq\n 1679091c5a880faf6fb5e6087eb1b2dc | qqq\n 8f14e45fceea167a5a36dedd4bea2543 | qqq\n c9f0f895fb98ab9159f51fd0297e236d | qqq\n 45c48cce2e2d7fbdea1afc51c7c6ad26 | qqq\n d3d9446802a44259755d38e6d163e820 | qqq\n(11 rows)\n\nUPDATE 11\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Nice, thanks to everybody who worked on this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While doing some research on OpenStack (see here for the introduction post), I thought blogging about a new feature for the next version of PostgreSQL might free my head a bit from all those OpenStack services, so here we go \ud83d\ude42 What you can do in PostgreSQL since a very long time, is to return [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,198],"tags":[77],"type_dbi":[],"class_list":["post-36798","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","category-database-management","tag-postgresql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries\" \/>\n<meta property=\"og:description\" content=\"While doing some research on OpenStack (see here for the introduction post), I thought blogging about a new feature for the next version of PostgreSQL might free my head a bit from all those OpenStack services, so here we go \ud83d\ude42 What you can do in PostgreSQL since a very long time, is to return [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-22T09:38:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-22T09:40:17+00:00\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"PostgreSQL 18: Add OLD\\\/NEW support to RETURNING in DML queries\",\"datePublished\":\"2025-01-22T09:38:22+00:00\",\"dateModified\":\"2025-01-22T09:40:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/\"},\"wordCount\":168,\"commentCount\":2,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\",\"Database management\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/\",\"name\":\"PostgreSQL 18: Add OLD\\\/NEW support to RETURNING in DML queries - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2025-01-22T09:38:22+00:00\",\"dateModified\":\"2025-01-22T09:40:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL 18: Add OLD\\\/NEW support to RETURNING in DML queries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/open-source-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries","og_description":"While doing some research on OpenStack (see here for the introduction post), I thought blogging about a new feature for the next version of PostgreSQL might free my head a bit from all those OpenStack services, so here we go \ud83d\ude42 What you can do in PostgreSQL since a very long time, is to return [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/","og_site_name":"dbi Blog","article_published_time":"2025-01-22T09:38:22+00:00","article_modified_time":"2025-01-22T09:40:17+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries","datePublished":"2025-01-22T09:38:22+00:00","dateModified":"2025-01-22T09:40:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/"},"wordCount":168,"commentCount":2,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring","Database management"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/","url":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/","name":"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2025-01-22T09:38:22+00:00","dateModified":"2025-01-22T09:40:17+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-18-add-old-new-support-to-returning-in-dml-queries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL 18: Add OLD\/NEW support to RETURNING in DML queries"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/36798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=36798"}],"version-history":[{"count":7,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/36798\/revisions"}],"predecessor-version":[{"id":36805,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/36798\/revisions\/36805"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=36798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=36798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=36798"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=36798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}