{"id":36089,"date":"2024-06-20T11:17:26","date_gmt":"2024-06-20T01:17:26","guid":{"rendered":"https:\/\/database.guide\/?p=36089"},"modified":"2024-06-20T11:17:28","modified_gmt":"2024-06-20T01:17:28","slug":"fix-source-array-too-small-when-updating-an-array-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/fix-source-array-too-small-when-updating-an-array-in-postgresql\/","title":{"rendered":"Fix &#8220;source array too small&#8221; When Updating an Array in PostgreSQL"},"content":{"rendered":"\n<p class=\"\">If you&#8217;re getting an error that reads &#8220;source array too small&#8221; when updating an array in PostgreSQL, it&#8217;s probably because you&#8217;re passing a slice that&#8217;s smaller than your specified range. For example, if you specify a range that covers four elements but provide an array that contains just three elements, then you&#8217;d get this error.<\/p>\n\n\n\n<p class=\"\">To fix the error, be sure to provide an an array that contains at least as many elements as are specified in the range. <\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example of Error<\/h2>\n\n\n\n<p class=\"\">Suppose we have the following array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT c2 FROM t1 WHERE c1 = 1;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{0,7,6,9,8,12,14}<\/pre>\n\n\n\n<p class=\"\">And suppose we try to update it like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE t1 \nSET c2&#91;1:3] = '{1,2}'\nWHERE c1 = 1;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  source array too small<\/pre>\n\n\n\n<p class=\"\">I got the error because my range specifies three elements but my array only contains two elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p class=\"\">We can fix this issue either by reducing the range to match the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE t1 \nSET c2&#91;1:2] = '{1,2}'\nWHERE c1 = 1;\n\nSELECT c2 FROM t1 WHERE c1 = 1;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{1,2,6,9,8,12,14}<\/pre>\n\n\n\n<p class=\"\">Or we can increase the number of elements in the array to match the range:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE t1 \nSET c2&#91;1:3] = '{1,2,3}'\nWHERE c1 = 1;\n\nSELECT c2 FROM t1 WHERE c1 = 1;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{1,2,3,9,8,12,14}<\/pre>\n\n\n\n<p class=\"\">Normally we would want the number of elements to match the range exactly, but it is possible to provide a larger slice. When we do this, the specified range takes precedence. Therefore, it will take as many elements from the new array as are specified in the range.<\/p>\n\n\n\n<p class=\"\">Here&#8217;s an example of what I mean:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE t1 \nSET c2&#91;1:2] = '{5,6,7}'\nWHERE c1 = 1;\n\nSELECT c2 FROM t1 WHERE c1 = 1;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{5,6,3,9,8,12,14}<\/pre>\n\n\n\n<p class=\"\">In this case only the first two elements were updated, even though I provided an array with three elements. That&#8217;s because the range was only two.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re getting an error that reads &#8220;source array too small&#8221; when updating an array in PostgreSQL, it&#8217;s probably because you&#8217;re passing a slice that&#8217;s smaller than your specified range. For example, if you specify a range that covers four elements but provide an array that contains just three elements, then you&#8217;d get this error. &#8230; <a title=\"Fix &#8220;source array too small&#8221; When Updating an Array in PostgreSQL\" class=\"read-more\" href=\"https:\/\/database.guide\/fix-source-array-too-small-when-updating-an-array-in-postgresql\/\" aria-label=\"Read more about Fix &#8220;source array too small&#8221; When Updating an Array in PostgreSQL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[169,76,10],"class_list":["post-36089","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-arrays","tag-errors","tag-how-to"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=36089"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36089\/revisions"}],"predecessor-version":[{"id":36102,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36089\/revisions\/36102"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}