{"id":16888,"date":"2021-11-30T16:31:29","date_gmt":"2021-11-30T15:31:29","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/"},"modified":"2021-11-30T16:31:29","modified_gmt":"2021-11-30T15:31:29","slug":"postgresbuild2021-feedback-on-questions-1","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/","title":{"rendered":"PostgresBuild2021 &#8211; questions feedback (1)"},"content":{"rendered":"<p><a href=\"https:\/\/www.postgresbuild.com\/\" target=\"_blank\" rel=\"noopener\">PostgresBuild2021<\/a> is currently ongoing and as usual, I had questions after my talk I could not immediately answer. One of the best ways to learn, is to present a topic. There are for sure questions you never thought about. The topic of my talk was &#8220;An introduction to server side programming&#8221; and I wanted to give people easy examples about how to write procedures and functions in PostgreSQL. It was not about control structures like IF\/THEN\/ELSE\/CASE\/LOOP but more about the general concepts like parameters, returning values, using exceptions, or giving more information about your functions and procedures to the optimizer. <\/p>\n<p><!--more--><\/p>\n<h3>Question 1: Can I use setof with in parameters?<\/h3>\n<p>The example in the talk is this one:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg\" alt=\"\" width=\"1169\" height=\"493\" class=\"aligncenter size-full wp-image-52656\" \/><\/a><\/p>\n<p>This is a simple function returning a setof t2, that is, all the rows in the table t2 at once. Can we use setof as a in parameters as well? Let&#8217;s try:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create table t ( a int, b text );\nCREATE TABLE\npostgres=# create function f ( a setof t ) returns void as $$ select 1; $$ language sql;\nERROR:  functions cannot accept set arguments\n<\/pre>\n<p>The answer is: No.<\/p>\n<h3>Question 2: When I commit inside a loop, does that generate a new transaction ID with each commit?<\/h3>\n<p>The example in the talk is this one:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/commit.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/commit.jpg\" alt=\"\" width=\"1200\" height=\"461\" class=\"aligncenter size-full wp-image-52658\" \/><\/a><\/p>\n<p>It doesn&#8217;t really matter if the commit is executed in a loop or like in the example above: Do we get a new transaction ID for each commit or not? Let&#8217;s try:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create table t ( a xid );\nCREATE TABLE\npostgres=# create procedure p() as \n           $$ \n           begin \n             insert into t values (pg_current_xact_id()::xid); \n             commit; \n             begin \n               insert into t values (pg_current_xact_id()::xid); \n               commit; \n             end; \n           end; \n           $$ language plpgsql;\nCREATE PROCEDURE\npostgres=# call p();\nCALL\npostgres=# select * from t;\n  a  \n-----\n 719\n 720\n(2 rows)\n<\/pre>\n<p>The answer is: Yes. The same, of course, applies for loops:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# drop procedure p();\nDROP PROCEDURE\npostgres=# truncate t;\nTRUNCATE TABLE\npostgres=# create or replace procedure p() as \n           $$ \n           begin \n             for i in 1..10 \n             loop \n               insert into t values (pg_current_xact_id()::xid); \n             end loop; \n           end; \n           $$ language plpgsql;\nCREATE PROCEDURE\npostgres=# call p();\nCALL\npostgres=# select * from t;\n  a  \n-----\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n(10 rows)\n\npostgres=# call p();\nCALL\npostgres=# select * from t;\n  a  \n-----\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 724\n 726\n 727\n 728\n 729\n 730\n 731\n 732\n 733\n 734\n 735\n(20 rows)\npostgres=# create or replace procedure p() as \n           $$ \n           begin \n             for i in 1..10 \n             loop \n               insert into t values (pg_current_xact_id()::xid); \n               commit;\n             end loop; \n           end; \n           $$ language plpgsql;\nCREATE PROCEDURE\n<\/pre>\n<p>The answer, again is: Yes. <\/p>\n<p>More to come as soon as I have the whole list of all the questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgresBuild2021 is currently ongoing and as usual, I had questions after my talk I could not immediately answer. One of the best ways to learn, is to present a topic. There are for sure questions you never thought about. The topic of my talk was &#8220;An introduction to server side programming&#8221; and I wanted to [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":16889,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229],"tags":[77],"type_dbi":[],"class_list":["post-16888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database-administration-monitoring","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>PostgresBuild2021 - questions feedback (1) - 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\/postgresbuild2021-feedback-on-questions-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgresBuild2021 - questions feedback (1)\" \/>\n<meta property=\"og:description\" content=\"PostgresBuild2021 is currently ongoing and as usual, I had questions after my talk I could not immediately answer. One of the best ways to learn, is to present a topic. There are for sure questions you never thought about. The topic of my talk was &#8220;An introduction to server side programming&#8221; and I wanted to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-30T15:31:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1169\" \/>\n\t<meta property=\"og:image:height\" content=\"493\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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\\\/postgresbuild2021-feedback-on-questions-1\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"PostgresBuild2021 &#8211; questions feedback (1)\",\"datePublished\":\"2021-11-30T15:31:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/\"},\"wordCount\":251,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/04\\\/setof.jpg\",\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/\",\"name\":\"PostgresBuild2021 - questions feedback (1) - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/04\\\/setof.jpg\",\"datePublished\":\"2021-11-30T15:31:29+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/04\\\/setof.jpg\",\"contentUrl\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/04\\\/setof.jpg\",\"width\":1169,\"height\":493},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresbuild2021-feedback-on-questions-1\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgresBuild2021 &#8211; questions feedback (1)\"}]},{\"@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":"PostgresBuild2021 - questions feedback (1) - 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\/postgresbuild2021-feedback-on-questions-1\/","og_locale":"en_US","og_type":"article","og_title":"PostgresBuild2021 - questions feedback (1)","og_description":"PostgresBuild2021 is currently ongoing and as usual, I had questions after my talk I could not immediately answer. One of the best ways to learn, is to present a topic. There are for sure questions you never thought about. The topic of my talk was &#8220;An introduction to server side programming&#8221; and I wanted to [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/","og_site_name":"dbi Blog","article_published_time":"2021-11-30T15:31:29+00:00","og_image":[{"width":1169,"height":493,"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg","type":"image\/jpeg"}],"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\/postgresbuild2021-feedback-on-questions-1\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"PostgresBuild2021 &#8211; questions feedback (1)","datePublished":"2021-11-30T15:31:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/"},"wordCount":251,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg","keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/","url":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/","name":"PostgresBuild2021 - questions feedback (1) - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg","datePublished":"2021-11-30T15:31:29+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/setof.jpg","width":1169,"height":493},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/postgresbuild2021-feedback-on-questions-1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PostgresBuild2021 &#8211; questions feedback (1)"}]},{"@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\/16888","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=16888"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/16888\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media\/16889"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=16888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=16888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=16888"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=16888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}