Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jFJ8T-0007Zw-9H for pgsql-general@arkaria.postgresql.org; Fri, 20 Mar 2020 15:00:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jFJ8S-0003kS-1p for pgsql-general@arkaria.postgresql.org; Fri, 20 Mar 2020 15:00:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jFJ8Q-0003jY-6M for pgsql-general@lists.postgresql.org; Fri, 20 Mar 2020 15:00:31 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jFJ8H-0003pi-6W for pgsql-general@lists.postgresql.org; Fri, 20 Mar 2020 15:00:29 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 2D7F2824; Fri, 20 Mar 2020 11:00:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 20 Mar 2020 11:00:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=0 5TDoZu+C/IJfLT6GcJ11E9G9lECSwY96nle01tJ5No=; b=LfQZWI6/fMO7oME0x OSqernDk0DiRNILHur4x4R8scm6fzHbHhiZk0wiUHCgHk2c0Upe57sDyxEuoEm89 eJoafZJ/FuH+KR9lXYIhCX2WDrZmOfoX7MeZiXLWsPDMdrOs5ZUhEbBvmXkwnk+Y pQgb9ogDxaJXVMQYiALeDkfS3NBnB/Lg/1nnRlYHvGbvOKR5cFE5Se+5xxhl1cBk 2V8Q3p7KmnBjhQXgWSvG5xcyLwXy3ZpBRHbER5vogc6/wDS3skOJSlM0zLzA8FMC dYXq4P3waqAFsMO71r5rbe6r9aAfCPcIi8yGqkH1sIdmU3AJ0SVwxNS+DiPmETsU +kusA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=05TDoZu+C/IJfLT6GcJ11E9G9lECSwY96nle01tJ5 No=; b=iLJhT0aKSQQF0csc5yVWRnN8IqONm1yAilGJbZYJIg95TkkpLd9gjCGtj GDyQpeq0cHz1Zllx0c75fsiopZbBAxXuhtLnWtKbvXrX/+/j2MSviuyluDi3gRf9 2aWocQ5UylOyvjx/37AmdE9yBxC+Ay3I7nlbVM8/W9ydFlJ50yx2+ie5tawj7HUO 8qB9+/afYAdPUXTh6/cnbkPS2k2Zt4BOXJnqwMfaJRQHZpc6oiqYc34M5afmFgWL rajoxPY/ik7Z9dNf/sApu4IvXyXjvGNYFzYWrdJYOkT+brM1879vOXHBWkTa5OXx VcqGMUilpi98eAeGUZDPcHDb6OtEA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudeguddgjedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvdduvddruddvledrud elfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegr ughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id DDEFE328006C; Fri, 20 Mar 2020 11:00:15 -0400 (EDT) Subject: Re: Duplicate key violation on upsert To: Matt Magoffin , pgsql-general@lists.postgresql.org References: <0E4E069D-40B1-46C1-9BA0-5A5E737F753B@msqr.us> From: Adrian Klaver Message-ID: <604644e7-6881-3fdb-8767-897fc40559ac@aklaver.com> Date: Fri, 20 Mar 2020 08:00:15 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <0E4E069D-40B1-46C1-9BA0-5A5E737F753B@msqr.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/20/20 2:17 AM, Matt Magoffin wrote: > Hello, > > I am experiencing a duplicate key violation in Postgres 9.6 on occasion > for one particular query, and I’m wondering where I’m going wrong. My > table looks like this: > >                       Table "solardatum.da_datum" >   Column   |           Type           | Collation | Nullable | Default > -----------+--------------------------+-----------+----------+--------- >  ts        | timestamp with time zone |           | not null | >  node_id   | bigint                   |           | not null | >  source_id | character varying(64)    |           | not null | >  posted    | timestamp with time zone |           | not null | >  jdata_i   | jsonb                    |           |          | >  jdata_a   | jsonb                    |           |          | >  jdata_s   | jsonb                    |           |          | >  jdata_t   | text[]                   |           |          | > Indexes: >     "da_datum_pkey" UNIQUE, btree (node_id, ts, source_id) CLUSTER, > tablespace "solarindex" >     "da_datum_reverse_pkey" UNIQUE, btree (node_id, ts DESC, > source_id), tablespace "solarindex" >     "da_datum_x_acc_idx" UNIQUE, btree (node_id, source_id, ts DESC, > jdata_a) WHERE jdata_a IS NOT NULL, tablespace "solarindex" > Triggers: >     aa_agg_stale_datum BEFORE INSERT OR DELETE OR UPDATE ON > solardatum.da_datum FOR EACH ROW EXECUTE > PROCEDURE solardatum.trigger_agg_stale_datum() >     ts_insert_blocker BEFORE INSERT ON solardatum.da_datum FOR EACH ROW > EXECUTE PROCEDURE _timescaledb_internal.insert_blocker() > > The error/query looks like: > > ERROR: duplicate key value violates unique constraint > “_hyper_1_1931_chunk_da_datum_x_acc_idx" What is the above index UNIQUE over? What is da_datum_x_acc_idx index below indexed over? >   Where: SQL statement "INSERT INTO solardatum.da_datum(ts, node_id, > source_id, posted, jdata_i, jdata_a, jdata_s, jdata_t) > VALUES (…) > ON CONFLICT (node_id, ts, source_id) DO UPDATE > SET jdata_i = EXCLUDED.jdata_i, > jdata_a = EXCLUDED.jdata_a, > jdata_s = EXCLUDED.jdata_s, > jdata_t = EXCLUDED.jdata_t, > posted = EXCLUDED.posted > RETURNING (xmax = 0)" > > I am using the TimescaleDB extension so there are child tables > inheriting from this main table and that’s why the reported index name > differs from the definition shown above. I’m not sure if the extension > is the problem, so I thought I’d start here to see if I’ve configured > something wrong or my expectations on how the upsert should work is > wrong. My expectation was that basically the insert would never fail > from a duplicate key violation. > > The error always references the da_datum_x_acc_idx index, which is a > partial index with jdata_a added as a covering column… that is, it’s > only in the index so I can get some index-only results with that column. > Is the partial index possibly an issue in this configuration? > > Thanks for any insight, > Matt -- Adrian Klaver adrian.klaver@aklaver.com