Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement - Mailing list pgsql-hackers

From Haritabh Gupta
Subject Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement
Date
Msg-id 177145861697.626.9698606909114409697.pgcf@coridan.postgresql.org
Whole thread Raw
In response to Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement  (Florin Irion <irionr@gmail.com>)
Responses Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement
List pgsql-hackers
Hi Florin,

Thanks for addressing the comments. I tested v7 and found that 
type modifiers (typmod) are lost in the base type output.

In build_create_domain_statement:

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

generate_qualified_type_name does not include the type modifier,
so domains over types like varchar(N), numeric(P,S), char(N), bit(N),
time(N) etc. silently lose their modifiers. The generated DDL does not
roundtrip correctly.

create domain d1 as varchar(100);
select pg_get_domain_ddl('d1');
                    pg_get_domain_ddl
----------------------------------------------------------
 CREATE DOMAIN public.d1 AS pg_catalog."varchar";
(1 row)

we should expect AS character varying(100).

Roundtrip confirms the semantic change:

```
select length(repeat('x', 150)::d1);  -- returns 100 (truncated)

drop domain d1;
-- re-execute generated DDL
create domain public.d1 as pg_catalog."varchar";

select length(repeat('x', 150)::d1);  -- returns 150 (not truncated)
```

Same issue with numeric(15,2): rounds to 2 decimals before roundtrip,
full precision after. Also confirmed with char(1), bit(8), time(3),
varbit(256).

I think for the base type we could use format_type_extended with
both FORMAT_TYPE_TYPEMOD_GIVEN and FORMAT_TYPE_FORCE_QUALIFY:

appendStringInfo(buf, "CREATE DOMAIN %s AS %s",
                 generate_qualified_type_name(typForm->oid),
                 format_type_extended(typForm->typbasetype,
                                     typForm->typtypmod,
                                     FORMAT_TYPE_TYPEMOD_GIVEN |
                                     FORMAT_TYPE_FORCE_QUALIFY));

Regards,
Haritabh

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: @remove-line-for-nolocal@ is dead code
Next
From: Tom Lane
Date:
Subject: Re: [PATCH] pg_get_domain_ddl: DDL reconstruction function for CREATE DOMAIN statement