diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2012-09-22 18:35:22 -0400 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2012-09-22 18:35:22 -0400 |
| commit | 31510194cc9d87b355cb56e7d88c18c985d7a32a (patch) | |
| tree | bbfc0569d0e8d881d3fba8d4532257646c4b3943 /src/backend/catalog/pg_enum.c | |
| parent | fcc1576687a122059db311791e45e2c5edc5d89b (diff) | |
| download | postgresql-31510194cc9d87b355cb56e7d88c18c985d7a32a.tar.gz | |
Minor corrections for ALTER TYPE ADD VALUE IF NOT EXISTS patch.
Produce a NOTICE when the label already exists, for consistency with other
CREATE IF NOT EXISTS commands. Also, fix the code so it produces something
more user-friendly than an index violation when the label already exists.
This not incidentally enables making a regression test that the previous
patch didn't make for fear of exposing an unpredictable OID in the results.
Also some wordsmithing on the documentation.
Diffstat (limited to 'src/backend/catalog/pg_enum.c')
| -rw-r--r-- | src/backend/catalog/pg_enum.c | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/backend/catalog/pg_enum.c b/src/backend/catalog/pg_enum.c index f3161efb20..bed0f357bb 100644 --- a/src/backend/catalog/pg_enum.c +++ b/src/backend/catalog/pg_enum.c @@ -212,19 +212,30 @@ AddEnumLabel(Oid enumTypeOid, */ LockDatabaseObject(TypeRelationId, enumTypeOid, 0, ExclusiveLock); - /* Do the "IF NOT EXISTS" test if specified */ - if (skipIfExists) + /* + * Check if label is already in use. The unique index on pg_enum would + * catch this anyway, but we prefer a friendlier error message, and + * besides we need a check to support IF NOT EXISTS. + */ + enum_tup = SearchSysCache2(ENUMTYPOIDNAME, + ObjectIdGetDatum(enumTypeOid), + CStringGetDatum(newVal)); + if (HeapTupleIsValid(enum_tup)) { - HeapTuple tup; - - tup = SearchSysCache2(ENUMTYPOIDNAME, - ObjectIdGetDatum(enumTypeOid), - CStringGetDatum(newVal)); - if (HeapTupleIsValid(tup)) + ReleaseSysCache(enum_tup); + if (skipIfExists) { - ReleaseSysCache(tup); + ereport(NOTICE, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("enum label \"%s\" already exists, skipping", + newVal))); return; } + else + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("enum label \"%s\" already exists", + newVal))); } pg_enum = heap_open(EnumRelationId, RowExclusiveLock); |
