Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement - Mailing list pgsql-hackers

From Akshay Joshi
Subject Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement
Date
Msg-id CANxoLDfR-8nOYetoSKiJ9yVvMkXodVRqR+e=7CSEwiJTrU7yrA@mail.gmail.com
Whole thread
In response to Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement  ("Euler Taveira" <euler@eulerto.com>)
List pgsql-hackers
Following suggestions from Alvaro and Mark, I have re-implemented this feature using variadic function argument pairs.
This patch incorporates Mark Wong’s documentation updates, Amul’s one review comment, and the majority of Euler’s comments.

New changes:
- Supports flexible DDL options as key-value pairs:
    - pretty - Format output for readability
    - owner - Include OWNER clause
    - tablespace - Include TABLESPACE clause
    - defaults - Include parameters even if it is set to default value.
- Option values accept: 'yes'/'on'/true/'1' (or their negatives)

Usage Examples:
  -- Basic usage with options
  SELECT pg_get_database_ddl('mydb', 'owner', 'yes', 'defaults', 'yes');
  -- Pretty-printed output without owner and tablespace
  SELECT pg_get_database_ddl('mydb', 'owner', 'no', 'tablespace', 'no', 'pretty', 'on');
  -- Using boolean values
  SELECT pg_get_database_ddl('mydb', 'owner', false, 'defaults', true);
  -- Using OID
  SELECT pg_get_database_ddl(16384, 'pretty', 'yes');

Note: To keep things clean, I’ve moved the logic into two generic functions (get_formatted_string and parse_ddl_options) and a common DDLOptionDef struct. This should simplify the work for the rest of the pg_get_<object>_ddl patches.

Attached is the v9 patch which is ready for review.


On Thu, Feb 26, 2026 at 2:49 AM Euler Taveira <euler@eulerto.com> wrote:
On Wed, Feb 25, 2026, at 8:53 AM, Álvaro Herrera wrote:
>
> I'm surprised to not have seen an update on this topic following the
> discovery by Mark Wong that commit d32d1463995c (in branch 18) already
> established a convention for passing arguments to functions: use argument
> pairs to variadic functions, the way pg_restore_relation_stats() and
> pg_restore_attribute_stats() work.  While I like my previous suggestion
> of using DefElems better, I think it's more sensible to follow this
> established precedent and not innovate on this.
>

This convention is much older than the referred commit. It predates from the
logical decoding (commit b89e151054a0). See pg_logical_slot_get_changes_guts()
that is an internal function for pg_logical_slot_FOO_changes().  It seems a
good idea to have a central function to validate the variadic parameter for all
of these functions.


--
Euler Taveira
EDB   https://www.enterprisedb.com/
Attachment

pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Non-text mode for pg_dumpall
Next
From: Andrey Borodin
Date:
Subject: Re: Modernize error message for malformed B-Tree tuple posting