public inbox for [email protected]  
help / color / mirror / Atom feed
From: SATYANARAYANA NARLAPURAM <[email protected]>
To: Jeff Davis <[email protected]>
Cc: Andrew Dunstan <[email protected]>
Cc: David G. Johnston <[email protected]>
Cc: Japin Li <[email protected]>
Cc: Zsolt Parragi <[email protected]>
Cc: Euler Taveira <[email protected]>
Cc: Álvaro Herrera <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: pg_get__*_ddl consolidation
Date: Mon, 13 Apr 2026 12:41:31 -0700
Message-ID: <CAHg+QDdWtv9PKtPZEokwGCNtbv4MVnfYw5wMZrsEj4xizSNe5Q@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<[email protected]>
	<[email protected]>
	<CAN4CZFNYM5jNA_gLu9miAXg_7c8Z2zf-ePc+0rzidAV3CBs=cw@mail.gmail.com>
	<[email protected]>
	<SY7PR01MB10921A6E1E08A48F3426FE529B651A@SY7PR01MB10921.ausprd01.prod.outlook.com>
	<CAKFQuwYcppypeGBwa7ZbDAfoUXSv+kLhJuAXsdwBmKrvy8wDFw@mail.gmail.com>
	<[email protected]>
	<[email protected]>
	<[email protected]>

Hi,

On Fri, Apr 10, 2026 at 1:03 PM Jeff Davis <[email protected]> wrote:

> On Sun, 2026-04-05 at 11:06 -0400, Andrew Dunstan wrote:
> > Pushed. I have moved the remaining get_*_ddl items to PG20-1
>
> The line:
>
>   role_settings = DatumGetArrayTypeP(datum);
>
> should be DatumGetArrayTypePCopy(), because it's being pfree()d later.
> The existing code will sometimes make a copy and sometimes not, e.g.:
>
>   -- settings are contrived to make the datum inline
>   CREATE USER u1;
>   ALTER ROLE u1 SET search_path = 'public, pg_catalog, pg_temp';
>   ALTER ROLE u1 SET work_mem='64MB';
>   ALTER ROLE u1 SET statement_timeout='30s';
>   ALTER ROLE u1 SET lock_timeout='10s';
>   ALTER ROLE u1 SET idle_in_transaction_session_timeout = '60s';
>   SELECT pg_get_role_ddl('u1');
>   ERROR:  pfree called with invalid pointer 0x7986dd0c7cc8 (header
> 0x0000400600000000)
>

Yes, it appears to be a bug. Attached a patch to fix this. Tested with the
attached patch and don't see server crashing after that.

postgres=#   CREATE DATABASE crashtest TEMPLATE template0 LC_COLLATE 'C'
LC_CTYPE 'C';
  ALTER DATABASE crashtest SET search_path = 'public, pg_catalog';
  ALTER DATABASE crashtest SET work_mem = '64MB';
  ALTER DATABASE crashtest SET statement_timeout = '30s';
  ALTER DATABASE crashtest SET random_page_cost = 1.5;
  SELECT pg_get_database_ddl('crashtest');
CREATE DATABASE
ALTER DATABASE
ALTER DATABASE
ALTER DATABASE
ALTER DATABASE
                                            pg_get_database_ddl

------------------------------------------------------------------------------------------------------------
 CREATE DATABASE crashtest WITH TEMPLATE = template0 ENCODING = 'UTF8'
LOCALE_PROVIDER = libc LOCALE = 'C';
 ALTER DATABASE crashtest OWNER TO azureuser;
 ALTER DATABASE crashtest SET search_path TO 'public, pg_catalog';
 ALTER DATABASE crashtest SET work_mem TO '64MB';
 ALTER DATABASE crashtest SET statement_timeout TO '30s';
 ALTER DATABASE crashtest SET random_page_cost TO '1.5';
(6 rows)

Thanks,
Satya


Attachments:

  [application/octet-stream] v1-0001-ddlutils-pfree-crash.patch (770B, 3-v1-0001-ddlutils-pfree-crash.patch)
  download | inline diff:
diff --git a/src/backend/utils/adt/ddlutils.c b/src/backend/utils/adt/ddlutils.c
index b16c277d..c4f9f86c 100644
--- a/src/backend/utils/adt/ddlutils.c
+++ b/src/backend/utils/adt/ddlutils.c
@@ -480,7 +480,7 @@ pg_get_role_ddl_internal(Oid roleid, bool pretty, bool memberships)
 		if (isnull)
 			continue;
 
-		role_settings = DatumGetArrayTypeP(datum);
+		role_settings = DatumGetArrayTypePCopy(datum);
 
 		deconstruct_array_builtin(role_settings, TEXTOID, &settings, &nulls, &nsettings);
 
@@ -1060,7 +1060,7 @@ pg_get_database_ddl_internal(Oid dbid, bool pretty,
 		if (isnull)
 			continue;
 
-		dbconfig = DatumGetArrayTypeP(datum);
+		dbconfig = DatumGetArrayTypePCopy(datum);
 
 		deconstruct_array_builtin(dbconfig, TEXTOID, &settings, &nulls, &nsettings);
 


view thread (31+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
  Subject: Re: pg_get__*_ddl consolidation
  In-Reply-To: <CAHg+QDdWtv9PKtPZEokwGCNtbv4MVnfYw5wMZrsEj4xizSNe5Q@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox