Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bLFDY-0006w9-5D for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Jul 2016 19:44:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bLFDX-0005mO-OW for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Jul 2016 19:44:11 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bLFDW-0005jg-IA for pgsql-bugs@postgresql.org; Thu, 07 Jul 2016 19:44:10 +0000 Received: from ohmu.fi ([2001:1bc8:100:6::f301]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bLFDS-0001ot-Vd for pgsql-bugs@postgresql.org; Thu, 07 Jul 2016 19:44:09 +0000 Received: from [127.0.0.1] (localhost [IPv6:::1]) by ohmu.fi (Postfix) with ESMTP id 67FC260504; Thu, 7 Jul 2016 22:44:00 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=ohmu20151013; t=1467920640; bh=EO9JuIu9tIVg9PkgNjMimDjxk1D5NwicGBDUUWONmJE=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From; b=simV8MR6IexIqDe8gMTtIQpcgJHZoKJUuUulY/NkzSweMFK9yqa3V7YS1NKu57YUT AzhQyl3a3GVI4YDcidvfnul7hdnvyrAejvM6EdPaUL5JsUlUrFwP2dI6OiN+aRS87v XUzYyCfm00REN2/hH2WBEr3muiAySBekIPvYKhZY= Subject: Re: BUG #14150: Attempted to delete invisible tuple To: Peter Geoghegan , Andres Freund References: <20160519123338.12513.20271@wrigleys.postgresql.org> <88248a24-47d1-d575-a63f-2b56a09f82e2@aiven.io> <62f3793f-13d6-567c-e512-f29d1fd76d00@aiven.io> <20160706214024.4bl2q3skcio4eoqk@alap3.anarazel.de> Cc: Michael Paquier , Peter Tripp , Virendra Negi , pgsql-bugs From: Oskari Saarenmaa Message-ID: <6e259534-eb94-0319-dace-ee83d9535a86@aiven.io> Date: Thu, 7 Jul 2016 22:43:59 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------F94C3678688BD07CB3279F0C" X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------F94C3678688BD07CB3279F0C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 07.07.2016, 05:15, Peter Geoghegan kirjoitti: > Maybe heap_abort_speculative() should be refactored to call another > function, and keep only the parts that specifically expect a > HeapTupleHeaderIsSpeculative() tuple. The function that it is made to > call (that consists of the majority of the current > heap_abort_speculative() implementation) could also be called by a > special super deletion variant of toast_delete(). No need to spread > knowledge about speculative insertion any further this way, AFAICT. > The UPSERT commit did modify two HeapTupleSatisfies* routines, but > that didn't include HeapTupleSatisfiesUpdate() (just > HeapTupleSatisfiesDirty(), and the aforementioned defensive code in > HeapTupleSatisfiesToast()). > > What do you think of this outline, Oskari and Andres? Something like the attached patch? It still modifies the toast_delete API, but we could also implement a new speculative-tuple-aware version of it if that's a concern. / Oskari --------------F94C3678688BD07CB3279F0C Content-Type: text/x-patch; name="heap_delete_speculative.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="heap_delete_speculative.patch" diff --git src/backend/access/heap/heapam.c src/backend/access/heap/heapam.c index 57da57a..00de8ae 100644 --- src/backend/access/heap/heapam.c +++ src/backend/access/heap/heapam.c @@ -3335,7 +3335,7 @@ l1: Assert(!HeapTupleHasExternal(&tp)); } else if (HeapTupleHasExternal(&tp)) - toast_delete(relation, &tp); + toast_delete(relation, &tp, false); /* * Mark tuple for invalidation from system caches at next command @@ -5894,8 +5894,15 @@ heap_finish_speculative(Relation relation, HeapTuple tuple) void heap_abort_speculative(Relation relation, HeapTuple tuple) { - TransactionId xid = GetCurrentTransactionId(); ItemPointer tid = &(tuple->t_self); + + heap_delete_speculative(relation, tid, false); +} + +void +heap_delete_speculative(Relation relation, ItemPointer tid, bool is_toast) +{ + TransactionId xid = GetCurrentTransactionId(); ItemId lp; HeapTupleData tp; Page page; @@ -5930,7 +5937,7 @@ heap_abort_speculative(Relation relation, HeapTuple tuple) */ if (tp.t_data->t_choice.t_heap.t_xmin != xid) elog(ERROR, "attempted to kill a tuple inserted by another transaction"); - if (!HeapTupleHeaderIsSpeculative(tp.t_data)) + if (!(is_toast || HeapTupleHeaderIsSpeculative(tp.t_data))) elog(ERROR, "attempted to kill a non-speculative tuple"); Assert(!HeapTupleHeaderIsHeapOnly(tp.t_data)); @@ -5999,8 +6006,8 @@ heap_abort_speculative(Relation relation, HeapTuple tuple) LockBuffer(buffer, BUFFER_LOCK_UNLOCK); - if (HeapTupleHasExternal(&tp)) - toast_delete(relation, &tp); + if (!is_toast && HeapTupleHasExternal(&tp)) + toast_delete(relation, &tp, true); /* * Never need to mark tuple for invalidation, since catalogs don't support diff --git src/backend/access/heap/tuptoaster.c src/backend/access/heap/tuptoaster.c index 4cffd21..a657330 100644 --- src/backend/access/heap/tuptoaster.c +++ src/backend/access/heap/tuptoaster.c @@ -66,7 +66,7 @@ typedef struct toast_compress_header #define TOAST_COMPRESS_SET_RAWSIZE(ptr, len) \ (((toast_compress_header *) (ptr))->rawsize = (len)) -static void toast_delete_datum(Relation rel, Datum value); +static void toast_delete_datum(Relation rel, Datum value, bool is_speculative); static Datum toast_save_datum(Relation rel, Datum value, struct varlena * oldexternal, int options); static bool toastrel_valueid_exists(Relation toastrel, Oid valueid); @@ -459,7 +459,7 @@ toast_datum_size(Datum value) * ---------- */ void -toast_delete(Relation rel, HeapTuple oldtup) +toast_delete(Relation rel, HeapTuple oldtup, bool is_speculative) { TupleDesc tupleDesc; Form_pg_attribute *att; @@ -506,7 +506,7 @@ toast_delete(Relation rel, HeapTuple oldtup) if (toast_isnull[i]) continue; else if (VARATT_IS_EXTERNAL_ONDISK(PointerGetDatum(value))) - toast_delete_datum(rel, value); + toast_delete_datum(rel, value, is_speculative); } } } @@ -1062,7 +1062,7 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup, if (need_delold) for (i = 0; i < numAttrs; i++) if (toast_delold[i]) - toast_delete_datum(rel, toast_oldvalues[i]); + toast_delete_datum(rel, toast_oldvalues[i], false); return result_tuple; } @@ -1654,7 +1654,7 @@ toast_save_datum(Relation rel, Datum value, * ---------- */ static void -toast_delete_datum(Relation rel, Datum value) +toast_delete_datum(Relation rel, Datum value, bool is_speculative) { struct varlena *attr = (struct varlena *) DatumGetPointer(value); struct varatt_external toast_pointer; @@ -1703,7 +1703,10 @@ toast_delete_datum(Relation rel, Datum value) /* * Have a chunk, delete it */ - simple_heap_delete(toastrel, &toasttup->t_self); + if (is_speculative) + heap_delete_speculative(toastrel, &toasttup->t_self, true); + else + simple_heap_delete(toastrel, &toasttup->t_self); } /* diff --git src/include/access/heapam.h src/include/access/heapam.h index b3a595c..ca0c87d 100644 --- src/include/access/heapam.h +++ src/include/access/heapam.h @@ -157,6 +157,7 @@ extern HTSU_Result heap_delete(Relation relation, ItemPointer tid, HeapUpdateFailureData *hufd); extern void heap_finish_speculative(Relation relation, HeapTuple tuple); extern void heap_abort_speculative(Relation relation, HeapTuple tuple); +extern void heap_delete_speculative(Relation relation, ItemPointer tid, bool is_toast); extern HTSU_Result heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, diff --git src/include/access/tuptoaster.h src/include/access/tuptoaster.h index 7b5ae62..011f5c1 100644 --- src/include/access/tuptoaster.h +++ src/include/access/tuptoaster.h @@ -142,7 +142,7 @@ extern HeapTuple toast_insert_or_update(Relation rel, * Called by heap_delete(). * ---------- */ -extern void toast_delete(Relation rel, HeapTuple oldtup); +extern void toast_delete(Relation rel, HeapTuple oldtup, bool is_speculative); /* ---------- * heap_tuple_fetch_attr() - --------------F94C3678688BD07CB3279F0C Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------F94C3678688BD07CB3279F0C--