Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cl1BU-0005IL-9i for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Mar 2017 22:32:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cl1BT-0006Dw-SX for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Mar 2017 22:32:51 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cl1BT-0006Dn-EU for pgsql-hackers@postgresql.org; Mon, 06 Mar 2017 22:32:51 +0000 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cl1BQ-000625-A0 for pgsql-hackers@postgresql.org; Mon, 06 Mar 2017 22:32:50 +0000 Received: by mail-wm0-x22f.google.com with SMTP id v186so75900982wmd.0 for ; Mon, 06 Mar 2017 14:32:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=/pTQig57SObSr3oyekq4Ri5kvwqK1azP0mXCRGlUtIQ=; b=gga7QAx72wqLBcPlDCoSY0+ggaymOeRYLYqR13762hCnPaED4DSeFlmNR3O0reQ6BZ tBRdTvNJhJJhdU4Bpk26xk/m22mqB2/K+VJD38GwEJkdq+fOJ+0POxOXy0jOUeMJiDqV P/O9+IHzaHv0p0wERS0zopSdY9Ys0ykPVwAhA+HEycMJaFmTVYRvztyxYT9JiNIVWKtc OmHYcwIxH3dyJrT233+gVslFIhE3J0mV/q5/CKvySfJLZv562WtCOk+0qHBOGRk/ryoL 4I1dOdAYp67V0WuCpYNWwYpTnq5KU/6JCNZvBP6UFbT9SoA2Bh7nrncfDyURWf6ATdz5 5m9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=/pTQig57SObSr3oyekq4Ri5kvwqK1azP0mXCRGlUtIQ=; b=DFpn4eA9SfIq9Nkq5EhoApslGLQx7KC5MoyxLGnMtZrU8Km9OPruybqUtvRALQTwng CvQt3ni8kdUaEQ7EU5yhok9mqvTlei478qFsRqtU53tBsjFJlPEoCSMKYtQig7GFtUgU eBQ7iNArnccf/NNBWGQyDD2f7TR5lKNCZC0o2FdSEcDEqSfrtdEtO6GQIuBOsjvUpj9b kcW8euBkStrcO5SiAOo+gkORE8g7J66MjeRHdqSZo8XUXS/LSoalY62TmdWjs2YO5qAg 17w/wkDvLnz1XDnBqjMTnhoIo4hrevTVS5s7VsHLLqO5n9u1KOhzQq0xswrLjgzuk+B1 81XQ== X-Gm-Message-State: AMke39mtbMx1YvitxqYKQBY5EY8IrRxycPvCojD0ciOW1judXsYrQ0MWv/MO0uVnE6yoZvdX3jbp8CzPBRnetdtuvScIDrh75d8FgKca2t4ZIe7RzwQYQf7scQCx1HE5Ua5JUdvkw1PA3XikfVoArV4UqyEy00acctIy/3szpCqV/JNKMvhR6/3Ebvh03JTRN+cETocJWOFn X-Received: by 10.28.152.212 with SMTP id a203mr16322371wme.7.1488839566408; Mon, 06 Mar 2017 14:32:46 -0800 (PST) Received: from [10.137.2.17] (ip-89-177-46-65.net.upcbroadband.cz. [89.177.46.65]) by smtp.gmail.com with ESMTPSA id o63sm16407693wmo.30.2017.03.06.14.32.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Mar 2017 14:32:45 -0800 (PST) Subject: Re: PATCH: two slab-like memory allocators To: Andres Freund References: <1671.1488259784@sss.pgh.pa.us> <20170228074420.aazv4iw6k562mnxg@alap3.anarazel.de> <20170228184122.extbigaer5ryrt3z@alap3.anarazel.de> <20170301041835.wli3viwnxr5wonfv@alap3.anarazel.de> <20170301042936.usv23f4iaumeyptf@alap3.anarazel.de> <20170301225545.eoevemua234g4fzq@alap3.anarazel.de> <20170306174406.pfs3as44gc7a5ps2@alap3.anarazel.de> <20170306190857.3pef5twyhnhxoqjs@alap3.anarazel.de> Cc: Robert Haas , Tom Lane , Petr Jelinek , Petr Jelinek , John Gorman , pgsql-hackers From: Tomas Vondra Message-ID: <699073c9-17ca-68fb-9463-17b89b72dc22@2ndquadrant.com> Date: Mon, 6 Mar 2017 23:32:30 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20170306190857.3pef5twyhnhxoqjs@alap3.anarazel.de> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 03/06/2017 08:08 PM, Andres Freund wrote: > Hi, > > On 2017-03-06 19:49:56 +0100, Tomas Vondra wrote: >> On 03/06/2017 07:05 PM, Robert Haas wrote: >>> On Mon, Mar 6, 2017 at 12:44 PM, Andres Freund wrote: >>>> On 2017-03-06 12:40:18 -0500, Robert Haas wrote: >>>>> On Wed, Mar 1, 2017 at 5:55 PM, Andres Freund wrote: >>>>>> The issue was that on 32bit platforms the Datum returned by some >>>>>> functions (int2int4_sum in this case) isn't actually a separately >>>>>> allocated Datum, but rather just something embedded in a larger >>>>>> struct. That, combined with the following code: >>>>>> if (!peraggstate->resulttypeByVal && !*isnull && >>>>>> !MemoryContextContains(CurrentMemoryContext, >>>>>> DatumGetPointer(*result))) >>>>>> seems somewhat problematic to me. MemoryContextContains() can give >>>>>> false positives when used on memory that's not a distinctly allocated >>>>>> chunk, and if so, we violate memory lifetime rules. It's quite >>>>>> unlikely, given the required bit patterns, but nonetheless it's making >>>>>> me somewhat uncomfortable. >>>>>> >>>>>> Do others think this isn't an issue and we can just live with it? >>>>> >>>>> I think it's 100% broken to call MemoryContextContains() on something >>>>> that's not guaranteed to be a palloc'd chunk. >>>> >>>> I agree, but to me it seems the only fix would be to just yank out the >>>> whole optimization? >>> >>> Dunno, haven't looked into it. >>> >> >> I think it might be fixable by adding a flag into the chunk, with 'true' for >> regular allocations, and 'false' for the optimized ones. And then only use >> MemoryContextContains() for 'flag=true' chunks. > > I'm not quite following here. We only get a Datum and the knowledge > that it's a pass-by-ref argument, so we really don't know that much. We > could create an "EmbeddedDatum" type that has a preceding chunk header > (appropriately for the version), that just gets zeroed out at start. Is > that what you mean? > Yes, that's roughly what I meant. > >> The question however is whether this won't make the optimization pointless. >> I also, wonder how much we save by this optimization and how widely it's >> used? Can someone point me to some numbers? > > I don't recall any recent numbers. I'm more than a bit doubful that it > really matters - it's only used for the results of aggregate/window > functions, and surely they've a good chunk of their own overhead... > And if the benefit is negligible, trying to keep the optimization might easily result in slowdown (compared to non-optimized code). But I'm puzzled why we haven't seen any reports of failures? I mean, doing sum(int4) is not particularly extravagant thing, if there really is an issue, shouldn't we see a lot of reports? What are we missing? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers