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 1hygk8-0004Ru-PC for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Aug 2019 18:14:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hygk6-0005yl-Rn for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Aug 2019 18:14:26 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hygk6-0005t8-BT for pgsql-hackers@lists.postgresql.org; Fri, 16 Aug 2019 18:14:26 +0000 Received: from mail-qt1-x836.google.com ([2607:f8b0:4864:20::836]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hygk3-0004Z7-RQ for pgsql-hackers@lists.postgresql.org; Fri, 16 Aug 2019 18:14:25 +0000 Received: by mail-qt1-x836.google.com with SMTP id q4so7080403qtp.1 for ; Fri, 16 Aug 2019 11:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=kqrQImKDHnWHfF36LonIN6ZRSiWTwG7GX9naZgM/h1Q=; b=eoHZF+ZFk6DQADnJkgEM4jRBXffVhnbwNhPp0FANdK5EJn0Swy3A18MtwJLsHpS8at vIsB8JsJmra/1+vSjtGMPsoFKXuyXYonmZ7J+ahChKhojdCb3t8jYz3/1UkvwO392mtv Jv7FKyhalKKdCq0mdBUfYCf4eJCxntQp2Y2u4ov+lePgiX7uStvMSnep9B2z8Zd20hcT M66xD/gyQKb+7tbP6DVenil7beIYvaCrLtCSwERwrh6D3QemlagxypW2OjM22H+3VBlX 0t5pJceYuamDNexhrsQ+MNMMHOLlmFdgcqFp1vTTLyKAvJBGvjsZq0wZl9d4QRm6a6RV qP7A== 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:subject:message-id:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=kqrQImKDHnWHfF36LonIN6ZRSiWTwG7GX9naZgM/h1Q=; b=kHeNrk8iZgF6Qso9Zk0io/u9dpJcIicP+GH4dhWhtMmoVO8anf/0jKqgoxUuyZhCk7 lLmTzscU0dTF+VDKPZ1BL1m6Tug/cUcevWV1qTMGN63UYa6XfT+LxyID3+zZqxOKiX5w QhYAO3D1bKHhqiB1yeD+gQfp5rlLkD5c3aAY+hdKWFHkq5g7AXNHUpnYM3z1Exlf0WB+ BDmP8DGetw6fsOovAdyhFAMBOGmH22u3T9CQRTzt0vtpMvFFpvmz+mDTt6yS7Z+Kt1qL fi87QLfWicUos75Roy3JJSBhUc/a9bBXQDamDae+Guq4OIhJZWW0PWaJOlzNtRrIaFVQ v43g== X-Gm-Message-State: APjAAAWotpWKzraV6xoK+f6Wc0Ki74aYaGCvAO0tI9VAUxoCGBU5nPDH 0/w8JDce04URGMFaKnAZq5GcT0KSrZMQb7K0ZvjeugMpZkU5HdRDPp8jx2DBGmEKQApdSsQzRc9 BPV2ggO+S6PIuALU5pfIjuwAcX0VQ2ELf1Eh6YUmg+KV6nn6YYSFeoExdFogWb/baTHWDOMoFOL qzGo7R4BC9t2zDsA== X-Google-Smtp-Source: APXvYqyPOYY1zEV2XAHjkTEEmm+FKz6eS+UIX6lumqc+H8QqrkatX2lWK4dbTHAoiHzdzvHYyOHlTA== X-Received: by 2002:ac8:3315:: with SMTP id t21mr10036994qta.392.1565979261271; Fri, 16 Aug 2019 11:14:21 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([179.56.49.205]) by smtp.gmail.com with ESMTPSA id q9sm3193720qtj.48.2019.08.16.11.14.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2019 11:14:20 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 641B112159C; Fri, 16 Aug 2019 14:14:18 -0400 (-04) Date: Fri, 16 Aug 2019 14:14:18 -0400 From: Alvaro Herrera To: pgsql-hackers@lists.postgresql.org Subject: allocation limit for encoding conversion Message-ID: <20190816181418.GA898@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="OXfL5xGRrasGEqWY" Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --OXfL5xGRrasGEqWY Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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, 2000000) a', true, false, ''); -- Álvaro Herrera --OXfL5xGRrasGEqWY Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-Use-huge-pallocs-on-encoding-conversions.patch" From a718f299d72617c366a08735895e0439482ede2f Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 8 Aug 2019 15:36:53 -0400 Subject: [PATCH v1] Use huge pallocs on encoding conversions --- src/backend/utils/mb/mbutils.c | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c index bec54bb5cb..e2853fcf6e 100644 --- a/src/backend/utils/mb/mbutils.c +++ b/src/backend/utils/mb/mbutils.c @@ -348,17 +348,8 @@ pg_do_encoding_conversion(unsigned char *src, int len, pg_encoding_to_char(src_encoding), pg_encoding_to_char(dest_encoding)))); - /* - * Allocate space for conversion result, being wary of integer overflow - */ - if ((Size) len >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH)) - ereport(ERROR, - (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), - errmsg("out of memory"), - errdetail("String of %d bytes is too long for encoding conversion.", - len))); - - result = palloc(len * MAX_CONVERSION_GROWTH + 1); + result = palloc_extended((Size) len * MAX_CONVERSION_GROWTH + 1, + MCXT_ALLOC_HUGE); OidFunctionCall5(proc, Int32GetDatum(src_encoding), @@ -681,17 +672,8 @@ perform_default_encoding_conversion(const char *src, int len, if (flinfo == NULL) return unconstify(char *, src); - /* - * Allocate space for conversion result, being wary of integer overflow - */ - if ((Size) len >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH)) - ereport(ERROR, - (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), - errmsg("out of memory"), - errdetail("String of %d bytes is too long for encoding conversion.", - len))); - - result = palloc(len * MAX_CONVERSION_GROWTH + 1); + result = palloc_extended((Size) len * MAX_CONVERSION_GROWTH + 1, + MCXT_ALLOC_HUGE); FunctionCall5(flinfo, Int32GetDatum(src_encoding), -- 2.17.1 --OXfL5xGRrasGEqWY--