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 1hyjpK-0002oY-KM for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Aug 2019 21:32:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hyjpJ-0001XP-BP for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Aug 2019 21:32:01 +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 1hyjpJ-0001XI-02 for pgsql-hackers@lists.postgresql.org; Fri, 16 Aug 2019 21:32:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hyjpC-0004Xh-4S for pgsql-hackers@lists.postgresql.org; Fri, 16 Aug 2019 21:31:59 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x7GLVndA027991; Fri, 16 Aug 2019 17:31:50 -0400 From: Tom Lane To: Alvaro Herrera cc: pgsql-hackers@lists.postgresql.org Subject: Re: allocation limit for encoding conversion In-reply-to: <20190816181418.GA898@alvherre.pgsql> References: <20190816181418.GA898@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Fri, 16 Aug 2019 14:14:18 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27989.1565991109.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 16 Aug 2019 17:31:49 -0400 Message-ID: <27990.1565991109@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alvaro Herrera writes: > Somebody ran into issues when generating large XML output (upwards of > 256 MB) and then sending via a connection with a different > client_encoding. This occurs because we pessimistically allocate 4x as > much memory as the string needs, and we run into the 1GB palloc > limitation. ISTM we can do better now by using huge allocations, as per > the preliminary attached patch (which probably needs an updated overflow > check rather than have it removed altogether); but at least it is able > to process this query, which it wasn't without the patch: > select query_to_xml( > 'select a, cash_words(a::text::money) from generate_series(0, 200000= 0) a', > true, false, ''); I fear that allowing pg_do_encoding_conversion to return strings longer than 1GB is just going to create failure cases somewhere else. However, it's certainly true that 4x growth is a pretty unlikely worst case. Maybe we could do something like 1. If string is short (say up to a few megabytes), continue to do it like now. This avoids adding overhead for typical cases. 2. Otherwise, run some lobotomized form of encoding conversion that just computes the space required (as an int64, I guess) without saving the result anywhere. 3. If space required > 1GB, fail. 4. Otherwise, allocate just the space required, and convert. regards, tom lane