Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qKGkS-0008Sf-Ux for pgsql-sql@arkaria.postgresql.org; Fri, 14 Jul 2023 11:14:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qKGjS-0010Cm-F9 for pgsql-sql@arkaria.postgresql.org; Fri, 14 Jul 2023 11:13:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qKGjS-0010Ce-5t for pgsql-sql@lists.postgresql.org; Fri, 14 Jul 2023 11:13:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qKGjP-000PUk-3o for pgsql-sql@lists.postgresql.org; Fri, 14 Jul 2023 11:13:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 36EBD0Bt3288507; Fri, 14 Jul 2023 07:13:00 -0400 From: Tom Lane To: Pavel Stehule cc: Shaozhong SHI , pgsql-sql Subject: Re: Memory allocation error In-reply-to: References: Comments: In-reply-to Pavel Stehule message dated "Fri, 14 Jul 2023 09:49:51 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3288455.1689333136.0@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 14 Jul 2023 07:13:00 -0400 Message-ID: <3288506.1689333180@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3288455.1689333136.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Pavel Stehule writes: > pá 14. 7. 2023 v 9:36 odesílatel Shaozhong SHI > napsal: >> On Fri, 14 Jul 2023 at 08:14, Pavel Stehule >> wrote: >>> It depends what you do. Postgres doesn't allow to allocate bigger blocks >>> than 1GB. Maybe you create too big string or too big value of some other >>> type. But it can be signal of some cache bloating. > The best way - do it all in one recursive query without any recursive > function. > This issue you can fix only by rewriting your code. Yeah, but nonetheless this error message is pretty user-unfriendly. The given example is too incomplete to run as-is, but I guessed that maybe the array_agg() was accumulating too many values, and sure enough it's possible to reproduce: regression=# select array_agg(x::text) from generate_series(1,100000000) x; ERROR: invalid memory alloc request size 1073741824 We can do better than that. The attached patch causes the error to be regression=# select array_agg(x::text) from generate_series(1,100000000) x; ERROR: array size exceeds the maximum allowed (1073741823) I'm not wedded to that wording, but it's an existing translatable string that at least points you in the direction of "my array is too big". (This is also what you get if the eventual array construction overruns the 1G limit, cf construct_md_array().) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="better-error-for-array_agg-overflow.patch"; charset="us-ascii" Content-ID: <3288455.1689333136.2@sss.pgh.pa.us> Content-Description: better-error-for-array_agg-overflow.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/ar= rayfuncs.c index 4359dbd83d..7828a6264b 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -5317,6 +5317,12 @@ accumArrayResult(ArrayBuildState *astate, if (astate->nelems >=3D astate->alen) { astate->alen *=3D 2; + /* give an array-related error if we go past MaxAllocSize */ + if (!AllocSizeIsValid(astate->alen * sizeof(Datum))) + ereport(ERROR, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("array size exceeds the maximum allowed (%d)", + (int) MaxAllocSize))); astate->dvalues =3D (Datum *) repalloc(astate->dvalues, astate->alen * sizeof(Datum)); astate->dnulls =3D (bool *) ------- =_aaaaaaaaaa0--