Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iGIvP-0006sX-RG for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Oct 2019 08:26:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iGIvO-0003aR-MH for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Oct 2019 08:26:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iGIvO-0003a7-6o for pgsql-hackers@lists.postgresql.org; Fri, 04 Oct 2019 08:26:54 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iGIvL-000860-Aa for pgsql-hackers@postgresql.org; Fri, 04 Oct 2019 08:26:53 +0000 Received: by mail-wm1-x343.google.com with SMTP id r19so4838737wmh.2 for ; Fri, 04 Oct 2019 01:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Yfv4IPNQOQzHuI7YXUM8lQ2w3Gc7UpnZhY6f9bIPPgA=; b=feADBIHoK4iYmB6yfZyWLuPWNZhmqNI4426kcAY5+d2Ho0b3FWNQi5z/Z7vXXl0TsF o6azl1mEsq5zOGE+LFWRnxSGHs+OBtXm3VoYbL8mB+GMn7tooVws6kDoX0d5b5crc3LJ ij9EyEpoP7668aGw3sdf3VBZccjXYRwjvqoEGaGZKB+hSKAmfroCMBxKmW1o7Ggf1NVS MFvCQJepn+KowvOFyGz/h0vo1ytGK1nJTuEjnRUa1aoe64WM/KBve/J/IJWccmoQ16qP cYzv0PslqmG/rTm7IvRbGlzsjUGZzHpCD7pe9EvXyfsa/uN2zYnTKd+nPE9YrhPP9ics QQyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Yfv4IPNQOQzHuI7YXUM8lQ2w3Gc7UpnZhY6f9bIPPgA=; b=oGmbFFg4cxVuPiDMp3VoMVBnmLCRfhRw8xXQT58rUzYOnB8e1BEcjO+EMxFx/7F41v i4DxNFrQ7Y5E9XZO1DkzsuCCGOE62K789loMJJ+dMLaqlBM+MgP0XCgbiZ2qq7WIL3Sg uJqeKwkjvLBT35QhgWcpW6Lci2pavjhfENfhvqoNafK2A6Yl0QnTJQcoB0oeFDSFIpp2 AJCjoluBdcaN64ZKLjtYYlmtPjbAfieN/vNar9F7LJAizwnHvyZhzuQV6Iiwv8uC6/Iy 44zwj7VWK/r3Bj9AkIlFYVRkWUqY8ZRnAee4zY/OqjNiDx8vdTGWCQaJGPaHg+5vqGoe ohZQ== X-Gm-Message-State: APjAAAWy7M+ycdq4GxjGVy4Gjs3hTjCeRvX5aghXOUjxyJdLse9olgUS 6kEe0OilA5741IPRcReyBfjKZw== X-Google-Smtp-Source: APXvYqxodKOwgmBhIC4nxhVTgbFRSRFuYHlvK8A3ByAh9EIIJ+UkzvMhjhegNRpOU41C6A9lc2WMlg== X-Received: by 2002:a05:600c:2386:: with SMTP id m6mr3031764wma.159.1570177607746; Fri, 04 Oct 2019 01:26:47 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id g3sm10750022wro.14.2019.10.04.01.26.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2019 01:26:46 -0700 (PDT) Date: Fri, 4 Oct 2019 10:26:44 +0200 From: Tomas Vondra To: Tom Lane Cc: Jeff Davis , Melanie Plageman , Soumyadeep Chakraborty , Robert Haas , Pg Hackers , soumyadeep2007@gmail.com Subject: Re: Memory Accounting Message-ID: <20191004082644.wytixiukkm6ditlu@development> References: <67f66d2fa39e35aef90093c74531a38dcd5c0f73.camel@j-davis.com> <20190926192244.mk5ru6nkofo7am3h@development> <57780817877d4b40106b248b63261e21a7a56261.camel@j-davis.com> <20190928221249.mo2jrtlakingnc5e@development> <20190928222206.thidzeygazl7axhg@development> <392e034c1b674a1a58c217dc440ea6197081ccfa.camel@j-davis.com> <20191001021114.iimd77xgnstaww7j@development> <15151.1570163761@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <15151.1570163761@sss.pgh.pa.us> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Oct 04, 2019 at 12:36:01AM -0400, Tom Lane wrote: >So ... why exactly did this patch define MemoryContextData.mem_allocated >as int64? That seems to me to be doubly wrong: it is not the right width >on 32-bit machines, and it is not the right signedness anywhere. I think >that field ought to be of type Size (a/k/a size_t, but memnodes.h always >calls it Size). > Yeah, I think that's an oversight. Maybe there's a reason why Jeff used int64, but I can't think of any. >I let this pass when the patch went in, but now I'm on the warpath >about it, because since c477f3e449 went in, some of the 32-bit buildfarm >members are failing with > >2019-10-04 00:41:56.569 CEST [66916:86] pg_regress/_int LOG: statement: CREATE INDEX text_idx on test__int using gist ( a gist__int_ops ); >TRAP: FailedAssertion("total_allocated == context->mem_allocated", File: "aset.c", Line: 1533) >2019-10-04 00:42:25.505 CEST [63836:11] LOG: server process (PID 66916) was terminated by signal 6: Abort trap >2019-10-04 00:42:25.505 CEST [63836:12] DETAIL: Failed process was running: CREATE INDEX text_idx on test__int using gist ( a gist__int_ops ); > >What I think is happening is that c477f3e449 allowed this bit in >AllocSetRealloc: > > context->mem_allocated += blksize - oldblksize; > >to be executed in situations where blksize < oldblksize, where before >that was not possible. Of course blksize and oldblksize are of type >Size, hence unsigned, so the subtraction result underflows in this >case. If mem_allocated is of the same width as Size then this does >not matter because the final result wraps around to the proper value, >but if we're going to allow mem_allocated to be wider than Size then >we will need more logic here to add or subtract as appropriate. > >(I'm not quite sure why we're not seeing this failure on *all* the >32-bit machines; maybe there's some other factor involved?) > Interesting failure mode (especially that it does *not* fail on some 32-bit machines). >I see no value in defining mem_allocated to be wider than Size. >Yes, the C standard contemplates the possibility that the total >available address space is larger than the largest chunk you can >ever ask malloc for; but nobody has built a platform like that in >this century, and they sucked to program on back in the dark ages >when they did exist. (I speak from experience.) I do not think >we need to design Postgres to allow for that. > >Likewise, there's no evident value in allowing mem_allocated >to be negative. > >I haven't chased down exactly what else would need to change. >It might be that s/int64/Size/g throughout the patch is >sufficient, but I haven't analyzed it. > I think so too, but I'll take a closer look in the afternoon, unless you beat me to it. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services