2007-04-02 05:49:42 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_enum.c
|
|
|
|
* routines to support manipulation of the pg_enum relation
|
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Copyright (c) 2006-2012, PostgreSQL Global Development Group
|
2007-04-02 05:49:42 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/catalog/pg_enum.c
|
2007-04-02 05:49:42 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "access/xact.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
#include "catalog/catalog.h"
|
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/pg_enum.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "storage/lmgr.h"
|
2011-04-25 18:00:21 +02:00
|
|
|
#include "miscadmin.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/fmgroids.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "utils/syscache.h"
|
2008-03-26 22:10:39 +01:00
|
|
|
#include "utils/tqual.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
|
2011-01-07 04:44:57 +01:00
|
|
|
/* Potentially set by contrib/pg_upgrade_support functions */
|
2011-04-10 17:42:00 +02:00
|
|
|
Oid binary_upgrade_next_pg_enum_oid = InvalidOid;
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
static void RenumberEnumType(Relation pg_enum, HeapTuple *existing, int nelems);
|
2007-04-02 05:49:42 +02:00
|
|
|
static int oid_cmp(const void *p1, const void *p2);
|
2010-10-25 05:04:37 +02:00
|
|
|
static int sort_order_cmp(const void *p1, const void *p2);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EnumValuesCreate
|
|
|
|
* Create an entry in pg_enum for each of the supplied enum values.
|
|
|
|
*
|
|
|
|
* vals is a list of Value strings.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-25 05:04:37 +02:00
|
|
|
EnumValuesCreate(Oid enumTypeOid, List *vals)
|
2007-04-02 05:49:42 +02:00
|
|
|
{
|
|
|
|
Relation pg_enum;
|
|
|
|
NameData enumlabel;
|
|
|
|
Oid *oids;
|
2009-12-24 23:17:58 +01:00
|
|
|
int elemno,
|
|
|
|
num_elems;
|
2007-04-02 05:49:42 +02:00
|
|
|
Datum values[Natts_pg_enum];
|
2008-11-02 02:45:28 +01:00
|
|
|
bool nulls[Natts_pg_enum];
|
2007-04-02 05:49:42 +02:00
|
|
|
ListCell *lc;
|
2007-11-15 22:14:46 +01:00
|
|
|
HeapTuple tup;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2009-12-24 23:17:58 +01:00
|
|
|
num_elems = list_length(vals);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* We do not bother to check the list of values for duplicates --- if you
|
|
|
|
* have any, you'll get a less-than-friendly unique-index violation. It is
|
|
|
|
* probably not worth trying harder.
|
2007-04-02 05:49:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
pg_enum = heap_open(EnumRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
/*
|
2010-10-25 05:04:37 +02:00
|
|
|
* Allocate OIDs for the enum's members.
|
|
|
|
*
|
|
|
|
* While this method does not absolutely guarantee that we generate no
|
2011-04-10 17:42:00 +02:00
|
|
|
* duplicate OIDs (since we haven't entered each oid into the table before
|
|
|
|
* allocating the next), trouble could only occur if the OID counter wraps
|
|
|
|
* all the way around before we finish. Which seems unlikely.
|
2007-04-02 05:49:42 +02:00
|
|
|
*/
|
2009-12-24 23:17:58 +01:00
|
|
|
oids = (Oid *) palloc(num_elems * sizeof(Oid));
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
for (elemno = 0; elemno < num_elems; elemno++)
|
2007-04-02 05:49:42 +02:00
|
|
|
{
|
2009-12-19 01:47:57 +01:00
|
|
|
/*
|
2010-10-25 05:04:37 +02:00
|
|
|
* We assign even-numbered OIDs to all the new enum labels. This
|
|
|
|
* tells the comparison functions the OIDs are in the correct sort
|
|
|
|
* order and can be compared directly.
|
2009-12-19 01:47:57 +01:00
|
|
|
*/
|
2011-04-10 17:42:00 +02:00
|
|
|
Oid new_oid;
|
2010-10-25 05:04:37 +02:00
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
do
|
|
|
|
{
|
2010-10-25 05:04:37 +02:00
|
|
|
new_oid = GetNewOid(pg_enum);
|
|
|
|
} while (new_oid & 1);
|
|
|
|
oids[elemno] = new_oid;
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/* sort them, just in case OID counter wrapped from high to low */
|
|
|
|
qsort(oids, num_elems, sizeof(Oid), oid_cmp);
|
|
|
|
|
2007-04-02 05:49:42 +02:00
|
|
|
/* and make the entries */
|
2008-11-02 02:45:28 +01:00
|
|
|
memset(nulls, false, sizeof(nulls));
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2009-12-24 23:17:58 +01:00
|
|
|
elemno = 0;
|
2007-04-02 05:49:42 +02:00
|
|
|
foreach(lc, vals)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
char *lab = strVal(lfirst(lc));
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/*
|
2007-04-03 00:14:17 +02:00
|
|
|
* labels are stored in a name field, for easier syscache lookup, so
|
|
|
|
* check the length to make sure it's within range.
|
|
|
|
*/
|
|
|
|
if (strlen(lab) > (NAMEDATALEN - 1))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_NAME),
|
2008-01-20 18:50:41 +01:00
|
|
|
errmsg("invalid enum label \"%s\"", lab),
|
|
|
|
errdetail("Labels must be %d characters or less.",
|
|
|
|
NAMEDATALEN - 1)));
|
2007-04-03 00:14:17 +02:00
|
|
|
|
2007-04-02 05:49:42 +02:00
|
|
|
values[Anum_pg_enum_enumtypid - 1] = ObjectIdGetDatum(enumTypeOid);
|
2010-10-25 05:04:37 +02:00
|
|
|
values[Anum_pg_enum_enumsortorder - 1] = Float4GetDatum(elemno + 1);
|
2007-04-02 05:49:42 +02:00
|
|
|
namestrcpy(&enumlabel, lab);
|
|
|
|
values[Anum_pg_enum_enumlabel - 1] = NameGetDatum(&enumlabel);
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
tup = heap_form_tuple(RelationGetDescr(pg_enum), values, nulls);
|
2009-12-24 23:17:58 +01:00
|
|
|
HeapTupleSetOid(tup, oids[elemno]);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
simple_heap_insert(pg_enum, tup);
|
|
|
|
CatalogUpdateIndexes(pg_enum, tup);
|
|
|
|
heap_freetuple(tup);
|
|
|
|
|
2009-12-24 23:17:58 +01:00
|
|
|
elemno++;
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
pfree(oids);
|
|
|
|
heap_close(pg_enum, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EnumValuesDelete
|
|
|
|
* Remove all the pg_enum entries for the specified enum type.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
EnumValuesDelete(Oid enumTypeOid)
|
|
|
|
{
|
|
|
|
Relation pg_enum;
|
|
|
|
ScanKeyData key[1];
|
|
|
|
SysScanDesc scan;
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
pg_enum = heap_open(EnumRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_enum_enumtypid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(enumTypeOid));
|
|
|
|
|
|
|
|
scan = systable_beginscan(pg_enum, EnumTypIdLabelIndexId, true,
|
|
|
|
SnapshotNow, 1, key);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(tup = systable_getnext(scan)))
|
|
|
|
{
|
|
|
|
simple_heap_delete(pg_enum, &tup->t_self);
|
|
|
|
}
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
|
|
|
heap_close(pg_enum, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/*
|
|
|
|
* AddEnumLabel
|
|
|
|
* Add a new label to the enum set. By default it goes at
|
|
|
|
* the end, but the user can choose to place it before or
|
|
|
|
* after any existing set member.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AddEnumLabel(Oid enumTypeOid,
|
|
|
|
const char *newVal,
|
|
|
|
const char *neighbor,
|
|
|
|
bool newValIsAfter)
|
|
|
|
{
|
|
|
|
Relation pg_enum;
|
|
|
|
Oid newOid;
|
|
|
|
Datum values[Natts_pg_enum];
|
|
|
|
bool nulls[Natts_pg_enum];
|
|
|
|
NameData enumlabel;
|
|
|
|
HeapTuple enum_tup;
|
|
|
|
float4 newelemorder;
|
|
|
|
HeapTuple *existing;
|
|
|
|
CatCList *list;
|
|
|
|
int nelems;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* check length of new label is ok */
|
|
|
|
if (strlen(newVal) > (NAMEDATALEN - 1))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_NAME),
|
|
|
|
errmsg("invalid enum label \"%s\"", newVal),
|
|
|
|
errdetail("Labels must be %d characters or less.",
|
|
|
|
NAMEDATALEN - 1)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire a lock on the enum type, which we won't release until commit.
|
|
|
|
* This ensures that two backends aren't concurrently modifying the same
|
2011-04-10 17:42:00 +02:00
|
|
|
* enum type. Without that, we couldn't be sure to get a consistent view
|
|
|
|
* of the enum members via the syscache. Note that this does not block
|
|
|
|
* other backends from inspecting the type; see comments for
|
2010-10-25 05:04:37 +02:00
|
|
|
* RenumberEnumType.
|
|
|
|
*/
|
|
|
|
LockDatabaseObject(TypeRelationId, enumTypeOid, 0, ExclusiveLock);
|
|
|
|
|
|
|
|
pg_enum = heap_open(EnumRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
/* If we have to renumber the existing members, we restart from here */
|
|
|
|
restart:
|
|
|
|
|
|
|
|
/* Get the list of existing members of the enum */
|
|
|
|
list = SearchSysCacheList1(ENUMTYPOIDNAME,
|
|
|
|
ObjectIdGetDatum(enumTypeOid));
|
2011-04-10 17:42:00 +02:00
|
|
|
nelems = list->n_members;
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
/* Sort the existing members by enumsortorder */
|
|
|
|
existing = (HeapTuple *) palloc(nelems * sizeof(HeapTuple));
|
|
|
|
for (i = 0; i < nelems; i++)
|
|
|
|
existing[i] = &(list->members[i]->tuple);
|
|
|
|
|
|
|
|
qsort(existing, nelems, sizeof(HeapTuple), sort_order_cmp);
|
|
|
|
|
|
|
|
if (neighbor == NULL)
|
|
|
|
{
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Put the new label at the end of the list. No change to existing
|
|
|
|
* tuples is required.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
if (nelems > 0)
|
|
|
|
{
|
|
|
|
Form_pg_enum en = (Form_pg_enum) GETSTRUCT(existing[nelems - 1]);
|
|
|
|
|
|
|
|
newelemorder = en->enumsortorder + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
newelemorder = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* BEFORE or AFTER was specified */
|
2011-04-10 17:42:00 +02:00
|
|
|
int nbr_index;
|
|
|
|
int other_nbr_index;
|
|
|
|
Form_pg_enum nbr_en;
|
|
|
|
Form_pg_enum other_nbr_en;
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
/* Locate the neighbor element */
|
|
|
|
for (nbr_index = 0; nbr_index < nelems; nbr_index++)
|
|
|
|
{
|
|
|
|
Form_pg_enum en = (Form_pg_enum) GETSTRUCT(existing[nbr_index]);
|
|
|
|
|
|
|
|
if (strcmp(NameStr(en->enumlabel), neighbor) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nbr_index >= nelems)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("\"%s\" is not an existing enum label",
|
|
|
|
neighbor)));
|
|
|
|
nbr_en = (Form_pg_enum) GETSTRUCT(existing[nbr_index]);
|
|
|
|
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Attempt to assign an appropriate enumsortorder value: one less than
|
|
|
|
* the smallest member, one more than the largest member, or halfway
|
|
|
|
* between two existing members.
|
2010-10-25 05:04:37 +02:00
|
|
|
*
|
|
|
|
* In the "halfway" case, because of the finite precision of float4,
|
2011-04-10 17:42:00 +02:00
|
|
|
* we might compute a value that's actually equal to one or the other
|
|
|
|
* of its neighbors. In that case we renumber the existing members
|
|
|
|
* and try again.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
if (newValIsAfter)
|
|
|
|
other_nbr_index = nbr_index + 1;
|
|
|
|
else
|
|
|
|
other_nbr_index = nbr_index - 1;
|
|
|
|
|
|
|
|
if (other_nbr_index < 0)
|
|
|
|
newelemorder = nbr_en->enumsortorder - 1;
|
|
|
|
else if (other_nbr_index >= nelems)
|
|
|
|
newelemorder = nbr_en->enumsortorder + 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
other_nbr_en = (Form_pg_enum) GETSTRUCT(existing[other_nbr_index]);
|
|
|
|
newelemorder = (nbr_en->enumsortorder +
|
|
|
|
other_nbr_en->enumsortorder) / 2;
|
2010-10-25 07:13:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* On some machines, newelemorder may be in a register that's
|
2011-04-10 17:42:00 +02:00
|
|
|
* wider than float4. We need to force it to be rounded to float4
|
|
|
|
* precision before making the following comparisons, or we'll get
|
|
|
|
* wrong results. (Such behavior violates the C standard, but
|
|
|
|
* fixing the compilers is out of our reach.)
|
2010-10-25 07:13:22 +02:00
|
|
|
*/
|
|
|
|
newelemorder = DatumGetFloat4(Float4GetDatum(newelemorder));
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (newelemorder == nbr_en->enumsortorder ||
|
|
|
|
newelemorder == other_nbr_en->enumsortorder)
|
|
|
|
{
|
|
|
|
RenumberEnumType(pg_enum, existing, nelems);
|
|
|
|
/* Clean up and start over */
|
|
|
|
pfree(existing);
|
|
|
|
ReleaseCatCacheList(list);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a new OID for the new label */
|
2011-04-25 18:00:21 +02:00
|
|
|
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_pg_enum_oid))
|
2010-10-25 05:04:37 +02:00
|
|
|
{
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Use binary-upgrade override for pg_enum.oid, if supplied. During
|
|
|
|
* binary upgrade, all pg_enum.oid's are set this way so they are
|
|
|
|
* guaranteed to be consistent.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
if (neighbor != NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("ALTER TYPE ADD BEFORE/AFTER is incompatible with binary upgrade")));
|
|
|
|
|
|
|
|
newOid = binary_upgrade_next_pg_enum_oid;
|
|
|
|
binary_upgrade_next_pg_enum_oid = InvalidOid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Normal case: we need to allocate a new Oid for the value.
|
|
|
|
*
|
|
|
|
* We want to give the new element an even-numbered Oid if it's safe,
|
|
|
|
* which is to say it compares correctly to all pre-existing even
|
|
|
|
* numbered Oids in the enum. Otherwise, we must give it an odd Oid.
|
|
|
|
*/
|
|
|
|
for (;;)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
bool sorts_ok;
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
/* Get a new OID (different from all existing pg_enum tuples) */
|
|
|
|
newOid = GetNewOid(pg_enum);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detect whether it sorts correctly relative to existing
|
|
|
|
* even-numbered labels of the enum. We can ignore existing
|
2011-04-10 17:42:00 +02:00
|
|
|
* labels with odd Oids, since a comparison involving one of those
|
|
|
|
* will not take the fast path anyway.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
sorts_ok = true;
|
|
|
|
for (i = 0; i < nelems; i++)
|
|
|
|
{
|
|
|
|
HeapTuple exists_tup = existing[i];
|
|
|
|
Form_pg_enum exists_en = (Form_pg_enum) GETSTRUCT(exists_tup);
|
|
|
|
Oid exists_oid = HeapTupleGetOid(exists_tup);
|
|
|
|
|
|
|
|
if (exists_oid & 1)
|
|
|
|
continue; /* ignore odd Oids */
|
|
|
|
|
|
|
|
if (exists_en->enumsortorder < newelemorder)
|
|
|
|
{
|
|
|
|
/* should sort before */
|
|
|
|
if (exists_oid >= newOid)
|
|
|
|
{
|
|
|
|
sorts_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* should sort after */
|
|
|
|
if (exists_oid <= newOid)
|
|
|
|
{
|
|
|
|
sorts_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sorts_ok)
|
|
|
|
{
|
|
|
|
/* If it's even and sorts OK, we're done. */
|
|
|
|
if ((newOid & 1) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* If it's odd, and sorts OK, loop back to get another OID and
|
|
|
|
* try again. Probably, the next available even OID will sort
|
|
|
|
* correctly too, so it's worth trying.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If it's odd, and does not sort correctly, we're done.
|
|
|
|
* (Probably, the next available even OID would sort
|
|
|
|
* incorrectly too, so no point in trying again.)
|
|
|
|
*/
|
|
|
|
if (newOid & 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's even, and does not sort correctly, loop back to get
|
|
|
|
* another OID and try again. (We *must* reject this case.)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done with info about existing members */
|
|
|
|
pfree(existing);
|
|
|
|
ReleaseCatCacheList(list);
|
|
|
|
|
|
|
|
/* Create the new pg_enum entry */
|
|
|
|
memset(nulls, false, sizeof(nulls));
|
|
|
|
values[Anum_pg_enum_enumtypid - 1] = ObjectIdGetDatum(enumTypeOid);
|
|
|
|
values[Anum_pg_enum_enumsortorder - 1] = Float4GetDatum(newelemorder);
|
|
|
|
namestrcpy(&enumlabel, newVal);
|
|
|
|
values[Anum_pg_enum_enumlabel - 1] = NameGetDatum(&enumlabel);
|
|
|
|
enum_tup = heap_form_tuple(RelationGetDescr(pg_enum), values, nulls);
|
|
|
|
HeapTupleSetOid(enum_tup, newOid);
|
|
|
|
simple_heap_insert(pg_enum, enum_tup);
|
|
|
|
CatalogUpdateIndexes(pg_enum, enum_tup);
|
|
|
|
heap_freetuple(enum_tup);
|
|
|
|
|
|
|
|
heap_close(pg_enum, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RenumberEnumType
|
|
|
|
* Renumber existing enum elements to have sort positions 1..n.
|
|
|
|
*
|
|
|
|
* We avoid doing this unless absolutely necessary; in most installations
|
|
|
|
* it will never happen. The reason is that updating existing pg_enum
|
|
|
|
* entries creates hazards for other backends that are concurrently reading
|
2011-04-10 17:42:00 +02:00
|
|
|
* pg_enum with SnapshotNow semantics. A concurrent SnapshotNow scan could
|
2010-10-25 05:04:37 +02:00
|
|
|
* see both old and new versions of an updated row as valid, or neither of
|
|
|
|
* them, if the commit happens between scanning the two versions. It's
|
|
|
|
* also quite likely for a concurrent scan to see an inconsistent set of
|
|
|
|
* rows (some members updated, some not).
|
|
|
|
*
|
|
|
|
* We can avoid these risks by reading pg_enum with an MVCC snapshot
|
|
|
|
* instead of SnapshotNow, but that forecloses use of the syscaches.
|
|
|
|
* We therefore make the following choices:
|
|
|
|
*
|
|
|
|
* 1. Any code that is interested in the enumsortorder values MUST read
|
|
|
|
* pg_enum with an MVCC snapshot, or else acquire lock on the enum type
|
|
|
|
* to prevent concurrent execution of AddEnumLabel(). The risk of
|
|
|
|
* seeing inconsistent values of enumsortorder is too high otherwise.
|
|
|
|
*
|
|
|
|
* 2. Code that is not examining enumsortorder can use a syscache
|
|
|
|
* (for example, enum_in and enum_out do so). The worst that can happen
|
|
|
|
* is a transient failure to find any valid value of the row. This is
|
|
|
|
* judged acceptable in view of the infrequency of use of RenumberEnumType.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
RenumberEnumType(Relation pg_enum, HeapTuple *existing, int nelems)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We should only need to increase existing elements' enumsortorders,
|
|
|
|
* never decrease them. Therefore, work from the end backwards, to avoid
|
|
|
|
* unwanted uniqueness violations.
|
|
|
|
*/
|
|
|
|
for (i = nelems - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
HeapTuple newtup;
|
|
|
|
Form_pg_enum en;
|
|
|
|
float4 newsortorder;
|
|
|
|
|
|
|
|
newtup = heap_copytuple(existing[i]);
|
|
|
|
en = (Form_pg_enum) GETSTRUCT(newtup);
|
|
|
|
|
|
|
|
newsortorder = i + 1;
|
|
|
|
if (en->enumsortorder != newsortorder)
|
|
|
|
{
|
|
|
|
en->enumsortorder = newsortorder;
|
|
|
|
|
|
|
|
simple_heap_update(pg_enum, &newtup->t_self, newtup);
|
|
|
|
|
|
|
|
CatalogUpdateIndexes(pg_enum, newtup);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_freetuple(newtup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the updates visible */
|
|
|
|
CommandCounterIncrement();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* qsort comparison function for oids */
|
2007-04-02 05:49:42 +02:00
|
|
|
static int
|
|
|
|
oid_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid v1 = *((const Oid *) p1);
|
|
|
|
Oid v2 = *((const Oid *) p2);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
if (v1 < v2)
|
|
|
|
return -1;
|
|
|
|
if (v1 > v2)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
/* qsort comparison function for tuples by sort order */
|
|
|
|
static int
|
|
|
|
sort_order_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
HeapTuple v1 = *((const HeapTuple *) p1);
|
|
|
|
HeapTuple v2 = *((const HeapTuple *) p2);
|
|
|
|
Form_pg_enum en1 = (Form_pg_enum) GETSTRUCT(v1);
|
|
|
|
Form_pg_enum en2 = (Form_pg_enum) GETSTRUCT(v2);
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
if (en1->enumsortorder < en2->enumsortorder)
|
|
|
|
return -1;
|
|
|
|
else if (en1->enumsortorder > en2->enumsortorder)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|