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 1mcQeH-0003Uz-Aq for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Oct 2021 11:17:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mcQeE-0007EE-D8 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Oct 2021 11:17:42 +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 1mcQeD-0007Dt-Fy for pgsql-hackers@lists.postgresql.org; Mon, 18 Oct 2021 11:17:42 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mcQe5-00048I-Vz for pgsql-hackers@postgresql.org; Mon, 18 Oct 2021 11:17:40 +0000 Received: from [192.168.1.113] (dsl-hkibng22-54faa4-119.dhcp.inet.fi [84.250.164.119]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id D4BCE1B0004B; Mon, 18 Oct 2021 14:17:30 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1634555851; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TDzyrOaeM1QxX/qHglkQIrymucg3mPL1RPq1ClkjV7Q=; b=DZbyoKIpBJArqN+YXk25Y0jEC47Nm4TuFoRqy8u0yVJN+gi8SKGczs30AVr+0Kxcs9Y7G1 FvB1LCFkMTFRrKIYsywS2t9NtsoWYg/e4ne9NoBmtGXdjx/qPBXh3IMXikDVQ6dU/POQrV UXfwjowZmhvgeyuNdSwaiak5gpbCJSVvqS2fShYHwPsqu75aR3yFgOqDGZLsE3NwXnV/Qf YVfXZc8no87Qmayqk841cP8l9VTe7RltRlNzk+bD2v40cNumNUMHH7HUCvqdFoseYtn+AY bzUS3tB3GhIFXilFMUkhSQBgjY2BHONOABb76vQQR6xoV/t8cyGWEYFqedISQQ== Subject: Re: ResourceOwner refactoring To: Aleksander Alekseev , "pgsql-hackers@postgresql.org" References: <202107141407.aydgrhuxskhj@alvherre.pgsql> <363e1ca2-0797-6023-2865-2d30121ac568@iki.fi> <908491f9-57ff-5061-30c6-d15d3dbf0e67@iki.fi> From: Heikki Linnakangas Message-ID: <1377f062-13ea-ebfb-ef8a-97739c156a05@iki.fi> Date: Mon, 18 Oct 2021 14:17:30 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------AF226508B1039DE40796DE51" Content-Language: en-US ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1634555851; a=rsa-sha256; cv=none; b=FMRVpVUIM6dOUPrXbuqvN6fTDSNrQIwFzM7ZRSTQAa4rsz9A3CmR//JVHxlPQJ85+flsuG c6f+6E2HhG86z7gV48ga5KZ3WV8A+OYzkO6wGE3GEsfqo6aUGA6sDVXXkYKzzZEpHg0wc6 VDsqUBa/9LKXGhBYYidApEnbAcwdlCyNSTlPasUZR2lkPMJFqYoUfOhlLTBRUWMN/OL7P1 yk2zLRRfJ5a7wbGjiV7vSVroXSH+we/EhrLHoeuQBPIfKwaT9uBpY6MpiK8yYIbKDxORlK o58FUEOm/39kfWbhrMblgsyrDK6nEFLnomBiZkW1YD+6Wbc0YbOnUT5bsyoc7w== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1634555851; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TDzyrOaeM1QxX/qHglkQIrymucg3mPL1RPq1ClkjV7Q=; b=kGvP7UxwGGBT45QyE/B+jd9KppkcMDar81EtFS16aW5FsiMTAyYnYREd4AF+L1YNfwmiV+ iD2jqjtBtLJthmrGsYLXNjlPUygaN7oHxQtziBtOQqJuKfZifhxRSqPw7kUUL5WzM4hDV+ xHWYuxUu2OtchzjmT279w+m7bD/ycLt7ppv6HnUSHUhGSB0h/BECJaFkon5SYlOC56aAyT qZzukhZOia25zlUpvg51E6IKF1boySniSX1qWXp/X/yPUGhkW3qTQrMgFqfgYC+Tbos8Qx IuloAW8zA7Q5TY2s6KtBB2E5GXggZq0J0ygOZe30oQ4juxb0+BKxxhhrbkIJjQ== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------AF226508B1039DE40796DE51 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 08/09/2021 13:19, Aleksander Alekseev wrote: > Hi Heikki, > >> Yeah, needed some manual fixing, but here you go. > > Thanks for working on this! > > v8-0002 didn't apply to the current master, so I rebased it. See > attached v9-* patches. I also included v9-0004 with some minor tweaks > from me. I have several notes regarding the code. Thanks! Attached is a newly rebased version. It includes your tweaks, and a few more comment and indentation tweaks. > 1. Not sure if I understand this code of ResourceOwnerReleaseAll(): > ``` > /* First handle all the entries in the array. */ > do > { > found = false; > for (int i = 0; i < owner->narr; i++) > { > if (owner->arr[i].kind->phase == phase) > { > Datum value = owner->arr[i].item; > ResourceOwnerFuncs *kind = owner->arr[i].kind; > > if (printLeakWarnings) > kind->PrintLeakWarning(value); > kind->ReleaseResource(value); > found = true; > } > } > > /* > * If any resources were released, check again because some of the > * elements might have been moved by the callbacks. We don't want to > * miss them. > */ > } while (found && owner->narr > 0); > ``` > > Shouldn't we mark the resource as released and/or decrease narr and/or > save the last processed i? Why this will not call ReleaseResource() > endlessly on the same resource (array item)? Same question for the > following code that iterates over the hash table. ReleaseResource() must call ResourceOwnerForget(), which removes the item from the or hash table. This works the same as the code in 'master': > /* > * Release buffer pins. Note that ReleaseBuffer will remove the > * buffer entry from our array, so we just have to iterate till there > * are none. > * > * During a commit, there shouldn't be any remaining pins --- that > * would indicate failure to clean up the executor correctly --- so > * issue warnings. In the abort case, just clean up quietly. > */ > while (ResourceArrayGetAny(&(owner->bufferarr), &foundres)) > { > Buffer res = DatumGetBuffer(foundres); > > if (isCommit) > PrintBufferLeakWarning(res); > ReleaseBuffer(res); > } That comment explains how it works. I added a comment like that in this patch, too. > 2. Just an idea/observation. It's possible that the performance of > ResourceOwnerEnlarge() can be slightly improved. Since the size of the > hash table is always a power of 2 and the code always doubles the size > of the hash table, (idx & mask) value will get one extra bit, which > can be 0 or 1. If it's 0, the value is already in its place, > otherwise, it should be moved on the known distance. In other words, > it's possible to copy `oldhash` to `newhash` and then move only half > of the items. I don't claim that this code necessarily should be > faster, or that this should be checked in the scope of this work. Hmm, the hash table uses open addressing, I think we want to also rearrange any existing entries that might not be at their right place because of collisions. We don't actually do that currently when an element is removed (even before this patch), but enlarging the array is a good opportunity for it. In any case, I haven't seen ResourceOwnerEnlarge() show up while profiling, so I'm going to leave it as it is. - Heikki --------------AF226508B1039DE40796DE51 Content-Type: text/x-patch; charset=UTF-8; name="v10-0001-Move-a-few-ResourceOwnerEnlarge-calls-for-safety.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="v10-0001-Move-a-few-ResourceOwnerEnlarge-calls-for-safety.pa"; filename*1="tch"