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 1vJOmb-004jca-0X for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Nov 2025 04:18:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vJOmX-00GEu5-3B for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Nov 2025 04:18:02 +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 1vJOmX-00GEtx-2C for pgsql-hackers@lists.postgresql.org; Thu, 13 Nov 2025 04:18:01 +0000 Received: from mail-m16.yeah.net ([1.95.21.14]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vJOmR-007T4a-2R for pgsql-hackers@postgresql.org; Thu, 13 Nov 2025 04:18:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yeah.net; s=s110527; h=Message-ID:Date:MIME-Version:Subject:To:From: Content-Type; bh=EQqHQHqeyi0hJTj4tZJTEd/V7v7mj6gbReqfeo3kQR0=; b=TkONtbBijJ0KmylPg8ln9XNGPQ/eydFprk22+DY5n/eF1lHLdgqRDEl57LGe67 JyAXZdOBwzQPxWDa4+tT4D6gf/YgGb8wjiluDuFyQyBiG5ePawSsICVGuF4ep8+L 2ER2ulp1ipoALCpKyerq2NJeR2bOcYboAitbCwVzItvRg= Received: from [192.168.10.60] (unknown []) by gzsmtp2 (Coremail) with SMTP id Ms8vCgC3vaXfWxVpwoHuAQ--.54100S2; Thu, 13 Nov 2025 12:17:36 +0800 (CST) Message-ID: Date: Thu, 13 Nov 2025 12:17:35 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement To: Akshay Joshi , pgsql-hackers References: Content-Language: en-US From: Quan Zongliang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: Ms8vCgC3vaXfWxVpwoHuAQ--.54100S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxGr1ftr4fAF1UXrW8GF45ZFb_yoW5Kry8p3 4fJr47AryUJry8tr18Jw1UJryUJr15Jw1UKr1UJF48Jr1UAr12qr4vqr109Fy5JrW8Jw15 Xr1DGr1UZr1DJ37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jOzVbUUUUU= X-Originating-IP: [111.167.159.4] X-CM-SenderInfo: htxd06xrqjzxxdqjq5hhdkh0dhw/1tbiOQKu0mkVW+K7VgAA3v List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/12/25 8:04 PM, Akshay Joshi wrote: > Hi Hackers, > > I’m submitting a patch as part of the broader Retail DDL Functions > project described by Andrew Dunstan https://www.postgresql.org/message- > id/945db7c5-be75-45bf-b55b-cb1e56f2e3e9%40dunslane.net www.postgresql.org/message-id/945db7c5-be75-45bf-b55b- > cb1e56f2e3e9%40dunslane.net> > > This patch adds a new system function pg_get_database_ddl(database_name/ > database_oid, pretty), which reconstructs the CREATE DATABASE statement > for a given database name or database oid. When the pretty flag is set > to true, the function returns a neatly formatted, multi-line DDL > statement instead of a single-line statement. > > *Usage examples:* > > 1) SELECT pg_get_database_ddl('test_get_database_ddl_builtin');  -- > *non-pretty formatted DDL* > > > pg_get_database_ddl > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- >   CREATE DATABASE test_get_database_ddl_builtin WITH OWNER = > regress_ddl_database ENCODING = "UTF8" LC_COLLATE = "C" LC_CTYPE = "C" > BUILTIN_LOCALE = "C.UTF-8" COLLATION_VERSION = "1" LOCALE_PROVIDER = > 'builtin' TABLESPACE = pg_default ALLOW_CONNECTIONS = true CONNECTION > LIMIT = -1; > > > 2) SELECT pg_get_database_ddl('test_get_database_ddl_builtin', true); > -- *pretty formatted DDL* > > CREATE DATABASE test_get_database_ddl_builtin >          WITH >          OWNER = regress_ddl_database >          ENCODING = "UTF8" >          LC_COLLATE = "C" >          LC_CTYPE = "C" >          BUILTIN_LOCALE = "C.UTF-8" >          COLLATION_VERSION = "1" >          LOCALE_PROVIDER = 'builtin' >          TABLESPACE = pg_default >          ALLOW_CONNECTIONS = true >          CONNECTION LIMIT = -1; > > 3) SELECT pg_get_database_ddl(16835);      -- *non-pretty formatted DDL > for OID* > 4) SELECT pg_get_database_ddl(16835, true);  -- *pretty formatted DDL > for OID* > > The patch includes documentation, in-code comments, and regression > tests, all of which pass successfully. > * > **Note:* To run the regression tests, particularly the pg_upgrade tests > successfully, I had to add a helper function, ddl_filter (in > database.sql), which removes locale and collation-related information > from the pg_get_database_ddl output. > I think we should check the connection permissions here. Otherwise: postgres=> SELECT pg_database_size('testdb'); ERROR: permission denied for database testdb postgres=> SELECT pg_get_database_ddl('testdb'); pg_get_database_ddl ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- CREATE DATABASE testdb WITH OWNER = quanzl ENCODING = "UTF8" LC_COLLATE = "zh_CN.UTF-8" LC_CTYPE = "zh_CN.UTF-8" LOCALE_PROVIDER = 'libc' TABLESPACE = pg_default ALLOW_CONNECTIONS = true CONNECTION LIMIT = -1; (1 row) Users without connection permissions should not generate DDL. Regards, Quan Zongliang > ----- > Regards, > Akshay Joshi > EDB (EnterpriseDB) > > >