[PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

Started by Tim Waizenegger3 months ago13 messages
#1Tim Waizenegger
tim.waizenegger@enterprisedb.com
1 attachment(s)

Hi all,

Following the recent "Retail DDL" discussion [1]/messages/by-id/945db7c5-be75-45bf-b55b-cb1e56f2e3e9@dunslane.net, we're submitting another
implementation: pg_get_domain_ddl().

This function reconstructs CREATE DOMAIN statements for existing domains,
following what seems to be the agreed pg_get_{objecttype}_ddl naming convention.

## Function

pg_get_domain_ddl(regtype) returns text

Returns a complete CREATE DOMAIN statement including base type, default values,
and all constraints. Uses get_typdefault() for proper expression handling and
supports schema-qualified domains.

## Example

```
CREATE DOMAIN regress_us_postal_code AS TEXT
DEFAULT '00000'
CONSTRAINT regress_us_postal_code_check
CHECK (
VALUE ~ '^\d{5}$'
OR VALUE ~ '^\d{5}-\d{4}$'
);
SELECT pg_get_domain_ddl('regress_us_postal_code');

pg_get_domain_ddl
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE DOMAIN public.regress_us_postal_code AS text DEFAULT
'00000'::text CONSTRAINT regress_us_postal_code_check CHECK (VALUE ~
'^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
(1 row)
```

## Implementation

- New "Get Object DDL Functions" documentation section
- Comprehensive regression tests in a separate file where we will add
tests for the other objects functions.

We're unsure about the place where to add the trigger to the `object_ddl` test.
We added it now in `src/test/regress/parallel_schedule`, please let us know
if there is a better place.

This is part of a coordinated effort where we've divided the DDL functions
among different contributors. Additional patches for other object types
(tables, indexes, etc.) will follow from other team members.
Already submitted are: CREATE TRIGGER [2]/messages/by-id/CAPXBC8K5awmtMoq66DGHe+nD7hUf6HPRVHLeGNBRpCDpzusOXQ@mail.gmail.com and CREATE POLICY [3]/messages/by-id/CANxoLDdJsRJqnjMXV3yjsk07Z5iRWxG-c2hZJC7bAKqf8ZXj_A@mail.gmail.com.

Patch attached. Feedback welcome.

[1]: /messages/by-id/945db7c5-be75-45bf-b55b-cb1e56f2e3e9@dunslane.net
[2]: /messages/by-id/CAPXBC8K5awmtMoq66DGHe+nD7hUf6HPRVHLeGNBRpCDpzusOXQ@mail.gmail.com
[3]: /messages/by-id/CANxoLDdJsRJqnjMXV3yjsk07Z5iRWxG-c2hZJC7bAKqf8ZXj_A@mail.gmail.com

---
Best regards,
Florin Irion
Tim Waizenegger

EDB (EnterpriseDB)

Attachments:

v1-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchapplication/octet-stream; name=v1-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchDownload
From 6dbbf85dfe261c15145e857c9ee5535c1e591545 Mon Sep 17 00:00:00 2001
From: Florin Irion <florin.irion@enterprisedb.com>
Date: Thu, 18 Sep 2025 18:52:43 +0200
Subject: [PATCH v1 1/2] Add pg_get_domain_ddl() function to reconstruct CREATE
 DOMAIN statements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch introduces a new system function pg_get_domain_ddl() that
reconstructs the CREATE DOMAIN statement for a given domain. The function
takes a regtype parameter and returns the complete DDL statement including
the domain name, base type, default value, and all associated constraints.

The function follows the same pattern as other DDL reconstruction functions
like pg_get_functiondef() and pg_get_constraintdef(), providing a
decompiled reconstruction rather than the original command text.

Key features:
* Supports domains with default values
* Includes all domain constraints (CHECK, NOT NULL)
* Properly quotes identifiers and schema names
* Handles complex constraint expressions

A new documentation section "Get Object DDL Functions" has been created
to group DDL reconstruction functions, starting with pg_get_domain_ddl().
This provides a foundation for future DDL functions for other object types.

Comprehensive regression tests are included covering various domain
configurations.

Reference: PG-151
Author: Florin Irion <florin.irion@enterprisedb.com>
Author: Tim Waizenegger <tim.waizenegger@enterprisedb.com>
Reviewed-by: Álvaro Herrera alvherre@alvh.no-ip.org
---
 doc/src/sgml/func/func-info.sgml         |  44 +++++++
 src/backend/utils/adt/ruleutils.c        |  75 +++++++++++
 src/include/catalog/pg_proc.dat          |   3 +
 src/test/regress/expected/object_ddl.out | 151 +++++++++++++++++++++++
 src/test/regress/parallel_schedule       |   2 +-
 src/test/regress/sql/object_ddl.sql      | 100 +++++++++++++++
 6 files changed, 374 insertions(+), 1 deletion(-)
 create mode 100644 src/test/regress/expected/object_ddl.out
 create mode 100644 src/test/regress/sql/object_ddl.sql

diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml
index c393832d94c..4602c8eb54e 100644
--- a/doc/src/sgml/func/func-info.sgml
+++ b/doc/src/sgml/func/func-info.sgml
@@ -3797,4 +3797,48 @@ acl      | {postgres=arwdDxtm/postgres,foo=r/postgres}
 
   </sect2>
 
+  <sect2 id="functions-get-object-ddl">
+   <title>Get Object DDL Functions</title>
+
+   <para>
+    The functions shown in <xref linkend="functions-get-object-ddl-table"/>
+    print the DDL statements for various database objects.
+    (This is a decompiled reconstruction, not the original text
+    of the command.)
+   </para>
+
+   <table id="functions-get-object-ddl-table">
+    <title>Get Object DDL Functions</title>
+    <tgroup cols="1">
+     <thead>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        Function
+       </para>
+       <para>
+        Description
+       </para></entry>
+      </row>
+     </thead>
+
+     <tbody>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>pg_get_domain_ddl</primary>
+        </indexterm>
+        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>text</type> )
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Reconstructs the creating command for a domain.
+        The result is a complete <command>CREATE DOMAIN</command> statement.
+       </para></entry>
+      </row>
+     </tbody>
+    </tgroup>
+   </table>
+
+  </sect2>
+
   </sect1>
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 050eef97a4c..af79634b44c 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -13738,3 +13738,78 @@ get_range_partbound_string(List *bound_datums)
 
 	return buf->data;
 }
+
+
+/*
+ * pg_get_domain_ddl - Get CREATE DOMAIN statement for a domain
+ */
+Datum
+pg_get_domain_ddl(PG_FUNCTION_ARGS)
+{
+	StringInfoData buf;
+	Oid			domain_oid = PG_GETARG_OID(0);
+	HeapTuple	typeTuple;
+	Form_pg_type typForm;
+	Relation	constraintRel;
+	SysScanDesc sscan;
+	ScanKeyData skey;
+	HeapTuple	constraintTup;
+	Node	   *defaultExpr;
+
+	/* Look up the domain in pg_type */
+	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+
+	/* function param is a regtype, so typeoid must be valid */
+	Assert(HeapTupleIsValid(typeTuple));
+
+	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
+	initStringInfo(&buf);
+	appendStringInfo(&buf, "CREATE DOMAIN %s.%s AS %s",
+					 quote_identifier(get_namespace_name(typForm->typnamespace)),
+					 quote_identifier(NameStr(typForm->typname)),
+					 format_type_be(typForm->typbasetype));
+
+	/* Get the default value expression, if any */
+	defaultExpr = get_typdefault(domain_oid);
+
+	if (defaultExpr != NULL)
+	{
+		char	   *defaultValue;
+
+		defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false,
+												 0, 0);
+		appendStringInfo(&buf, " DEFAULT %s", defaultValue);
+	}
+
+	/* table scan to look up constraints belonging to this domain */
+	constraintRel = table_open(ConstraintRelationId, AccessShareLock);
+
+	ScanKeyInit(&skey,
+				Anum_pg_constraint_contypid,
+				BTEqualStrategyNumber, F_OIDEQ,
+				ObjectIdGetDatum(domain_oid));
+
+	sscan = systable_beginscan(constraintRel,
+							   ConstraintTypidIndexId,
+							   true,
+							   NULL,
+							   1,
+							   &skey);
+
+	while (HeapTupleIsValid(constraintTup = systable_getnext(sscan)))
+	{
+		Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+		char	   *val = NULL;
+
+		val = pg_get_constraintdef_worker(con->oid, false, PRETTYFLAG_PAREN, true);
+		appendStringInfo(&buf, " CONSTRAINT %s %s",
+						 quote_identifier(NameStr(con->conname)), val);
+	}
+	systable_endscan(sscan);
+	table_close(constraintRel, AccessShareLock);
+	ReleaseSysCache(typeTuple);
+
+	appendStringInfo(&buf, ";");
+
+	PG_RETURN_TEXT_P(cstring_to_text(buf.data));
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index b51d2b17379..897bc1f6270 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -8515,6 +8515,9 @@
 { oid => '2508', descr => 'constraint description with pretty-print option',
   proname => 'pg_get_constraintdef', provolatile => 's', prorettype => 'text',
   proargtypes => 'oid bool', prosrc => 'pg_get_constraintdef_ext' },
+{ oid => '8024', descr => 'get CREATE statement for DOMAIN',
+  proname => 'pg_get_domain_ddl', prorettype => 'text',
+  proargtypes => 'regtype', prosrc => 'pg_get_domain_ddl' },
 { oid => '2509',
   descr => 'deparse an encoded expression with pretty-print option',
   proname => 'pg_get_expr', provolatile => 's', prorettype => 'text',
diff --git a/src/test/regress/expected/object_ddl.out b/src/test/regress/expected/object_ddl.out
new file mode 100644
index 00000000000..a35b0ec19ca
--- /dev/null
+++ b/src/test/regress/expected/object_ddl.out
@@ -0,0 +1,151 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+                                                                                  pg_get_domain_ddl                                                                                  
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+                                               pg_get_domain_ddl                                               
+---------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS integer CONSTRAINT regress_domain_not_null_not_null NOT NULL;
+(1 row)
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+SELECT pg_get_domain_ddl('regress_domain_check');
+                                                           pg_get_domain_ddl                                                            
+----------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS integer CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT regress_b CHECK (VALUE > 10);
+(1 row)
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+                                                                                                pg_get_domain_ddl                                                                                                
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS integer CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10) CONSTRAINT "regress_ConstraintC" CHECK (VALUE <> 55);
+(1 row)
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+ERROR:  type "regress_nonexistent_domain" does not exist
+LINE 1: SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regty...
+                                 ^
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+                  pg_get_domain_ddl                  
+-----------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS text;
+(1 row)
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+                                                              pg_get_domain_ddl                                                              
+---------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS regress_base_domain CONSTRAINT regress_derived_domain_check CHECK (length(VALUE::text) > 3);
+(1 row)
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                         pg_get_domain_ddl                                         
+---------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS integer DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS integer DEFAULT nextval('regress_test_seq_renamed'::regclass);
+(1 row)
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+                           pg_get_domain_ddl                           
+-----------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS numeric DEFAULT 0.00;
+(1 row)
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+                                                             pg_get_domain_ddl                                                             
+-------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS integer[] CONSTRAINT regress_int_array_domain_check CHECK (array_length(VALUE, 1) <= 5);
+(1 row)
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+                                   pg_get_domain_ddl                                   
+---------------------------------------------------------------------------------------
+ CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test'::text;
+(1 row)
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+                                                                                                                                                                pg_get_domain_ddl                                                                                                                                                                
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS character varying DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK (length(VALUE::text) >= 5) CONSTRAINT regress_comprehensive_domain_check1 CHECK (VALUE::text !~ '^\s*$'::text);
+(1 row)
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+                                                                   pg_get_domain_ddl                                                                    
+--------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS regress_address_type CONSTRAINT regress_address_domain_check CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
+(1 row)
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index f9450cdc477..70ac529259b 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -28,7 +28,7 @@ test: strings md5 numerology point lseg line box path polygon circle date time t
 # geometry depends on point, lseg, line, box, path, polygon, circle
 # horology depends on date, time, timetz, timestamp, timestamptz, interval
 # ----------
-test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import
+test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import object_ddl
 
 # ----------
 # Load huge amounts of data
diff --git a/src/test/regress/sql/object_ddl.sql b/src/test/regress/sql/object_ddl.sql
new file mode 100644
index 00000000000..7182202ad5f
--- /dev/null
+++ b/src/test/regress/sql/object_ddl.sql
@@ -0,0 +1,100 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+
+SELECT pg_get_domain_ddl('regress_domain_check');
+
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
-- 
2.50.1 (Apple Git-155)

#2jian he
jian.universality@gmail.com
In reply to: Tim Waizenegger (#1)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Thu, Oct 16, 2025 at 5:17 PM Tim Waizenegger
<tim.waizenegger@enterprisedb.com> wrote:

Hi all,

Following the recent "Retail DDL" discussion [1], we're submitting another
implementation: pg_get_domain_ddl().

This function reconstructs CREATE DOMAIN statements for existing domains,
following what seems to be the agreed pg_get_{objecttype}_ddl naming convention.

## Function

pg_get_domain_ddl(regtype) returns text

Returns a complete CREATE DOMAIN statement including base type, default values,
and all constraints. Uses get_typdefault() for proper expression handling and
supports schema-qualified domains.

        <indexterm>
+         <primary>pg_get_domain_ddl</primary>
+        </indexterm>
+        <function>pg_get_domain_ddl</function> (
<parameter>domain</parameter> <type>text</type> )
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Reconstructs the creating command for a domain.
+        The result is a complete <command>CREATE DOMAIN</command> statement.
+       </para></entry>

<type>text</type>

should be
<type>regtype</type>

+ Oid domain_oid = PG_GETARG_OID(0);
+ HeapTuple typeTuple;
,....
+
+ /* Look up the domain in pg_type */
+ typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+

select pg_get_domain_ddl(-1);
will cause segfault.
see /messages/by-id/3759807.1711658868@sss.pgh.pa.us
and pg_get_trigger_ddl thread.

NOT VALID check constraint handling is tricky currently.
create domain x as int;
alter domain x add constraint cc check(value > 2) not valid;

select pg_get_domain_ddl('x'::regtype);
CREATE DOMAIN public.x AS integer CONSTRAINT cc CHECK (VALUE > 2) NOT VALID;
but putting the above to psql would result in syntax error.

https://www.postgresql.org/docs/current/sql-createdomain.html
[ COLLATE collation ]
part not handled?

create domain d0 as text collate "C";
select pg_get_domain_ddl('d0'::regtype);
pg_get_domain_ddl
----------------------------------
CREATE DOMAIN public.d0 AS text;
(1 row)

we should expect
CREATE DOMAIN public.d0 AS text COLLATE "C";

#3Tim Waizenegger
tim.waizenegger@enterprisedb.com
In reply to: jian he (#2)
1 attachment(s)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Thu, Oct 16, 2025 at 1:05 PM jian he <jian.universality@gmail.com> wrote:

On Thu, Oct 16, 2025 at 5:17 PM Tim Waizenegger
<tim.waizenegger@enterprisedb.com> wrote:

Hi all,

Following the recent "Retail DDL" discussion [1], we're submitting another
implementation: pg_get_domain_ddl().

select pg_get_domain_ddl(-1);
will cause segfault.
see /messages/by-id/3759807.1711658868@sss.pgh.pa.us
and pg_get_trigger_ddl thread.

NOT VALID check constraint handling is tricky currently.
create domain x as int;
alter domain x add constraint cc check(value > 2) not valid;

select pg_get_domain_ddl('x'::regtype);
CREATE DOMAIN public.x AS integer CONSTRAINT cc CHECK (VALUE > 2) NOT VALID;
but putting the above to psql would result in syntax error.

https://www.postgresql.org/docs/current/sql-createdomain.html
[ COLLATE collation ]
part not handled?

create domain d0 as text collate "C";
select pg_get_domain_ddl('d0'::regtype);
pg_get_domain_ddl
----------------------------------
CREATE DOMAIN public.d0 AS text;
(1 row)

we should expect
CREATE DOMAIN public.d0 AS text COLLATE "C";

Thanks for the feedback! We addressed the issues mentioned above and
also added more extensive test cases:

postgres=# select pg_get_domain_ddl(-1);
pg_get_domain_ddl
-------------------

(1 row)

postgres=# create domain d0 as text collate "C";
CREATE DOMAIN
postgres=# select pg_get_domain_ddl('d0'::regtype);
pg_get_domain_ddl
----------------------------------------------
CREATE DOMAIN public.d0 AS text COLLATE "C";
(1 row)

postgres=# create domain x as int;
CREATE DOMAIN
postgres=# alter domain x add constraint cc check(value > 2) not valid;
ALTER DOMAIN
postgres=# select pg_get_domain_ddl('x'::regtype);
pg_get_domain_ddl
----------------------------------------------------------------------
CREATE DOMAIN public.x AS integer; +
ALTER DOMAIN public.x ADD CONSTRAINT cc CHECK (VALUE > 2) NOT VALID;
(1 row)

updated patch is attached

---
Best regards,
Florin Irion
Tim Waizenegger

EDB (EnterpriseDB)

Attachments:

v1-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchapplication/octet-stream; name=v1-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchDownload
From 41ac9f6f14778d36098caf0a7bba523e2f0f99bb Mon Sep 17 00:00:00 2001
From: Florin Irion <florin.irion@enterprisedb.com>
Date: Thu, 18 Sep 2025 18:52:43 +0200
Subject: [PATCH v1] Add pg_get_domain_ddl() function to reconstruct CREATE
 DOMAIN statements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch introduces a new system function pg_get_domain_ddl() that
reconstructs the CREATE DOMAIN statement for a given domain. The function
takes a regtype parameter and returns the complete DDL statement including
the domain name, base type, default value, and all associated constraints.

The function follows the same pattern as other DDL reconstruction functions
like pg_get_functiondef() and pg_get_constraintdef(), providing a
decompiled reconstruction rather than the original command text.

Key features:
* Supports domains with default values
* Includes all domain constraints (CHECK, NOT NULL)
* NOT VALID constraint are handled with an extra ALTER command.
* Properly quotes identifiers and schema names
* Handles complex constraint expressions

A new documentation section "Get Object DDL Functions" has been created
to group DDL reconstruction functions, starting with pg_get_domain_ddl().
This provides a foundation for future DDL functions for other object types.

Comprehensive regression tests are included covering various domain
configurations.

Reference: PG-151
Author: Florin Irion <florin.irion@enterprisedb.com>
Author: Tim Waizenegger <tim.waizenegger@enterprisedb.com>
Reviewed-by: Álvaro Herrera alvherre@alvh.no-ip.org
---
 doc/src/sgml/func/func-info.sgml         |  44 ++++++
 src/backend/utils/adt/ruleutils.c        | 174 ++++++++++++++++++++++
 src/include/catalog/pg_proc.dat          |   3 +
 src/test/regress/expected/object_ddl.out | 182 +++++++++++++++++++++++
 src/test/regress/parallel_schedule       |   2 +-
 src/test/regress/sql/object_ddl.sql      | 117 +++++++++++++++
 6 files changed, 521 insertions(+), 1 deletion(-)
 create mode 100644 src/test/regress/expected/object_ddl.out
 create mode 100644 src/test/regress/sql/object_ddl.sql

diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml
index c393832d94c..9a937df960d 100644
--- a/doc/src/sgml/func/func-info.sgml
+++ b/doc/src/sgml/func/func-info.sgml
@@ -3797,4 +3797,48 @@ acl      | {postgres=arwdDxtm/postgres,foo=r/postgres}
 
   </sect2>
 
+  <sect2 id="functions-get-object-ddl">
+   <title>Get Object DDL Functions</title>
+
+   <para>
+    The functions shown in <xref linkend="functions-get-object-ddl-table"/>
+    print the DDL statements for various database objects.
+    (This is a decompiled reconstruction, not the original text
+    of the command.)
+   </para>
+
+   <table id="functions-get-object-ddl-table">
+    <title>Get Object DDL Functions</title>
+    <tgroup cols="1">
+     <thead>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        Function
+       </para>
+       <para>
+        Description
+       </para></entry>
+      </row>
+     </thead>
+
+     <tbody>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>pg_get_domain_ddl</primary>
+        </indexterm>
+        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>text</type> )
+        <returnvalue>regtype</returnvalue>
+       </para>
+       <para>
+        Reconstructs the creating command for a domain.
+        The result is a complete <command>CREATE DOMAIN</command> statement.
+       </para></entry>
+      </row>
+     </tbody>
+    </tgroup>
+   </table>
+
+  </sect2>
+
   </sect1>
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 050eef97a4c..7b2ce4e460f 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -13738,3 +13738,177 @@ get_range_partbound_string(List *bound_datums)
 
 	return buf->data;
 }
+
+
+/*
+ * Helper function to scan domain constraints
+ */
+static void
+scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons)
+{
+	Relation	constraintRel;
+	SysScanDesc sscan;
+	ScanKeyData skey;
+	HeapTuple	constraintTup;
+
+	*validcons = NIL;
+	*invalidcons = NIL;
+
+	constraintRel = table_open(ConstraintRelationId, AccessShareLock);
+
+	ScanKeyInit(&skey,
+				Anum_pg_constraint_contypid,
+				BTEqualStrategyNumber, F_OIDEQ,
+				ObjectIdGetDatum(domain_oid));
+
+	sscan = systable_beginscan(constraintRel,
+							   ConstraintTypidIndexId,
+							   true,
+							   NULL,
+							   1,
+							   &skey);
+
+	while (HeapTupleIsValid(constraintTup = systable_getnext(sscan)))
+	{
+		Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+
+		if (con->convalidated)
+			*validcons = lappend_oid(*validcons, con->oid);
+		else
+			*invalidcons = lappend_oid(*invalidcons, con->oid);
+	}
+
+	systable_endscan(sscan);
+	table_close(constraintRel, AccessShareLock);
+}
+
+/*
+ * Helper function to build CREATE DOMAIN statement
+ */
+static void
+build_create_domain_statement(StringInfo buf, Form_pg_type typForm,
+							 Node *defaultExpr, List *validConstraints)
+{
+	HeapTuple	baseTypeTuple;
+	Form_pg_type baseTypeForm;
+	Oid			baseCollation = InvalidOid;
+
+	appendStringInfo(buf, "CREATE DOMAIN %s.%s AS %s",
+					 quote_identifier(get_namespace_name(typForm->typnamespace)),
+					 quote_identifier(NameStr(typForm->typname)),
+					 format_type_be(typForm->typbasetype));
+
+	/* Add collation if it differs from base type's collation */
+	if (OidIsValid(typForm->typcollation))
+	{
+		/* Get base type's collation for comparison */
+		baseTypeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typForm->typbasetype));
+		if (HeapTupleIsValid(baseTypeTuple))
+		{
+			baseTypeForm = (Form_pg_type) GETSTRUCT(baseTypeTuple);
+			baseCollation = baseTypeForm->typcollation;
+			ReleaseSysCache(baseTypeTuple);
+		}
+
+		/* Only add COLLATE if domain's collation differs from base type's */
+		if (typForm->typcollation != baseCollation)
+		{
+			appendStringInfo(buf, " COLLATE %s",
+							 generate_collation_name(typForm->typcollation));
+		}
+	}
+
+	/* Add default value if present */
+	if (defaultExpr != NULL)
+	{
+		char *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, 0, 0);
+		appendStringInfo(buf, " DEFAULT %s", defaultValue);
+	}
+
+	/* Add valid constraints */
+	ListCell *lc;
+	foreach(lc, validConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		HeapTuple	constraintTup;
+		Form_pg_constraint con;
+		char	   *constraintDef;
+
+		/* Look up the constraint info */
+		constraintTup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid));
+		if (!HeapTupleIsValid(constraintTup))
+			continue;	/* constraint was dropped concurrently */
+
+		con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+		constraintDef = pg_get_constraintdef_worker(constraintOid, false, PRETTYFLAG_PAREN, true);
+
+		appendStringInfo(buf, " CONSTRAINT %s %s",
+						 quote_identifier(NameStr(con->conname)),
+						 constraintDef);
+
+		ReleaseSysCache(constraintTup);
+	}
+
+	appendStringInfoChar(buf, ';');
+}
+
+/*
+ * Helper function to add ALTER DOMAIN statements for invalid constraints
+ */
+static void
+add_alter_domain_statements(StringInfo buf, List *invalidConstraints)
+{
+	ListCell *lc;
+
+	foreach(lc, invalidConstraints)
+	{
+		Oid constraintOid = lfirst_oid(lc);
+		char *alterStmt = pg_get_constraintdef_worker(constraintOid, true, PRETTYFLAG_PAREN, true);
+
+		if (alterStmt)
+			appendStringInfo(buf, "\n%s;", alterStmt);
+	}
+}
+
+/*
+ * pg_get_domain_ddl - Get CREATE DOMAIN statement for a domain
+ */
+Datum
+pg_get_domain_ddl(PG_FUNCTION_ARGS)
+{
+	StringInfoData buf;
+	Oid			domain_oid = PG_GETARG_OID(0);
+	HeapTuple	typeTuple;
+	Form_pg_type typForm;
+	Node	   *defaultExpr;
+	List	   *validConstraints;
+	List	   *invalidConstraints;
+
+	/* Look up the domain in pg_type */
+	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+	if (!HeapTupleIsValid(typeTuple))
+		PG_RETURN_NULL();
+
+	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
+
+	/* Get default expression */
+	defaultExpr = get_typdefault(domain_oid);
+
+	/* Scan for valid and invalid constraints */
+	scan_domain_constraints(domain_oid, &validConstraints, &invalidConstraints);
+
+	/* Build the DDL statement */
+	initStringInfo(&buf);
+	build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints);
+
+	/* Add ALTER DOMAIN statements for invalid constraints */
+	if (list_length(invalidConstraints) > 0)
+		add_alter_domain_statements(&buf, invalidConstraints);
+
+	/* Cleanup */
+	list_free(validConstraints);
+	list_free(invalidConstraints);
+	ReleaseSysCache(typeTuple);
+
+	PG_RETURN_TEXT_P(cstring_to_text(buf.data));
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index b51d2b17379..897bc1f6270 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -8515,6 +8515,9 @@
 { oid => '2508', descr => 'constraint description with pretty-print option',
   proname => 'pg_get_constraintdef', provolatile => 's', prorettype => 'text',
   proargtypes => 'oid bool', prosrc => 'pg_get_constraintdef_ext' },
+{ oid => '8024', descr => 'get CREATE statement for DOMAIN',
+  proname => 'pg_get_domain_ddl', prorettype => 'text',
+  proargtypes => 'regtype', prosrc => 'pg_get_domain_ddl' },
 { oid => '2509',
   descr => 'deparse an encoded expression with pretty-print option',
   proname => 'pg_get_expr', provolatile => 's', prorettype => 'text',
diff --git a/src/test/regress/expected/object_ddl.out b/src/test/regress/expected/object_ddl.out
new file mode 100644
index 00000000000..3dc1f1bc049
--- /dev/null
+++ b/src/test/regress/expected/object_ddl.out
@@ -0,0 +1,182 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+                                                                                  pg_get_domain_ddl                                                                                  
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+                                               pg_get_domain_ddl                                               
+---------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS integer CONSTRAINT regress_domain_not_null_not_null NOT NULL;
+(1 row)
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+SELECT pg_get_domain_ddl('regress_domain_check');
+                                                           pg_get_domain_ddl                                                            
+----------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS integer CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT regress_b CHECK (VALUE > 10);
+(1 row)
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+                                                                                                pg_get_domain_ddl                                                                                                
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS integer CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10) CONSTRAINT "regress_ConstraintC" CHECK (VALUE <> 55);
+(1 row)
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+ERROR:  type "regress_nonexistent_domain" does not exist
+LINE 1: SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regty...
+                                 ^
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+                  pg_get_domain_ddl                  
+-----------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS text;
+(1 row)
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+                                                              pg_get_domain_ddl                                                              
+---------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS regress_base_domain CONSTRAINT regress_derived_domain_check CHECK (length(VALUE::text) > 3);
+(1 row)
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                         pg_get_domain_ddl                                         
+---------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS integer DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS integer DEFAULT nextval('regress_test_seq_renamed'::regclass);
+(1 row)
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+                           pg_get_domain_ddl                           
+-----------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS numeric DEFAULT 0.00;
+(1 row)
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+                                                             pg_get_domain_ddl                                                             
+-------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS integer[] CONSTRAINT regress_int_array_domain_check CHECK (array_length(VALUE, 1) <= 5);
+(1 row)
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+                                   pg_get_domain_ddl                                   
+---------------------------------------------------------------------------------------
+ CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test'::text;
+(1 row)
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+                                                                                                                                                                pg_get_domain_ddl                                                                                                                                                                
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS character varying DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK (length(VALUE::text) >= 5) CONSTRAINT regress_comprehensive_domain_check1 CHECK (VALUE::text !~ '^\s*$'::text);
+(1 row)
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+                                                                   pg_get_domain_ddl                                                                    
+--------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS regress_address_type CONSTRAINT regress_address_domain_check CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
+(1 row)
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+                                            pg_get_domain_ddl                                            
+---------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS integer;                                              +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+(1 row)
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+                                                 pg_get_domain_ddl                                                  
+--------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS integer CONSTRAINT regress_domain_mixed_check CHECK (VALUE <> 0);    +
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE >= 1 AND VALUE <= 100) NOT VALID;
+(1 row)
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+                           pg_get_domain_ddl                           
+-----------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS text COLLATE "C";
+(1 row)
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index f9450cdc477..70ac529259b 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -28,7 +28,7 @@ test: strings md5 numerology point lseg line box path polygon circle date time t
 # geometry depends on point, lseg, line, box, path, polygon, circle
 # horology depends on date, time, timetz, timestamp, timestamptz, interval
 # ----------
-test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import
+test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import object_ddl
 
 # ----------
 # Load huge amounts of data
diff --git a/src/test/regress/sql/object_ddl.sql b/src/test/regress/sql/object_ddl.sql
new file mode 100644
index 00000000000..aa2ab62c193
--- /dev/null
+++ b/src/test/regress/sql/object_ddl.sql
@@ -0,0 +1,117 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+
+SELECT pg_get_domain_ddl('regress_domain_check');
+
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
-- 
2.50.1 (Apple Git-155)

#4Chao Li
li.evan.chao@gmail.com
In reply to: Tim Waizenegger (#3)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

Hi Tim,

Thanks for working on this. I haven’t finished reviewing the entire patch. But I got a quick question:

On Oct 22, 2025, at 17:32, Tim Waizenegger <tim.waizenegger@enterprisedb.com> wrote:

updated patch is attached

---
Best regards,
Florin Irion
Tim Waizenegger

EDB (EnterpriseDB)
<v1-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patch>

```
+/*
+ * pg_get_domain_ddl - Get CREATE DOMAIN statement for a domain
+ */
+Datum
+pg_get_domain_ddl(PG_FUNCTION_ARGS)
+{
+	StringInfoData buf;
+	Oid			domain_oid = PG_GETARG_OID(0);
+	HeapTuple	typeTuple;
+	Form_pg_type typForm;
+	Node	   *defaultExpr;
```

While reviewing a similar patch of pg_get_policy_ddl(), it take the last parameter as a pretty flag. I wonder why pg_get_domain_ddl() doesn’t support an argument for pretty?

See the code snippet from the other patch:

```
+/*
+ * pg_get_policy_ddl
+ *
+ * Generate a CREATE POLICY statement for the specified policy.
+ *
+ * tableID - Table ID of the policy.
+ * policyName - Name of the policy for which to generate the DDL.
+ * pretty - If true, format the DDL with indentation and line breaks.
+ */
+Datum
+pg_get_policy_ddl(PG_FUNCTION_ARGS)
+{
+	Oid			tableID = PG_GETARG_OID(0);
+	Name		policyName = PG_GETARG_NAME(1);
+	bool		pretty = PG_GETARG_BOOL(2);  # <====== This is the pretty arg
+	bool		attrIsNull;
```

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#5Tim Waizenegger
tim.waizenegger@enterprisedb.com
In reply to: Chao Li (#4)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Wed, Oct 22, 2025 at 12:27 PM Chao Li <li.evan.chao@gmail.com> wrote:

Hi Tim,

Thanks for working on this. I haven’t finished reviewing the entire patch. But I got a quick question:

While reviewing a similar patch of pg_get_policy_ddl(), it take the last parameter as a pretty flag. I wonder why pg_get_domain_ddl() doesn’t support an argument for pretty?

That's a good point; we'll add pretty printing support for consistency
with the other functions. I'll send a new patch in the coming days.

Best regards,
Florin Irion
Tim Waizenegger
EDB (EnterpriseDB)

#6jian he
jian.universality@gmail.com
In reply to: Tim Waizenegger (#3)
1 attachment(s)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Wed, Oct 22, 2025 at 5:32 PM Tim Waizenegger
<tim.waizenegger@enterprisedb.com> wrote:

updated patch is attached

I’ve done some refactoring, hope it’s now more intuitive to you.
Since a domain’s base type can itself be another domain, it’s better to use

appendStringInfo(&buf, "CREATE DOMAIN %s AS %s",
generate_qualified_type_name(domain_oid),
generate_qualified_type_name(typForm->typbasetype));

then the domain's base type is also fully qualified.

I also refactored the logic for printing domain constraints, which should reduce
syscache lookups or table scans compared to your version.

please check the attached.

Attachments:

v2-0001-refactor-pg_get_domain_ddl.no-cfbotapplication/octet-stream; name=v2-0001-refactor-pg_get_domain_ddl.no-cfbotDownload
From 8a0cf326ca9e79af7a329cc584503f861ecb586e Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Thu, 23 Oct 2025 12:10:40 +0800
Subject: [PATCH v2 1/1] refactor pg_get_domain_ddl

discussion: https://postgr.es/m/CAPgqM1V4LW2qiDLPsusb7s0kYbSDJjH5Tt+-ZzVmPU7xV0TJNQ@mail.gmail.com
---
 doc/src/sgml/func/func-info.sgml         |   4 +-
 src/backend/utils/adt/ruleutils.c        | 217 +++++++++--------------
 src/test/regress/expected/object_ddl.out |  98 +++++-----
 3 files changed, 138 insertions(+), 181 deletions(-)

diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml
index 9a937df960d..9c76a1e8986 100644
--- a/doc/src/sgml/func/func-info.sgml
+++ b/doc/src/sgml/func/func-info.sgml
@@ -3827,8 +3827,8 @@ acl      | {postgres=arwdDxtm/postgres,foo=r/postgres}
         <indexterm>
          <primary>pg_get_domain_ddl</primary>
         </indexterm>
-        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>text</type> )
-        <returnvalue>regtype</returnvalue>
+        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>regtype</type> )
+        <returnvalue>text</returnvalue>
        </para>
        <para>
         Reconstructs the creating command for a domain.
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index b2d6935cbbc..b033680e7f8 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -13741,21 +13741,72 @@ get_range_partbound_string(List *bound_datums)
 
 
 /*
- * Helper function to scan domain constraints
+ * pg_get_domain_ddl - Get CREATE DOMAIN statement for a domain
  */
-static void
-scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons)
+Datum
+pg_get_domain_ddl(PG_FUNCTION_ARGS)
 {
+	StringInfoData buf;
+	StringInfoData constr_buf;
+	Oid			domain_oid = PG_GETARG_OID(0);
+	HeapTuple	typeTuple;
+	HeapTuple	baseTypeTuple;
+	Form_pg_type typForm;
+	Form_pg_type baseTypeForm;
+	Oid			baseCollation = InvalidOid;
+	Node	   *defaultExpr;
 	Relation	constraintRel;
 	SysScanDesc sscan;
 	ScanKeyData skey;
 	HeapTuple	constraintTup;
 
-	*validcons = NIL;
-	*invalidcons = NIL;
+	/* Look up the domain in pg_type */
+	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+	if (!HeapTupleIsValid(typeTuple))
+		PG_RETURN_NULL();
+
+	/* Build the DDL statement */
+	initStringInfo(&buf);
+	initStringInfo(&constr_buf);
+
+	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
+
+	appendStringInfo(&buf, "CREATE DOMAIN %s AS %s",
+					 generate_qualified_type_name(domain_oid),
+					 generate_qualified_type_name(typForm->typbasetype));
+
+	/* Add collation if it differs from base type's collation */
+	if (OidIsValid(typForm->typcollation))
+	{
+		/* Get base type's collation for comparison */
+		baseTypeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typForm->typbasetype));
+
+		Assert(HeapTupleIsValid(baseTypeTuple));
+
+		baseTypeForm = (Form_pg_type) GETSTRUCT(baseTypeTuple);
+		baseCollation = baseTypeForm->typcollation;
+
+		ReleaseSysCache(baseTypeTuple);
+
+		/* Only add COLLATE if domain's collation differs from base type's */
+		if (typForm->typcollation != baseCollation)
+			appendStringInfo(&buf, " COLLATE %s",
+							 generate_collation_name(typForm->typcollation));
+	}
+
+	/* Get default expression */
+	defaultExpr = get_typdefault(domain_oid);
+
+	/* Add default value if present */
+	if (defaultExpr != NULL)
+	{
+		char *defaultValue = deparse_expression_pretty(defaultExpr, NIL,
+													   false, false, 0, 0);
+
+		appendStringInfo(&buf, " DEFAULT %s", defaultValue);
+	}
 
 	constraintRel = table_open(ConstraintRelationId, AccessShareLock);
-
 	ScanKeyInit(&skey,
 				Anum_pg_constraint_contypid,
 				BTEqualStrategyNumber, F_OIDEQ,
@@ -13770,144 +13821,50 @@ scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons)
 
 	while (HeapTupleIsValid(constraintTup = systable_getnext(sscan)))
 	{
+		char	   *constraintDef;
+
 		Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(constraintTup);
 
 		if (con->convalidated)
-			*validcons = lappend_oid(*validcons, con->oid);
+		{
+			constraintDef = pg_get_constraintdef_worker(con->oid, false,
+														PRETTYFLAG_PAREN,
+														true);
+			if (constraintDef)
+				appendStringInfo(&buf, " CONSTRAINT %s %s",
+								 quote_identifier(NameStr(con->conname)),
+								 constraintDef);
+		}
 		else
-			*invalidcons = lappend_oid(*invalidcons, con->oid);
-	}
+		{
+			constraintDef = pg_get_constraintdef_worker(con->oid, true,
+														PRETTYFLAG_PAREN,
+														true);
+			if (constraintDef)
+			{
+				appendStringInfoString(&constr_buf, constraintDef);
 
+				appendStringInfoChar(&constr_buf, ';');
+				appendStringInfoChar(&constr_buf, '\n');
+			}
+		}
+	}
 	systable_endscan(sscan);
 	table_close(constraintRel, AccessShareLock);
-}
 
-/*
- * Helper function to build CREATE DOMAIN statement
- */
-static void
-build_create_domain_statement(StringInfo buf, Form_pg_type typForm,
-							 Node *defaultExpr, List *validConstraints)
-{
-	HeapTuple	baseTypeTuple;
-	Form_pg_type baseTypeForm;
-	Oid			baseCollation = InvalidOid;
+	appendStringInfoChar(&buf, ';');
 
-	appendStringInfo(buf, "CREATE DOMAIN %s.%s AS %s",
-					 quote_identifier(get_namespace_name(typForm->typnamespace)),
-					 quote_identifier(NameStr(typForm->typname)),
-					 format_type_be(typForm->typbasetype));
-
-	/* Add collation if it differs from base type's collation */
-	if (OidIsValid(typForm->typcollation))
-	{
-		/* Get base type's collation for comparison */
-		baseTypeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typForm->typbasetype));
-		if (HeapTupleIsValid(baseTypeTuple))
-		{
-			baseTypeForm = (Form_pg_type) GETSTRUCT(baseTypeTuple);
-			baseCollation = baseTypeForm->typcollation;
-			ReleaseSysCache(baseTypeTuple);
-		}
-
-		/* Only add COLLATE if domain's collation differs from base type's */
-		if (typForm->typcollation != baseCollation)
-		{
-			appendStringInfo(buf, " COLLATE %s",
-							 generate_collation_name(typForm->typcollation));
-		}
-	}
-
-	/* Add default value if present */
-	if (defaultExpr != NULL)
-	{
-		char *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, 0, 0);
-		appendStringInfo(buf, " DEFAULT %s", defaultValue);
-	}
-
-	/* Add valid constraints */
-	ListCell *lc;
-	foreach(lc, validConstraints)
-	{
-		Oid			constraintOid = lfirst_oid(lc);
-		HeapTuple	constraintTup;
-		Form_pg_constraint con;
-		char	   *constraintDef;
-
-		/* Look up the constraint info */
-		constraintTup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid));
-		if (!HeapTupleIsValid(constraintTup))
-			continue;	/* constraint was dropped concurrently */
-
-		con = (Form_pg_constraint) GETSTRUCT(constraintTup);
-		constraintDef = pg_get_constraintdef_worker(constraintOid, false, PRETTYFLAG_PAREN, true);
-
-		appendStringInfo(buf, " CONSTRAINT %s %s",
-						 quote_identifier(NameStr(con->conname)),
-						 constraintDef);
-
-		ReleaseSysCache(constraintTup);
-	}
-
-	appendStringInfoChar(buf, ';');
-}
-
-/*
- * Helper function to add ALTER DOMAIN statements for invalid constraints
- */
-static void
-add_alter_domain_statements(StringInfo buf, List *invalidConstraints)
-{
-	ListCell *lc;
-
-	foreach(lc, invalidConstraints)
+	if (constr_buf.len != 0)
 	{
-		Oid constraintOid = lfirst_oid(lc);
-		char *alterStmt = pg_get_constraintdef_worker(constraintOid, true, PRETTYFLAG_PAREN, true);
+		appendStringInfoChar(&buf, '\n');
+		appendStringInfoString(&buf, constr_buf.data);
 
-		if (alterStmt)
-			appendStringInfo(buf, "\n%s;", alterStmt);
+		/* truncate the last newline */
+		if (buf.len > 0 && buf.data[buf.len - 1] == '\n')
+			buf.data[--(buf.len)] = '\0';
 	}
-}
-
-/*
- * pg_get_domain_ddl - Get CREATE DOMAIN statement for a domain
- */
-Datum
-pg_get_domain_ddl(PG_FUNCTION_ARGS)
-{
-	StringInfoData buf;
-	Oid			domain_oid = PG_GETARG_OID(0);
-	HeapTuple	typeTuple;
-	Form_pg_type typForm;
-	Node	   *defaultExpr;
-	List	   *validConstraints;
-	List	   *invalidConstraints;
-
-	/* Look up the domain in pg_type */
-	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
-	if (!HeapTupleIsValid(typeTuple))
-		PG_RETURN_NULL();
-
-	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
-
-	/* Get default expression */
-	defaultExpr = get_typdefault(domain_oid);
-
-	/* Scan for valid and invalid constraints */
-	scan_domain_constraints(domain_oid, &validConstraints, &invalidConstraints);
-
-	/* Build the DDL statement */
-	initStringInfo(&buf);
-	build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints);
-
-	/* Add ALTER DOMAIN statements for invalid constraints */
-	if (list_length(invalidConstraints) > 0)
-		add_alter_domain_statements(&buf, invalidConstraints);
 
 	/* Cleanup */
-	list_free(validConstraints);
-	list_free(invalidConstraints);
 	ReleaseSysCache(typeTuple);
 
 	PG_RETURN_TEXT_P(cstring_to_text(buf.data));
diff --git a/src/test/regress/expected/object_ddl.out b/src/test/regress/expected/object_ddl.out
index 3dc1f1bc049..8eb14cc5058 100644
--- a/src/test/regress/expected/object_ddl.out
+++ b/src/test/regress/expected/object_ddl.out
@@ -10,25 +10,25 @@ CREATE DOMAIN regress_us_postal_code AS TEXT
     OR VALUE ~ '^\d{5}-\d{4}$'
     );
 SELECT pg_get_domain_ddl('regress_us_postal_code');
-                                                                                  pg_get_domain_ddl                                                                                  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_us_postal_code AS text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+                                                                                       pg_get_domain_ddl                                                                                        
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
 (1 row)
 
 CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
 SELECT pg_get_domain_ddl('regress_domain_not_null');
-                                               pg_get_domain_ddl                                               
----------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_domain_not_null AS integer CONSTRAINT regress_domain_not_null_not_null NOT NULL;
+                                                   pg_get_domain_ddl                                                   
+-----------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4 CONSTRAINT regress_domain_not_null_not_null NOT NULL;
 (1 row)
 
 CREATE DOMAIN regress_domain_check AS INT
     CONSTRAINT regress_a CHECK (VALUE < 100)
     CONSTRAINT regress_b CHECK (VALUE > 10);
 SELECT pg_get_domain_ddl('regress_domain_check');
-                                                           pg_get_domain_ddl                                                            
-----------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_domain_check AS integer CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT regress_b CHECK (VALUE > 10);
+                                                               pg_get_domain_ddl                                                                
+------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4 CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT regress_b CHECK (VALUE > 10);
 (1 row)
 
 CREATE DOMAIN "regress_domain with space" AS INT
@@ -36,9 +36,9 @@ CREATE DOMAIN "regress_domain with space" AS INT
     CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
     CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
 SELECT pg_get_domain_ddl('"regress_domain with space"');
-                                                                                                pg_get_domain_ddl                                                                                                
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public."regress_domain with space" AS integer CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10) CONSTRAINT "regress_ConstraintC" CHECK (VALUE <> 55);
+                                                                                                    pg_get_domain_ddl                                                                                                    
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4 CONSTRAINT regress_a CHECK (VALUE < 100) CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10) CONSTRAINT "regress_ConstraintC" CHECK (VALUE <> 55);
 (1 row)
 
 -- Test error cases
@@ -55,60 +55,60 @@ SELECT pg_get_domain_ddl(NULL);  -- should return NULL
 -- Test domains with no constraints
 CREATE DOMAIN regress_simple_domain AS text;
 SELECT pg_get_domain_ddl('regress_simple_domain');
-                  pg_get_domain_ddl                  
------------------------------------------------------
- CREATE DOMAIN public.regress_simple_domain AS text;
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
 (1 row)
 
 -- Test domain over another domain
 CREATE DOMAIN regress_base_domain AS varchar(10);
 CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
 SELECT pg_get_domain_ddl('regress_derived_domain');
-                                                              pg_get_domain_ddl                                                              
----------------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_derived_domain AS regress_base_domain CONSTRAINT regress_derived_domain_check CHECK (length(VALUE::text) > 3);
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain CONSTRAINT regress_derived_domain_check CHECK (length(VALUE::text) > 3);
 (1 row)
 
 -- Test domain with complex default expressions
 CREATE SEQUENCE regress_test_seq;
 CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
-SELECT pg_get_domain_ddl('regress_seq_domain');
-                                         pg_get_domain_ddl                                         
----------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_seq_domain AS integer DEFAULT nextval('regress_test_seq'::regclass);
-(1 row)
-
--- Test domain with a renamed sequence as default expression
-ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
 SELECT pg_get_domain_ddl('regress_seq_domain');
                                              pg_get_domain_ddl                                             
 -----------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_seq_domain AS integer DEFAULT nextval('regress_test_seq_renamed'::regclass);
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                                 pg_get_domain_ddl                                                 
+-------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq_renamed'::regclass);
 (1 row)
 
 -- Test domain with type modifiers
 CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
 SELECT pg_get_domain_ddl('regress_precise_numeric');
-                           pg_get_domain_ddl                           
------------------------------------------------------------------------
- CREATE DOMAIN public.regress_precise_numeric AS numeric DEFAULT 0.00;
+                                 pg_get_domain_ddl                                  
+------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric" DEFAULT 0.00;
 (1 row)
 
 -- Test domain over array type
 CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
 SELECT pg_get_domain_ddl('regress_int_array_domain');
-                                                             pg_get_domain_ddl                                                             
--------------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_int_array_domain AS integer[] CONSTRAINT regress_int_array_domain_check CHECK (array_length(VALUE, 1) <= 5);
+                                                                pg_get_domain_ddl                                                                 
+--------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4 CONSTRAINT regress_int_array_domain_check CHECK (array_length(VALUE, 1) <= 5);
 (1 row)
 
 -- Test domain in non-public schema
 CREATE SCHEMA regress_test_schema;
 CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
 SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
-                                   pg_get_domain_ddl                                   
----------------------------------------------------------------------------------------
- CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test'::text;
+                                        pg_get_domain_ddl                                         
+--------------------------------------------------------------------------------------------------
+ CREATE DOMAIN regress_test_schema.regress_schema_domain AS pg_catalog.text DEFAULT 'test'::text;
 (1 row)
 
 -- Test domain with multiple constraint types combined
@@ -118,18 +118,18 @@ CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
     CHECK (LENGTH(VALUE) >= 5)
     CHECK (VALUE !~ '^\s*$');  -- not just whitespace
 SELECT pg_get_domain_ddl('regress_comprehensive_domain');
-                                                                                                                                                                pg_get_domain_ddl                                                                                                                                                                
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_comprehensive_domain AS character varying DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK (length(VALUE::text) >= 5) CONSTRAINT regress_comprehensive_domain_check1 CHECK (VALUE::text !~ '^\s*$'::text);
+                                                                                                                                                                 pg_get_domain_ddl                                                                                                                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar" DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK (length(VALUE::text) >= 5) CONSTRAINT regress_comprehensive_domain_check1 CHECK (VALUE::text !~ '^\s*$'::text);
 (1 row)
 
 -- Test domain over composite type
 CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
 CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
 SELECT pg_get_domain_ddl('regress_address_domain');
-                                                                   pg_get_domain_ddl                                                                    
---------------------------------------------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_address_domain AS regress_address_type CONSTRAINT regress_address_domain_check CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
+                                                                       pg_get_domain_ddl                                                                       
+---------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type CONSTRAINT regress_address_domain_check CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
 (1 row)
 
 -- Test domain with NOT VALID constraint
@@ -138,7 +138,7 @@ ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE
 SELECT pg_get_domain_ddl('regress_domain_not_valid');
                                             pg_get_domain_ddl                                            
 ---------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_domain_not_valid AS integer;                                              +
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                      +
  ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
 (1 row)
 
@@ -146,18 +146,18 @@ SELECT pg_get_domain_ddl('regress_domain_not_valid');
 CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
 ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
 SELECT pg_get_domain_ddl('regress_domain_mixed');
-                                                 pg_get_domain_ddl                                                  
---------------------------------------------------------------------------------------------------------------------
- CREATE DOMAIN public.regress_domain_mixed AS integer CONSTRAINT regress_domain_mixed_check CHECK (VALUE <> 0);    +
+                                                   pg_get_domain_ddl                                                    
+------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4 CONSTRAINT regress_domain_mixed_check CHECK (VALUE <> 0);+
  ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE >= 1 AND VALUE <= 100) NOT VALID;
 (1 row)
 
 -- Test domain with collation
 CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
 SELECT pg_get_domain_ddl('regress_domain_with_collate');
-                           pg_get_domain_ddl                           
------------------------------------------------------------------------
- CREATE DOMAIN public.regress_domain_with_collate AS text COLLATE "C";
+                                pg_get_domain_ddl                                 
+----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text COLLATE "C";
 (1 row)
 
 -- Cleanup
-- 
2.34.1

#7Akshay Joshi
akshay.joshi@enterprisedb.com
In reply to: Tim Waizenegger (#5)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Wed, 22 Oct, 2025, 17:30 Tim Waizenegger, <
tim.waizenegger@enterprisedb.com> wrote:

On Wed, Oct 22, 2025 at 12:27 PM Chao Li <li.evan.chao@gmail.com> wrote:

Hi Tim,

Thanks for working on this. I haven’t finished reviewing the entire

patch. But I got a quick question:

While reviewing a similar patch of pg_get_policy_ddl(), it take the last

parameter as a pretty flag. I wonder why pg_get_domain_ddl() doesn’t
support an argument for pretty?

That's a good point; we'll add pretty printing support for consistency
with the other functions. I'll send a new patch in the coming days.

I've already implemented a generic function for pretty-formatted DDL in the
ruleutils.c file as part of my pg_get_policy_ddl patch. I suggest reusing
it once my patch is accepted and committed by the community.

Show quoted text

Best regards,
Florin Irion
Tim Waizenegger
EDB (EnterpriseDB)

#8Tim Waizenegger
tim.waizenegger@enterprisedb.com
In reply to: Akshay Joshi (#7)
1 attachment(s)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Wed, Oct 22, 2025 at 12:27 PM Chao Li <li.evan.chao@gmail.com> wrote:

While reviewing a similar patch of pg_get_policy_ddl(), it take the last parameter as a pretty flag. I wonder why pg_get_domain_ddl() doesn’t support an argument for pretty?

We have now added pretty printing support in the latest version; see
attached patch. FYI, we tried to stay consistent in the implementation
with pg_get_policy_ddl from
/messages/by-id/CANxoLDdJsRJqnjMXV3yjsk07Z5iRWxG-c2hZJC7bAKqf8ZXj_A@mail.gmail.com
or

On Thu, Oct 23, 2025 at 11:20 AM Akshay Joshi
<akshay.joshi@enterprisedb.com> wrote:

I've already implemented a generic function for pretty-formatted DDL in the ruleutils.c file as part of my pg_get_policy_ddl patch. I suggest reusing it once my patch is accepted and committed by the community.

Thanks Akshay, we adopted your "get_formatted_string()" function into
our path and tried to follow similar implementation patterns as well.

On Thu, Oct 23, 2025 at 6:22 AM jian he <jian.universality@gmail.com> wrote:

I’ve done some refactoring, hope it’s now more intuitive to you.
Since a domain’s base type can itself be another domain, it’s better to use

appendStringInfo(&buf, "CREATE DOMAIN %s AS %s",
generate_qualified_type_name(domain_oid),
generate_qualified_type_name(typForm->typbasetype));

then the domain's base type is also fully qualified.

Thanks for the feedback and refactoring Jian! We adopted the
"generate_qualified_type_name" into our patch; this is much better.

I also refactored the logic for printing domain constraints, which should reduce
syscache lookups or table scans compared to your version.

we did a lot of refactoring as well while integrating the
pretty-printing support and aligning with e.g. the pg_get_policy_ddl
command. Some of this refactoring follows your suggestiong.
There is one change we decided not to adopt: constructing the
ddl-strings _while_ scanning for constraints in order to optimize the
syscache lookups. The reason is this:

the optimization will save one "SearchSysCache1" per constraint in the
domain. But we still call "pg_get_constraintdef_worker" for each
constraint which does a full table scan.
So in that context, saving the cache lookup seems like a minor
improvement. To us it seemed more desirable to leave the code
unoptimized in this location so that constraint scan and constraint
processing can be decoupled into individual single-purpose
functions/blocks.
Let us know what you think.

Best regards,
Florin Irion
Tim Waizenegger
EDB (EnterpriseDB)

Attachments:

v3-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchapplication/octet-stream; name=v3-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchDownload
From e8bd8c712308d59bba19f1236b3d691a718a26b6 Mon Sep 17 00:00:00 2001
From: Florin Irion <florin.irion@enterprisedb.com>
Date: Thu, 18 Sep 2025 18:52:43 +0200
Subject: [PATCH v3] Add pg_get_domain_ddl() function to reconstruct CREATE
 DOMAIN statements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch introduces a new system function pg_get_domain_ddl() that
reconstructs the CREATE DOMAIN statement for a given domain. The function
takes a regtype parameter and returns the complete DDL statement including
the domain name, base type, default value, and all associated constraints.

The function follows the same pattern as other DDL reconstruction functions
like pg_get_functiondef() and pg_get_constraintdef(), providing a
decompiled reconstruction rather than the original command text.

Key features:
* Supports domains with default values
* Includes all domain constraints (CHECK, NOT NULL)
* NOT VALID constraint are handled with an extra ALTER command.
* Properly quotes identifiers and schema names
* Handles complex constraint expressions
* pretty printing support

A new documentation section "Get Object DDL Functions" has been created
to group DDL reconstruction functions, starting with pg_get_domain_ddl().
This provides a foundation for future DDL functions for other object types.

Comprehensive regression tests are included covering various domain
configurations.

Reference: PG-151
Author: Florin Irion <florin.irion@enterprisedb.com>
Author: Tim Waizenegger <tim.waizenegger@enterprisedb.com>
Reviewed-by: Álvaro Herrera alvherre@alvh.no-ip.org
Reviewed-by: jian he <jian.universality@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
---
 doc/src/sgml/func/func-info.sgml         |  45 ++++
 src/backend/catalog/system_functions.sql |   6 +
 src/backend/utils/adt/ruleutils.c        | 227 ++++++++++++++++
 src/include/catalog/pg_proc.dat          |   3 +
 src/test/regress/expected/object_ddl.out | 328 +++++++++++++++++++++++
 src/test/regress/parallel_schedule       |   2 +-
 src/test/regress/sql/object_ddl.sql      | 135 ++++++++++
 7 files changed, 745 insertions(+), 1 deletion(-)
 create mode 100644 src/test/regress/expected/object_ddl.out
 create mode 100644 src/test/regress/sql/object_ddl.sql

diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml
index d4508114a48..55527f468ae 100644
--- a/doc/src/sgml/func/func-info.sgml
+++ b/doc/src/sgml/func/func-info.sgml
@@ -3797,4 +3797,49 @@ acl      | {postgres=arwdDxtm/postgres,foo=r/postgres}
 
   </sect2>
 
+  <sect2 id="functions-get-object-ddl">
+   <title>Get Object DDL Functions</title>
+
+   <para>
+    The functions shown in <xref linkend="functions-get-object-ddl-table"/>
+    print the DDL statements for various database objects.
+    (This is a decompiled reconstruction, not the original text
+    of the command.)
+   </para>
+
+   <table id="functions-get-object-ddl-table">
+    <title>Get Object DDL Functions</title>
+    <tgroup cols="1">
+     <thead>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        Function
+       </para>
+       <para>
+        Description
+       </para></entry>
+      </row>
+     </thead>
+
+     <tbody>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>pg_get_domain_ddl</primary>
+        </indexterm>
+        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>regtype</type>
+         <optional> <parameter>pretty</parameter> <type>boolean</type> </optional>)
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Reconstructs the creating command for a domain.
+        The result is a complete <command>CREATE DOMAIN</command> statement.
+       </para></entry>
+      </row>
+     </tbody>
+    </tgroup>
+   </table>
+
+  </sect2>
+
   </sect1>
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 2d946d6d9e9..5a96ff1efcb 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -657,6 +657,12 @@ LANGUAGE INTERNAL
 STRICT VOLATILE PARALLEL UNSAFE
 AS 'pg_replication_origin_session_setup';
 
+CREATE OR REPLACE FUNCTION
+ pg_get_domain_ddl(domain_name regtype, pretty bool DEFAULT false)
+ RETURNS text
+ LANGUAGE internal
+AS 'pg_get_domain_ddl_ext';
+
 --
 -- The default permissions for functions mean that anyone can execute them.
 -- A number of functions shouldn't be executable by just anyone, but rather
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 556ab057e5a..a6f59e6776e 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -546,6 +546,11 @@ static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan,
 										  deparse_context *context,
 										  bool showimplicit,
 										  bool needcomma);
+static void get_formatted_string(StringInfo buf,
+								 int prettyFlags,
+								 int noOfTabChars,
+								 const char *fmt,...) pg_attribute_printf(4, 5);
+static char *pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags);
 
 #define only_marker(rte)  ((rte)->inh ? "" : "ONLY ")
 
@@ -13743,3 +13748,225 @@ get_range_partbound_string(List *bound_datums)
 
 	return buf.data;
 }
+
+/*
+ * get_formatted_string
+ *
+ * Return a formatted version of the string.
+ *
+ * pretty - If pretty is true, the output includes tabs (\t) and newlines (\n).
+ * noOfTabChars - indent with specified no of tabs.
+ * fmt - printf-style format string used by appendStringInfoVA.
+ */
+static void
+get_formatted_string(StringInfo buf, int prettyFlags, int noOfTabChars, const char *fmt,...)
+{
+	va_list		args;
+
+	if (prettyFlags & PRETTYFLAG_INDENT)
+	{
+		appendStringInfoChar(buf, '\n');
+		/* Indent with tabs */
+		for (int i = 0; i < noOfTabChars; i++)
+		{
+			appendStringInfoChar(buf, '\t');
+		}
+	}
+	else
+		appendStringInfoChar(buf, ' ');
+
+	va_start(args, fmt);
+	appendStringInfoVA(buf, fmt, args);
+	va_end(args);
+}
+
+
+/*
+ * Helper function to scan domain constraints
+ */
+static void
+scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons)
+{
+	Relation	constraintRel;
+	SysScanDesc sscan;
+	ScanKeyData skey;
+	HeapTuple	constraintTup;
+
+	*validcons = NIL;
+	*invalidcons = NIL;
+
+	constraintRel = table_open(ConstraintRelationId, AccessShareLock);
+
+	ScanKeyInit(&skey,
+				Anum_pg_constraint_contypid,
+				BTEqualStrategyNumber, F_OIDEQ,
+				ObjectIdGetDatum(domain_oid));
+
+	sscan = systable_beginscan(constraintRel,
+							   ConstraintTypidIndexId,
+							   true,
+							   NULL,
+							   1,
+							   &skey);
+
+	while (HeapTupleIsValid(constraintTup = systable_getnext(sscan)))
+	{
+		Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+
+		if (con->convalidated)
+			*validcons = lappend_oid(*validcons, con->oid);
+		else
+			*invalidcons = lappend_oid(*invalidcons, con->oid);
+	}
+
+	systable_endscan(sscan);
+	table_close(constraintRel, AccessShareLock);
+}
+
+/*
+ * Helper function to build CREATE DOMAIN statement
+ */
+static void
+build_create_domain_statement(StringInfo buf, Form_pg_type typForm,
+							  Node *defaultExpr, List *validConstraints, int prettyFlags)
+{
+	HeapTuple	baseTypeTuple;
+	Form_pg_type baseTypeForm;
+	Oid			baseCollation = InvalidOid;
+	ListCell   *lc;
+
+	appendStringInfo(buf, "CREATE DOMAIN %s AS %s",
+					 generate_qualified_type_name(typForm->oid),
+					 generate_qualified_type_name(typForm->typbasetype));
+
+	/* Add collation if it differs from base type's collation */
+	if (OidIsValid(typForm->typcollation))
+	{
+		/* Get base type's collation for comparison */
+		baseTypeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typForm->typbasetype));
+		if (HeapTupleIsValid(baseTypeTuple))
+		{
+			baseTypeForm = (Form_pg_type) GETSTRUCT(baseTypeTuple);
+			baseCollation = baseTypeForm->typcollation;
+			ReleaseSysCache(baseTypeTuple);
+		}
+
+		/* Only add COLLATE if domain's collation differs from base type's */
+		if (typForm->typcollation != baseCollation)
+		{
+			get_formatted_string(buf, prettyFlags, 1, "COLLATE %s",
+								 generate_collation_name(typForm->typcollation));
+		}
+	}
+
+	/* Add default value if present */
+	if (defaultExpr != NULL)
+	{
+		char	   *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, prettyFlags, 0);
+
+		get_formatted_string(buf, prettyFlags, 1, "DEFAULT %s", defaultValue);
+	}
+
+	/* Add valid constraints */
+	foreach(lc, validConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		HeapTuple	constraintTup;
+		Form_pg_constraint con;
+		char	   *constraintDef;
+
+		/* Look up the constraint info */
+		constraintTup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid));
+		if (!HeapTupleIsValid(constraintTup))
+			continue;			/* constraint was dropped concurrently */
+
+		con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+		constraintDef = pg_get_constraintdef_worker(constraintOid, false, prettyFlags, true);
+
+		get_formatted_string(buf, prettyFlags, 1, "CONSTRAINT %s",
+							 quote_identifier(NameStr(con->conname)));
+		get_formatted_string(buf, prettyFlags, 2, "%s", constraintDef);
+
+		ReleaseSysCache(constraintTup);
+	}
+
+	appendStringInfoChar(buf, ';');
+}
+
+/*
+ * Helper function to add ALTER DOMAIN statements for invalid constraints
+ */
+static void
+add_alter_domain_statements(StringInfo buf, List *invalidConstraints, int prettyFlags)
+{
+	ListCell   *lc;
+
+	foreach(lc, invalidConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		char	   *alterStmt = pg_get_constraintdef_worker(constraintOid, true, prettyFlags, true);
+
+		if (alterStmt)
+			appendStringInfo(buf, "\n%s;", alterStmt);
+	}
+}
+
+/*
+ * pg_get_domain_ddl_ext - Get CREATE DOMAIN statement for a domain with pretty-print option
+ */
+Datum
+pg_get_domain_ddl_ext(PG_FUNCTION_ARGS)
+{
+	Oid			domain_oid = PG_GETARG_OID(0);
+	bool		pretty = PG_GETARG_BOOL(1);
+	char	   *res;
+	int			prettyFlags;
+
+	prettyFlags = pretty ? GET_PRETTY_FLAGS(pretty) : 0;
+
+	res = pg_get_domain_ddl_worker(domain_oid, prettyFlags);
+	if (res == NULL)
+		PG_RETURN_NULL();
+	PG_RETURN_TEXT_P(string_to_text(res));
+}
+
+
+
+static char *
+pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags)
+{
+	StringInfoData buf;
+	HeapTuple	typeTuple;
+	Form_pg_type typForm;
+	Node	   *defaultExpr;
+	List	   *validConstraints;
+	List	   *invalidConstraints;
+
+	/* Look up the domain in pg_type */
+	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+	if (!HeapTupleIsValid(typeTuple))
+		return NULL;
+
+	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
+
+	/* Get default expression */
+	defaultExpr = get_typdefault(domain_oid);
+
+	/* Scan for valid and invalid constraints */
+	scan_domain_constraints(domain_oid, &validConstraints, &invalidConstraints);
+
+	/* Build the DDL statement */
+	initStringInfo(&buf);
+	build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints, prettyFlags);
+
+	/* Add ALTER DOMAIN statements for invalid constraints */
+	if (list_length(invalidConstraints) > 0)
+		add_alter_domain_statements(&buf, invalidConstraints, prettyFlags);
+
+	/* Cleanup */
+	list_free(validConstraints);
+	list_free(invalidConstraints);
+	ReleaseSysCache(typeTuple);
+
+	return buf.data;
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 5cf9e12fcb9..476874d0063 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -8515,6 +8515,9 @@
 { oid => '2508', descr => 'constraint description with pretty-print option',
   proname => 'pg_get_constraintdef', provolatile => 's', prorettype => 'text',
   proargtypes => 'oid bool', prosrc => 'pg_get_constraintdef_ext' },
+{ oid => '8024', descr => 'get CREATE statement for DOMAIN with pretty option',
+  proname => 'pg_get_domain_ddl', prorettype => 'text',
+  proargtypes => 'regtype bool', prosrc => 'pg_get_domain_ddl_ext' },
 { oid => '2509',
   descr => 'deparse an encoded expression with pretty-print option',
   proname => 'pg_get_expr', provolatile => 's', prorettype => 'text',
diff --git a/src/test/regress/expected/object_ddl.out b/src/test/regress/expected/object_ddl.out
new file mode 100644
index 00000000000..9aad54347da
--- /dev/null
+++ b/src/test/regress/expected/object_ddl.out
@@ -0,0 +1,328 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => false);
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', false);
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => true);
+                                 pg_get_domain_ddl                                 
+-----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text                   +
+         DEFAULT '00000'::text                                                    +
+         CONSTRAINT regress_us_postal_code_check                                  +
+                 CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', true);
+                                 pg_get_domain_ddl                                 
+-----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text                   +
+         DEFAULT '00000'::text                                                    +
+         CONSTRAINT regress_us_postal_code_check                                  +
+                 CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+                                                   pg_get_domain_ddl                                                   
+-----------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4 CONSTRAINT regress_domain_not_null_not_null NOT NULL;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_not_null', pretty => true);
+                        pg_get_domain_ddl                        
+-----------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4+
+         CONSTRAINT regress_domain_not_null_not_null            +
+                 NOT NULL;
+(1 row)
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+SELECT pg_get_domain_ddl('regress_domain_check');
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4 CONSTRAINT regress_a CHECK ((VALUE < 100)) CONSTRAINT regress_b CHECK ((VALUE > 10));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_check', pretty => true);
+                      pg_get_domain_ddl                       
+--------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4+
+         CONSTRAINT regress_a                                +
+                 CHECK (VALUE < 100)                         +
+         CONSTRAINT regress_b                                +
+                 CHECK (VALUE > 10);
+(1 row)
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+                                                                                                       pg_get_domain_ddl                                                                                                       
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4 CONSTRAINT regress_a CHECK ((VALUE < 100)) CONSTRAINT "regress_Constraint B" CHECK ((VALUE > 10)) CONSTRAINT "regress_ConstraintC" CHECK ((VALUE <> 55));
+(1 row)
+
+SELECT pg_get_domain_ddl('"regress_domain with space"', pretty => true);
+                          pg_get_domain_ddl                          
+---------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4+
+         CONSTRAINT regress_a                                       +
+                 CHECK (VALUE < 100)                                +
+         CONSTRAINT "regress_Constraint B"                          +
+                 CHECK (VALUE > 10)                                 +
+         CONSTRAINT "regress_ConstraintC"                           +
+                 CHECK (VALUE <> 55);
+(1 row)
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+ERROR:  type "regress_nonexistent_domain" does not exist
+LINE 1: SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regty...
+                                 ^
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+SELECT pg_get_domain_ddl(NULL, pretty => true);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_simple_domain', pretty => true);
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
+(1 row)
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+                                                                   pg_get_domain_ddl                                                                    
+--------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain CONSTRAINT regress_derived_domain_check CHECK ((length((VALUE)::text) > 3));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_derived_domain', pretty => true);
+                             pg_get_domain_ddl                             
+---------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain+
+         CONSTRAINT regress_derived_domain_check                          +
+                 CHECK (length(VALUE::text) > 3);
+(1 row)
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_seq_domain', pretty => true);
+                     pg_get_domain_ddl                      
+------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4+
+         DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                                 pg_get_domain_ddl                                                 
+-------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq_renamed'::regclass);
+(1 row)
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+                                 pg_get_domain_ddl                                  
+------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric" DEFAULT 0.00;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_precise_numeric', pretty => true);
+                          pg_get_domain_ddl                           
+----------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric"+
+         DEFAULT 0.00;
+(1 row)
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4 CONSTRAINT regress_int_array_domain_check CHECK ((array_length(VALUE, 1) <= 5));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_int_array_domain', pretty => true);
+                         pg_get_domain_ddl                         
+-------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4+
+         CONSTRAINT regress_int_array_domain_check                +
+                 CHECK (array_length(VALUE, 1) <= 5);
+(1 row)
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+                                        pg_get_domain_ddl                                         
+--------------------------------------------------------------------------------------------------
+ CREATE DOMAIN regress_test_schema.regress_schema_domain AS pg_catalog.text DEFAULT 'test'::text;
+(1 row)
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+                                                                                                                                                                     pg_get_domain_ddl                                                                                                                                                                      
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar" DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK ((length((VALUE)::text) >= 5)) CONSTRAINT regress_comprehensive_domain_check1 CHECK (((VALUE)::text !~ '^\s*$'::text));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_comprehensive_domain', pretty => true);
+                             pg_get_domain_ddl                             
+---------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar"+
+         DEFAULT 'default_value'::character varying                       +
+         CONSTRAINT regress_comprehensive_domain_not_null                 +
+                 NOT NULL                                                 +
+         CONSTRAINT regress_comprehensive_domain_check                    +
+                 CHECK (length(VALUE::text) >= 5)                         +
+         CONSTRAINT regress_comprehensive_domain_check1                   +
+                 CHECK (VALUE::text !~ '^\s*$'::text);
+(1 row)
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+                                                                        pg_get_domain_ddl                                                                        
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type CONSTRAINT regress_address_domain_check CHECK (((VALUE).zipcode ~ '^\d{5}$'::text));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_address_domain', pretty => true);
+                             pg_get_domain_ddl                              
+----------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type+
+         CONSTRAINT regress_address_domain_check                           +
+                 CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
+(1 row)
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                        +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK ((VALUE > 0)) NOT VALID;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_not_valid', pretty => true);
+                                            pg_get_domain_ddl                                            
+---------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                      +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+(1 row)
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+                                                    pg_get_domain_ddl                                                     
+--------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4 CONSTRAINT regress_domain_mixed_check CHECK ((VALUE <> 0));+
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (((VALUE >= 1) AND (VALUE <= 100))) NOT VALID;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_mixed', pretty => true);
+                                                 pg_get_domain_ddl                                                  
+--------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4                                                      +
+         CONSTRAINT regress_domain_mixed_check                                                                     +
+                 CHECK (VALUE <> 0);                                                                               +
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE >= 1 AND VALUE <= 100) NOT VALID;
+(1 row)
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+                                pg_get_domain_ddl                                 
+----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text COLLATE "C";
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_with_collate', pretty => true);
+                          pg_get_domain_ddl                          
+---------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text+
+         COLLATE "C";
+(1 row)
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index f56482fb9f1..8b6881c397f 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -28,7 +28,7 @@ test: strings md5 numerology point lseg line box path polygon circle date time t
 # geometry depends on point, lseg, line, box, path, polygon, circle
 # horology depends on date, time, timetz, timestamp, timestamptz, interval
 # ----------
-test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import
+test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import object_ddl
 
 # ----------
 # Load huge amounts of data
diff --git a/src/test/regress/sql/object_ddl.sql b/src/test/regress/sql/object_ddl.sql
new file mode 100644
index 00000000000..98fb20017ea
--- /dev/null
+++ b/src/test/regress/sql/object_ddl.sql
@@ -0,0 +1,135 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => false);
+SELECT pg_get_domain_ddl('regress_us_postal_code', false);
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => true);
+SELECT pg_get_domain_ddl('regress_us_postal_code', true);
+
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+SELECT pg_get_domain_ddl('regress_domain_not_null', pretty => true);
+
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+
+SELECT pg_get_domain_ddl('regress_domain_check');
+SELECT pg_get_domain_ddl('regress_domain_check', pretty => true);
+
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+SELECT pg_get_domain_ddl('"regress_domain with space"', pretty => true);
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+SELECT pg_get_domain_ddl(NULL, pretty => true);  -- should return NULL
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+SELECT pg_get_domain_ddl('regress_simple_domain', pretty => true);
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+SELECT pg_get_domain_ddl('regress_derived_domain', pretty => true);
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+SELECT pg_get_domain_ddl('regress_seq_domain', pretty => true);
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+SELECT pg_get_domain_ddl('regress_precise_numeric', pretty => true);
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+SELECT pg_get_domain_ddl('regress_int_array_domain', pretty => true);
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+SELECT pg_get_domain_ddl('regress_comprehensive_domain', pretty => true);
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+SELECT pg_get_domain_ddl('regress_address_domain', pretty => true);
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+SELECT pg_get_domain_ddl('regress_domain_not_valid', pretty => true);
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+SELECT pg_get_domain_ddl('regress_domain_mixed', pretty => true);
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+SELECT pg_get_domain_ddl('regress_domain_with_collate', pretty => true);
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
-- 
2.50.1 (Apple Git-155)

#9Florin Irion
irionr@gmail.com
In reply to: Tim Waizenegger (#8)
1 attachment(s)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

Hello, Cirrus-CI was complaining because we don't sort the constraints
and thus
they were making the test fail because of the random order.
Made it sort with `list_sort`and `list_oid_cmp`not sure if that's the best
thing to sort them.
Check v4 attached.
Cheers,
Florin Irion
Tim Waizenegger
EDB (EnterpriseDB)

Attachments:

v4-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchtext/plain; charset=UTF-8; name=v4-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchDownload
From 2aeca53cbaec510ee54145039742e5d484940895 Mon Sep 17 00:00:00 2001
From: Florin Irion <florin.irion@enterprisedb.com>
Date: Thu, 18 Sep 2025 18:52:43 +0200
Subject: [PATCH v4] Add pg_get_domain_ddl() function to reconstruct CREATE
 DOMAIN statements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch introduces a new system function pg_get_domain_ddl() that
reconstructs the CREATE DOMAIN statement for a given domain. The function
takes a regtype parameter and returns the complete DDL statement including
the domain name, base type, default value, and all associated constraints.

The function follows the same pattern as other DDL reconstruction functions
like pg_get_functiondef() and pg_get_constraintdef(), providing a
decompiled reconstruction rather than the original command text.

Key features:
* Supports domains with default values
* Includes all domain constraints (CHECK, NOT NULL)
* NOT VALID constraint are handled with an extra ALTER command.
* Properly quotes identifiers and schema names
* Handles complex constraint expressions
* pretty printing support

A new documentation section "Get Object DDL Functions" has been created
to group DDL reconstruction functions, starting with pg_get_domain_ddl().
This provides a foundation for future DDL functions for other object types.

Comprehensive regression tests are included covering various domain
configurations.

Reference: PG-151
Author: Florin Irion <florin.irion@enterprisedb.com>
Author: Tim Waizenegger <tim.waizenegger@enterprisedb.com>
Reviewed-by: Álvaro Herrera alvherre@alvh.no-ip.org
Reviewed-by: jian he <jian.universality@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
---
 doc/src/sgml/func/func-info.sgml         |  45 ++++
 src/backend/catalog/system_functions.sql |   6 +
 src/backend/utils/adt/ruleutils.c        | 233 ++++++++++++++++
 src/include/catalog/pg_proc.dat          |   3 +
 src/test/regress/expected/object_ddl.out | 328 +++++++++++++++++++++++
 src/test/regress/parallel_schedule       |   2 +-
 src/test/regress/sql/object_ddl.sql      | 135 ++++++++++
 7 files changed, 751 insertions(+), 1 deletion(-)
 create mode 100644 src/test/regress/expected/object_ddl.out
 create mode 100644 src/test/regress/sql/object_ddl.sql

diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml
index d4508114a48..55527f468ae 100644
--- a/doc/src/sgml/func/func-info.sgml
+++ b/doc/src/sgml/func/func-info.sgml
@@ -3797,4 +3797,49 @@ acl      | {postgres=arwdDxtm/postgres,foo=r/postgres}
 
   </sect2>
 
+  <sect2 id="functions-get-object-ddl">
+   <title>Get Object DDL Functions</title>
+
+   <para>
+    The functions shown in <xref linkend="functions-get-object-ddl-table"/>
+    print the DDL statements for various database objects.
+    (This is a decompiled reconstruction, not the original text
+    of the command.)
+   </para>
+
+   <table id="functions-get-object-ddl-table">
+    <title>Get Object DDL Functions</title>
+    <tgroup cols="1">
+     <thead>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        Function
+       </para>
+       <para>
+        Description
+       </para></entry>
+      </row>
+     </thead>
+
+     <tbody>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>pg_get_domain_ddl</primary>
+        </indexterm>
+        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>regtype</type>
+         <optional> <parameter>pretty</parameter> <type>boolean</type> </optional>)
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Reconstructs the creating command for a domain.
+        The result is a complete <command>CREATE DOMAIN</command> statement.
+       </para></entry>
+      </row>
+     </tbody>
+    </tgroup>
+   </table>
+
+  </sect2>
+
   </sect1>
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 2d946d6d9e9..5a96ff1efcb 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -657,6 +657,12 @@ LANGUAGE INTERNAL
 STRICT VOLATILE PARALLEL UNSAFE
 AS 'pg_replication_origin_session_setup';
 
+CREATE OR REPLACE FUNCTION
+ pg_get_domain_ddl(domain_name regtype, pretty bool DEFAULT false)
+ RETURNS text
+ LANGUAGE internal
+AS 'pg_get_domain_ddl_ext';
+
 --
 -- The default permissions for functions mean that anyone can execute them.
 -- A number of functions shouldn't be executable by just anyone, but rather
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 556ab057e5a..34d63f2f502 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -546,6 +546,11 @@ static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan,
 										  deparse_context *context,
 										  bool showimplicit,
 										  bool needcomma);
+static void get_formatted_string(StringInfo buf,
+								 int prettyFlags,
+								 int noOfTabChars,
+								 const char *fmt,...) pg_attribute_printf(4, 5);
+static char *pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags);
 
 #define only_marker(rte)  ((rte)->inh ? "" : "ONLY ")
 
@@ -13743,3 +13748,231 @@ get_range_partbound_string(List *bound_datums)
 
 	return buf.data;
 }
+
+/*
+ * get_formatted_string
+ *
+ * Return a formatted version of the string.
+ *
+ * pretty - If pretty is true, the output includes tabs (\t) and newlines (\n).
+ * noOfTabChars - indent with specified no of tabs.
+ * fmt - printf-style format string used by appendStringInfoVA.
+ */
+static void
+get_formatted_string(StringInfo buf, int prettyFlags, int noOfTabChars, const char *fmt,...)
+{
+	va_list		args;
+
+	if (prettyFlags & PRETTYFLAG_INDENT)
+	{
+		appendStringInfoChar(buf, '\n');
+		/* Indent with tabs */
+		for (int i = 0; i < noOfTabChars; i++)
+		{
+			appendStringInfoChar(buf, '\t');
+		}
+	}
+	else
+		appendStringInfoChar(buf, ' ');
+
+	va_start(args, fmt);
+	appendStringInfoVA(buf, fmt, args);
+	va_end(args);
+}
+
+
+/*
+ * Helper function to scan domain constraints
+ */
+static void
+scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons)
+{
+	Relation	constraintRel;
+	SysScanDesc sscan;
+	ScanKeyData skey;
+	HeapTuple	constraintTup;
+
+	*validcons = NIL;
+	*invalidcons = NIL;
+
+	constraintRel = table_open(ConstraintRelationId, AccessShareLock);
+
+	ScanKeyInit(&skey,
+				Anum_pg_constraint_contypid,
+				BTEqualStrategyNumber, F_OIDEQ,
+				ObjectIdGetDatum(domain_oid));
+
+	sscan = systable_beginscan(constraintRel,
+							   ConstraintTypidIndexId,
+							   true,
+							   NULL,
+							   1,
+							   &skey);
+
+	while (HeapTupleIsValid(constraintTup = systable_getnext(sscan)))
+	{
+		Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+
+		if (con->convalidated)
+			*validcons = lappend_oid(*validcons, con->oid);
+		else
+			*invalidcons = lappend_oid(*invalidcons, con->oid);
+	}
+
+	systable_endscan(sscan);
+	table_close(constraintRel, AccessShareLock);
+
+	/* Sort constraints by OID for stable output */
+	if (list_length(*validcons) > 1)
+		list_sort(*validcons, list_oid_cmp);
+	if (list_length(*invalidcons) > 1)
+		list_sort(*invalidcons, list_oid_cmp);
+}
+
+/*
+ * Helper function to build CREATE DOMAIN statement
+ */
+static void
+build_create_domain_statement(StringInfo buf, Form_pg_type typForm,
+							  Node *defaultExpr, List *validConstraints, int prettyFlags)
+{
+	HeapTuple	baseTypeTuple;
+	Form_pg_type baseTypeForm;
+	Oid			baseCollation = InvalidOid;
+	ListCell   *lc;
+
+	appendStringInfo(buf, "CREATE DOMAIN %s AS %s",
+					 generate_qualified_type_name(typForm->oid),
+					 generate_qualified_type_name(typForm->typbasetype));
+
+	/* Add collation if it differs from base type's collation */
+	if (OidIsValid(typForm->typcollation))
+	{
+		/* Get base type's collation for comparison */
+		baseTypeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typForm->typbasetype));
+		if (HeapTupleIsValid(baseTypeTuple))
+		{
+			baseTypeForm = (Form_pg_type) GETSTRUCT(baseTypeTuple);
+			baseCollation = baseTypeForm->typcollation;
+			ReleaseSysCache(baseTypeTuple);
+		}
+
+		/* Only add COLLATE if domain's collation differs from base type's */
+		if (typForm->typcollation != baseCollation)
+		{
+			get_formatted_string(buf, prettyFlags, 1, "COLLATE %s",
+								 generate_collation_name(typForm->typcollation));
+		}
+	}
+
+	/* Add default value if present */
+	if (defaultExpr != NULL)
+	{
+		char	   *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, prettyFlags, 0);
+
+		get_formatted_string(buf, prettyFlags, 1, "DEFAULT %s", defaultValue);
+	}
+
+	/* Add valid constraints */
+	foreach(lc, validConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		HeapTuple	constraintTup;
+		Form_pg_constraint con;
+		char	   *constraintDef;
+
+		/* Look up the constraint info */
+		constraintTup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid));
+		if (!HeapTupleIsValid(constraintTup))
+			continue;			/* constraint was dropped concurrently */
+
+		con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+		constraintDef = pg_get_constraintdef_worker(constraintOid, false, prettyFlags, true);
+
+		get_formatted_string(buf, prettyFlags, 1, "CONSTRAINT %s",
+							 quote_identifier(NameStr(con->conname)));
+		get_formatted_string(buf, prettyFlags, 2, "%s", constraintDef);
+
+		ReleaseSysCache(constraintTup);
+	}
+
+	appendStringInfoChar(buf, ';');
+}
+
+/*
+ * Helper function to add ALTER DOMAIN statements for invalid constraints
+ */
+static void
+add_alter_domain_statements(StringInfo buf, List *invalidConstraints, int prettyFlags)
+{
+	ListCell   *lc;
+
+	foreach(lc, invalidConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		char	   *alterStmt = pg_get_constraintdef_worker(constraintOid, true, prettyFlags, true);
+
+		if (alterStmt)
+			appendStringInfo(buf, "\n%s;", alterStmt);
+	}
+}
+
+/*
+ * pg_get_domain_ddl_ext - Get CREATE DOMAIN statement for a domain with pretty-print option
+ */
+Datum
+pg_get_domain_ddl_ext(PG_FUNCTION_ARGS)
+{
+	Oid			domain_oid = PG_GETARG_OID(0);
+	bool		pretty = PG_GETARG_BOOL(1);
+	char	   *res;
+	int			prettyFlags;
+
+	prettyFlags = pretty ? GET_PRETTY_FLAGS(pretty) : 0;
+
+	res = pg_get_domain_ddl_worker(domain_oid, prettyFlags);
+	if (res == NULL)
+		PG_RETURN_NULL();
+	PG_RETURN_TEXT_P(string_to_text(res));
+}
+
+
+
+static char *
+pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags)
+{
+	StringInfoData buf;
+	HeapTuple	typeTuple;
+	Form_pg_type typForm;
+	Node	   *defaultExpr;
+	List	   *validConstraints;
+	List	   *invalidConstraints;
+
+	/* Look up the domain in pg_type */
+	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+	if (!HeapTupleIsValid(typeTuple))
+		return NULL;
+
+	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
+
+	/* Get default expression */
+	defaultExpr = get_typdefault(domain_oid);
+
+	/* Scan for valid and invalid constraints */
+	scan_domain_constraints(domain_oid, &validConstraints, &invalidConstraints);
+
+	/* Build the DDL statement */
+	initStringInfo(&buf);
+	build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints, prettyFlags);
+
+	/* Add ALTER DOMAIN statements for invalid constraints */
+	if (list_length(invalidConstraints) > 0)
+		add_alter_domain_statements(&buf, invalidConstraints, prettyFlags);
+
+	/* Cleanup */
+	list_free(validConstraints);
+	list_free(invalidConstraints);
+	ReleaseSysCache(typeTuple);
+
+	return buf.data;
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 5cf9e12fcb9..476874d0063 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -8515,6 +8515,9 @@
 { oid => '2508', descr => 'constraint description with pretty-print option',
   proname => 'pg_get_constraintdef', provolatile => 's', prorettype => 'text',
   proargtypes => 'oid bool', prosrc => 'pg_get_constraintdef_ext' },
+{ oid => '8024', descr => 'get CREATE statement for DOMAIN with pretty option',
+  proname => 'pg_get_domain_ddl', prorettype => 'text',
+  proargtypes => 'regtype bool', prosrc => 'pg_get_domain_ddl_ext' },
 { oid => '2509',
   descr => 'deparse an encoded expression with pretty-print option',
   proname => 'pg_get_expr', provolatile => 's', prorettype => 'text',
diff --git a/src/test/regress/expected/object_ddl.out b/src/test/regress/expected/object_ddl.out
new file mode 100644
index 00000000000..9aad54347da
--- /dev/null
+++ b/src/test/regress/expected/object_ddl.out
@@ -0,0 +1,328 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => false);
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', false);
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => true);
+                                 pg_get_domain_ddl                                 
+-----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text                   +
+         DEFAULT '00000'::text                                                    +
+         CONSTRAINT regress_us_postal_code_check                                  +
+                 CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', true);
+                                 pg_get_domain_ddl                                 
+-----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text                   +
+         DEFAULT '00000'::text                                                    +
+         CONSTRAINT regress_us_postal_code_check                                  +
+                 CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+                                                   pg_get_domain_ddl                                                   
+-----------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4 CONSTRAINT regress_domain_not_null_not_null NOT NULL;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_not_null', pretty => true);
+                        pg_get_domain_ddl                        
+-----------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4+
+         CONSTRAINT regress_domain_not_null_not_null            +
+                 NOT NULL;
+(1 row)
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+SELECT pg_get_domain_ddl('regress_domain_check');
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4 CONSTRAINT regress_a CHECK ((VALUE < 100)) CONSTRAINT regress_b CHECK ((VALUE > 10));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_check', pretty => true);
+                      pg_get_domain_ddl                       
+--------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4+
+         CONSTRAINT regress_a                                +
+                 CHECK (VALUE < 100)                         +
+         CONSTRAINT regress_b                                +
+                 CHECK (VALUE > 10);
+(1 row)
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+                                                                                                       pg_get_domain_ddl                                                                                                       
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4 CONSTRAINT regress_a CHECK ((VALUE < 100)) CONSTRAINT "regress_Constraint B" CHECK ((VALUE > 10)) CONSTRAINT "regress_ConstraintC" CHECK ((VALUE <> 55));
+(1 row)
+
+SELECT pg_get_domain_ddl('"regress_domain with space"', pretty => true);
+                          pg_get_domain_ddl                          
+---------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4+
+         CONSTRAINT regress_a                                       +
+                 CHECK (VALUE < 100)                                +
+         CONSTRAINT "regress_Constraint B"                          +
+                 CHECK (VALUE > 10)                                 +
+         CONSTRAINT "regress_ConstraintC"                           +
+                 CHECK (VALUE <> 55);
+(1 row)
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+ERROR:  type "regress_nonexistent_domain" does not exist
+LINE 1: SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regty...
+                                 ^
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+SELECT pg_get_domain_ddl(NULL, pretty => true);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_simple_domain', pretty => true);
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
+(1 row)
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+                                                                   pg_get_domain_ddl                                                                    
+--------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain CONSTRAINT regress_derived_domain_check CHECK ((length((VALUE)::text) > 3));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_derived_domain', pretty => true);
+                             pg_get_domain_ddl                             
+---------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain+
+         CONSTRAINT regress_derived_domain_check                          +
+                 CHECK (length(VALUE::text) > 3);
+(1 row)
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_seq_domain', pretty => true);
+                     pg_get_domain_ddl                      
+------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4+
+         DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                                 pg_get_domain_ddl                                                 
+-------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq_renamed'::regclass);
+(1 row)
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+                                 pg_get_domain_ddl                                  
+------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric" DEFAULT 0.00;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_precise_numeric', pretty => true);
+                          pg_get_domain_ddl                           
+----------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric"+
+         DEFAULT 0.00;
+(1 row)
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4 CONSTRAINT regress_int_array_domain_check CHECK ((array_length(VALUE, 1) <= 5));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_int_array_domain', pretty => true);
+                         pg_get_domain_ddl                         
+-------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4+
+         CONSTRAINT regress_int_array_domain_check                +
+                 CHECK (array_length(VALUE, 1) <= 5);
+(1 row)
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+                                        pg_get_domain_ddl                                         
+--------------------------------------------------------------------------------------------------
+ CREATE DOMAIN regress_test_schema.regress_schema_domain AS pg_catalog.text DEFAULT 'test'::text;
+(1 row)
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+                                                                                                                                                                     pg_get_domain_ddl                                                                                                                                                                      
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar" DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK ((length((VALUE)::text) >= 5)) CONSTRAINT regress_comprehensive_domain_check1 CHECK (((VALUE)::text !~ '^\s*$'::text));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_comprehensive_domain', pretty => true);
+                             pg_get_domain_ddl                             
+---------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar"+
+         DEFAULT 'default_value'::character varying                       +
+         CONSTRAINT regress_comprehensive_domain_not_null                 +
+                 NOT NULL                                                 +
+         CONSTRAINT regress_comprehensive_domain_check                    +
+                 CHECK (length(VALUE::text) >= 5)                         +
+         CONSTRAINT regress_comprehensive_domain_check1                   +
+                 CHECK (VALUE::text !~ '^\s*$'::text);
+(1 row)
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+                                                                        pg_get_domain_ddl                                                                        
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type CONSTRAINT regress_address_domain_check CHECK (((VALUE).zipcode ~ '^\d{5}$'::text));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_address_domain', pretty => true);
+                             pg_get_domain_ddl                              
+----------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type+
+         CONSTRAINT regress_address_domain_check                           +
+                 CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
+(1 row)
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                        +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK ((VALUE > 0)) NOT VALID;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_not_valid', pretty => true);
+                                            pg_get_domain_ddl                                            
+---------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                      +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+(1 row)
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+                                                    pg_get_domain_ddl                                                     
+--------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4 CONSTRAINT regress_domain_mixed_check CHECK ((VALUE <> 0));+
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (((VALUE >= 1) AND (VALUE <= 100))) NOT VALID;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_mixed', pretty => true);
+                                                 pg_get_domain_ddl                                                  
+--------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4                                                      +
+         CONSTRAINT regress_domain_mixed_check                                                                     +
+                 CHECK (VALUE <> 0);                                                                               +
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE >= 1 AND VALUE <= 100) NOT VALID;
+(1 row)
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+                                pg_get_domain_ddl                                 
+----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text COLLATE "C";
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_with_collate', pretty => true);
+                          pg_get_domain_ddl                          
+---------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text+
+         COLLATE "C";
+(1 row)
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index f56482fb9f1..8b6881c397f 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -28,7 +28,7 @@ test: strings md5 numerology point lseg line box path polygon circle date time t
 # geometry depends on point, lseg, line, box, path, polygon, circle
 # horology depends on date, time, timetz, timestamp, timestamptz, interval
 # ----------
-test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import
+test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import object_ddl
 
 # ----------
 # Load huge amounts of data
diff --git a/src/test/regress/sql/object_ddl.sql b/src/test/regress/sql/object_ddl.sql
new file mode 100644
index 00000000000..98fb20017ea
--- /dev/null
+++ b/src/test/regress/sql/object_ddl.sql
@@ -0,0 +1,135 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => false);
+SELECT pg_get_domain_ddl('regress_us_postal_code', false);
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => true);
+SELECT pg_get_domain_ddl('regress_us_postal_code', true);
+
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+SELECT pg_get_domain_ddl('regress_domain_not_null', pretty => true);
+
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+
+SELECT pg_get_domain_ddl('regress_domain_check');
+SELECT pg_get_domain_ddl('regress_domain_check', pretty => true);
+
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+SELECT pg_get_domain_ddl('"regress_domain with space"', pretty => true);
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+SELECT pg_get_domain_ddl(NULL, pretty => true);  -- should return NULL
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+SELECT pg_get_domain_ddl('regress_simple_domain', pretty => true);
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+SELECT pg_get_domain_ddl('regress_derived_domain', pretty => true);
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+SELECT pg_get_domain_ddl('regress_seq_domain', pretty => true);
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+SELECT pg_get_domain_ddl('regress_precise_numeric', pretty => true);
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+SELECT pg_get_domain_ddl('regress_int_array_domain', pretty => true);
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+SELECT pg_get_domain_ddl('regress_comprehensive_domain', pretty => true);
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+SELECT pg_get_domain_ddl('regress_address_domain', pretty => true);
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+SELECT pg_get_domain_ddl('regress_domain_not_valid', pretty => true);
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+SELECT pg_get_domain_ddl('regress_domain_mixed', pretty => true);
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+SELECT pg_get_domain_ddl('regress_domain_with_collate', pretty => true);
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
-- 
2.45.1

#10Man Zeng
zengman@halodbtech.com
In reply to: Florin Irion (#9)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

Quick correction with an apology: I accidentally created a new thread (/messages/by-id/tencent_64301BB7627E58CD256CE15F@qq.com) and submitted the patch there—my apologies for the mix-up! Let’s just continue the discussion here as planned.

--
Regrads,
Man Zeng

#11Chao Li
li.evan.chao@gmail.com
In reply to: Florin Irion (#9)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

On Nov 12, 2025, at 00:14, Florin Irion <irionr@gmail.com> wrote:

Hello, Cirrus-CI was complaining because we don't sort the constraints and thus
they were making the test fail because of the random order.
Made it sort with `list_sort`and `list_oid_cmp`not sure if that's the best
thing to sort them.
Check v4 attached.
Cheers,
Florin Irion
Tim Waizenegger
EDB (EnterpriseDB)
<v4-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patch>

I just tested v4, and see two problems:

```
evantest=# CREATE DOMAIN public.int AS pg_catalog.int4;
CREATE DOMAIN
evantest=# SELECT pg_get_domain_ddl('int');
ERROR: cache lookup failed for type 0
evantest=#
evantest=#
evantest=# SELECT pg_get_domain_ddl('pg_class');
ERROR: cache lookup failed for type 0
evantest=#
evantest=#
evantest=# SELECT pg_get_domain_ddl('public.int');
pg_get_domain_ddl
------------------------------------------------
CREATE DOMAIN public."int" AS pg_catalog.int4;
(1 row)

evantest=# show search_path;
search_path
-----------------
"$user", public
(1 row)
```

1. The error message "cache lookup failed for type 0” looks not good. At lease saying something like “domain ‘int’ does not exist”.

2. I created a domain “int” in “public”, as you see, “public” is in the search_path, but SELECT pg_get_domain_ddl('int’); failed.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#12Neil Chen
carpenter.nail.cz@gmail.com
In reply to: Florin Irion (#9)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

Hi Florin,

+pg_get_domain_ddl_ext(PG_FUNCTION_ARGS)

+{
+ Oid domain_oid = PG_GETARG_OID(0);
+ bool pretty = PG_GETARG_BOOL(1);
+ char   *res;
+ int prettyFlags;
+
+ prettyFlags = pretty ? GET_PRETTY_FLAGS(pretty) : 0;

Seems like we should directly use GET_PRETTY_FLAGS here, as it already
checks the value of "pretty". For a "display-oriented" result, using
PRETTYFLAG_INDENT looks more appropriate.

+ appendStringInfo(buf, "CREATE DOMAIN %s AS %s",

+ generate_qualified_type_name(typForm->oid),
+ generate_qualified_type_name(typForm->typbasetype));

It might be good to first call get_typtype to check if it is TYPTYPE_DOMAIN.

#13Florin Irion
irionr@gmail.com
In reply to: Chao Li (#11)
1 attachment(s)
Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement

Hello,

On 20/11/25 07:55, Man Zeng wrote:

Quick correction with an apology: I accidentally created a new thread (/messages/by-id/tencent_64301BB7627E58CD256CE15F@qq.com) and submitted the patch there—my apologies for the mix-up! Let’s just continue the discussion here as planned.

On 20/11/25 09:47, Chao Li wrote:

1. The error message "cache lookup failed for type 0” looks not good. At lease saying something like “domain ‘int’ does not exist”.

2. I created a domain “int” in “public”, as you see, “public” is in the search_path, but SELECT pg_get_domain_ddl('int’); failed.

Thank you both Man Zeng and Chao Li for checking this. Changes added in v5.
I don't think there is a way to make the path issue work, so we just
give more info
to the caller. We exit with error when a built-in name is used and we
throw also a
hint saying that schema-qualified domain name should be used to be sure
it's not
conflicting with a built in  name.

On 20/11/25 10:44, Neil Chen wrote:

Hi Florin,

+pg_get_domain_ddl_ext(PG_FUNCTION_ARGS)
+{
+ Oid domain_oid = PG_GETARG_OID(0);
+ bool pretty = PG_GETARG_BOOL(1);
+ char   *res;
+ int prettyFlags;
+
+ prettyFlags = pretty ? GET_PRETTY_FLAGS(pretty) : 0;

Seems like we should directly use GET_PRETTY_FLAGS here, as it already
checks the value of "pretty". For a "display-oriented" result, using
PRETTYFLAG_INDENT looks more appropriate.

Well, actually no,
GET_PRETTY_FLAGS(false) returns PRETTYFLAG_INDENT
But we actually want 0 when pretty is false (no indentation, just spaces)

+ appendStringInfo(buf, "CREATE DOMAIN %s AS %s",
+ generate_qualified_type_name(typForm->oid),
+ generate_qualified_type_name(typForm->typbasetype));

It might be good to first call get_typtype to check if it is
TYPTYPE_DOMAIN.

I added this in `pg_get_domain_ddl_worker`, as we need to make this
check ASAP.

Cheers,
Florin Irion
Tim Waizenegger
EDB (EnterpriseDB)

Attachments:

v5-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchtext/plain; charset=UTF-8; name=v5-0001-Add-pg_get_domain_ddl-function-to-reconstruct-CRE.patchDownload
From 30738bef278cd0140e9b3030dde62b5f048a0dfc Mon Sep 17 00:00:00 2001
From: Florin Irion <florin.irion@enterprisedb.com>
Date: Thu, 18 Sep 2025 18:52:43 +0200
Subject: [PATCH v5] Add pg_get_domain_ddl() function to reconstruct CREATE
 DOMAIN statements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch introduces a new system function pg_get_domain_ddl() that
reconstructs the CREATE DOMAIN statement for a given domain. The function
takes a regtype parameter and returns the complete DDL statement including
the domain name, base type, default value, and all associated constraints.

The function follows the same pattern as other DDL reconstruction functions
like pg_get_functiondef() and pg_get_constraintdef(), providing a
decompiled reconstruction rather than the original command text.

Key features:
* Supports domains with default values
* Includes all domain constraints (CHECK, NOT NULL)
* NOT VALID constraint are handled with an extra ALTER command.
* Properly quotes identifiers and schema names
* Handles complex constraint expressions
* pretty printing support
* warn against conflicting built-in names

A new documentation section "Get Object DDL Functions" has been created
to group DDL reconstruction functions, starting with pg_get_domain_ddl().
This provides a foundation for future DDL functions for other object types.

Comprehensive regression tests are included covering various domain
configurations.

Reference: PG-151
Author: Florin Irion <florin.irion@enterprisedb.com>
Author: Tim Waizenegger <tim.waizenegger@enterprisedb.com>
Reviewed-by: Álvaro Herrera alvherre@alvh.no-ip.org
Reviewed-by: jian he <jian.universality@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Neil Chen <carpenter.nail.cz@gmail.com>
Reviewed-by: Man Zeng <zengman@halodbtech.com>
---
 doc/src/sgml/func/func-info.sgml         |  53 ++++
 src/backend/catalog/system_functions.sql |   6 +
 src/backend/utils/adt/ruleutils.c        | 240 ++++++++++++++++
 src/include/catalog/pg_proc.dat          |   3 +
 src/test/regress/expected/object_ddl.out | 348 +++++++++++++++++++++++
 src/test/regress/parallel_schedule       |   2 +-
 src/test/regress/sql/object_ddl.sql      | 145 ++++++++++
 7 files changed, 796 insertions(+), 1 deletion(-)
 create mode 100644 src/test/regress/expected/object_ddl.out
 create mode 100644 src/test/regress/sql/object_ddl.sql

diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml
index d4508114a48..4bba7551c21 100644
--- a/doc/src/sgml/func/func-info.sgml
+++ b/doc/src/sgml/func/func-info.sgml
@@ -3797,4 +3797,57 @@ acl      | {postgres=arwdDxtm/postgres,foo=r/postgres}
 
   </sect2>
 
+  <sect2 id="functions-get-object-ddl">
+   <title>Get Object DDL Functions</title>
+
+   <para>
+    The functions shown in <xref linkend="functions-get-object-ddl-table"/>
+    print the DDL statements for various database objects.
+    (This is a decompiled reconstruction, not the original text
+    of the command.)
+   </para>
+
+   <table id="functions-get-object-ddl-table">
+    <title>Get Object DDL Functions</title>
+    <tgroup cols="1">
+     <thead>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        Function
+       </para>
+       <para>
+        Description
+       </para></entry>
+      </row>
+     </thead>
+
+     <tbody>
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>pg_get_domain_ddl</primary>
+        </indexterm>
+        <function>pg_get_domain_ddl</function> ( <parameter>domain</parameter> <type>regtype</type>
+         <optional> <parameter>pretty</parameter> <type>boolean</type> </optional>)
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Reconstructs the creating command for a domain.
+        The result is a complete <command>CREATE DOMAIN</command> statement.
+       </para>
+       <para>
+        The <parameter>domain</parameter> parameter uses type <type>regtype</type>,
+        which follows the standard <varname>search_path</varname> for type name
+        resolution. If a domain name conflicts with a built-in type name
+        (for example, a domain named <literal>int</literal>), you must use a
+        schema-qualified name (for example, <literal>'public.int'::regtype</literal>)
+        to reference the domain.
+       </para></entry>
+      </row>
+     </tbody>
+    </tgroup>
+   </table>
+
+  </sect2>
+
   </sect1>
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 2d946d6d9e9..5a96ff1efcb 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -657,6 +657,12 @@ LANGUAGE INTERNAL
 STRICT VOLATILE PARALLEL UNSAFE
 AS 'pg_replication_origin_session_setup';
 
+CREATE OR REPLACE FUNCTION
+ pg_get_domain_ddl(domain_name regtype, pretty bool DEFAULT false)
+ RETURNS text
+ LANGUAGE internal
+AS 'pg_get_domain_ddl_ext';
+
 --
 -- The default permissions for functions mean that anyone can execute them.
 -- A number of functions shouldn't be executable by just anyone, but rather
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 6cf90be40bb..ae676557210 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -546,6 +546,11 @@ static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan,
 										  deparse_context *context,
 										  bool showimplicit,
 										  bool needcomma);
+static void get_formatted_string(StringInfo buf,
+								 int prettyFlags,
+								 int noOfTabChars,
+								 const char *fmt,...) pg_attribute_printf(4, 5);
+static char *pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags);
 
 #define only_marker(rte)  ((rte)->inh ? "" : "ONLY ")
 
@@ -13743,3 +13748,238 @@ get_range_partbound_string(List *bound_datums)
 
 	return buf.data;
 }
+
+/*
+ * get_formatted_string
+ *
+ * Return a formatted version of the string.
+ *
+ * pretty - If pretty is true, the output includes tabs (\t) and newlines (\n).
+ * noOfTabChars - indent with specified no of tabs.
+ * fmt - printf-style format string used by appendStringInfoVA.
+ */
+static void
+get_formatted_string(StringInfo buf, int prettyFlags, int noOfTabChars, const char *fmt,...)
+{
+	va_list		args;
+
+	if (prettyFlags & PRETTYFLAG_INDENT)
+	{
+		appendStringInfoChar(buf, '\n');
+		/* Indent with tabs */
+		for (int i = 0; i < noOfTabChars; i++)
+		{
+			appendStringInfoChar(buf, '\t');
+		}
+	}
+	else
+		appendStringInfoChar(buf, ' ');
+
+	va_start(args, fmt);
+	appendStringInfoVA(buf, fmt, args);
+	va_end(args);
+}
+
+
+/*
+ * Helper function to scan domain constraints
+ */
+static void
+scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons)
+{
+	Relation	constraintRel;
+	SysScanDesc sscan;
+	ScanKeyData skey;
+	HeapTuple	constraintTup;
+
+	*validcons = NIL;
+	*invalidcons = NIL;
+
+	constraintRel = table_open(ConstraintRelationId, AccessShareLock);
+
+	ScanKeyInit(&skey,
+				Anum_pg_constraint_contypid,
+				BTEqualStrategyNumber, F_OIDEQ,
+				ObjectIdGetDatum(domain_oid));
+
+	sscan = systable_beginscan(constraintRel,
+							   ConstraintTypidIndexId,
+							   true,
+							   NULL,
+							   1,
+							   &skey);
+
+	while (HeapTupleIsValid(constraintTup = systable_getnext(sscan)))
+	{
+		Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+
+		if (con->convalidated)
+			*validcons = lappend_oid(*validcons, con->oid);
+		else
+			*invalidcons = lappend_oid(*invalidcons, con->oid);
+	}
+
+	systable_endscan(sscan);
+	table_close(constraintRel, AccessShareLock);
+
+	/* Sort constraints by OID for stable output */
+	if (list_length(*validcons) > 1)
+		list_sort(*validcons, list_oid_cmp);
+	if (list_length(*invalidcons) > 1)
+		list_sort(*invalidcons, list_oid_cmp);
+}
+
+/*
+ * Helper function to build CREATE DOMAIN statement
+ */
+static void
+build_create_domain_statement(StringInfo buf, Form_pg_type typForm,
+							  Node *defaultExpr, List *validConstraints, int prettyFlags)
+{
+	HeapTuple	baseTypeTuple;
+	Form_pg_type baseTypeForm;
+	Oid			baseCollation = InvalidOid;
+	ListCell   *lc;
+
+	appendStringInfo(buf, "CREATE DOMAIN %s AS %s",
+					 generate_qualified_type_name(typForm->oid),
+					 generate_qualified_type_name(typForm->typbasetype));
+
+	/* Add collation if it differs from base type's collation */
+	if (OidIsValid(typForm->typcollation))
+	{
+		/* Get base type's collation for comparison */
+		baseTypeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typForm->typbasetype));
+		if (HeapTupleIsValid(baseTypeTuple))
+		{
+			baseTypeForm = (Form_pg_type) GETSTRUCT(baseTypeTuple);
+			baseCollation = baseTypeForm->typcollation;
+			ReleaseSysCache(baseTypeTuple);
+		}
+
+		/* Only add COLLATE if domain's collation differs from base type's */
+		if (typForm->typcollation != baseCollation)
+		{
+			get_formatted_string(buf, prettyFlags, 1, "COLLATE %s",
+								 generate_collation_name(typForm->typcollation));
+		}
+	}
+
+	/* Add default value if present */
+	if (defaultExpr != NULL)
+	{
+		char	   *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, prettyFlags, 0);
+
+		get_formatted_string(buf, prettyFlags, 1, "DEFAULT %s", defaultValue);
+	}
+
+	/* Add valid constraints */
+	foreach(lc, validConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		HeapTuple	constraintTup;
+		Form_pg_constraint con;
+		char	   *constraintDef;
+
+		/* Look up the constraint info */
+		constraintTup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid));
+		if (!HeapTupleIsValid(constraintTup))
+			continue;			/* constraint was dropped concurrently */
+
+		con = (Form_pg_constraint) GETSTRUCT(constraintTup);
+		constraintDef = pg_get_constraintdef_worker(constraintOid, false, prettyFlags, true);
+
+		get_formatted_string(buf, prettyFlags, 1, "CONSTRAINT %s",
+							 quote_identifier(NameStr(con->conname)));
+		get_formatted_string(buf, prettyFlags, 2, "%s", constraintDef);
+
+		ReleaseSysCache(constraintTup);
+	}
+
+	appendStringInfoChar(buf, ';');
+}
+
+/*
+ * Helper function to add ALTER DOMAIN statements for invalid constraints
+ */
+static void
+add_alter_domain_statements(StringInfo buf, List *invalidConstraints, int prettyFlags)
+{
+	ListCell   *lc;
+
+	foreach(lc, invalidConstraints)
+	{
+		Oid			constraintOid = lfirst_oid(lc);
+		char	   *alterStmt = pg_get_constraintdef_worker(constraintOid, true, prettyFlags, true);
+
+		if (alterStmt)
+			appendStringInfo(buf, "\n%s;", alterStmt);
+	}
+}
+
+/*
+ * pg_get_domain_ddl_ext - Get CREATE DOMAIN statement for a domain with pretty-print option
+ */
+Datum
+pg_get_domain_ddl_ext(PG_FUNCTION_ARGS)
+{
+	Oid			domain_oid = PG_GETARG_OID(0);
+	bool		pretty = PG_GETARG_BOOL(1);
+	char	   *res;
+	int			prettyFlags;
+
+	prettyFlags = pretty ? GET_PRETTY_FLAGS(pretty) : 0;
+
+	res = pg_get_domain_ddl_worker(domain_oid, prettyFlags);
+	if (res == NULL)
+		PG_RETURN_NULL();
+	PG_RETURN_TEXT_P(string_to_text(res));
+}
+
+
+
+static char *
+pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags)
+{
+	StringInfoData buf;
+	HeapTuple	typeTuple;
+	Form_pg_type typForm;
+	Node	   *defaultExpr;
+	List	   *validConstraints;
+	List	   *invalidConstraints;
+
+	/* Look up the domain in pg_type */
+	typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid));
+	if (!HeapTupleIsValid(typeTuple))
+		return NULL;
+
+	typForm = (Form_pg_type) GETSTRUCT(typeTuple);
+
+	/* Check that this is actually a domain */
+	if (typForm->typtype != TYPTYPE_DOMAIN)
+		ereport(ERROR,
+				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+				 errmsg("\"%s\" is not a domain", format_type_be(domain_oid)),
+				 errhint("Use a schema-qualified name if the domain name conflicts with a built-in name.")));
+
+	/* Get default expression */
+	defaultExpr = get_typdefault(domain_oid);
+
+	/* Scan for valid and invalid constraints */
+	scan_domain_constraints(domain_oid, &validConstraints, &invalidConstraints);
+
+	/* Build the DDL statement */
+	initStringInfo(&buf);
+	build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints, prettyFlags);
+
+	/* Add ALTER DOMAIN statements for invalid constraints */
+	if (list_length(invalidConstraints) > 0)
+		add_alter_domain_statements(&buf, invalidConstraints, prettyFlags);
+
+	/* Cleanup */
+	list_free(validConstraints);
+	list_free(invalidConstraints);
+	ReleaseSysCache(typeTuple);
+
+	return buf.data;
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 66af2d96d67..2f7869103f3 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -8515,6 +8515,9 @@
 { oid => '2508', descr => 'constraint description with pretty-print option',
   proname => 'pg_get_constraintdef', provolatile => 's', prorettype => 'text',
   proargtypes => 'oid bool', prosrc => 'pg_get_constraintdef_ext' },
+{ oid => '8024', descr => 'get CREATE statement for DOMAIN with pretty option',
+  proname => 'pg_get_domain_ddl', prorettype => 'text',
+  proargtypes => 'regtype bool', prosrc => 'pg_get_domain_ddl_ext' },
 { oid => '2509',
   descr => 'deparse an encoded expression with pretty-print option',
   proname => 'pg_get_expr', provolatile => 's', prorettype => 'text',
diff --git a/src/test/regress/expected/object_ddl.out b/src/test/regress/expected/object_ddl.out
new file mode 100644
index 00000000000..1241b89a770
--- /dev/null
+++ b/src/test/regress/expected/object_ddl.out
@@ -0,0 +1,348 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => false);
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', false);
+                                                                                          pg_get_domain_ddl                                                                                           
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text DEFAULT '00000'::text CONSTRAINT regress_us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => true);
+                                 pg_get_domain_ddl                                 
+-----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text                   +
+         DEFAULT '00000'::text                                                    +
+         CONSTRAINT regress_us_postal_code_check                                  +
+                 CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_us_postal_code', true);
+                                 pg_get_domain_ddl                                 
+-----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_us_postal_code AS pg_catalog.text                   +
+         DEFAULT '00000'::text                                                    +
+         CONSTRAINT regress_us_postal_code_check                                  +
+                 CHECK (VALUE ~ '^\d{5}$'::text OR VALUE ~ '^\d{5}-\d{4}$'::text);
+(1 row)
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+                                                   pg_get_domain_ddl                                                   
+-----------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4 CONSTRAINT regress_domain_not_null_not_null NOT NULL;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_not_null', pretty => true);
+                        pg_get_domain_ddl                        
+-----------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_null AS pg_catalog.int4+
+         CONSTRAINT regress_domain_not_null_not_null            +
+                 NOT NULL;
+(1 row)
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+SELECT pg_get_domain_ddl('regress_domain_check');
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4 CONSTRAINT regress_a CHECK ((VALUE < 100)) CONSTRAINT regress_b CHECK ((VALUE > 10));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_check', pretty => true);
+                      pg_get_domain_ddl                       
+--------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_check AS pg_catalog.int4+
+         CONSTRAINT regress_a                                +
+                 CHECK (VALUE < 100)                         +
+         CONSTRAINT regress_b                                +
+                 CHECK (VALUE > 10);
+(1 row)
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+                                                                                                       pg_get_domain_ddl                                                                                                       
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4 CONSTRAINT regress_a CHECK ((VALUE < 100)) CONSTRAINT "regress_Constraint B" CHECK ((VALUE > 10)) CONSTRAINT "regress_ConstraintC" CHECK ((VALUE <> 55));
+(1 row)
+
+SELECT pg_get_domain_ddl('"regress_domain with space"', pretty => true);
+                          pg_get_domain_ddl                          
+---------------------------------------------------------------------
+ CREATE DOMAIN public."regress_domain with space" AS pg_catalog.int4+
+         CONSTRAINT regress_a                                       +
+                 CHECK (VALUE < 100)                                +
+         CONSTRAINT "regress_Constraint B"                          +
+                 CHECK (VALUE > 10)                                 +
+         CONSTRAINT "regress_ConstraintC"                           +
+                 CHECK (VALUE <> 55);
+(1 row)
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+ERROR:  type "regress_nonexistent_domain" does not exist
+LINE 1: SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regty...
+                                 ^
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+SELECT pg_get_domain_ddl(NULL, pretty => true);  -- should return NULL
+ pg_get_domain_ddl 
+-------------------
+ 
+(1 row)
+
+SELECT pg_get_domain_ddl('pg_class');  -- should fail - not a domain
+ERROR:  "pg_class" is not a domain
+HINT:  Use a schema-qualified name if the domain name conflicts with a built-in name.
+SELECT pg_get_domain_ddl('integer');  -- should fail - not a domain
+ERROR:  "integer" is not a domain
+HINT:  Use a schema-qualified name if the domain name conflicts with a built-in name.
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_simple_domain', pretty => true);
+                       pg_get_domain_ddl                        
+----------------------------------------------------------------
+ CREATE DOMAIN public.regress_simple_domain AS pg_catalog.text;
+(1 row)
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+                                                                   pg_get_domain_ddl                                                                    
+--------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain CONSTRAINT regress_derived_domain_check CHECK ((length((VALUE)::text) > 3));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_derived_domain', pretty => true);
+                             pg_get_domain_ddl                             
+---------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_derived_domain AS public.regress_base_domain+
+         CONSTRAINT regress_derived_domain_check                          +
+                 CHECK (length(VALUE::text) > 3);
+(1 row)
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_seq_domain', pretty => true);
+                     pg_get_domain_ddl                      
+------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4+
+         DEFAULT nextval('regress_test_seq'::regclass);
+(1 row)
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+                                                 pg_get_domain_ddl                                                 
+-------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_seq_domain AS pg_catalog.int4 DEFAULT nextval('regress_test_seq_renamed'::regclass);
+(1 row)
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+                                 pg_get_domain_ddl                                  
+------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric" DEFAULT 0.00;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_precise_numeric', pretty => true);
+                          pg_get_domain_ddl                           
+----------------------------------------------------------------------
+ CREATE DOMAIN public.regress_precise_numeric AS pg_catalog."numeric"+
+         DEFAULT 0.00;
+(1 row)
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+                                                                 pg_get_domain_ddl                                                                  
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4 CONSTRAINT regress_int_array_domain_check CHECK ((array_length(VALUE, 1) <= 5));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_int_array_domain', pretty => true);
+                         pg_get_domain_ddl                         
+-------------------------------------------------------------------
+ CREATE DOMAIN public.regress_int_array_domain AS pg_catalog._int4+
+         CONSTRAINT regress_int_array_domain_check                +
+                 CHECK (array_length(VALUE, 1) <= 5);
+(1 row)
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+                                        pg_get_domain_ddl                                         
+--------------------------------------------------------------------------------------------------
+ CREATE DOMAIN regress_test_schema.regress_schema_domain AS pg_catalog.text DEFAULT 'test'::text;
+(1 row)
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+                                                                                                                                                                     pg_get_domain_ddl                                                                                                                                                                      
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar" DEFAULT 'default_value'::character varying CONSTRAINT regress_comprehensive_domain_not_null NOT NULL CONSTRAINT regress_comprehensive_domain_check CHECK ((length((VALUE)::text) >= 5)) CONSTRAINT regress_comprehensive_domain_check1 CHECK (((VALUE)::text !~ '^\s*$'::text));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_comprehensive_domain', pretty => true);
+                             pg_get_domain_ddl                             
+---------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_comprehensive_domain AS pg_catalog."varchar"+
+         DEFAULT 'default_value'::character varying                       +
+         CONSTRAINT regress_comprehensive_domain_not_null                 +
+                 NOT NULL                                                 +
+         CONSTRAINT regress_comprehensive_domain_check                    +
+                 CHECK (length(VALUE::text) >= 5)                         +
+         CONSTRAINT regress_comprehensive_domain_check1                   +
+                 CHECK (VALUE::text !~ '^\s*$'::text);
+(1 row)
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+                                                                        pg_get_domain_ddl                                                                        
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type CONSTRAINT regress_address_domain_check CHECK (((VALUE).zipcode ~ '^\d{5}$'::text));
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_address_domain', pretty => true);
+                             pg_get_domain_ddl                              
+----------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_address_domain AS public.regress_address_type+
+         CONSTRAINT regress_address_domain_check                           +
+                 CHECK ((VALUE).zipcode ~ '^\d{5}$'::text);
+(1 row)
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+                                             pg_get_domain_ddl                                             
+-----------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                        +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK ((VALUE > 0)) NOT VALID;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_not_valid', pretty => true);
+                                            pg_get_domain_ddl                                            
+---------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_not_valid AS pg_catalog.int4;                                      +
+ ALTER DOMAIN public.regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+(1 row)
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+                                                    pg_get_domain_ddl                                                     
+--------------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4 CONSTRAINT regress_domain_mixed_check CHECK ((VALUE <> 0));+
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (((VALUE >= 1) AND (VALUE <= 100))) NOT VALID;
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_mixed', pretty => true);
+                                                 pg_get_domain_ddl                                                  
+--------------------------------------------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_mixed AS pg_catalog.int4                                                      +
+         CONSTRAINT regress_domain_mixed_check                                                                     +
+                 CHECK (VALUE <> 0);                                                                               +
+ ALTER DOMAIN public.regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE >= 1 AND VALUE <= 100) NOT VALID;
+(1 row)
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+                                pg_get_domain_ddl                                 
+----------------------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text COLLATE "C";
+(1 row)
+
+SELECT pg_get_domain_ddl('regress_domain_with_collate', pretty => true);
+                          pg_get_domain_ddl                          
+---------------------------------------------------------------------
+ CREATE DOMAIN public.regress_domain_with_collate AS pg_catalog.text+
+         COLLATE "C";
+(1 row)
+
+-- Test domain that shadows a built-in type name (must use schema-qualified name)
+CREATE DOMAIN public.int AS pg_catalog.int4;
+-- This should fail because 'int' resolves to pg_catalog.int4, not public.int
+SELECT pg_get_domain_ddl('int');  -- should fail
+ERROR:  "integer" is not a domain
+HINT:  Use a schema-qualified name if the domain name conflicts with a built-in name.
+-- This should work with schema-qualified name
+SELECT pg_get_domain_ddl('public.int');
+               pg_get_domain_ddl                
+------------------------------------------------
+ CREATE DOMAIN public."int" AS pg_catalog.int4;
+(1 row)
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
+DROP DOMAIN public.int;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index cc6d799bcea..5a615806dcf 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -28,7 +28,7 @@ test: strings md5 numerology point lseg line box path polygon circle date time t
 # geometry depends on point, lseg, line, box, path, polygon, circle
 # horology depends on date, time, timetz, timestamp, timestamptz, interval
 # ----------
-test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import pg_ndistinct pg_dependencies
+test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import pg_ndistinct pg_dependencies object_ddl
 
 # ----------
 # Load huge amounts of data
diff --git a/src/test/regress/sql/object_ddl.sql b/src/test/regress/sql/object_ddl.sql
new file mode 100644
index 00000000000..4eb0e5b86d6
--- /dev/null
+++ b/src/test/regress/sql/object_ddl.sql
@@ -0,0 +1,145 @@
+--
+-- Test for the following functions to get object DDL:
+-- - pg_get_domain_ddl
+--
+
+CREATE DOMAIN regress_us_postal_code AS TEXT
+    DEFAULT '00000'
+    CONSTRAINT regress_us_postal_code_check
+        CHECK (
+            VALUE ~ '^\d{5}$'
+    OR VALUE ~ '^\d{5}-\d{4}$'
+    );
+
+SELECT pg_get_domain_ddl('regress_us_postal_code');
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => false);
+SELECT pg_get_domain_ddl('regress_us_postal_code', false);
+SELECT pg_get_domain_ddl('regress_us_postal_code', pretty => true);
+SELECT pg_get_domain_ddl('regress_us_postal_code', true);
+
+
+CREATE DOMAIN regress_domain_not_null AS INT NOT NULL;
+
+SELECT pg_get_domain_ddl('regress_domain_not_null');
+SELECT pg_get_domain_ddl('regress_domain_not_null', pretty => true);
+
+
+CREATE DOMAIN regress_domain_check AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT regress_b CHECK (VALUE > 10);
+
+SELECT pg_get_domain_ddl('regress_domain_check');
+SELECT pg_get_domain_ddl('regress_domain_check', pretty => true);
+
+
+CREATE DOMAIN "regress_domain with space" AS INT
+    CONSTRAINT regress_a CHECK (VALUE < 100)
+    CONSTRAINT "regress_Constraint B" CHECK (VALUE > 10)
+    CONSTRAINT "regress_ConstraintC" CHECK (VALUE != 55);
+
+SELECT pg_get_domain_ddl('"regress_domain with space"');
+SELECT pg_get_domain_ddl('"regress_domain with space"', pretty => true);
+
+-- Test error cases
+SELECT pg_get_domain_ddl('regress_nonexistent_domain'::regtype);  -- should fail
+SELECT pg_get_domain_ddl(NULL);  -- should return NULL
+SELECT pg_get_domain_ddl(NULL, pretty => true);  -- should return NULL
+SELECT pg_get_domain_ddl('pg_class');  -- should fail - not a domain
+SELECT pg_get_domain_ddl('integer');  -- should fail - not a domain
+
+-- Test domains with no constraints
+CREATE DOMAIN regress_simple_domain AS text;
+SELECT pg_get_domain_ddl('regress_simple_domain');
+SELECT pg_get_domain_ddl('regress_simple_domain', pretty => true);
+
+-- Test domain over another domain
+CREATE DOMAIN regress_base_domain AS varchar(10);
+CREATE DOMAIN regress_derived_domain AS regress_base_domain CHECK (LENGTH(VALUE) > 3);
+SELECT pg_get_domain_ddl('regress_derived_domain');
+SELECT pg_get_domain_ddl('regress_derived_domain', pretty => true);
+
+-- Test domain with complex default expressions
+CREATE SEQUENCE regress_test_seq;
+CREATE DOMAIN regress_seq_domain AS int DEFAULT nextval('regress_test_seq');
+SELECT pg_get_domain_ddl('regress_seq_domain');
+SELECT pg_get_domain_ddl('regress_seq_domain', pretty => true);
+
+-- Test domain with a renamed sequence as default expression
+ALTER SEQUENCE regress_test_seq RENAME TO regress_test_seq_renamed;
+SELECT pg_get_domain_ddl('regress_seq_domain');
+
+-- Test domain with type modifiers
+CREATE DOMAIN regress_precise_numeric AS numeric(10,2) DEFAULT 0.00;
+SELECT pg_get_domain_ddl('regress_precise_numeric');
+SELECT pg_get_domain_ddl('regress_precise_numeric', pretty => true);
+
+-- Test domain over array type
+CREATE DOMAIN regress_int_array_domain AS int[] CHECK (array_length(VALUE, 1) <= 5);
+SELECT pg_get_domain_ddl('regress_int_array_domain');
+SELECT pg_get_domain_ddl('regress_int_array_domain', pretty => true);
+
+-- Test domain in non-public schema
+CREATE SCHEMA regress_test_schema;
+CREATE DOMAIN regress_test_schema.regress_schema_domain AS text DEFAULT 'test';
+SELECT pg_get_domain_ddl('regress_test_schema.regress_schema_domain');
+
+-- Test domain with multiple constraint types combined
+CREATE DOMAIN regress_comprehensive_domain AS varchar(50)
+    NOT NULL
+    DEFAULT 'default_value'
+    CHECK (LENGTH(VALUE) >= 5)
+    CHECK (VALUE !~ '^\s*$');  -- not just whitespace
+SELECT pg_get_domain_ddl('regress_comprehensive_domain');
+SELECT pg_get_domain_ddl('regress_comprehensive_domain', pretty => true);
+
+-- Test domain over composite type
+CREATE TYPE regress_address_type AS (street text, city text, zipcode text);
+CREATE DOMAIN regress_address_domain AS regress_address_type CHECK ((VALUE).zipcode ~ '^\d{5}$');
+SELECT pg_get_domain_ddl('regress_address_domain');
+SELECT pg_get_domain_ddl('regress_address_domain', pretty => true);
+
+-- Test domain with NOT VALID constraint
+CREATE DOMAIN regress_domain_not_valid AS int;
+ALTER DOMAIN regress_domain_not_valid ADD CONSTRAINT check_positive CHECK (VALUE > 0) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_not_valid');
+SELECT pg_get_domain_ddl('regress_domain_not_valid', pretty => true);
+
+-- Test domain with mix of valid and not valid constraints
+CREATE DOMAIN regress_domain_mixed AS int CHECK (VALUE != 0);
+ALTER DOMAIN regress_domain_mixed ADD CONSTRAINT check_range CHECK (VALUE BETWEEN 1 AND 100) NOT VALID;
+SELECT pg_get_domain_ddl('regress_domain_mixed');
+SELECT pg_get_domain_ddl('regress_domain_mixed', pretty => true);
+
+-- Test domain with collation
+CREATE DOMAIN regress_domain_with_collate AS text COLLATE "C";
+SELECT pg_get_domain_ddl('regress_domain_with_collate');
+SELECT pg_get_domain_ddl('regress_domain_with_collate', pretty => true);
+
+-- Test domain that shadows a built-in type name (must use schema-qualified name)
+CREATE DOMAIN public.int AS pg_catalog.int4;
+-- This should fail because 'int' resolves to pg_catalog.int4, not public.int
+SELECT pg_get_domain_ddl('int');  -- should fail
+-- This should work with schema-qualified name
+SELECT pg_get_domain_ddl('public.int');
+
+-- Cleanup
+DROP DOMAIN regress_us_postal_code;
+DROP DOMAIN regress_domain_not_null;
+DROP DOMAIN regress_domain_check;
+DROP DOMAIN "regress_domain with space";
+DROP DOMAIN regress_comprehensive_domain;
+DROP DOMAIN regress_test_schema.regress_schema_domain;
+DROP SCHEMA regress_test_schema;
+DROP DOMAIN regress_address_domain;
+DROP TYPE regress_address_type;
+DROP DOMAIN regress_int_array_domain;
+DROP DOMAIN regress_precise_numeric;
+DROP DOMAIN regress_seq_domain;
+DROP SEQUENCE regress_test_seq_renamed;
+DROP DOMAIN regress_derived_domain;
+DROP DOMAIN regress_base_domain;
+DROP DOMAIN regress_simple_domain;
+DROP DOMAIN regress_domain_not_valid;
+DROP DOMAIN regress_domain_mixed;
+DROP DOMAIN regress_domain_with_collate;
+DROP DOMAIN public.int;
-- 
2.45.1