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 1w62SS-003t15-2R for pgsql-bugs@arkaria.postgresql.org; Fri, 27 Mar 2026 08:22:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w62SR-008NWV-0K for pgsql-bugs@arkaria.postgresql.org; Fri, 27 Mar 2026 08:22:19 +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 1w62SQ-008NWM-2b for pgsql-bugs@lists.postgresql.org; Fri, 27 Mar 2026 08:22:19 +0000 Received: from mail-qv1-xf29.google.com ([2607:f8b0:4864:20::f29]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w62SO-00000001RcJ-2htd for pgsql-bugs@lists.postgresql.org; Fri, 27 Mar 2026 08:22:18 +0000 Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-899e87b04d8so26651056d6.3 for ; Fri, 27 Mar 2026 01:22:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774599735; cv=none; d=google.com; s=arc-20240605; b=QL+uamti5NoBClHPjsutpSUq/dL04hTMSqJipE1NuPo+3SzSnRST91aM+bUYcofLwW AKi0dFhbuwx65AI2ZXilfZrr1ZCHGsJGXjgRyV3fN5Fuff4RoAve+/VoTRz2DL43yaLE h862ArXEwqnyMWavBtvhyflhuMEBrEPATg57DaFKcXj2j3NOCoiPrbA2iKx8nEk09SPa 7Pyg2nETxdq+zr8jjLBJIEJTUOjzAfGO0IKVHhQUnT3xEYTsgpHQZNqjG/NSwHlhVku2 +yHOEtY/IHH5F1WQ6BAmUwF8XwefScnSTmhrD5ZmHBmhRh8cPClDSQTyiSs1jCfv186u EKKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=EFWuiitnNrAwvqFk00a6obrhjJqsooRZttctuASwpaU=; fh=AN65gTTE1ve+EIG2GtiWlHWJipwFInGfyBqoPMSK+CI=; b=TMbp++rpNBwhI5gv/W29LR9XHbHv743TnkXaiHP3HEXSNtAlQWsNdYrqPeu1ZKOzTm xNaVdLuQdbGy1sZUDtknbnd9Zv4JhJcnBS5B3K1yNCaYYqo7m0elqlxW1CNpWauFsxF1 RZV2z7X0fBorktN5cCh/CilYdTHr5rb8/3h5av9LxFBVRQod0hdhMUZGlEEWjzL80aK6 H545T28THPH/cVN6JLULSGOEz6WrkjP8z1ZY9/u10A3DHcehd4/ORKNvIwUXeF7Cv0/R 1/7eSpqHQkpCeBw6RDYSdkfo4aJbSZy8cPeYvJKdbm5/DBbt+KYz/KsYB1oftzKdZ3tl E0ug==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fittl.com; s=google; t=1774599735; x=1775204535; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EFWuiitnNrAwvqFk00a6obrhjJqsooRZttctuASwpaU=; b=BhVjQ2R4fFkCQe6hJuy/ugOGndCndfGTIxp4i3L9TGnPUmnGYom0b5K6GfdmP0XUSN Xf9+udjakj1uClSj6y2NuzHqA/jRC/os39XTG3kcRMDP/AwV2ALiIknbs2/Z3jisAd5R rargsgdxA1dPNr+RkB0/4iXb53XsVtBpBrpKQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774599735; x=1775204535; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=EFWuiitnNrAwvqFk00a6obrhjJqsooRZttctuASwpaU=; b=TQZ0k70xjgef5vebnwJYEnxZPIYTUo6xVoTF8Ynl0U6A8aG/5k6Xsse+wNm0wyMZ6M VvH82X0vzh1PbyiD2jXhLK5z0MI6kIkwtjBbpfHo8LLlJ+qcWFhOVilztQIDvTpiaoik IyT8+kqTsPp1VBxYWZER0XJjT1gn7d5nKQydEJbHN/bMh7ywNTdBBP7GpXCKbkFDa6Yc 4QELivSIgADSIC7DQ2AK9CyS2avj1fwJqUVIU9ZuiBu7ipd99YZ6YMTm7xof6QEL3+hM w8li5kKq4hnO6vQGi3ELcqM6IMHruTCH6dJXRFFtlBLtiwS6HzcbfUHv6YRJGa59MTIu nECg== X-Gm-Message-State: AOJu0YxtqUDBv1QphW3wrHYLzqMpqC448PlbcoTs9frvzI8qbWHhIE5Q Q0V6fWZGsvDs0e0NX5qhlm4Ky/IKkusylIfy+XVGkgNlHxsfZ7CpYKg8DS+BHmngAWOeFK7Fgkk rtvhNlPjTpXU61fBxwQQYeGY4SDBINQv2BRWlfsyK X-Gm-Gg: ATEYQzyna0F8UQHKASw48b5ETRgDXtKm/+qBM8zf5sejcQ/hpQuTEmYAH41/YQAFHuS PDPSTHnzlEBjwWeV4GB0Af7G2zrgxoETnHuVYtm/sD5D3HKD4eieLQ+JzmsBlZqbEPGbOWYZvxj wd66hFxLmKmpTFC1l/FSDGwTtDmcqx49GpYGcSrH6AyJxPEeFE9qp248sgMaLlJRUTXCqUZxps3 gxZA9IDYoSfLkTSSUqj/M1cylpy98hb7ohFVQmmHIEtzb4e3lukVrlTu6DbMUORByG5U2DIys6p uG/jYaKxCV+VSMVEpyBPOsubo9T/c6PhH6Rom+69s5q6rCYxwBY= X-Received: by 2002:a05:6214:600f:b0:89a:1536:2528 with SMTP id 6a1803df08f44-89ce8d47deemr16256056d6.26.1774599735086; Fri, 27 Mar 2026 01:22:15 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Lukas Fittl Date: Fri, 27 Mar 2026 01:21:39 -0700 X-Gm-Features: AQROBzAq3kzrcJHcBG1KdMQ4HeJMaM57bsqifjktZtycrj5r1sX0zIHNGsX8hxk Message-ID: Subject: Re: Memory leak in pg_stat_statements when qtext file contains invalid encoding To: Gaurav Singh Cc: pgsql-bugs@lists.postgresql.org 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 Hi Gaurav, On Fri, Mar 27, 2026 at 12:54=E2=80=AFAM Gaurav Singh wrote: > If the qtext file contains an invalid encoding, pg_any_to_server calls er= eport(ERROR) which longjmps out of the function. > The cleanup code at the bottom of the function is never reached. > > LWLockRelease(pgss->lock); > if (qbuffer) > free(qbuffer); > On every subsequent call, the malloc'd buffer (the entire file contents) = is leaked, and the LWLock release is also skipped. I don't think the analysis is correct in regards to the LWLock release - that should be taken care of by LWLockReleaseAll on abort. But I think you're correct about qbuffer - because that buffer is using malloc (not palloc), its not part of any memory context, and so it will happily leak on abort. It appears our use of malloc in pg_stat_statements is so that we can fail on OOM and return NULL without a jump. I think that makes sense for when a GC cycle was triggered during regular query execution (since we don't want to error the original query), but it seems like just bubbling up the OOM if needed when querying the pg_stat_statements function seems fine. I wonder if its worth separating the two cases, since the issue you're describing (the call to pg_any_to_server failing) only happens when returning the query text file contents to the client. I think your PG_FINALLY suggestion could also work, but it feels a bit tedious to wrap the whole pg_stat_statements_internal function in it. Thanks, Lukas PS: I would recommend reviewing the use of a text format email client for posting to the Postgres mailing lists, or significantly reducing your formatting when sending HTML emails - your email has a lot of styling that is hard to read (even for me in Gmail), and even harder to quote in a plain text email response. -- Lukas Fittl