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 1vThCg-0086Ub-13 for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Dec 2025 13:59:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vThCe-003urZ-0o for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Dec 2025 13:59:33 +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.96) (envelope-from ) id 1vThCd-003urR-1R for pgsql-hackers@lists.postgresql.org; Thu, 11 Dec 2025 13:59:32 +0000 Received: from fout-b4-smtp.messagingengine.com ([202.12.124.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vThCc-0009UB-1F for pgsql-hackers@postgresql.org; Thu, 11 Dec 2025 13:59:31 +0000 Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfout.stl.internal (Postfix) with ESMTP id B84571D0011A; Thu, 11 Dec 2025 08:59:28 -0500 (EST) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-06.internal (MEProxy); Thu, 11 Dec 2025 08:59:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eulerto.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1765461568; x=1765547968; bh=Z5ecVyGN5p4ukMGPR0RdO/gzfx/a2ilAz1rwvW4wylU=; b= J+yrAlY9PgOFZNZiNKj7jlpNfqg70g91B8hwVs6+cyOgz/cD4bd2pqhwSNglulhL 940guctOVFcGZpDh74kzuUPOM2lh4BA0A6SzsjsMJgYxpDJ+ncaZs7H3x2sO4XAh hGFfMWkp7pVh4BF2i6oykCWHiGgcje939kQTM9EJabXAHZCH9guuKTok5qpIxBAh W6j8+/RgwoIfMYajGth2ie+LUoGyJyVDuOjKq3nvFgiu4F/y4m+K1oWJ7vRbe1ql DfSHOge8JYngf7vjTLQk9jJuXPZMiGf077yUMJKy+Il+jAkElY/R9Kd4lqJfQX85 iP517Jzlrqno2e6bIeSQBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1765461568; x= 1765547968; bh=Z5ecVyGN5p4ukMGPR0RdO/gzfx/a2ilAz1rwvW4wylU=; b=g 6DG2r/2H2wFn5JAqPgRqoc4qoWPbKD+dY9XrwEyT/Xst+44WuM2+v9y0QI3Ms8ek hbzZQIsbiTijr4R3MONYwqvzx9qHZJexvSQjtIU77Rbx9S8XvhEKh+gHGSsU+BwT 8Y2hO1eO8NXLBTyqkp2n1QY3QDjt1W6yND5bj6LtyCRCIT+2GoCwQeJQgvmTTQPB ntCKaKqIYelqCDTqdyQpRt179Q6FJL8WB12T5vCRHLqkYSoTA+VkrWzNRGAthQS5 BaH2jtt06N0xUzwVvQnv9yTHVOURtH8l0keEdcVW5oC4+l6CN3ePSC44YQ0bL9Bd FzcK/ZjGc8IrSgH9o0aUQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvheeglecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggffhffvvefkjghfufgtgfesthejredtredttdenucfhrhhomhepfdfguhhlvghr ucfvrghvvghirhgrfdcuoegvuhhlvghrsegvuhhlvghrthhordgtohhmqeenucggtffrrg htthgvrhhnpeelvefgieelgeffgfelgfeffeffgefhfedvfedtuddvffeiieejffduleei hfdvveenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtohhmnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepvghulhgvrhesvghulhgv rhhtohdrtghomhdpnhgspghrtghpthhtohepiedpmhhouggvpehsmhhtphhouhhtpdhrtg hpthhtoheprghkshhhrgihrdhjohhshhhisegvnhhtvghrphhrihhsvggusgdrtghomhdp rhgtphhtthhopehlihdrvghvrghnrdgthhgrohesghhmrghilhdrtghomhdprhgtphhtth hopehjrghpihhnlhhisehhohhtmhgrihhlrdgtohhmpdhrtghpthhtoheprghlvhhhvghr rhgvsehkuhhrihhlvghmuhdruggvpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrsh esphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepqhhurghniihonhhglhhirghn gheshigvrghhrdhnvght X-ME-Proxy: Feedback-ID: i0c21471d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id CCA971820074; Thu, 11 Dec 2025 08:59:27 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: AyfDEN4X9YCs Date: Thu, 11 Dec 2025 10:59:07 -0300 From: "Euler Taveira" To: "Akshay Joshi" , =?UTF-8?Q?=C3=81lvaro_Herrera?= Cc: "Chao Li" , japin , "Quan Zongliang" , pgsql-hackers Message-Id: <4e60bcae-8222-4e1f-8e5b-d73b59c93304@app.fastmail.com> In-Reply-To: References: <202511191045.tckydhpoxumx@alvherre.pgsql> Subject: Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Nov 20, 2025, at 6:18 AM, Akshay Joshi wrote: > > Implemented in the suggested solution. Attached is the v5 patch for review. > I reviewed your patch and have some suggestions for this patch. * You shouldn't include the property if the value is default. A long command adds nothing. Clarity? Tell someone that needs to select, copy and paste a long statement. It is a good goal to provide a short command to reconstruct the object. If you don't know why it didn't include CONNECTION LIMIT, it is time to check the manual again. $ psql -AtqX -c "SELECT pg_get_database_ddl('postgres')" -d postgres CREATE DATABASE postgres WITH OWNER = euler ENCODING = "UTF8" LC_COLLATE = "pt_BR.UTF-8" LC_CTYPE = "pt_BR.UTF-8" COLLATION_VERSION = "2.36" LOCALE_PROVIDER = 'libc' TABLESPACE = pg_default ALLOW_CONNECTIONS = true CONNECTION LIMIT = -1; * Use single quotes. The encoding, locale, lc_collate, lc_type, collation_version and some other properties should use single quotes. The locale_provider doesn't need a single quote because it is an enum. See how pg_dumpall constructs the command. Use simple_quote_literal. $ pg_dumpall --binary-upgrade | grep 'p5' -- Database "p5" dump -- Name: p5; Type: DATABASE; Schema: -; Owner: euler CREATE DATABASE p5 WITH TEMPLATE = template0 OID = 16392 STRATEGY = FILE_COPY ENCODING = 'UTF8' LOCALE_PROVIDER = libc LOCALE = 'en_US.UTF-8' COLLATION_VERSION = '2.36'; ALTER DATABASE p5 OWNER TO euler; \connect p5 * OWNER. There is no guarantee that the owner exists in the cluster you will use this output. That's something that pg_dumpall treats separately (see above). Does it mean we should include the owner? No. We can make it an option. * LOCALE. Why didn't you include it? I know there are some combinations that does not work together but this function can provide a minimal set of properties related to locale. postgres=# CREATE DATABASE p6 LOCALE_PROVIDER builtin LOCALE 'C' TEMPLATE template0; CREATE DATABASE * STRATEGY. Although this is a runtime property, it should be an option. * TEMPLATE. Ditto. * options. Since I mentioned options for some properties (owner, strategy, template), these properties can be accommodated as a VARIADIC argument. The function signature can be something like pg_get_database_ddl(oid, VARIADIC options text[]) I would include the pretty print into options too. * Tabs. I don't think we use tabs to format output. Use spaces. A good practice is to use EXPLAIN style (2 spaces)and depending on the nesting, 4 spaces are fine too. $ psql -AtqX -c "SELECT pg_get_database_ddl('postgres', true)" -d postgres CREATE DATABASE postgres WITH OWNER = euler ENCODING = "UTF8" LC_COLLATE = "pt_BR.UTF-8" LC_CTYPE = "pt_BR.UTF-8" COLLATION_VERSION = "2.36" LOCALE_PROVIDER = 'libc' TABLESPACE = pg_default ALLOW_CONNECTIONS = true CONNECTION LIMIT = -1; * permission. I don't think you need to check for permissions inside the function. I wouldn't want a different behavior than pg_dump(all). You can always adjust it in system_functions.sql. * typo. +-- +-- Reconsturct DDL +-- s/Reconsturct/Reconstruct/ -- Euler Taveira EDB https://www.enterprisedb.com/