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 1nXNTY-0005UJ-Ay for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Mar 2022 13:26:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nXNTX-00066u-6b for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Mar 2022 13:26:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXNTW-00066k-SB for pgsql-hackers@lists.postgresql.org; Thu, 24 Mar 2022 13:26:02 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXNTT-00015G-IT for pgsql-hackers@lists.postgresql.org; Thu, 24 Mar 2022 13:26:02 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id E35D25C01B5; Thu, 24 Mar 2022 09:25:56 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 24 Mar 2022 09:25:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=RfInhDTMan6Vop/MWRNOfM+1xCz4vhVwVVI5RpCG7yg=; b=M9F0gofu f+jnSs6DRVvx25FiHfWgf7n/mO4QM/4i3DfIF3dYGv7sqbk1yhXm5y65kEznvV5G 5UB+E3GUf+H36o0l8BNzDx8KzAf7G6/YBRmidXBn/Kg3CXl6Xk4TrZ4BiTLl/A7W 4m4oXy8fDK9+fEMTfFYGECDPDk4zWPjQ2f75cPG5Pk5gUrZNLmB+d3TGw2C2E6u+ 5H8ossCbDTHe7Qcr6jkixsFUL3mO7OZ7sta9ItGN9gvUXKFBJM1PJC/X4x1/Cxhc JkLpKO4rBxM7chfFsS6iEJEFRW2RoQt4F24e//pGt1zD2ZS1BueMyKvhx5Vte/pE GxLDGEecXurRyQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudegledggeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggugfgjsehmkeerredttdejnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeetvdfhteeuueekfefhfedugeffhfehhfdvleffleetkeeg geefhffggeekjedtudenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtohhmne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhh vghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 24 Mar 2022 09:25:56 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 101E72A08E0; Thu, 24 Mar 2022 10:26:10 -0300 (-03) Date: Thu, 24 Mar 2022 14:26:10 +0100 From: Alvaro Herrera To: Michael Paquier Cc: Pg Hackers Subject: Re: turn fastgetattr and heap_getattr to inline functions Message-ID: <202203241326.puromteoovaz@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="r7zmaabpzc6r33ml" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --r7zmaabpzc6r33ml Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2022-Mar-24, Michael Paquier wrote: > Hmm. I think that you'd better add a return at the end of each > function? Some compilers are dumb in detecting that all the code > paths return (aka recent d0083c1) and could generate warnings, even if > things are coded to return all the time, like in your patch. Hmm, OK to do something about that. I added pg_unreachable(): looking at LWLockAttemptLock(), it looks that that should be sufficient. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ --r7zmaabpzc6r33ml Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="v2-0001-Change-fastgetattr-and-heap_getattr-to-inline-fun.patch" From bd535ae2c8044de0c1eb6f4e17dd0f73c50e1622 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 24 Mar 2022 10:40:21 +0100 Subject: [PATCH v2] Change fastgetattr and heap_getattr to inline functions They were macros previously, but recent callsite additions made Coverity complain about one of the assertions being always true. This change could have been made a long time ago, but the Coverity complain broke the inertia. --- src/backend/access/heap/heapam.c | 46 --------- src/include/access/htup_details.h | 155 ++++++++++++++---------------- 2 files changed, 73 insertions(+), 128 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 3746336a09..74ad445e59 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -1131,52 +1131,6 @@ heapgettup_pagemode(HeapScanDesc scan, } -#if defined(DISABLE_COMPLEX_MACRO) -/* - * This is formatted so oddly so that the correspondence to the macro - * definition in access/htup_details.h is maintained. - */ -Datum -fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, - bool *isnull) -{ - return ( - (attnum) > 0 ? - ( - (*(isnull) = false), - HeapTupleNoNulls(tup) ? - ( - TupleDescAttr((tupleDesc), (attnum) - 1)->attcacheoff >= 0 ? - ( - fetchatt(TupleDescAttr((tupleDesc), (attnum) - 1), - (char *) (tup)->t_data + (tup)->t_data->t_hoff + - TupleDescAttr((tupleDesc), (attnum) - 1)->attcacheoff) - ) - : - nocachegetattr((tup), (attnum), (tupleDesc)) - ) - : - ( - att_isnull((attnum) - 1, (tup)->t_data->t_bits) ? - ( - (*(isnull) = true), - (Datum) NULL - ) - : - ( - nocachegetattr((tup), (attnum), (tupleDesc)) - ) - ) - ) - : - ( - (Datum) NULL - ) - ); -} -#endif /* defined(DISABLE_COMPLEX_MACRO) */ - - /* ---------------------------------------------------------------- * heap access method interface * ---------------------------------------------------------------- diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h index b2d52ed16c..0c5c7b352f 100644 --- a/src/include/access/htup_details.h +++ b/src/include/access/htup_details.h @@ -690,88 +690,6 @@ struct MinimalTupleData #define HeapTupleClearHeapOnly(tuple) \ HeapTupleHeaderClearHeapOnly((tuple)->t_data) - -/* ---------------- - * fastgetattr - * - * Fetch a user attribute's value as a Datum (might be either a - * value, or a pointer into the data area of the tuple). - * - * This must not be used when a system attribute might be requested. - * Furthermore, the passed attnum MUST be valid. Use heap_getattr() - * instead, if in doubt. - * - * This gets called many times, so we macro the cacheable and NULL - * lookups, and call nocachegetattr() for the rest. - * ---------------- - */ - -#if !defined(DISABLE_COMPLEX_MACRO) - -#define fastgetattr(tup, attnum, tupleDesc, isnull) \ -( \ - AssertMacro((attnum) > 0), \ - (*(isnull) = false), \ - HeapTupleNoNulls(tup) ? \ - ( \ - TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff >= 0 ? \ - ( \ - fetchatt(TupleDescAttr((tupleDesc), (attnum)-1), \ - (char *) (tup)->t_data + (tup)->t_data->t_hoff + \ - TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff)\ - ) \ - : \ - nocachegetattr((tup), (attnum), (tupleDesc)) \ - ) \ - : \ - ( \ - att_isnull((attnum)-1, (tup)->t_data->t_bits) ? \ - ( \ - (*(isnull) = true), \ - (Datum)NULL \ - ) \ - : \ - ( \ - nocachegetattr((tup), (attnum), (tupleDesc)) \ - ) \ - ) \ -) -#else /* defined(DISABLE_COMPLEX_MACRO) */ - -extern Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, - bool *isnull); -#endif /* defined(DISABLE_COMPLEX_MACRO) */ - - -/* ---------------- - * heap_getattr - * - * Extract an attribute of a heap tuple and return it as a Datum. - * This works for either system or user attributes. The given attnum - * is properly range-checked. - * - * If the field in question has a NULL value, we return a zero Datum - * and set *isnull == true. Otherwise, we set *isnull == false. - * - * is the pointer to the heap tuple. is the attribute - * number of the column (field) caller wants. is a - * pointer to the structure describing the row and all its fields. - * ---------------- - */ -#define heap_getattr(tup, attnum, tupleDesc, isnull) \ - ( \ - ((attnum) > 0) ? \ - ( \ - ((attnum) > (int) HeapTupleHeaderGetNatts((tup)->t_data)) ? \ - getmissingattr((tupleDesc), (attnum), (isnull)) \ - : \ - fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \ - ) \ - : \ - heap_getsysattr((tup), (attnum), (tupleDesc), (isnull)) \ - ) - - /* prototypes for functions in common/heaptuple.c */ extern Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull); @@ -815,4 +733,77 @@ extern size_t varsize_any(void *p); extern HeapTuple heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc); extern MinimalTuple minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc); +/* + * fastgetattr + * Fetch a user attribute's value as a Datum (might be either a + * value, or a pointer into the data area of the tuple). + * + * This must not be used when a system attribute might be requested. + * Furthermore, the passed attnum MUST be valid. Use heap_getattr() + * instead, if in doubt. + * + * This gets called many times, so we macro the cacheable and NULL + * lookups, and call nocachegetattr() for the rest. + */ +static inline Datum +fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull) +{ + AssertMacro(attnum > 0); + + *isnull = false; + if (HeapTupleNoNulls(tup)) + { + Form_pg_attribute att; + + att = TupleDescAttr(tupleDesc, attnum - 1); + if (att->attcacheoff >= 0) + return fetchatt(att, (char *) tup->t_data + tup->t_data->t_hoff + + att->attcacheoff); + else + return nocachegetattr(tup, attnum, tupleDesc); + } + else + { + if (att_isnull(attnum - 1, tup->t_data->t_bits)) + { + *isnull = true; + return (Datum) NULL; + } + else + return nocachegetattr(tup, attnum, tupleDesc); + } + + pg_unreachable(); +} + +/* + * heap_getattr + * Extract an attribute of a heap tuple and return it as a Datum. + * This works for either system or user attributes. The given attnum + * is properly range-checked. + * + * If the field in question has a NULL value, we return a zero Datum + * and set *isnull == true. Otherwise, we set *isnull == false. + * + * is the pointer to the heap tuple. is the attribute + * number of the column (field) caller wants. is a + * pointer to the structure describing the row and all its fields. + * + */ +static inline Datum +heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull) +{ + if (attnum > 0) + { + if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data)) + return getmissingattr(tupleDesc, attnum, isnull); + else + return fastgetattr(tup, attnum, tupleDesc, isnull); + } + else + return heap_getsysattr(tup, attnum, tupleDesc, isnull); + + pg_unreachable(); +} + #endif /* HTUP_DETAILS_H */ -- 2.30.2 --r7zmaabpzc6r33ml--