Re: get a relations DDL server-side

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jordan Deitch" <jd(at)rsa(dot)pub>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: get a relations DDL server-side
Date: 2020-01-31 20:01:25
Message-ID: 5519.1580500885@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Jordan Deitch" <jd(at)rsa(dot)pub> writes:
> I would like to introduce the ability to get object DDL (server-side) by introducing a new function with roughly the following prototype:
> get_ddl(regclass)
> RETURNS text
> LANGUAGE C STRICT PARALLEL SAFE;

Umm ... "regclass" would only be appropriate for relations.

If you actually want to support more than one type of object with a single
function, you'll need two OIDs. Catalog's OID and object's OID are the
usual choices, per pg_describe_object() and similar functions.

I don't think "get_ddl" is a particularly apt function name, either.
It ignores the precedent of existing functions with essentially this
same functionality, such as pg_get_triggerdef(), pg_get_constraintdef(),
etc. One wonders why duplicate that existing functionality, so maybe
you should think about adding per-object-type functions instead of
trying to make one function to rule them all.

The larger reason why this doesn't exist already, BTW, is that we've
tended to find that it's not all that useful to get back a monolithic
chunk of DDL text for complicated objects such as tables. You should
provide a little more clarity as to what use-case you foresee, because
otherwise there are just a *whole* lot of things that aren't clear.
Some examples:

* Should the output include a CREATE COMMENT if the object has a comment?
* What about ownership and ACL (grants)?
* On tables, are foreign keys part of the table, or are they distinct
objects? (Hint: both answers can be correct depending on use-case)
* How about indexes, and do you want to treat constraint indexes
differently from other ones? (Constraint indexes *could* be made
part of the table's DDL, but other indexes need a separate CREATE)
* Do you need options, such as whether to pretty-print expressions?

You might also find it instructive to dig through the archives for
past discussions about moving more of pg_dump's logic into the server;
that's the area where this has come up over and over.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2020-01-31 20:59:26 widen vacuum buffer counters
Previous Message Tom Lane 2020-01-31 19:45:14 Re: pg_restore crash when there is a failure before all child process is created