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.96) (envelope-from ) id 1wgyc5-006Wmz-0O for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jul 2026 05:44:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wgyc2-004VgU-2B for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jul 2026 05:44:54 +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.96) (envelope-from ) id 1wgyc2-004VgM-1H for pgsql-hackers@lists.postgresql.org; Tue, 07 Jul 2026 05:44:54 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wgyc0-00000002BfL-1quG for pgsql-hackers@lists.postgresql.org; Tue, 07 Jul 2026 05:44:54 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-493b27c7451so2251955e9.0 for ; Mon, 06 Jul 2026 22:44:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783403091; x=1784007891; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=pHRrepPBjzo/Ik+SI5mG4Xm76rWmEj24STytRMjGzRY=; b=h8i7ByMzJ2MJZYihrsJSCoHB9/ytI3aShQ13QF05T2nO3btWArbEpBGD+ar5DZFbS3 l8/05L7K3LbB5evbSG2/pD+5iKYs/pF0yePspecEGrJLutGKtW4m72WIyzzHwao0PqhV hcY8p5iarEucy78j0ToSCtHDf77cnJ9O0ICJekl+cC0JjTT0hCRLDe9KZagl0fAsuXUH 3XrqhxotsVwwvmATqUhqpVaXP7FXjN9TTBJaydCxE6qC2CAYcoj4vsXDrN4RTECBUwNK UxYVyjLjK2obfXWzAs1ucq/lQy/krHav27cJ0H/AvI5d5qg4/4ArwvP08lLXPsIKId2i PVnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783403091; x=1784007891; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=pHRrepPBjzo/Ik+SI5mG4Xm76rWmEj24STytRMjGzRY=; b=aXrLXbAct3RAURVLKz88QwmnFlHgg7VcL+OWKoojYUn5VS6zHWLK86kcnttYzCHfsc 4aXq1nJNPq8yx/omkRURpFoeRCZj4kCP42hj8Jw4DmuOSIJ3YOkA1GKCSQveJn0ksjFw WKiA2dldDhip21Jz7a+h5aX6JRJrLWMbXN8q2eIIVJW8Mc2u2FyAZFgZXTJW6J2qG12H 1rEA9Kj1SQCS4sL9l3vE9tMJdgj/YV08bbjbv9gxySVdBAjitflco7X2rbABDANaxFJW 2pOtl20Peo+2UnX7rAh2swFayAsSfV2ZSEZNmeK+2h6h6ZIA55ZW/je+E6UwC0Zy4VlA YNtw== X-Gm-Message-State: AOJu0YzzSffHv2PuByzG01RzamVL3eYjxL1uB/NvarRLfpAD6hLqyvG5 RNGb89UysHg4sT9ww2htawRG/umBcXN2RkTxQ70Ioh/0k64l/uOP9kSgeErSUjkhg1C6UzAg X-Gm-Gg: AfdE7clkCud0hYbg2WVDqV+SclTSDrfHua3t0nYo8cHCGwszj5l4DHRTxzjP/x51ATq IZLcW5GFAzRWJVSErG/Zyk9bfLv3FkXr/2C6YQOou+Ci9kfOXJSput5omb+gqgkvlapOArKFy8y fGqb29VK9eG6hgvBY2AGnPeFIJVo4JLhHY3uZXsK2mCIekQev9f2MF8WsYO8T3MfzZYgR09YDi+ /umW5PKxInHoJdkUt1tp3zJIwC/2Uh4NV4dr9Jtt9WZZoAU0KC2ZglgWwU2qUWDimIjZl6YzW8V HIW+x/iFdjlkxzXOCKfUnUffyLXU0bxvr1+Rqdp0ij8sEGiyMl9f3QUUQH/CgKLSeSRRLQXnxVf dMPMF+S4URzzcipmadSqyFYwHYcxETJcblXalGlQ/DysbifN7ktULXs1KNpJ9fbeFnQDCRiweCa LF77riGc6OK17T6MEX98s4z/tcmXhNaxAJDBtaRmyC5pLHFmf8AmSuFKU= X-Received: by 2002:a05:600c:45d4:b0:492:714d:8c4 with SMTP id 5b1f17b1804b1-493e101430cmr8730225e9.11.1783403090675; Mon, 06 Jul 2026 22:44:50 -0700 (PDT) Received: from pg-ThinkPad-T14-Gen-4 (ppp91-78-198-89.pppoe.mtu-net.ru. [91.78.198.89]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47a9de1d905sm31076084f8f.2.2026.07.06.22.44.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jul 2026 22:44:50 -0700 (PDT) Date: Tue, 7 Jul 2026 10:44:45 +0500 From: Andrey Rachitskiy To: Tom Lane Cc: PostgreSQL Hackers , Nikolay Shaplov Subject: Re: [PATCH] Limit PL/Perl scalar copies to work_mem Message-ID: <20260707104445.4e0816b1@pg-ThinkPad-T14-Gen-4> In-Reply-To: <1792143.1783389377@sss.pgh.pa.us> References: <20260707034157.5e88bf34@pg-ThinkPad-T14-Gen-4> <1792143.1783389377@sss.pgh.pa.us> X-Mailer: Claws Mail 4.3.1 (GTK 3.24.52; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks for the review, Tom. You're right that work_mem is a poor fit for a hard failure here, and more generally that this isn't the sort of problem PL/Perl can solve with a small boundary check alone. I should have raised the idea on the list for discussion before sending a patch =E2=80=94 I'll do that next = time rather than charging ahead with a fix. Thanks for the feedback. On Mon, 06 Jul 2026 21:56:17 -0400, Tom Lane wrote: > Andrey Rachitskiy writes: > > When a PL/Perl function returns a large text value, sv2cstr() > > copies the entire Perl string into backend memory with no size > > check. The helper is used on the path from Perl return values and > > SPI arguments to PostgreSQL text datums; it simply palloc()s a copy > > after SvPVutf8(). A user who is allowed to create untrusted PL/Perl > > functions can therefore force the backend to allocate strings far > > larger than any session limit. On a memory-constrained host this > > can get the backend process killed by the OOM killer (SIGKILL) > > rather than raising a catchable PostgreSQL error. >=20 > This is true of very many operations in PG, not only PL/Perl. > Our general answer to that is to disable memory overcommit > so that the OOM killer won't apply. One should also note that > the same PL/Perl function can (try to) allocate enormous amounts > of memory entirely within Perl, where we have no ability to stop > it. I don't see how constraining the size of a function result > string helps noticeably. >=20 > > This patch rejects Perl strings larger than work_mem * 1024 bytes, >=20 > Our normal understanding of work_mem is that it's a point beyond which > we'll spill to disk, or otherwise try to reduce our memory consumption > at the cost of longer runtime. Not a point at which an outright query > failure is OK. >=20 > So, even if I thought this were something we should address, > I don't believe this is an appropriate approach to a fix. >=20 > regards, tom lane --=20 Regards, Andrey Rachitskiy