2011-02-12 14:54:13 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_collation.c
|
|
|
|
* routines to support manipulation of the pg_collation relation
|
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
2011-02-12 14:54:13 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/catalog/pg_collation.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
#include "access/genam.h"
|
2011-02-12 14:54:13 +01:00
|
|
|
#include "access/heapam.h"
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2011-02-12 14:54:13 +01:00
|
|
|
#include "access/sysattr.h"
|
|
|
|
#include "catalog/dependency.h"
|
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/objectaccess.h"
|
|
|
|
#include "catalog/pg_collation.h"
|
|
|
|
#include "catalog/pg_collation_fn.h"
|
|
|
|
#include "catalog/pg_namespace.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/fmgroids.h"
|
2011-03-26 04:10:07 +01:00
|
|
|
#include "utils/rel.h"
|
2011-02-12 14:54:13 +01:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
#include "utils/tqual.h"
|
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
/*
|
|
|
|
* CollationCreate
|
|
|
|
*
|
|
|
|
* Add a new tuple to pg_collation.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
CollationCreate(const char *collname, Oid collnamespace,
|
|
|
|
Oid collowner,
|
|
|
|
int32 collencoding,
|
|
|
|
const char *collcollate, const char *collctype)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
TupleDesc tupDesc;
|
|
|
|
HeapTuple tup;
|
|
|
|
Datum values[Natts_pg_collation];
|
2011-03-11 19:20:11 +01:00
|
|
|
bool nulls[Natts_pg_collation];
|
2011-04-10 17:42:00 +02:00
|
|
|
NameData name_name,
|
|
|
|
name_collate,
|
|
|
|
name_ctype;
|
2011-02-12 14:54:13 +01:00
|
|
|
Oid oid;
|
|
|
|
ObjectAddress myself,
|
|
|
|
referenced;
|
|
|
|
|
|
|
|
AssertArg(collname);
|
|
|
|
AssertArg(collnamespace);
|
|
|
|
AssertArg(collowner);
|
|
|
|
AssertArg(collcollate);
|
|
|
|
AssertArg(collctype);
|
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
/*
|
|
|
|
* Make sure there is no existing collation of same name & encoding.
|
|
|
|
*
|
2011-04-10 17:42:00 +02:00
|
|
|
* This would be caught by the unique index anyway; we're just giving a
|
|
|
|
* friendlier error message. The unique index provides a backstop against
|
|
|
|
* race conditions.
|
2011-03-11 19:20:11 +01:00
|
|
|
*/
|
2011-02-12 14:54:13 +01:00
|
|
|
if (SearchSysCacheExists3(COLLNAMEENCNSP,
|
|
|
|
PointerGetDatum(collname),
|
|
|
|
Int32GetDatum(collencoding),
|
|
|
|
ObjectIdGetDatum(collnamespace)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("collation \"%s\" for encoding \"%s\" already exists",
|
|
|
|
collname, pg_encoding_to_char(collencoding))));
|
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Also forbid matching an any-encoding entry. This test of course is not
|
2011-04-10 17:42:00 +02:00
|
|
|
* backed up by the unique index, but it's not a problem since we don't
|
|
|
|
* support adding any-encoding entries after initdb.
|
2011-03-11 19:20:11 +01:00
|
|
|
*/
|
|
|
|
if (SearchSysCacheExists3(COLLNAMEENCNSP,
|
|
|
|
PointerGetDatum(collname),
|
|
|
|
Int32GetDatum(-1),
|
|
|
|
ObjectIdGetDatum(collnamespace)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("collation \"%s\" already exists",
|
|
|
|
collname)));
|
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
/* open pg_collation */
|
|
|
|
rel = heap_open(CollationRelationId, RowExclusiveLock);
|
2011-03-11 19:20:11 +01:00
|
|
|
tupDesc = RelationGetDescr(rel);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
|
|
|
/* form a tuple */
|
2011-03-11 19:20:11 +01:00
|
|
|
memset(nulls, 0, sizeof(nulls));
|
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
namestrcpy(&name_name, collname);
|
|
|
|
values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
|
|
|
|
values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
|
|
|
|
values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
|
|
|
|
values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
|
|
|
|
namestrcpy(&name_collate, collcollate);
|
|
|
|
values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
|
|
|
|
namestrcpy(&name_ctype, collctype);
|
|
|
|
values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
|
|
|
|
|
|
|
|
tup = heap_form_tuple(tupDesc, values, nulls);
|
|
|
|
|
|
|
|
/* insert a new tuple */
|
|
|
|
oid = simple_heap_insert(rel, tup);
|
|
|
|
Assert(OidIsValid(oid));
|
|
|
|
|
|
|
|
/* update the index if any */
|
|
|
|
CatalogUpdateIndexes(rel, tup);
|
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
/* set up dependencies for the new collation */
|
2011-02-12 14:54:13 +01:00
|
|
|
myself.classId = CollationRelationId;
|
2011-03-11 19:20:11 +01:00
|
|
|
myself.objectId = oid;
|
2011-02-12 14:54:13 +01:00
|
|
|
myself.objectSubId = 0;
|
|
|
|
|
|
|
|
/* create dependency on namespace */
|
|
|
|
referenced.classId = NamespaceRelationId;
|
|
|
|
referenced.objectId = collnamespace;
|
|
|
|
referenced.objectSubId = 0;
|
|
|
|
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
|
|
|
|
|
|
|
|
/* create dependency on owner */
|
|
|
|
recordDependencyOnOwner(CollationRelationId, HeapTupleGetOid(tup),
|
|
|
|
collowner);
|
|
|
|
|
|
|
|
/* dependency on extension */
|
2011-07-23 22:59:39 +02:00
|
|
|
recordDependencyOnCurrentExtension(&myself, false);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
|
|
|
/* Post creation hook for new collation */
|
2013-03-07 02:52:06 +01:00
|
|
|
InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
|
|
|
heap_freetuple(tup);
|
|
|
|
heap_close(rel, RowExclusiveLock);
|
|
|
|
|
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RemoveCollationById
|
|
|
|
*
|
|
|
|
* Remove a tuple from pg_collation by Oid. This function is solely
|
|
|
|
* called inside catalog/dependency.c
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RemoveCollationById(Oid collationOid)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
ScanKeyData scanKeyData;
|
2011-03-11 19:20:11 +01:00
|
|
|
SysScanDesc scandesc;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
|
|
|
rel = heap_open(CollationRelationId, RowExclusiveLock);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
|
|
|
ScanKeyInit(&scanKeyData,
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(collationOid));
|
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
scandesc = systable_beginscan(rel, CollationOidIndexId, true,
|
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of
concurrent updates, the scan can fail to see either the old or the new
versions of the row. In many cases, we work around this by requiring
DDL operations to hold AccessExclusiveLock on the object being
modified; in some cases, the existing locking is inadequate and random
failures occur as a result. This commit doesn't change anything
related to locking, but will hopefully pave the way to allowing lock
strength reductions in the future.
The major issue has held us back from making this change in the past
is that taking an MVCC snapshot is significantly more expensive than
using a static special snapshot such as SnapshotNow. However, testing
of various worst-case scenarios reveals that this problem is not
severe except under fairly extreme workloads. To mitigate those
problems, we avoid retaking the MVCC snapshot for each new scan;
instead, we take a new snapshot only when invalidation messages have
been processed. The catcache machinery already requires that
invalidation messages be sent before releasing the related heavyweight
lock; else other backends might rely on locally-cached data rather
than scanning the catalog at all. Thus, making snapshot reuse
dependent on the same guarantees shouldn't break anything that wasn't
already subtly broken.
Patch by me. Review by Michael Paquier and Andres Freund.
2013-07-02 15:47:01 +02:00
|
|
|
NULL, 1, &scanKeyData);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
tuple = systable_getnext(scandesc);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
2011-03-11 19:20:11 +01:00
|
|
|
if (HeapTupleIsValid(tuple))
|
2011-02-12 14:54:13 +01:00
|
|
|
simple_heap_delete(rel, &tuple->t_self);
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not find tuple for collation %u", collationOid);
|
2011-03-11 19:20:11 +01:00
|
|
|
|
|
|
|
systable_endscan(scandesc);
|
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
heap_close(rel, RowExclusiveLock);
|
|
|
|
}
|