0

PostgreSQL provides the ability to magically generate constraint names in statements like CREATE TABLE, ALTER TABLE if none are provided explicitly. The naming convention is well known and I personally like it very much. But how stable and official is it? Is it something which one can rely on for different major releases or even the next 50 years?

I always had the impression that this is an implementation detail and while a lot of people rely on it, one shouldn't and always use explicit names to properly document things instead. I think I've read something like that in the official documentation in the past, but couldn't find it anymore...

So is there a definitive, official statement how reliable this naming scheme is or if users should always try to provide explicit names?

Community
  • 1
  • 1
Thorsten Schöning
  • 3,501
  • 2
  • 25
  • 46
  • You should probably ask this on one of the Postgres mailing lists because then you'd get an answer directly from the Postgres developers –  Aug 19 '15 at 09:06

1 Answers1

2

Strictly, if it's not in the documentation, you should not rely on it.

The docs only say:

If you don't specify a constraint name in this way, the system chooses a name for you.

so strictly I should recommend not baking the constraint names into the application unless you specify them explicitly in the SQL. This will also make the connection more apparent when reading the SQL - you bothered to specify constraint names for a reason.

That said, constraint name generation has not AFAIK changed, at least since I started using Pg around 7.4. So while it's not part of the official documented API, it's probably also not especially bad to rely on it. Also, constraint names are always going to be preserved by pg_dump and pg_upgrade, so it likely doesn't matter much unless you are doing a clean reload into a new version that has changed default constraint name generation.

TL;DR: It doesn't look like they're officially defined and documented, but they're unlikely to change, and if they do the impact is minimal. So relying on them is probably OK. Just document that in the app.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778