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 1s7JYn-004wkc-VZ for pgsql-hackers@arkaria.postgresql.org; Wed, 15 May 2024 18:41:07 +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 1s7JYn-004LJg-TQ for pgsql-hackers@arkaria.postgresql.org; Wed, 15 May 2024 18:41:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s7JYn-004LHa-Ds for pgsql-hackers@lists.postgresql.org; Wed, 15 May 2024 18:41:05 +0000 Received: from smtp.outgoing.loopia.se ([93.188.3.37]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s7JYg-000NBe-C4 for pgsql-hackers@lists.postgresql.org; Wed, 15 May 2024 18:41:04 +0000 Received: from s807.loopia.se (localhost [127.0.0.1]) by s807.loopia.se (Postfix) with ESMTP id 083DA3016893 for ; Wed, 15 May 2024 20:40:55 +0200 (CEST) Received: from s934.loopia.se (unknown [172.22.191.5]) by s807.loopia.se (Postfix) with ESMTP id EBEF12E2A3DE for ; Wed, 15 May 2024 20:40:54 +0200 (CEST) Received: from s471.loopia.se (unknown [172.22.191.5]) by s934.loopia.se (Postfix) with ESMTP id EA0557CEA2E for ; Wed, 15 May 2024 20:40:54 +0200 (CEST) X-Virus-Scanned: amavisd-new at amavis.loopia.se X-Spam-Flag: NO X-Spam-Score: -1.2 X-Spam-Level: X-Spam-Status: No, score=-1.2 tagged_above=-999 required=6.2 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1] autolearn=disabled Authentication-Results: s471.loopia.se (amavisd-new); dkim=pass (2048-bit key) header.d=yesql.se Received: from s981.loopia.se ([172.22.191.6]) by s471.loopia.se (s471.loopia.se [172.22.190.35]) (amavisd-new, port 10024) with LMTP id BVT9FUBwGg67 for ; Wed, 15 May 2024 20:40:54 +0200 (CEST) X-Loopia-Auth: user X-Loopia-User: daniel@yesql.se X-Loopia-Originating-IP: 89.255.232.193 Received: from smtpclient.apple (customer-89-255-232-193.stosn.net [89.255.232.193]) (Authenticated sender: daniel@yesql.se) by s981.loopia.se (Postfix) with ESMTPSA id 55F2D22B16DB for ; Wed, 15 May 2024 20:40:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yesql.se; s=loopiadkim1707475645; t=1715798454; bh=LVfQ4yRxdF1L2Xq7IC2vUbFf89YqNTMRi+H+okQ2cpk=; h=From:Subject:Date:To; b=FDUgCVxHm96nKdnQ7iBCGYgaIZKFHXpngnyJ889xg/+dTVA9eyByKQZHI2iUnbhF2 8mxj7uAPOR2IV2sPE2tj9GUfb/iROgAyGDVCalnzmPAP0uJmo4KpJK+vpb3eAmABOC QLwP99230rHeqiZAHx5YwzTc/p7nphNBLwYKpGxI1H2158a/Q0YvlMCl1j3QSmv+bB cFZF+q704W1S7ezScsMMglCABBPtUxc5mlEd0gpjmN0ybeqi/Bg1/KyJSF2lnQ3LvN yOWYSZ7EMp72jiI+OMtcPggw+FdTcvlY7NxsjfZTQnKfHzWatsPXn1fNglGl2zeFXN 4Fphv7Sq1JIkg== From: Daniel Gustafsson Content-Type: multipart/mixed; boundary="Apple-Mail=_89EE57CA-704B-42EC-9174-03867D6FDC16" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.500.171.1.1\)) Subject: Fix PGresult leak in pg_dump during binary upgrade Message-Id: <374550C1-F4ED-4D9D-9498-0FD029CCF674@yesql.se> Date: Wed, 15 May 2024 20:40:43 +0200 To: PostgreSQL Hackers X-Mailer: Apple Mail (2.3774.500.171.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_89EE57CA-704B-42EC-9174-03867D6FDC16 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii While looking at pg_dump performance today I noticed that pg_dump fails = to clear query results in binary_upgrade_set_pg_class_oids during binary = upgrade mode. 9a974cbcba00 moved the query to the outer block, but left the = PQclear and query buffer destruction in the is_index conditional, making it not = always be executed. 353708e1fb2d fixed the leak of the query buffer but left = the PGresult leak. The attached fixes the PGresult leak which when = upgrading large schemas can be non-trivial. This needs to be backpatched down to v15. -- Daniel Gustafsson --Apple-Mail=_89EE57CA-704B-42EC-9174-03867D6FDC16 Content-Disposition: attachment; filename=0001-Fix-query-result-leak-during-binary-upgrade.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0001-Fix-query-result-leak-during-binary-upgrade.patch" Content-Transfer-Encoding: quoted-printable =46rom=206d6f286d62d211e2238948c1ab8d7dd3d5f6c00a=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Daniel=20Gustafsson=20= =0ADate:=20Wed,=2015=20May=202024=2020:32:14=20= +0200=0ASubject:=20[PATCH]=20Fix=20query=20result=20leak=20during=20= binary=20upgrade=0A=0A9a974cbcba00=20moved=20the=20query=20in=20= binary_upgrade_set_pg_class_oids=20to=20the=0Aouter=20level,=20but=20= left=20the=20PQclear=20and=20query=20buffer=20destruction=20in=20the=0A= is_index=20conditional.=20=20353708e1fb2d=20fixed=20the=20leak=20of=20= the=20query=20buffer=0Abut=20left=20the=20PGresult=20leak.=20This=20= moves=20clearing=20the=20result=20to=20the=20outer=0Alevel=20ensuring=20= that=20it=20will=20be=20called.=0A---=0A=20src/bin/pg_dump/pg_dump.c=20|=20= 4=20++--=0A=201=20file=20changed,=202=20insertions(+),=202=20= deletions(-)=0A=0Adiff=20--git=20a/src/bin/pg_dump/pg_dump.c=20= b/src/bin/pg_dump/pg_dump.c=0Aindex=20ac920f64c7..5dfa7b3bcb=20100644=0A= ---=20a/src/bin/pg_dump/pg_dump.c=0A+++=20b/src/bin/pg_dump/pg_dump.c=0A= @@=20-5479,8=20+5479,6=20@@=20binary_upgrade_set_pg_class_oids(Archive=20= *fout,=0A=20=09=09=09=09=09=09=09=20=20"SELECT=20= pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid)= ;\n",=0A=20=09=09=09=09=09=09=09=20=20toast_index_relfilenumber);=0A=20=09= =09}=0A-=0A-=09=09PQclear(upgrade_res);=0A=20=09}=0A=20=09else=0A=20=09{=0A= @@=20-5493,6=20+5491,8=20@@=20binary_upgrade_set_pg_class_oids(Archive=20= *fout,=0A=20=09=09=09=09=09=09=20=20relfilenumber);=0A=20=09}=0A=20=0A+=09= PQclear(upgrade_res);=0A+=0A=20=09appendPQExpBufferChar(upgrade_buffer,=20= '\n');=0A=20=0A=20=09destroyPQExpBuffer(upgrade_query);=0A--=20=0A2.39.3=20= (Apple=20Git-146)=0A=0A= --Apple-Mail=_89EE57CA-704B-42EC-9174-03867D6FDC16--