Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lpESD-000188-Ph for pgsql-general@arkaria.postgresql.org; Fri, 04 Jun 2021 18:21:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lpESC-0000pG-Mk for pgsql-general@arkaria.postgresql.org; Fri, 04 Jun 2021 18:21:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lpESB-0000p7-RN for pgsql-general@lists.postgresql.org; Fri, 04 Jun 2021 18:21:56 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lpES8-0000ou-K2 for pgsql-general@lists.postgresql.org; Fri, 04 Jun 2021 18:21:54 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id DA3336B8; Fri, 4 Jun 2021 14:21:50 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Fri, 04 Jun 2021 14:21:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=V 3gbBh2OfO2uEIPvlEGSxFVGNZEZIsDZc7nbJ5qNcnc=; b=rzOcnwH/t5gvYYzmG vO8uH5aMQqIUj5yLM3Qf0ga1SRmvF4t/v5kKPoNKyqHUdLqV52pI7U8+eHgRGpOH grLXKaJMCVASBEz0GeD+w/X/IiMHglwV4tTRqIboe7iZvr+h5hfQ0aln6nj3hHgz IwNahN6/v1fYhlCJhVreXc1x4kUTdVXdv3Igp9yUSWq2pzfsoN9KF3CtEIk/wAlm 0VfjjvgWi6DdBg9ImVSuyg+6tRn3O0Zv6Bgt/pz4NN9wS7Nrim3Hjs3W8oNmtJ6x FLPo2DId6+sLicVs/3mNoSnqo2I27iOTlDGP/1+FeMeR/5o3oA3djXV55LTAW/MK s4UbQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=fm3; bh=V3gbBh2OfO2uEIPvlEGSxFVGNZEZIsDZc7nbJ5qNc nc=; b=UnmvEbBYEo4vzrTxP47ieb0K0MKk4wZW6Dyc6vwqOJGmvcC+Y4CUCuELF N8aQbxs3NXX+wNadHBTZauSoNiKHfWObfopZ7iUKq3EgBg8P1K1C+d78ifFxE9Lk Dhg8qWs3oRH1EKv2/+iwTmQ5ab/AOioPFckxwFX0Ytt4EhJzW6HtdJ9xfVXRnKf+ TwaptSj29T4JfaJRXA1DTJy8FU/BODRSbhaeI0JOZv0SNnbZHO+IQI73uPJcs5j0 US8XHz1Yd6McaCvGRuBTYY5plQ514rSj/GaE3pi+6iUlMss2LB7Qezyjjg80WG8o Owqx9oaI5A1GGq2/MotCRx8zzyJdw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfedtuddguddvgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeevveefve efueegffekkefhgeeiueffgfetueejvdfghfeujeejhfekjeelgedtgeenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 4 Jun 2021 14:21:49 -0400 (EDT) Subject: Re: Struggling with EXCLUDE USING gist To: Laura Smith , Julien Rouhaud Cc: postgre References: From: Adrian Klaver Message-ID: Date: Fri, 4 Jun 2021 11:21:48 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: 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: Archived-At: Precedence: bulk On 6/4/21 10:37 AM, Laura Smith wrote: > > > > Sent with ProtonMail Secure Email. > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ > On Friday, 4 June 2021 18:11, Julien Rouhaud wrote: > >> On Sat, Jun 5, 2021 at 12:48 AM Laura Smith >> n5d9xq3ti233xiyif2vp@protonmail.ch wrote: >> >>> All the examples I've seen around the internet make this sound so easy. >>> But I seem to be missing some important step because all I'm getting are messages such as "DETAIL: Key (t_val, t_version)=(def, [-infinity,infinity)) conflicts with existing key (t_val, t_version)=(def, [-infinity,"2021-06-04 16:56:08.008122+01"))." >>> [...] >>> CREATE FUNCTION create_or_update_test(p_val text) RETURNS bool AS $$ >>> DECLARE >>> v_version text; >>> v_range tstzrange; >>> BEGIN >>> -- N.B. Have coded it this way round (not insert first) because "ON CONFLICT does not support deferrable unique constraints/exclusion constraints as arbiters" >>> SELECT t_version,t_range into v_version,v_range from test_v where t_val='abc'; >>> IF NOT FOUND THEN >>> INSERT INTO test(t_val) values(p_val) >>> END IF; >>> -- If range conflict, adjust old and set new >>> UPDATE test set t_range=tstzrange(lower(v_range),now(),'[)') where t_version=v_version; >>> INSERT INTO test(t_val) values(p_val); >>> RETURN FOUND; >>> END; >>> $$ language plpgsql; >> >> You need to provide more information. I suspect that what's happening >> is a concurrency issue where the create_or_update_test() is called >> multiple time and both initially see and empty table so try to insert >> an -infinity/infinity range before updating it, so the 2nd call will >> fail once the 1st one commits. > > > Happy to provide more information although not quite sure how much more I can provide ? Perhaps my use case ? > > My use-case is version tracking for items. > > My implementation concept : > Default insert is tstzrange('-infinity','infinity') > When a "new" version of the item comes along: > (a) the "old" item becomes archived (i.e. valid until 'infinity' => valid until 'now()' ) > (b) the "new" item becomes current (i.e. valid until 'infinity') > > If tstzrange and EXCLUDE USING is the wrong way to do this sort of thing, then I'm all ears to other suggestions. But I've seen so many examples out on the web that suggest this is exactly the sort of thing that tstzrange and EXCLUDE using *is* very good for ? > > What I got to work: create table ts_range( id integer, tsrange_fld tstzrange default tstzrange('-infinity', 'infinity'), EXCLUDE USING gist (id WITH=, tsrange_fld WITH &&) ); test_(aklaver)(5432)=> insert into ts_range values (1); INSERT 0 1 test_(aklaver)(5432)=> select * from ts_range ; id | tsrange_fld ----+---------------------- 1 | [-infinity,infinity) update ts_range set tsrange_fld = tstzrange('-infinity', 'now') where id = 1; UPDATE 1 test_(aklaver)(5432)=> select * from ts_range ; id | tsrange_fld ----+--------------------------------------------- 1 | [-infinity,"2021-06-04 11:19:39.861045-07") (1 row) insert into ts_range values (1, tstzrange('now', 'infinity')); INSERT 0 1 test_(aklaver)(5432)=> select * from ts_range ; id | tsrange_fld ----+--------------------------------------------- 1 | [-infinity,"2021-06-04 11:19:39.861045-07") 1 | ["2021-06-04 11:19:53.672274-07",infinity) (2 rows) -- Adrian Klaver adrian.klaver@aklaver.com