patch: Distinguish between unique indexes and unique constraints

Started by Josh Kupershmidtover 15 years ago5 messages
#1Josh Kupershmidt
schmiddy@gmail.com
1 attachment(s)

Addressing TODO item "Distinguish between unique indexes and unique
constraints in \d+" for psql, and picking up from thread:
http://archives.postgresql.org/message-id/8780.1271187360@sss.pgh.pa.us

Attached is a simple patch which clarifies unique constraints with
"UNIQUE CONSTRAINT" in psql's \d+ description of a table. The
appearance of unique indexes is left as-is.

== Old \d+ display ==
Indexes:
"name_uniq_constr" UNIQUE, btree (name)

== New \d+ display ==
Indexes:
"name_uniq_constr" UNIQUE CONSTRAINT, btree (name)

Josh

Attachments:

psql_constraints.patchapplication/octet-stream; name=psql_constraints.patchDownload
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 67a2b47..a6ee4ab 100644
*** a/src/bin/psql/describe.c
--- b/src/bin/psql/describe.c
*************** describeOneTableDetails(const char *sche
*** 1591,1598 ****
  						/* Label as primary key or unique (but not both) */
  						if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
  							appendPQExpBuffer(&buf, " PRIMARY KEY,");
! 						else if (strcmp(PQgetvalue(result, i, 2), "t") == 0)
! 							appendPQExpBuffer(&buf, " UNIQUE,");
  
  						/* Everything after "USING" is echoed verbatim */
  						indexdef = PQgetvalue(result, i, 5);
--- 1591,1602 ----
  						/* Label as primary key or unique (but not both) */
  						if (strcmp(PQgetvalue(result, i, 1), "t") == 0)
  							appendPQExpBuffer(&buf, " PRIMARY KEY,");
! 						else if (strcmp(PQgetvalue(result, i, 2), "t") == 0) {
! 							if (verbose && strcmp(PQgetvalue(result, i, 7), "u") == 0)
! 								appendPQExpBuffer(&buf, " UNIQUE CONSTRAINT,");
! 							else
! 								appendPQExpBuffer(&buf, " UNIQUE,");
! 						}
  
  						/* Everything after "USING" is echoed verbatim */
  						indexdef = PQgetvalue(result, i, 5);
#2Robert Haas
robertmhaas@gmail.com
In reply to: Josh Kupershmidt (#1)
Re: patch: Distinguish between unique indexes and unique constraints

On Sat, Apr 17, 2010 at 11:53 PM, Josh Kupershmidt <schmiddy@gmail.com> wrote:

Addressing TODO item "Distinguish between unique indexes and unique
constraints in \d+" for psql, and picking up from thread:
http://archives.postgresql.org/message-id/8780.1271187360@sss.pgh.pa.us

Attached is a simple patch which clarifies unique constraints with
"UNIQUE CONSTRAINT" in psql's \d+ description of a table. The
appearance of unique indexes is left as-is.

== Old \d+ display ==
Indexes:
   "name_uniq_constr" UNIQUE, btree (name)

== New \d+ display ==
Indexes:
   "name_uniq_constr" UNIQUE CONSTRAINT, btree (name)

You know, I've never really understood the difference between these
two types of things, or why we need to support both. Which may be
just because I'm slow?

...Robert

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#2)
Re: patch: Distinguish between unique indexes and unique constraints

Robert Haas <robertmhaas@gmail.com> writes:

You know, I've never really understood the difference between these
two types of things, or why we need to support both. Which may be
just because I'm slow?

Unique constraints are defined by the SQL standard, and have a syntax
that can't support a lot of the extensions that CREATE INDEX allows.
There's also restrictions in the information_schema views.
So unifying the two concepts completely would be a mess.

regards, tom lane

#4Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#3)
Re: patch: Distinguish between unique indexes and unique constraints

On Sun, Apr 18, 2010 at 11:23 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Robert Haas <robertmhaas@gmail.com> writes:

You know, I've never really understood the difference between these
two types of things, or why we need to support both.  Which may be
just because I'm slow?

Unique constraints are defined by the SQL standard, and have a syntax
that can't support a lot of the extensions that CREATE INDEX allows.
There's also restrictions in the information_schema views.
So unifying the two concepts completely would be a mess.

I thought it might be something like that.

Josh - you may want to add your patch here:

https://commitfest.postgresql.org/action/commitfest_view/open

...Robert

#5Josh Kupershmidt
schmiddy@gmail.com
In reply to: Robert Haas (#4)
Re: patch: Distinguish between unique indexes and unique constraints

On Sun, Apr 18, 2010 at 11:41 AM, Robert Haas <robertmhaas@gmail.com> wrote:

Josh - you may want to add your patch here:

https://commitfest.postgresql.org/action/commitfest_view/open

Added, thanks!

Josh