{"id":7750,"date":"2021-04-30T12:43:45","date_gmt":"2021-04-30T17:43:45","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=7750"},"modified":"2021-04-30T13:05:36","modified_gmt":"2021-04-30T18:05:36","slug":"postgresql-sequences","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=7750","title":{"rendered":"PostgreSQL Sequences"},"content":{"rendered":"<p>I&#8217;m having a problem with a database refusing to change INSERTS to UPDATES on the ON CONFLICT condition &#8212; it insists that a &#8216;duplicate key value violates unique constraint&#8217;. A little time with a search engine tells me that sequences can get out of sync, and then you cannot insert items into the table. How do you know your sequence is out of sync?<\/p>\n<pre>SELECT NEXTVAL((SELECT PG_GET_SERIAL_SEQUENCE('\"table_name\"', 'name_of_column_with_sequence'))) as \"NextValue\", MAX(\"name_of_column_with_sequence\") AS \"Max Value\" FROM \"table_name\";<\/pre>\n<p>So null seems like it would be a problem!<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=7756\" rel=\"attachment wp-att-7751\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-7756 size-full\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/04\/PostgresSequenceValueTable.png\" alt=\"\" width=\"359\" height=\"206\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/04\/PostgresSequenceValueTable.png 359w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/04\/PostgresSequenceValueTable-300x172.png 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/a><\/p>\n<p>For future reference, when the next value is smaller than the max value in the table, the solution is to set the series value based on the max value<\/p>\n<pre class=\"lang-sql s-code-block hljs\"><code><span class=\"hljs-keyword\">SELECT<\/span> SETVAL((<span class=\"hljs-keyword\">SELECT<\/span> PG_GET_SERIAL_SEQUENCE(<span class=\"hljs-string\">'\"table_name\"'<\/span>, <span class=\"hljs-string\">'name_of_column_with_sequence'<\/span>)), (<span class=\"hljs-keyword\">SELECT<\/span> (<span class=\"hljs-built_in\">MAX<\/span>(\"name_of_column_with_sequence\") <span class=\"hljs-operator\">+<\/span> <span class=\"hljs-number\">1<\/span>) <span class=\"hljs-keyword\">FROM<\/span> \"<span class=\"hljs-string\">table_name<\/span>\"), <span class=\"hljs-literal\">FALSE<\/span>);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m having a problem with a database refusing to change INSERTS to UPDATES on the ON CONFLICT condition &#8212; it insists that a &#8216;duplicate key value violates unique constraint&#8217;. A little time with a search engine tells me that sequences can get out of sync, and then you cannot insert items into the table. How &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1003],"tags":[1350,1348,1349,922],"class_list":["post-7750","post","type-post","status-publish","format-standard","hentry","category-database","tag-pgadmin","tag-postgres","tag-postgresql","tag-sql"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7750"}],"version-history":[{"count":4,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7750\/revisions"}],"predecessor-version":[{"id":7758,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7750\/revisions\/7758"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}