2002-04-15 07:22:04 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* typecmds.c
|
|
|
|
* Routines for SQL commands that manipulate types (and domains).
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2002-04-15 07:22:04 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-07-14 16:52:27 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.95 2006/07/14 14:52:18 momjian Exp $
|
2002-04-15 07:22:04 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* The "DefineFoo" routines take the parse tree and pick out the
|
|
|
|
* appropriate arguments/flags, passing the results to the
|
|
|
|
* corresponding "FooDefine" routines (in src/catalog) that do
|
|
|
|
* the actual catalog-munging. These routines also verify permission
|
|
|
|
* of the user to execute the command.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* These things must be defined and committed in the following order:
|
|
|
|
* "create function":
|
2003-05-09 00:19:58 +02:00
|
|
|
* input/output, recv/send functions
|
2002-04-15 07:22:04 +02:00
|
|
|
* "create type":
|
|
|
|
* type
|
|
|
|
* "create operator":
|
|
|
|
* operators
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
#include "access/genam.h"
|
2006-07-11 20:26:11 +02:00
|
|
|
#include "access/heapam.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/xact.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/dependency.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "catalog/heap.h"
|
2002-12-06 06:00:34 +01:00
|
|
|
#include "catalog/indexing.h"
|
2002-11-15 03:50:21 +01:00
|
|
|
#include "catalog/pg_constraint.h"
|
2003-01-04 01:46:08 +01:00
|
|
|
#include "catalog/pg_depend.h"
|
2005-08-01 06:03:59 +02:00
|
|
|
#include "catalog/pg_namespace.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "commands/defrem.h"
|
2002-08-15 18:36:08 +02:00
|
|
|
#include "commands/tablecmds.h"
|
2002-12-06 06:00:34 +01:00
|
|
|
#include "commands/typecmds.h"
|
|
|
|
#include "executor/executor.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "miscadmin.h"
|
2006-03-14 23:48:25 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
2003-02-03 22:15:45 +01:00
|
|
|
#include "optimizer/planmain.h"
|
2002-11-15 03:50:21 +01:00
|
|
|
#include "optimizer/var.h"
|
|
|
|
#include "parser/parse_coerce.h"
|
|
|
|
#include "parser/parse_expr.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "parser/parse_func.h"
|
|
|
|
#include "parser/parse_type.h"
|
|
|
|
#include "utils/acl.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/fmgroids.h"
|
2002-04-27 05:45:03 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2005-05-06 19:24:55 +02:00
|
|
|
#include "utils/memutils.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
|
|
|
|
/* result structure for get_rels_with_domain() */
|
|
|
|
typedef struct
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Relation rel; /* opened and locked relation */
|
|
|
|
int natts; /* number of attributes of interest */
|
|
|
|
int *atts; /* attribute numbers */
|
2003-01-04 01:46:08 +01:00
|
|
|
/* atts[] is of allocated length RelationGetNumberOfAttributes(rel) */
|
2003-08-08 23:42:59 +02:00
|
|
|
} RelToCheck;
|
2003-01-04 01:46:08 +01:00
|
|
|
|
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
static Oid findTypeInputFunction(List *procname, Oid typeOid);
|
|
|
|
static Oid findTypeOutputFunction(List *procname, Oid typeOid);
|
|
|
|
static Oid findTypeReceiveFunction(List *procname, Oid typeOid);
|
|
|
|
static Oid findTypeSendFunction(List *procname, Oid typeOid);
|
2004-02-13 00:41:04 +01:00
|
|
|
static Oid findTypeAnalyzeFunction(List *procname, Oid typeOid);
|
2003-01-04 01:46:08 +01:00
|
|
|
static List *get_rels_with_domain(Oid domainOid, LOCKMODE lockmode);
|
2006-03-14 23:48:25 +01:00
|
|
|
static void checkDomainOwner(HeapTuple tup, TypeName *typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
static char *domainAddConstraint(Oid domainOid, Oid domainNamespace,
|
2003-08-04 02:43:34 +02:00
|
|
|
Oid baseTypeOid,
|
|
|
|
int typMod, Constraint *constr,
|
2004-06-10 19:56:03 +02:00
|
|
|
char *domainName);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DefineType
|
|
|
|
* Registers a new type.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DefineType(List *names, List *parameters)
|
|
|
|
{
|
|
|
|
char *typeName;
|
|
|
|
Oid typeNamespace;
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult aclresult;
|
2003-05-09 00:19:58 +02:00
|
|
|
int16 internalLength = -1; /* default: variable-length */
|
2002-04-15 07:22:04 +02:00
|
|
|
Oid elemType = InvalidOid;
|
|
|
|
List *inputName = NIL;
|
|
|
|
List *outputName = NIL;
|
2003-05-09 00:19:58 +02:00
|
|
|
List *receiveName = NIL;
|
|
|
|
List *sendName = NIL;
|
2004-02-13 00:41:04 +01:00
|
|
|
List *analyzeName = NIL;
|
2002-04-15 07:22:04 +02:00
|
|
|
char *defaultValue = NULL;
|
|
|
|
bool byValue = false;
|
|
|
|
char delimiter = DEFAULT_TYPDELIM;
|
|
|
|
char alignment = 'i'; /* default alignment */
|
2003-08-04 02:43:34 +02:00
|
|
|
char storage = 'p'; /* default TOAST storage method */
|
2002-04-15 07:22:04 +02:00
|
|
|
Oid inputOid;
|
|
|
|
Oid outputOid;
|
2003-05-09 00:19:58 +02:00
|
|
|
Oid receiveOid = InvalidOid;
|
|
|
|
Oid sendOid = InvalidOid;
|
2004-02-13 00:41:04 +01:00
|
|
|
Oid analyzeOid = InvalidOid;
|
2002-04-15 07:22:04 +02:00
|
|
|
char *shadow_type;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *pl;
|
2002-04-15 07:22:04 +02:00
|
|
|
Oid typoid;
|
2002-08-22 02:01:51 +02:00
|
|
|
Oid resulttype;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Convert list of names to a name and namespace */
|
|
|
|
typeNamespace = QualifiedNameGetCreationNamespace(names, &typeName);
|
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/* Check we have creation rights in target namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(typeNamespace, GetUserId(), ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
get_namespace_name(typeNamespace));
|
2002-04-27 05:45:03 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* Type names must be one character shorter than other names, allowing
|
2005-10-15 04:49:52 +02:00
|
|
|
* room to create the corresponding array type name with prepended "_".
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
|
|
|
if (strlen(typeName) > (NAMEDATALEN - 2))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_NAME),
|
|
|
|
errmsg("type names must be %d characters or less",
|
|
|
|
NAMEDATALEN - 2)));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2006-02-28 23:37:27 +01:00
|
|
|
/*
|
|
|
|
* Look to see if type already exists (presumably as a shell; if not,
|
|
|
|
* TypeCreate will complain). If it doesn't, create it as a shell, so
|
|
|
|
* that the OID is known for use in the I/O function definitions.
|
|
|
|
*/
|
|
|
|
typoid = GetSysCacheOid(TYPENAMENSP,
|
|
|
|
CStringGetDatum(typeName),
|
|
|
|
ObjectIdGetDatum(typeNamespace),
|
|
|
|
0, 0);
|
|
|
|
if (!OidIsValid(typoid))
|
|
|
|
{
|
|
|
|
typoid = TypeShellMake(typeName, typeNamespace);
|
|
|
|
/* Make new shell type visible for modification below */
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the command was a parameterless CREATE TYPE, we're done ---
|
|
|
|
* creating the shell type was all we're supposed to do.
|
|
|
|
*/
|
|
|
|
if (parameters == NIL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Complain if dummy CREATE TYPE and entry already exists */
|
|
|
|
if (parameters == NIL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("type \"%s\" already exists", typeName)));
|
|
|
|
}
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
foreach(pl, parameters)
|
|
|
|
{
|
|
|
|
DefElem *defel = (DefElem *) lfirst(pl);
|
|
|
|
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(defel->defname, "internallength") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
internalLength = defGetTypeLength(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "externallength") == 0)
|
2002-09-04 22:31:48 +02:00
|
|
|
; /* ignored -- remove after 7.3 */
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "input") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
inputName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "output") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
outputName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "receive") == 0)
|
2003-05-09 00:19:58 +02:00
|
|
|
receiveName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "send") == 0)
|
2003-05-09 00:19:58 +02:00
|
|
|
sendName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "analyze") == 0 ||
|
|
|
|
pg_strcasecmp(defel->defname, "analyse") == 0)
|
2004-02-13 00:41:04 +01:00
|
|
|
analyzeName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "delimiter") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
char *p = defGetString(defel);
|
|
|
|
|
|
|
|
delimiter = p[0];
|
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "element") == 0)
|
2002-08-22 02:01:51 +02:00
|
|
|
{
|
2006-03-14 23:48:25 +01:00
|
|
|
elemType = typenameTypeId(NULL, defGetTypeName(defel));
|
2002-08-22 02:01:51 +02:00
|
|
|
/* disallow arrays of pseudotypes */
|
|
|
|
if (get_typtype(elemType) == 'p')
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
|
|
|
errmsg("array element type cannot be %s",
|
|
|
|
format_type_be(elemType))));
|
2002-08-22 02:01:51 +02:00
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "default") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
defaultValue = defGetString(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "passedbyvalue") == 0)
|
2004-05-14 18:11:25 +02:00
|
|
|
byValue = defGetBoolean(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "alignment") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
char *a = defGetString(defel);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Note: if argument was an unquoted identifier, parser will have
|
|
|
|
* applied translations to it, so be prepared to recognize
|
|
|
|
* translated type names as well as the nominal form.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(a, "double") == 0 ||
|
|
|
|
pg_strcasecmp(a, "float8") == 0 ||
|
|
|
|
pg_strcasecmp(a, "pg_catalog.float8") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
alignment = 'd';
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(a, "int4") == 0 ||
|
|
|
|
pg_strcasecmp(a, "pg_catalog.int4") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
alignment = 'i';
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(a, "int2") == 0 ||
|
|
|
|
pg_strcasecmp(a, "pg_catalog.int2") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
alignment = 's';
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(a, "char") == 0 ||
|
|
|
|
pg_strcasecmp(a, "pg_catalog.bpchar") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
alignment = 'c';
|
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("alignment \"%s\" not recognized", a)));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "storage") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
char *a = defGetString(defel);
|
|
|
|
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(a, "plain") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
storage = 'p';
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(a, "external") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
storage = 'e';
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(a, "extended") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
storage = 'x';
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(a, "main") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
storage = 'm';
|
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("storage \"%s\" not recognized", a)));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(WARNING,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("type attribute \"%s\" not recognized",
|
|
|
|
defel->defname)));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure we have our required definitions
|
|
|
|
*/
|
|
|
|
if (inputName == NIL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("type input function must be specified")));
|
2002-04-15 07:22:04 +02:00
|
|
|
if (outputName == NIL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("type output function must be specified")));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
/*
|
|
|
|
* Convert I/O proc names to OIDs
|
|
|
|
*/
|
2003-05-09 00:19:58 +02:00
|
|
|
inputOid = findTypeInputFunction(inputName, typoid);
|
|
|
|
outputOid = findTypeOutputFunction(outputName, typoid);
|
|
|
|
if (receiveName)
|
|
|
|
receiveOid = findTypeReceiveFunction(receiveName, typoid);
|
|
|
|
if (sendName)
|
|
|
|
sendOid = findTypeSendFunction(sendName, typoid);
|
2002-08-22 02:01:51 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-21 20:39:26 +02:00
|
|
|
* Verify that I/O procs return the expected thing. If we see OPAQUE,
|
|
|
|
* complain and change it to the correct type-safe choice.
|
2002-08-22 02:01:51 +02:00
|
|
|
*/
|
|
|
|
resulttype = get_func_rettype(inputOid);
|
2002-09-21 20:39:26 +02:00
|
|
|
if (resulttype != typoid)
|
2002-08-23 18:41:38 +02:00
|
|
|
{
|
|
|
|
if (resulttype == OPAQUEOID)
|
2002-09-21 20:39:26 +02:00
|
|
|
{
|
2003-07-20 23:56:35 +02:00
|
|
|
/* backwards-compatibility hack */
|
2003-10-02 08:34:04 +02:00
|
|
|
ereport(WARNING,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("changing return type of function %s from \"opaque\" to %s",
|
2003-07-20 23:56:35 +02:00
|
|
|
NameListToString(inputName), typeName)));
|
2002-09-21 20:39:26 +02:00
|
|
|
SetFunctionReturnType(inputOid, typoid);
|
|
|
|
}
|
2002-08-23 18:41:38 +02:00
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("type input function %s must return type %s",
|
2003-07-20 23:56:35 +02:00
|
|
|
NameListToString(inputName), typeName)));
|
2002-08-23 18:41:38 +02:00
|
|
|
}
|
2002-08-22 02:01:51 +02:00
|
|
|
resulttype = get_func_rettype(outputOid);
|
2002-08-23 18:41:38 +02:00
|
|
|
if (resulttype != CSTRINGOID)
|
|
|
|
{
|
|
|
|
if (resulttype == OPAQUEOID)
|
2002-09-21 20:39:26 +02:00
|
|
|
{
|
2003-07-20 23:56:35 +02:00
|
|
|
/* backwards-compatibility hack */
|
2003-10-02 08:34:04 +02:00
|
|
|
ereport(WARNING,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("changing return type of function %s from \"opaque\" to \"cstring\"",
|
2003-07-20 23:56:35 +02:00
|
|
|
NameListToString(outputName))));
|
2002-09-21 20:39:26 +02:00
|
|
|
SetFunctionReturnType(outputOid, CSTRINGOID);
|
|
|
|
}
|
2002-08-23 18:41:38 +02:00
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("type output function %s must return type \"cstring\"",
|
|
|
|
NameListToString(outputName))));
|
2002-08-23 18:41:38 +02:00
|
|
|
}
|
2003-05-09 00:19:58 +02:00
|
|
|
if (receiveOid)
|
|
|
|
{
|
|
|
|
resulttype = get_func_rettype(receiveOid);
|
|
|
|
if (resulttype != typoid)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("type receive function %s must return type %s",
|
|
|
|
NameListToString(receiveName), typeName)));
|
2003-05-09 00:19:58 +02:00
|
|
|
}
|
|
|
|
if (sendOid)
|
|
|
|
{
|
|
|
|
resulttype = get_func_rettype(sendOid);
|
|
|
|
if (resulttype != BYTEAOID)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("type send function %s must return type \"bytea\"",
|
|
|
|
NameListToString(sendName))));
|
2003-05-09 00:19:58 +02:00
|
|
|
}
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2004-02-13 00:41:04 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Convert analysis function proc name to an OID. If no analysis function
|
|
|
|
* is specified, we'll use zero to select the built-in default algorithm.
|
2004-02-13 00:41:04 +01:00
|
|
|
*/
|
|
|
|
if (analyzeName)
|
|
|
|
analyzeOid = findTypeAnalyzeFunction(analyzeName, typoid);
|
|
|
|
|
2006-01-13 19:06:45 +01:00
|
|
|
/*
|
|
|
|
* Check permissions on functions. We choose to require the creator/owner
|
|
|
|
* of a type to also own the underlying functions. Since creating a type
|
|
|
|
* is tantamount to granting public execute access on the functions, the
|
|
|
|
* minimum sane check would be for execute-with-grant-option. But we don't
|
|
|
|
* have a way to make the type go away if the grant option is revoked, so
|
|
|
|
* ownership seems better.
|
|
|
|
*/
|
|
|
|
if (inputOid && !pg_proc_ownercheck(inputOid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
|
|
|
|
NameListToString(inputName));
|
|
|
|
if (outputOid && !pg_proc_ownercheck(outputOid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
|
|
|
|
NameListToString(outputName));
|
|
|
|
if (receiveOid && !pg_proc_ownercheck(receiveOid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
|
|
|
|
NameListToString(receiveName));
|
|
|
|
if (sendOid && !pg_proc_ownercheck(sendOid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
|
|
|
|
NameListToString(sendName));
|
|
|
|
if (analyzeOid && !pg_proc_ownercheck(analyzeOid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
|
|
|
|
NameListToString(analyzeName));
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* now have TypeCreate do all the real work.
|
|
|
|
*/
|
|
|
|
typoid =
|
2002-09-04 22:31:48 +02:00
|
|
|
TypeCreate(typeName, /* type name */
|
|
|
|
typeNamespace, /* namespace */
|
|
|
|
InvalidOid, /* relation oid (n/a here) */
|
|
|
|
0, /* relation kind (ditto) */
|
|
|
|
internalLength, /* internal size */
|
|
|
|
'b', /* type-type (base type) */
|
|
|
|
delimiter, /* array element delimiter */
|
|
|
|
inputOid, /* input procedure */
|
|
|
|
outputOid, /* output procedure */
|
2003-05-09 00:19:58 +02:00
|
|
|
receiveOid, /* receive procedure */
|
|
|
|
sendOid, /* send procedure */
|
2004-02-13 00:41:04 +01:00
|
|
|
analyzeOid, /* analyze procedure */
|
2002-09-04 22:31:48 +02:00
|
|
|
elemType, /* element type ID */
|
|
|
|
InvalidOid, /* base type ID (only for domains) */
|
2002-04-15 07:22:04 +02:00
|
|
|
defaultValue, /* default type value */
|
2002-09-04 22:31:48 +02:00
|
|
|
NULL, /* no binary form available */
|
|
|
|
byValue, /* passed by value */
|
|
|
|
alignment, /* required alignment */
|
|
|
|
storage, /* TOAST strategy */
|
|
|
|
-1, /* typMod (Domains only) */
|
|
|
|
0, /* Array Dimensions of typbasetype */
|
|
|
|
false); /* Type NOT NULL */
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* When we create a base type (as opposed to a complex type) we need to
|
|
|
|
* have an array entry for it in pg_type as well.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
|
|
|
shadow_type = makeArrayTypeName(typeName);
|
|
|
|
|
|
|
|
/* alignment must be 'i' or 'd' for arrays */
|
|
|
|
alignment = (alignment == 'd') ? 'd' : 'i';
|
|
|
|
|
|
|
|
TypeCreate(shadow_type, /* type name */
|
|
|
|
typeNamespace, /* namespace */
|
|
|
|
InvalidOid, /* relation oid (n/a here) */
|
2002-08-29 02:17:06 +02:00
|
|
|
0, /* relation kind (ditto) */
|
2002-04-15 07:22:04 +02:00
|
|
|
-1, /* internal size */
|
|
|
|
'b', /* type-type (base type) */
|
|
|
|
DEFAULT_TYPDELIM, /* array element delimiter */
|
|
|
|
F_ARRAY_IN, /* input procedure */
|
|
|
|
F_ARRAY_OUT, /* output procedure */
|
2003-05-09 00:19:58 +02:00
|
|
|
F_ARRAY_RECV, /* receive procedure */
|
|
|
|
F_ARRAY_SEND, /* send procedure */
|
2004-02-13 00:41:04 +01:00
|
|
|
InvalidOid, /* analyze procedure - default */
|
2002-04-15 07:22:04 +02:00
|
|
|
typoid, /* element type ID */
|
|
|
|
InvalidOid, /* base type ID */
|
|
|
|
NULL, /* never a default type value */
|
|
|
|
NULL, /* binary default isn't sent either */
|
|
|
|
false, /* never passed by value */
|
|
|
|
alignment, /* see above */
|
|
|
|
'x', /* ARRAY is always toastable */
|
|
|
|
-1, /* typMod (Domains only) */
|
|
|
|
0, /* Array dimensions of typbasetype */
|
|
|
|
false); /* Type NOT NULL */
|
|
|
|
|
|
|
|
pfree(shadow_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RemoveType
|
|
|
|
* Removes a datatype.
|
|
|
|
*/
|
|
|
|
void
|
2005-11-19 18:39:45 +01:00
|
|
|
RemoveType(List *names, DropBehavior behavior, bool missing_ok)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
TypeName *typename;
|
|
|
|
Oid typeoid;
|
|
|
|
HeapTuple tup;
|
2002-07-12 20:43:19 +02:00
|
|
|
ObjectAddress object;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Use LookupTypeName here so that shell types can be removed. */
|
2006-03-14 23:48:25 +01:00
|
|
|
typeoid = LookupTypeName(NULL, typename);
|
2002-04-15 07:22:04 +02:00
|
|
|
if (!OidIsValid(typeoid))
|
2005-11-19 18:39:45 +01:00
|
|
|
{
|
|
|
|
if (!missing_ok)
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("type \"%s\" does not exist",
|
|
|
|
TypeNameToString(typename))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(NOTICE,
|
2005-11-22 19:17:34 +01:00
|
|
|
(errmsg("type \"%s\" does not exist, skipping",
|
2005-11-19 18:39:45 +01:00
|
|
|
TypeNameToString(typename))));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
tup = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(typeoid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeoid);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/* Permission check: must own type or its namespace */
|
|
|
|
if (!pg_type_ownercheck(typeoid, GetUserId()) &&
|
2005-10-15 04:49:52 +02:00
|
|
|
!pg_namespace_ownercheck(((Form_pg_type) GETSTRUCT(tup))->typnamespace,
|
|
|
|
GetUserId()))
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
|
|
|
|
TypeNameToString(typename));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
ReleaseSysCache(tup);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* Do the deletion
|
|
|
|
*/
|
2005-04-14 03:38:22 +02:00
|
|
|
object.classId = TypeRelationId;
|
2002-07-12 20:43:19 +02:00
|
|
|
object.objectId = typeoid;
|
|
|
|
object.objectSubId = 0;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
performDeletion(&object, behavior);
|
|
|
|
}
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* Guts of type deletion.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RemoveTypeById(Oid typeOid)
|
|
|
|
{
|
|
|
|
Relation relation;
|
|
|
|
HeapTuple tup;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
relation = heap_open(TypeRelationId, RowExclusiveLock);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
tup = SearchSysCache(TYPEOID,
|
2002-07-12 20:43:19 +02:00
|
|
|
ObjectIdGetDatum(typeOid),
|
2002-04-15 07:22:04 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeOid);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
simple_heap_delete(relation, &tup->t_self);
|
|
|
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DefineDomain
|
|
|
|
* Registers a new domain.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DefineDomain(CreateDomainStmt *stmt)
|
|
|
|
{
|
|
|
|
char *domainName;
|
|
|
|
Oid domainNamespace;
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult aclresult;
|
2002-04-15 07:22:04 +02:00
|
|
|
int16 internalLength;
|
|
|
|
Oid inputProcedure;
|
|
|
|
Oid outputProcedure;
|
2003-05-09 00:19:58 +02:00
|
|
|
Oid receiveProcedure;
|
|
|
|
Oid sendProcedure;
|
2004-02-13 00:41:04 +01:00
|
|
|
Oid analyzeProcedure;
|
2002-04-15 07:22:04 +02:00
|
|
|
bool byValue;
|
2006-04-06 00:11:58 +02:00
|
|
|
Oid typelem;
|
2002-04-15 07:22:04 +02:00
|
|
|
char delimiter;
|
|
|
|
char alignment;
|
|
|
|
char storage;
|
|
|
|
char typtype;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
2002-07-17 00:12:20 +02:00
|
|
|
Node *defaultExpr = NULL;
|
2002-04-15 07:22:04 +02:00
|
|
|
char *defaultValue = NULL;
|
|
|
|
char *defaultValueBin = NULL;
|
|
|
|
bool typNotNull = false;
|
2002-07-17 00:12:20 +02:00
|
|
|
bool nullDefined = false;
|
2004-05-26 06:41:50 +02:00
|
|
|
int32 typNDims = list_length(stmt->typename->arrayBounds);
|
2002-04-15 07:22:04 +02:00
|
|
|
HeapTuple typeTup;
|
|
|
|
List *schema = stmt->constraints;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *listptr;
|
2002-07-12 20:43:19 +02:00
|
|
|
Oid basetypeoid;
|
2002-07-17 00:12:20 +02:00
|
|
|
Oid domainoid;
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_type baseType;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Convert list of names to a name and namespace */
|
|
|
|
domainNamespace = QualifiedNameGetCreationNamespace(stmt->domainname,
|
|
|
|
&domainName);
|
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/* Check we have creation rights in target namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(domainNamespace, GetUserId(),
|
|
|
|
ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
get_namespace_name(domainNamespace));
|
2002-04-27 05:45:03 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Domainnames, unlike typenames don't need to account for the '_' prefix.
|
|
|
|
* So they can be one character longer. (This test is presently useless
|
|
|
|
* since the parser will have truncated the name to fit. But leave it
|
|
|
|
* here since we may someday support arrays of domains, in which case
|
|
|
|
* we'll be back to needing to enforce NAMEDATALEN-2.)
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
|
|
|
if (strlen(domainName) > (NAMEDATALEN - 1))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_NAME),
|
|
|
|
errmsg("domain names must be %d characters or less",
|
|
|
|
NAMEDATALEN - 1)));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up the base type.
|
|
|
|
*/
|
2006-03-14 23:48:25 +01:00
|
|
|
typeTup = typenameType(NULL, stmt->typename);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
baseType = (Form_pg_type) GETSTRUCT(typeTup);
|
2002-07-20 07:16:59 +02:00
|
|
|
basetypeoid = HeapTupleGetOid(typeTup);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
2006-04-06 00:11:58 +02:00
|
|
|
* Base type must be a plain base type or another domain. Domains over
|
|
|
|
* pseudotypes would create a security hole. Domains over composite
|
|
|
|
* types might be made to work in the future, but not today.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2002-07-12 20:43:19 +02:00
|
|
|
typtype = baseType->typtype;
|
2006-04-06 00:11:58 +02:00
|
|
|
if (typtype != 'b' && typtype != 'd')
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
|
|
|
errmsg("\"%s\" is not a valid base type for a domain",
|
|
|
|
TypeNameToString(stmt->typename))));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* passed by value */
|
2002-07-12 20:43:19 +02:00
|
|
|
byValue = baseType->typbyval;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Required Alignment */
|
2002-07-12 20:43:19 +02:00
|
|
|
alignment = baseType->typalign;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* TOAST Strategy */
|
2002-07-12 20:43:19 +02:00
|
|
|
storage = baseType->typstorage;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Storage Length */
|
2002-07-12 20:43:19 +02:00
|
|
|
internalLength = baseType->typlen;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2006-04-06 00:11:58 +02:00
|
|
|
/* Array element type (in case base type is an array) */
|
|
|
|
typelem = baseType->typelem;
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/* Array element Delimiter */
|
2002-07-12 20:43:19 +02:00
|
|
|
delimiter = baseType->typdelim;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* I/O Functions */
|
2006-04-06 00:11:58 +02:00
|
|
|
inputProcedure = F_DOMAIN_IN;
|
2002-07-12 20:43:19 +02:00
|
|
|
outputProcedure = baseType->typoutput;
|
2006-04-06 00:11:58 +02:00
|
|
|
receiveProcedure = F_DOMAIN_RECV;
|
2003-05-09 00:19:58 +02:00
|
|
|
sendProcedure = baseType->typsend;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2004-02-13 00:41:04 +01:00
|
|
|
/* Analysis function */
|
|
|
|
analyzeProcedure = baseType->typanalyze;
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/* Inherited default value */
|
2002-09-04 22:31:48 +02:00
|
|
|
datum = SysCacheGetAttr(TYPEOID, typeTup,
|
2002-04-15 07:22:04 +02:00
|
|
|
Anum_pg_type_typdefault, &isnull);
|
|
|
|
if (!isnull)
|
|
|
|
defaultValue = DatumGetCString(DirectFunctionCall1(textout, datum));
|
|
|
|
|
|
|
|
/* Inherited default binary value */
|
2002-09-04 22:31:48 +02:00
|
|
|
datum = SysCacheGetAttr(TYPEOID, typeTup,
|
2002-04-15 07:22:04 +02:00
|
|
|
Anum_pg_type_typdefaultbin, &isnull);
|
|
|
|
if (!isnull)
|
|
|
|
defaultValueBin = DatumGetCString(DirectFunctionCall1(textout, datum));
|
|
|
|
|
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Run through constraints manually to avoid the additional processing
|
|
|
|
* conducted by DefineRelation() and friends.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
|
|
|
foreach(listptr, schema)
|
|
|
|
{
|
2002-11-15 03:50:21 +01:00
|
|
|
Node *newConstraint = lfirst(listptr);
|
2003-07-20 23:56:35 +02:00
|
|
|
Constraint *constr;
|
2002-04-15 07:22:04 +02:00
|
|
|
ParseState *pstate;
|
|
|
|
|
2002-12-09 21:31:05 +01:00
|
|
|
/* Check for unsupported constraint types */
|
|
|
|
if (IsA(newConstraint, FkConstraint))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
2003-09-10 01:22:21 +02:00
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("foreign key constraints not possible for domains")));
|
2002-12-09 21:31:05 +01:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
/* otherwise it should be a plain Constraint */
|
2002-12-09 21:31:05 +01:00
|
|
|
if (!IsA(newConstraint, Constraint))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "unrecognized node type: %d",
|
|
|
|
(int) nodeTag(newConstraint));
|
2002-11-15 03:50:21 +01:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
constr = (Constraint *) newConstraint;
|
2002-12-09 21:31:05 +01:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
switch (constr->contype)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
2002-12-09 21:31:05 +01:00
|
|
|
case CONSTR_DEFAULT:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The inherited default value may be overridden by the user
|
|
|
|
* with the DEFAULT <expr> statement.
|
2002-09-04 22:31:48 +02:00
|
|
|
*/
|
2002-07-17 00:12:20 +02:00
|
|
|
if (defaultExpr)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("multiple default expressions")));
|
2003-07-20 23:56:35 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/* Create a dummy ParseState for transformExpr */
|
|
|
|
pstate = make_parsestate(NULL);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Cook the constr->raw_expr into an expression. Note: Name is
|
|
|
|
* strictly for error message
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2003-07-20 23:56:35 +02:00
|
|
|
defaultExpr = cookDefault(pstate, constr->raw_expr,
|
2002-07-17 00:12:20 +02:00
|
|
|
basetypeoid,
|
|
|
|
stmt->typename->typmod,
|
|
|
|
domainName);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Expression must be stored as a nodeToString result, but we
|
|
|
|
* also require a valid textual representation (mainly to make
|
|
|
|
* life easier for pg_dump).
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2002-07-17 00:12:20 +02:00
|
|
|
defaultValue = deparse_expression(defaultExpr,
|
2005-10-15 04:49:52 +02:00
|
|
|
deparse_context_for(domainName,
|
|
|
|
InvalidOid),
|
2002-09-20 00:48:34 +02:00
|
|
|
false, false);
|
2002-07-17 00:12:20 +02:00
|
|
|
defaultValueBin = nodeToString(defaultExpr);
|
2002-04-15 07:22:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSTR_NOTNULL:
|
2002-12-12 21:35:16 +01:00
|
|
|
if (nullDefined && !typNotNull)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("conflicting NULL/NOT NULL constraints")));
|
2002-07-17 00:12:20 +02:00
|
|
|
typNotNull = true;
|
|
|
|
nullDefined = true;
|
2002-09-04 22:31:48 +02:00
|
|
|
break;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
case CONSTR_NULL:
|
2002-12-12 21:35:16 +01:00
|
|
|
if (nullDefined && typNotNull)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("conflicting NULL/NOT NULL constraints")));
|
2002-07-17 00:12:20 +02:00
|
|
|
typNotNull = false;
|
|
|
|
nullDefined = true;
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSTR_CHECK:
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-12-09 21:31:05 +01:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Check constraints are handled after domain creation, as
|
|
|
|
* they require the Oid of the domain
|
2002-12-09 21:31:05 +01:00
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-12-09 21:31:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All else are error cases
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
case CONSTR_UNIQUE:
|
|
|
|
ereport(ERROR,
|
2003-09-10 01:22:21 +02:00
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("unique constraints not possible for domains")));
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-12-09 21:31:05 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
case CONSTR_PRIMARY:
|
|
|
|
ereport(ERROR,
|
2003-09-10 01:22:21 +02:00
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("primary key constraints not possible for domains")));
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-12-09 21:31:05 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
case CONSTR_ATTR_DEFERRABLE:
|
|
|
|
case CONSTR_ATTR_NOT_DEFERRABLE:
|
|
|
|
case CONSTR_ATTR_DEFERRED:
|
|
|
|
case CONSTR_ATTR_IMMEDIATE:
|
|
|
|
ereport(ERROR,
|
2003-07-20 23:56:35 +02:00
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2003-09-10 01:22:21 +02:00
|
|
|
errmsg("specifying constraint deferrability not supported for domains")));
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
default:
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "unrecognized constraint subtype: %d",
|
|
|
|
(int) constr->contype);
|
2002-09-04 22:31:48 +02:00
|
|
|
break;
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Have TypeCreate do all the real work.
|
|
|
|
*/
|
2002-07-17 00:12:20 +02:00
|
|
|
domainoid =
|
2002-09-04 22:31:48 +02:00
|
|
|
TypeCreate(domainName, /* type name */
|
2002-07-17 00:12:20 +02:00
|
|
|
domainNamespace, /* namespace */
|
2002-09-04 22:31:48 +02:00
|
|
|
InvalidOid, /* relation oid (n/a here) */
|
|
|
|
0, /* relation kind (ditto) */
|
2002-07-17 00:12:20 +02:00
|
|
|
internalLength, /* internal size */
|
2002-09-04 22:31:48 +02:00
|
|
|
'd', /* type-type (domain type) */
|
|
|
|
delimiter, /* array element delimiter */
|
2002-07-17 00:12:20 +02:00
|
|
|
inputProcedure, /* input procedure */
|
|
|
|
outputProcedure, /* output procedure */
|
2003-05-09 00:19:58 +02:00
|
|
|
receiveProcedure, /* receive procedure */
|
|
|
|
sendProcedure, /* send procedure */
|
2004-02-13 00:41:04 +01:00
|
|
|
analyzeProcedure, /* analyze procedure */
|
2006-04-06 00:11:58 +02:00
|
|
|
typelem, /* element type ID */
|
2002-09-04 22:31:48 +02:00
|
|
|
basetypeoid, /* base type ID */
|
|
|
|
defaultValue, /* default type value (text) */
|
2002-07-17 00:12:20 +02:00
|
|
|
defaultValueBin, /* default type value (binary) */
|
2003-08-04 02:43:34 +02:00
|
|
|
byValue, /* passed by value */
|
|
|
|
alignment, /* required alignment */
|
|
|
|
storage, /* TOAST strategy */
|
|
|
|
stmt->typename->typmod, /* typeMod value */
|
|
|
|
typNDims, /* Array dimensions for base type */
|
|
|
|
typNotNull); /* Type NOT NULL */
|
2002-11-15 03:50:21 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Process constraints which refer to the domain ID returned by TypeCreate
|
2002-11-15 03:50:21 +01:00
|
|
|
*/
|
|
|
|
foreach(listptr, schema)
|
|
|
|
{
|
|
|
|
Constraint *constr = lfirst(listptr);
|
|
|
|
|
2002-12-09 21:31:05 +01:00
|
|
|
/* it must be a Constraint, per check above */
|
|
|
|
|
2002-11-15 03:50:21 +01:00
|
|
|
switch (constr->contype)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
case CONSTR_CHECK:
|
2002-12-12 21:35:16 +01:00
|
|
|
domainAddConstraint(domainoid, domainNamespace,
|
|
|
|
basetypeoid, stmt->typename->typmod,
|
2004-06-10 19:56:03 +02:00
|
|
|
constr, domainName);
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
/* Other constraint types were fully processed above */
|
2002-12-09 21:31:05 +01:00
|
|
|
|
2002-11-15 03:50:21 +01:00
|
|
|
default:
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-11-15 03:50:21 +01:00
|
|
|
}
|
2004-06-10 19:56:03 +02:00
|
|
|
|
|
|
|
/* CCI so we can detect duplicate constraint names */
|
|
|
|
CommandCounterIncrement();
|
2002-11-15 03:50:21 +01:00
|
|
|
}
|
2002-07-17 00:12:20 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* Now we can clean up.
|
|
|
|
*/
|
|
|
|
ReleaseSysCache(typeTup);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RemoveDomain
|
|
|
|
* Removes a domain.
|
2002-07-12 20:43:19 +02:00
|
|
|
*
|
|
|
|
* This is identical to RemoveType except we insist it be a domain.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
|
|
|
void
|
2005-11-19 18:39:45 +01:00
|
|
|
RemoveDomain(List *names, DropBehavior behavior, bool missing_ok)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
TypeName *typename;
|
2003-02-20 00:41:15 +01:00
|
|
|
Oid typeoid;
|
2002-04-15 07:22:04 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
char typtype;
|
2002-07-12 20:43:19 +02:00
|
|
|
ObjectAddress object;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/* Use LookupTypeName here so that shell types can be removed. */
|
2006-03-14 23:48:25 +01:00
|
|
|
typeoid = LookupTypeName(NULL, typename);
|
2003-02-20 00:41:15 +01:00
|
|
|
if (!OidIsValid(typeoid))
|
2005-11-19 18:39:45 +01:00
|
|
|
{
|
|
|
|
if (!missing_ok)
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("type \"%s\" does not exist",
|
|
|
|
TypeNameToString(typename))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(NOTICE,
|
2005-11-22 19:17:34 +01:00
|
|
|
(errmsg("type \"%s\" does not exist, skipping",
|
2005-11-19 18:39:45 +01:00
|
|
|
TypeNameToString(typename))));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
tup = SearchSysCache(TYPEOID,
|
2003-02-20 00:41:15 +01:00
|
|
|
ObjectIdGetDatum(typeoid),
|
2002-04-15 07:22:04 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeoid);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/* Permission check: must own type or its namespace */
|
2003-02-20 00:41:15 +01:00
|
|
|
if (!pg_type_ownercheck(typeoid, GetUserId()) &&
|
2005-10-15 04:49:52 +02:00
|
|
|
!pg_namespace_ownercheck(((Form_pg_type) GETSTRUCT(tup))->typnamespace,
|
|
|
|
GetUserId()))
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
|
|
|
|
TypeNameToString(typename));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Check that this is actually a domain */
|
|
|
|
typtype = ((Form_pg_type) GETSTRUCT(tup))->typtype;
|
|
|
|
|
|
|
|
if (typtype != 'd')
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a domain",
|
|
|
|
TypeNameToString(typename))));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* Do the deletion
|
|
|
|
*/
|
2005-04-14 03:38:22 +02:00
|
|
|
object.classId = TypeRelationId;
|
2003-02-20 00:41:15 +01:00
|
|
|
object.objectId = typeoid;
|
2002-07-12 20:43:19 +02:00
|
|
|
object.objectSubId = 0;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
performDeletion(&object, behavior);
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-05-09 00:19:58 +02:00
|
|
|
* Find suitable I/O functions for a type.
|
2002-08-22 02:01:51 +02:00
|
|
|
*
|
2002-09-21 20:39:26 +02:00
|
|
|
* typeOid is the type's OID (which will already exist, if only as a shell
|
|
|
|
* type).
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2003-05-09 00:19:58 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
static Oid
|
2003-05-09 00:19:58 +02:00
|
|
|
findTypeInputFunction(List *procname, Oid typeOid)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
2005-03-29 05:01:32 +02:00
|
|
|
Oid argList[3];
|
2002-04-15 07:22:04 +02:00
|
|
|
Oid procOid;
|
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Input functions can take a single argument of type CSTRING, or three
|
|
|
|
* arguments (string, typioparam OID, typmod).
|
2003-05-09 00:19:58 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* For backwards compatibility we allow OPAQUE in place of CSTRING; if we
|
|
|
|
* see this, we issue a warning and fix up the pg_proc entry.
|
2003-05-09 00:19:58 +02:00
|
|
|
*/
|
|
|
|
argList[0] = CSTRINGOID;
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
if (OidIsValid(procOid))
|
|
|
|
return procOid;
|
|
|
|
|
|
|
|
argList[1] = OIDOID;
|
|
|
|
argList[2] = INT4OID;
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 3, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
if (OidIsValid(procOid))
|
|
|
|
return procOid;
|
|
|
|
|
|
|
|
/* No luck, try it with OPAQUE */
|
|
|
|
argList[0] = OPAQUEOID;
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
|
|
|
|
if (!OidIsValid(procOid))
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
2003-05-09 00:19:58 +02:00
|
|
|
argList[1] = OIDOID;
|
|
|
|
argList[2] = INT4OID;
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 3, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OidIsValid(procOid))
|
|
|
|
{
|
|
|
|
/* Found, but must complain and fix the pg_proc entry */
|
2003-10-02 08:34:04 +02:00
|
|
|
ereport(WARNING,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("changing argument type of function %s from \"opaque\" to \"cstring\"",
|
2003-07-20 23:56:35 +02:00
|
|
|
NameListToString(procname))));
|
2003-05-09 00:19:58 +02:00
|
|
|
SetFunctionArgType(procOid, 0, CSTRINGOID);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Need CommandCounterIncrement since DefineType will likely try to
|
|
|
|
* alter the pg_proc tuple again.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2003-05-09 00:19:58 +02:00
|
|
|
CommandCounterIncrement();
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
return procOid;
|
|
|
|
}
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/* Use CSTRING (preferred) in the error message */
|
|
|
|
argList[0] = CSTRINGOID;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
|
|
|
func_signature_string(procname, 1, argList))));
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
return InvalidOid; /* keep compiler quiet */
|
|
|
|
}
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
static Oid
|
|
|
|
findTypeOutputFunction(List *procname, Oid typeOid)
|
|
|
|
{
|
2005-05-01 20:56:19 +02:00
|
|
|
Oid argList[1];
|
2003-05-09 00:19:58 +02:00
|
|
|
Oid procOid;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/*
|
2005-05-01 20:56:19 +02:00
|
|
|
* Output functions can take a single argument of the type.
|
2003-05-09 00:19:58 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* For backwards compatibility we allow OPAQUE in place of the actual type
|
|
|
|
* name; if we see this, we issue a warning and fix up the pg_proc entry.
|
2003-05-09 00:19:58 +02:00
|
|
|
*/
|
|
|
|
argList[0] = typeOid;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
if (OidIsValid(procOid))
|
|
|
|
return procOid;
|
2002-09-21 20:39:26 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/* No luck, try it with OPAQUE */
|
|
|
|
argList[0] = OPAQUEOID;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
if (OidIsValid(procOid))
|
2002-08-22 02:01:51 +02:00
|
|
|
{
|
2003-05-09 00:19:58 +02:00
|
|
|
/* Found, but must complain and fix the pg_proc entry */
|
2003-10-02 08:34:04 +02:00
|
|
|
ereport(WARNING,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("changing argument type of function %s from \"opaque\" to %s",
|
|
|
|
NameListToString(procname), format_type_be(typeOid))));
|
2003-05-09 00:19:58 +02:00
|
|
|
SetFunctionArgType(procOid, 0, typeOid);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Need CommandCounterIncrement since DefineType will likely try to
|
|
|
|
* alter the pg_proc tuple again.
|
2002-08-22 02:01:51 +02:00
|
|
|
*/
|
2003-05-09 00:19:58 +02:00
|
|
|
CommandCounterIncrement();
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
return procOid;
|
|
|
|
}
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/* Use type name, not OPAQUE, in the failure message. */
|
|
|
|
argList[0] = typeOid;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
|
|
|
func_signature_string(procname, 1, argList))));
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
return InvalidOid; /* keep compiler quiet */
|
|
|
|
}
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
static Oid
|
|
|
|
findTypeReceiveFunction(List *procname, Oid typeOid)
|
|
|
|
{
|
2005-07-10 23:14:00 +02:00
|
|
|
Oid argList[3];
|
2003-05-09 00:19:58 +02:00
|
|
|
Oid procOid;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Receive functions can take a single argument of type INTERNAL, or three
|
|
|
|
* arguments (internal, typioparam OID, typmod).
|
2003-05-09 00:19:58 +02:00
|
|
|
*/
|
|
|
|
argList[0] = INTERNALOID;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
if (OidIsValid(procOid))
|
|
|
|
return procOid;
|
2002-09-21 20:39:26 +02:00
|
|
|
|
2003-05-10 01:01:45 +02:00
|
|
|
argList[1] = OIDOID;
|
2005-07-10 23:14:00 +02:00
|
|
|
argList[2] = INT4OID;
|
2003-05-10 01:01:45 +02:00
|
|
|
|
2005-07-10 23:14:00 +02:00
|
|
|
procOid = LookupFuncName(procname, 3, argList, true);
|
2003-05-10 01:01:45 +02:00
|
|
|
if (OidIsValid(procOid))
|
|
|
|
return procOid;
|
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
|
|
|
func_signature_string(procname, 1, argList))));
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
return InvalidOid; /* keep compiler quiet */
|
|
|
|
}
|
2002-09-21 20:39:26 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
static Oid
|
|
|
|
findTypeSendFunction(List *procname, Oid typeOid)
|
|
|
|
{
|
2005-05-01 20:56:19 +02:00
|
|
|
Oid argList[1];
|
2003-05-09 00:19:58 +02:00
|
|
|
Oid procOid;
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2003-05-09 00:19:58 +02:00
|
|
|
/*
|
2005-05-01 20:56:19 +02:00
|
|
|
* Send functions can take a single argument of the type.
|
2003-05-09 00:19:58 +02:00
|
|
|
*/
|
|
|
|
argList[0] = typeOid;
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
2003-05-09 00:19:58 +02:00
|
|
|
if (OidIsValid(procOid))
|
|
|
|
return procOid;
|
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
|
|
|
func_signature_string(procname, 1, argList))));
|
2002-08-22 02:01:51 +02:00
|
|
|
|
|
|
|
return InvalidOid; /* keep compiler quiet */
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
2002-08-15 18:36:08 +02:00
|
|
|
|
2004-02-13 00:41:04 +01:00
|
|
|
static Oid
|
|
|
|
findTypeAnalyzeFunction(List *procname, Oid typeOid)
|
|
|
|
{
|
2005-03-29 05:01:32 +02:00
|
|
|
Oid argList[1];
|
2004-02-13 00:41:04 +01:00
|
|
|
Oid procOid;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Analyze functions always take one INTERNAL argument and return bool.
|
2004-02-13 00:41:04 +01:00
|
|
|
*/
|
|
|
|
argList[0] = INTERNALOID;
|
|
|
|
|
|
|
|
procOid = LookupFuncName(procname, 1, argList, true);
|
|
|
|
if (!OidIsValid(procOid))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
|
|
|
func_signature_string(procname, 1, argList))));
|
|
|
|
|
|
|
|
if (get_func_rettype(procOid) != BOOLOID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("type analyze function %s must return type \"boolean\"",
|
|
|
|
NameListToString(procname))));
|
2004-02-13 00:41:04 +01:00
|
|
|
|
|
|
|
return procOid;
|
|
|
|
}
|
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
|
2002-08-15 18:36:08 +02:00
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
* DefineCompositeType
|
|
|
|
*
|
|
|
|
* Create a Composite Type relation.
|
|
|
|
* `DefineRelation' does all the work, we just provide the correct
|
|
|
|
* arguments!
|
|
|
|
*
|
|
|
|
* If the relation already exists, then 'DefineRelation' will abort
|
|
|
|
* the xact...
|
|
|
|
*
|
|
|
|
* DefineCompositeType returns relid for use when creating
|
|
|
|
* an implicit composite type during function creation
|
|
|
|
*-------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
DefineCompositeType(const RangeVar *typevar, List *coldeflist)
|
|
|
|
{
|
|
|
|
CreateStmt *createStmt = makeNode(CreateStmt);
|
|
|
|
|
|
|
|
if (coldeflist == NIL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("composite type must have at least one attribute")));
|
2002-08-15 18:36:08 +02:00
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* now set the parameters for keys/inheritance etc. All of these are
|
|
|
|
* uninteresting for composite types...
|
2002-08-15 18:36:08 +02:00
|
|
|
*/
|
|
|
|
createStmt->relation = (RangeVar *) typevar;
|
|
|
|
createStmt->tableElts = coldeflist;
|
|
|
|
createStmt->inhRelations = NIL;
|
|
|
|
createStmt->constraints = NIL;
|
2006-07-02 04:23:23 +02:00
|
|
|
createStmt->options = list_make1(defWithOids(false));
|
2002-11-11 23:19:25 +01:00
|
|
|
createStmt->oncommit = ONCOMMIT_NOOP;
|
2004-06-18 08:14:31 +02:00
|
|
|
createStmt->tablespacename = NULL;
|
2002-08-15 18:36:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* finally create the relation...
|
|
|
|
*/
|
|
|
|
return DefineRelation(createStmt, RELKIND_COMPOSITE_TYPE);
|
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* AlterDomainDefault
|
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Routine implementing ALTER DOMAIN SET/DROP DEFAULT statements.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterDomainDefault(List *names, Node *defaultRaw)
|
|
|
|
{
|
|
|
|
TypeName *typename;
|
|
|
|
Oid domainoid;
|
|
|
|
HeapTuple tup;
|
|
|
|
ParseState *pstate;
|
|
|
|
Relation rel;
|
|
|
|
char *defaultValue;
|
2003-08-04 02:43:34 +02:00
|
|
|
Node *defaultExpr = NULL; /* NULL if no default specified */
|
2002-12-06 06:00:34 +01:00
|
|
|
Datum new_record[Natts_pg_type];
|
|
|
|
char new_record_nulls[Natts_pg_type];
|
|
|
|
char new_record_repl[Natts_pg_type];
|
|
|
|
HeapTuple newtuple;
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_type typTup;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
|
|
|
domainoid = typenameTypeId(NULL, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Look up the domain in the type table */
|
2005-04-14 22:03:27 +02:00
|
|
|
rel = heap_open(TypeRelationId, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(domainoid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", domainoid);
|
2006-03-14 23:48:25 +01:00
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Check it's a domain and check user has permission for ALTER DOMAIN */
|
|
|
|
checkDomainOwner(tup, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Setup new tuple */
|
|
|
|
MemSet(new_record, (Datum) 0, sizeof(new_record));
|
|
|
|
MemSet(new_record_nulls, ' ', sizeof(new_record_nulls));
|
|
|
|
MemSet(new_record_repl, ' ', sizeof(new_record_repl));
|
|
|
|
|
|
|
|
/* Store the new default, if null then skip this step */
|
|
|
|
if (defaultRaw)
|
|
|
|
{
|
|
|
|
/* Create a dummy ParseState for transformExpr */
|
|
|
|
pstate = make_parsestate(NULL);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Cook the colDef->raw_expr into an expression. Note: Name is
|
|
|
|
* strictly for error message
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
defaultExpr = cookDefault(pstate, defaultRaw,
|
|
|
|
typTup->typbasetype,
|
|
|
|
typTup->typtypmod,
|
|
|
|
NameStr(typTup->typname));
|
|
|
|
|
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Expression must be stored as a nodeToString result, but we also
|
2005-10-15 04:49:52 +02:00
|
|
|
* require a valid textual representation (mainly to make life easier
|
|
|
|
* for pg_dump).
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
defaultValue = deparse_expression(defaultExpr,
|
2005-10-15 04:49:52 +02:00
|
|
|
deparse_context_for(NameStr(typTup->typname),
|
|
|
|
InvalidOid),
|
2002-12-06 06:00:34 +01:00
|
|
|
false, false);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
/*
|
|
|
|
* Form an updated tuple with the new default and write it back.
|
|
|
|
*/
|
|
|
|
new_record[Anum_pg_type_typdefaultbin - 1] = DirectFunctionCall1(textin,
|
2005-10-15 04:49:52 +02:00
|
|
|
CStringGetDatum(
|
|
|
|
nodeToString(defaultExpr)));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
new_record_repl[Anum_pg_type_typdefaultbin - 1] = 'r';
|
|
|
|
new_record[Anum_pg_type_typdefault - 1] = DirectFunctionCall1(textin,
|
2005-10-15 04:49:52 +02:00
|
|
|
CStringGetDatum(defaultValue));
|
2002-12-06 06:00:34 +01:00
|
|
|
new_record_repl[Anum_pg_type_typdefault - 1] = 'r';
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
else
|
2005-10-15 04:49:52 +02:00
|
|
|
/* Default is NULL, drop it */
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
|
|
|
new_record_nulls[Anum_pg_type_typdefaultbin - 1] = 'n';
|
|
|
|
new_record_repl[Anum_pg_type_typdefaultbin - 1] = 'r';
|
|
|
|
new_record_nulls[Anum_pg_type_typdefault - 1] = 'n';
|
|
|
|
new_record_repl[Anum_pg_type_typdefault - 1] = 'r';
|
|
|
|
}
|
|
|
|
|
2005-01-28 00:24:11 +01:00
|
|
|
newtuple = heap_modifytuple(tup, RelationGetDescr(rel),
|
|
|
|
new_record, new_record_nulls,
|
|
|
|
new_record_repl);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
simple_heap_update(rel, &tup->t_self, newtuple);
|
|
|
|
|
|
|
|
CatalogUpdateIndexes(rel, newtuple);
|
|
|
|
|
|
|
|
/* Rebuild dependencies */
|
|
|
|
GenerateTypeDependencies(typTup->typnamespace,
|
|
|
|
domainoid,
|
|
|
|
typTup->typrelid,
|
2003-08-04 02:43:34 +02:00
|
|
|
0, /* relation kind is n/a */
|
2005-07-07 22:40:02 +02:00
|
|
|
typTup->typowner,
|
2002-12-06 06:00:34 +01:00
|
|
|
typTup->typinput,
|
|
|
|
typTup->typoutput,
|
2003-05-09 00:19:58 +02:00
|
|
|
typTup->typreceive,
|
|
|
|
typTup->typsend,
|
2004-02-13 00:41:04 +01:00
|
|
|
typTup->typanalyze,
|
2002-12-06 06:00:34 +01:00
|
|
|
typTup->typelem,
|
|
|
|
typTup->typbasetype,
|
2003-01-08 22:40:39 +01:00
|
|
|
defaultExpr,
|
2003-08-04 02:43:34 +02:00
|
|
|
true); /* Rebuild is true */
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
heap_freetuple(newtuple);
|
2003-04-08 18:57:45 +02:00
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* AlterDomainNotNull
|
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Routine implementing ALTER DOMAIN SET/DROP NOT NULL statements.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterDomainNotNull(List *names, bool notNull)
|
|
|
|
{
|
|
|
|
TypeName *typename;
|
|
|
|
Oid domainoid;
|
2003-01-04 01:46:08 +01:00
|
|
|
Relation typrel;
|
2002-12-06 06:00:34 +01:00
|
|
|
HeapTuple tup;
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_type typTup;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
|
|
|
domainoid = typenameTypeId(NULL, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Look up the domain in the type table */
|
2005-04-14 22:03:27 +02:00
|
|
|
typrel = heap_open(TypeRelationId, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(domainoid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", domainoid);
|
2003-01-04 01:46:08 +01:00
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Check it's a domain and check user has permission for ALTER DOMAIN */
|
|
|
|
checkDomainOwner(tup, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Is the domain already set to the desired constraint? */
|
2002-12-06 06:00:34 +01:00
|
|
|
if (typTup->typnotnull == notNull)
|
2003-01-04 01:46:08 +01:00
|
|
|
{
|
|
|
|
heap_close(typrel, RowExclusiveLock);
|
|
|
|
return;
|
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Adding a NOT NULL constraint requires checking existing columns */
|
2002-12-06 06:00:34 +01:00
|
|
|
if (notNull)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
List *rels;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *rt;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Fetch relation list with attributes based on this domain */
|
2003-01-04 01:46:08 +01:00
|
|
|
/* ShareLock is sufficient to prevent concurrent data changes */
|
|
|
|
|
|
|
|
rels = get_rels_with_domain(domainoid, ShareLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
foreach(rt, rels)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
2003-01-04 01:46:08 +01:00
|
|
|
RelToCheck *rtc = (RelToCheck *) lfirst(rt);
|
|
|
|
Relation testrel = rtc->rel;
|
|
|
|
TupleDesc tupdesc = RelationGetDescr(testrel);
|
2002-12-06 06:00:34 +01:00
|
|
|
HeapScanDesc scan;
|
2003-01-04 01:46:08 +01:00
|
|
|
HeapTuple tuple;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Scan all tuples in this relation */
|
|
|
|
scan = heap_beginscan(testrel, SnapshotNow, 0, NULL);
|
2002-12-06 06:00:34 +01:00
|
|
|
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int i;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Test attributes that are of the domain */
|
2002-12-06 06:00:34 +01:00
|
|
|
for (i = 0; i < rtc->natts; i++)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int attnum = rtc->atts[i];
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2004-06-04 22:35:21 +02:00
|
|
|
if (heap_attisnull(tuple, attnum))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NOT_NULL_VIOLATION),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("column \"%s\" of table \"%s\" contains null values",
|
2005-10-15 04:49:52 +02:00
|
|
|
NameStr(tupdesc->attrs[attnum - 1]->attname),
|
|
|
|
RelationGetRelationName(testrel))));
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
heap_endscan(scan);
|
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Close each rel after processing, but keep lock */
|
|
|
|
heap_close(testrel, NoLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Okay to update pg_type row. We can scribble on typTup because it's a
|
|
|
|
* copy.
|
2003-01-04 01:46:08 +01:00
|
|
|
*/
|
|
|
|
typTup->typnotnull = notNull;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
simple_heap_update(typrel, &tup->t_self, tup);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
CatalogUpdateIndexes(typrel, tup);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Clean up */
|
2003-01-04 01:46:08 +01:00
|
|
|
heap_freetuple(tup);
|
|
|
|
heap_close(typrel, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AlterDomainDropConstraint
|
|
|
|
*
|
|
|
|
* Implements the ALTER DOMAIN DROP CONSTRAINT statement
|
|
|
|
*/
|
|
|
|
void
|
2006-03-14 23:48:25 +01:00
|
|
|
AlterDomainDropConstraint(List *names, const char *constrName,
|
|
|
|
DropBehavior behavior)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
|
|
|
TypeName *typename;
|
|
|
|
Oid domainoid;
|
|
|
|
HeapTuple tup;
|
|
|
|
Relation rel;
|
|
|
|
Relation conrel;
|
|
|
|
SysScanDesc conscan;
|
|
|
|
ScanKeyData key[1];
|
|
|
|
HeapTuple contup;
|
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
|
|
|
domainoid = typenameTypeId(NULL, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Look up the domain in the type table */
|
2005-04-14 22:03:27 +02:00
|
|
|
rel = heap_open(TypeRelationId, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(domainoid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", domainoid);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Check it's a domain and check user has permission for ALTER DOMAIN */
|
|
|
|
checkDomainOwner(tup, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Grab an appropriate lock on the pg_constraint relation */
|
2005-04-14 22:03:27 +02:00
|
|
|
conrel = heap_open(ConstraintRelationId, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Use the index to scan only constraints of the target relation */
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_constraint_contypid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(HeapTupleGetOid(tup)));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
conscan = systable_beginscan(conrel, ConstraintTypidIndexId, true,
|
2002-12-06 06:00:34 +01:00
|
|
|
SnapshotNow, 1, key);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan over the result set, removing any matching entries.
|
|
|
|
*/
|
|
|
|
while ((contup = systable_getnext(conscan)) != NULL)
|
|
|
|
{
|
|
|
|
Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(contup);
|
|
|
|
|
|
|
|
if (strcmp(NameStr(con->conname), constrName) == 0)
|
|
|
|
{
|
|
|
|
ObjectAddress conobj;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
conobj.classId = ConstraintRelationId;
|
2002-12-06 06:00:34 +01:00
|
|
|
conobj.objectId = HeapTupleGetOid(contup);
|
|
|
|
conobj.objectSubId = 0;
|
|
|
|
|
|
|
|
performDeletion(&conobj, behavior);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Clean up after the scan */
|
|
|
|
systable_endscan(conscan);
|
|
|
|
heap_close(conrel, RowExclusiveLock);
|
|
|
|
|
|
|
|
heap_close(rel, NoLock);
|
2003-04-08 18:57:45 +02:00
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* AlterDomainAddConstraint
|
|
|
|
*
|
|
|
|
* Implements the ALTER DOMAIN .. ADD CONSTRAINT statement.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterDomainAddConstraint(List *names, Node *newConstraint)
|
|
|
|
{
|
|
|
|
TypeName *typename;
|
|
|
|
Oid domainoid;
|
2003-01-04 01:46:08 +01:00
|
|
|
Relation typrel;
|
2002-12-06 06:00:34 +01:00
|
|
|
HeapTuple tup;
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_type typTup;
|
|
|
|
List *rels;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *rt;
|
2003-08-04 02:43:34 +02:00
|
|
|
EState *estate;
|
2002-12-12 21:35:16 +01:00
|
|
|
ExprContext *econtext;
|
2003-08-04 02:43:34 +02:00
|
|
|
char *ccbin;
|
|
|
|
Expr *expr;
|
|
|
|
ExprState *exprstate;
|
2002-12-06 06:00:34 +01:00
|
|
|
Constraint *constr;
|
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
|
|
|
domainoid = typenameTypeId(NULL, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Look up the domain in the type table */
|
2005-04-14 22:03:27 +02:00
|
|
|
typrel = heap_open(TypeRelationId, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(domainoid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", domainoid);
|
2002-12-09 21:31:05 +01:00
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Check it's a domain and check user has permission for ALTER DOMAIN */
|
|
|
|
checkDomainOwner(tup, typename);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-12-09 21:31:05 +01:00
|
|
|
/* Check for unsupported constraint types */
|
|
|
|
if (IsA(newConstraint, FkConstraint))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
2003-09-10 01:22:21 +02:00
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("foreign key constraints not possible for domains")));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
/* otherwise it should be a plain Constraint */
|
2002-12-09 21:31:05 +01:00
|
|
|
if (!IsA(newConstraint, Constraint))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "unrecognized node type: %d",
|
|
|
|
(int) nodeTag(newConstraint));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
constr = (Constraint *) newConstraint;
|
2002-12-09 21:31:05 +01:00
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
switch (constr->contype)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
case CONSTR_CHECK:
|
2002-12-09 21:31:05 +01:00
|
|
|
/* processed below */
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-12-09 21:31:05 +01:00
|
|
|
case CONSTR_UNIQUE:
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
2003-09-10 01:22:21 +02:00
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("unique constraints not possible for domains")));
|
2002-12-09 21:31:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSTR_PRIMARY:
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
2003-09-10 01:22:21 +02:00
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("primary key constraints not possible for domains")));
|
2002-12-09 21:31:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSTR_ATTR_DEFERRABLE:
|
|
|
|
case CONSTR_ATTR_NOT_DEFERRABLE:
|
|
|
|
case CONSTR_ATTR_DEFERRED:
|
|
|
|
case CONSTR_ATTR_IMMEDIATE:
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2003-09-10 01:22:21 +02:00
|
|
|
errmsg("specifying constraint deferrability not supported for domains")));
|
2002-12-09 21:31:05 +01:00
|
|
|
break;
|
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
default:
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "unrecognized constraint subtype: %d",
|
|
|
|
(int) constr->contype);
|
2002-12-06 06:00:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Since all other constraint types throw errors, this must be a check
|
2005-10-15 04:49:52 +02:00
|
|
|
* constraint. First, process the constraint expression and add an entry
|
|
|
|
* to pg_constraint.
|
2002-12-09 21:31:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ccbin = domainAddConstraint(HeapTupleGetOid(tup), typTup->typnamespace,
|
|
|
|
typTup->typbasetype, typTup->typtypmod,
|
2004-06-10 19:56:03 +02:00
|
|
|
constr, NameStr(typTup->typname));
|
2002-12-09 21:31:05 +01:00
|
|
|
|
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Test all values stored in the attributes based on the domain the
|
|
|
|
* constraint is being added to.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
2002-12-13 20:46:01 +01:00
|
|
|
expr = (Expr *) stringToNode(ccbin);
|
2002-12-12 21:35:16 +01:00
|
|
|
|
2002-12-15 17:17:59 +01:00
|
|
|
/* Need an EState to run ExecEvalExpr */
|
|
|
|
estate = CreateExecutorState();
|
|
|
|
econtext = GetPerTupleExprContext(estate);
|
|
|
|
|
|
|
|
/* build execution state for expr */
|
|
|
|
exprstate = ExecPrepareExpr(expr, estate);
|
2002-12-12 21:35:16 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Fetch relation list with attributes based on this domain */
|
|
|
|
/* ShareLock is sufficient to prevent concurrent data changes */
|
|
|
|
|
|
|
|
rels = get_rels_with_domain(domainoid, ShareLock);
|
2002-12-12 16:49:42 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
foreach(rt, rels)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
2003-01-04 01:46:08 +01:00
|
|
|
RelToCheck *rtc = (RelToCheck *) lfirst(rt);
|
|
|
|
Relation testrel = rtc->rel;
|
|
|
|
TupleDesc tupdesc = RelationGetDescr(testrel);
|
2002-12-06 06:00:34 +01:00
|
|
|
HeapScanDesc scan;
|
2003-01-04 01:46:08 +01:00
|
|
|
HeapTuple tuple;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Scan all tuples in this relation */
|
2002-12-12 21:35:16 +01:00
|
|
|
scan = heap_beginscan(testrel, SnapshotNow, 0, NULL);
|
2002-12-06 06:00:34 +01:00
|
|
|
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int i;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Test attributes that are of the domain */
|
2002-12-06 06:00:34 +01:00
|
|
|
for (i = 0; i < rtc->natts; i++)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int attnum = rtc->atts[i];
|
|
|
|
Datum d;
|
|
|
|
bool isNull;
|
|
|
|
Datum conResult;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
d = heap_getattr(tuple, attnum, tupdesc, &isNull);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
econtext->domainValue_datum = d;
|
|
|
|
econtext->domainValue_isNull = isNull;
|
|
|
|
|
2002-12-15 17:17:59 +01:00
|
|
|
conResult = ExecEvalExprSwitchContext(exprstate,
|
|
|
|
econtext,
|
|
|
|
&isNull, NULL);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-12-12 21:35:16 +01:00
|
|
|
if (!isNull && !DatumGetBool(conResult))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CHECK_VIOLATION),
|
2003-09-29 02:05:25 +02:00
|
|
|
errmsg("column \"%s\" of table \"%s\" contains values that violate the new constraint",
|
2005-10-15 04:49:52 +02:00
|
|
|
NameStr(tupdesc->attrs[attnum - 1]->attname),
|
2003-09-29 02:05:25 +02:00
|
|
|
RelationGetRelationName(testrel))));
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ResetExprContext(econtext);
|
|
|
|
}
|
|
|
|
heap_endscan(scan);
|
|
|
|
|
2002-12-12 21:35:16 +01:00
|
|
|
/* Hold relation lock till commit (XXX bad for concurrency) */
|
|
|
|
heap_close(testrel, NoLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
|
2002-12-15 17:17:59 +01:00
|
|
|
FreeExecutorState(estate);
|
2002-12-12 21:35:16 +01:00
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
/* Clean up */
|
2003-01-04 01:46:08 +01:00
|
|
|
heap_close(typrel, RowExclusiveLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_rels_with_domain
|
|
|
|
*
|
|
|
|
* Fetch all relations / attributes which are using the domain
|
2003-01-04 01:46:08 +01:00
|
|
|
*
|
|
|
|
* The result is a list of RelToCheck structs, one for each distinct
|
|
|
|
* relation, each containing one or more attribute numbers that are of
|
|
|
|
* the domain type. We have opened each rel and acquired the specified lock
|
|
|
|
* type on it.
|
|
|
|
*
|
|
|
|
* XXX this is completely broken because there is no way to lock the domain
|
|
|
|
* to prevent columns from being added or dropped while our command runs.
|
|
|
|
* We can partially protect against column drops by locking relations as we
|
|
|
|
* come across them, but there is still a race condition (the window between
|
|
|
|
* seeing a pg_depend entry and acquiring lock on the relation it references).
|
|
|
|
* Also, holding locks on all these relations simultaneously creates a non-
|
|
|
|
* trivial risk of deadlock. We can minimize but not eliminate the deadlock
|
|
|
|
* risk by using the weakest suitable lock (ShareLock for most callers).
|
|
|
|
*
|
|
|
|
* XXX to support domains over domains, we'd need to make this smarter,
|
|
|
|
* or make its callers smarter, so that we could find columns of derived
|
|
|
|
* domains. Arrays of domains would be a problem too.
|
2002-12-06 06:00:34 +01:00
|
|
|
*
|
|
|
|
* Generally used for retrieving a list of tests when adding
|
|
|
|
* new constraints to a domain.
|
|
|
|
*/
|
2003-01-04 01:46:08 +01:00
|
|
|
static List *
|
|
|
|
get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
List *result = NIL;
|
2003-01-04 01:46:08 +01:00
|
|
|
Relation depRel;
|
|
|
|
ScanKeyData key[2];
|
|
|
|
SysScanDesc depScan;
|
|
|
|
HeapTuple depTup;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We scan pg_depend to find those things that depend on the domain. (We
|
|
|
|
* assume we can ignore refobjsubid for a domain.)
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
depRel = heap_open(DependRelationId, AccessShareLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_depend_refclassid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
2005-04-14 03:38:22 +02:00
|
|
|
ObjectIdGetDatum(TypeRelationId));
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[1],
|
|
|
|
Anum_pg_depend_refobjid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(domainOid));
|
2003-01-04 01:46:08 +01:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
|
2003-01-04 01:46:08 +01:00
|
|
|
SnapshotNow, 2, key);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
|
2003-01-04 01:46:08 +01:00
|
|
|
RelToCheck *rtc = NULL;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *rellist;
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_attribute pg_att;
|
2003-01-04 01:46:08 +01:00
|
|
|
int ptr;
|
|
|
|
|
2004-05-05 19:06:56 +02:00
|
|
|
/* Ignore dependees that aren't user columns of relations */
|
2003-01-04 01:46:08 +01:00
|
|
|
/* (we assume system columns are never of domain types) */
|
2005-04-14 03:38:22 +02:00
|
|
|
if (pg_depend->classid != RelationRelationId ||
|
2003-01-04 01:46:08 +01:00
|
|
|
pg_depend->objsubid <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* See if we already have an entry for this relation */
|
|
|
|
foreach(rellist, result)
|
|
|
|
{
|
|
|
|
RelToCheck *rt = (RelToCheck *) lfirst(rellist);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
if (RelationGetRelid(rt->rel) == pg_depend->objid)
|
|
|
|
{
|
|
|
|
rtc = rt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
if (rtc == NULL)
|
|
|
|
{
|
|
|
|
/* First attribute found for this relation */
|
|
|
|
Relation rel;
|
|
|
|
|
|
|
|
/* Acquire requested lock on relation */
|
2004-05-05 19:06:56 +02:00
|
|
|
rel = relation_open(pg_depend->objid, lockmode);
|
|
|
|
|
|
|
|
/* It could be a view or composite type; if so ignore it */
|
|
|
|
if (rel->rd_rel->relkind != RELKIND_RELATION)
|
|
|
|
{
|
|
|
|
relation_close(rel, lockmode);
|
|
|
|
continue;
|
|
|
|
}
|
2003-01-04 01:46:08 +01:00
|
|
|
|
|
|
|
/* Build the RelToCheck entry with enough space for all atts */
|
|
|
|
rtc = (RelToCheck *) palloc(sizeof(RelToCheck));
|
|
|
|
rtc->rel = rel;
|
|
|
|
rtc->natts = 0;
|
|
|
|
rtc->atts = (int *) palloc(sizeof(int) * RelationGetNumberOfAttributes(rel));
|
|
|
|
result = lcons(rtc, result);
|
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Confirm column has not been dropped, and is of the expected type.
|
|
|
|
* This defends against an ALTER DROP COLUMN occuring just before we
|
|
|
|
* acquired lock ... but if the whole table were dropped, we'd still
|
|
|
|
* have a problem.
|
2003-01-04 01:46:08 +01:00
|
|
|
*/
|
|
|
|
if (pg_depend->objsubid > RelationGetNumberOfAttributes(rtc->rel))
|
|
|
|
continue;
|
|
|
|
pg_att = rtc->rel->rd_att->attrs[pg_depend->objsubid - 1];
|
|
|
|
if (pg_att->attisdropped || pg_att->atttypid != domainOid)
|
|
|
|
continue;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Okay, add column to result. We store the columns in column-number
|
|
|
|
* order; this is just a hack to improve predictability of regression
|
|
|
|
* test output ...
|
2003-01-04 01:46:08 +01:00
|
|
|
*/
|
|
|
|
Assert(rtc->natts < RelationGetNumberOfAttributes(rtc->rel));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
ptr = rtc->natts++;
|
2003-08-04 02:43:34 +02:00
|
|
|
while (ptr > 0 && rtc->atts[ptr - 1] > pg_depend->objsubid)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
rtc->atts[ptr] = rtc->atts[ptr - 1];
|
2003-01-04 01:46:08 +01:00
|
|
|
ptr--;
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
2003-01-04 01:46:08 +01:00
|
|
|
rtc->atts[ptr] = pg_depend->objsubid;
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
systable_endscan(depScan);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
relation_close(depRel, AccessShareLock);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
return result;
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-14 23:48:25 +01:00
|
|
|
* checkDomainOwner
|
2002-12-06 06:00:34 +01:00
|
|
|
*
|
2006-03-14 23:48:25 +01:00
|
|
|
* Check that the type is actually a domain and that the current user
|
|
|
|
* has permission to do ALTER DOMAIN on it. Throw an error if not.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
2003-01-04 01:46:08 +01:00
|
|
|
static void
|
2006-03-14 23:48:25 +01:00
|
|
|
checkDomainOwner(HeapTuple tup, TypeName *typename)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_type typTup = (Form_pg_type) GETSTRUCT(tup);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/* Check that this is actually a domain */
|
|
|
|
if (typTup->typtype != 'd')
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a domain",
|
|
|
|
TypeNameToString(typename))));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2003-01-04 01:46:08 +01:00
|
|
|
/* Permission check: must own type */
|
|
|
|
if (!pg_type_ownercheck(HeapTupleGetOid(tup), GetUserId()))
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
|
|
|
|
TypeNameToString(typename));
|
2003-01-04 01:46:08 +01:00
|
|
|
}
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
2002-12-12 21:35:16 +01:00
|
|
|
* domainAddConstraint - code shared between CREATE and ALTER DOMAIN
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
2002-12-12 21:35:16 +01:00
|
|
|
static char *
|
2002-12-06 06:00:34 +01:00
|
|
|
domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
|
2002-12-12 21:35:16 +01:00
|
|
|
int typMod, Constraint *constr,
|
2004-06-10 19:56:03 +02:00
|
|
|
char *domainName)
|
2002-12-06 06:00:34 +01:00
|
|
|
{
|
|
|
|
Node *expr;
|
|
|
|
char *ccsrc;
|
|
|
|
char *ccbin;
|
|
|
|
ParseState *pstate;
|
2003-08-04 02:43:34 +02:00
|
|
|
CoerceToDomainValue *domVal;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Assign or validate constraint name
|
|
|
|
*/
|
|
|
|
if (constr->name)
|
|
|
|
{
|
|
|
|
if (ConstraintNameIsUsed(CONSTRAINT_DOMAIN,
|
|
|
|
domainOid,
|
|
|
|
domainNamespace,
|
|
|
|
constr->name))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("constraint \"%s\" for domain \"%s\" already exists",
|
|
|
|
constr->name, domainName)));
|
2002-12-06 06:00:34 +01:00
|
|
|
}
|
|
|
|
else
|
2004-06-10 19:56:03 +02:00
|
|
|
constr->name = ChooseConstraintName(domainName,
|
|
|
|
NULL,
|
|
|
|
"check",
|
|
|
|
domainNamespace,
|
|
|
|
NIL);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
2002-12-12 21:35:16 +01:00
|
|
|
* Convert the A_EXPR in raw_expr into an EXPR
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
pstate = make_parsestate(NULL);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Set up a CoerceToDomainValue to represent the occurrence of VALUE in
|
|
|
|
* the expression. Note that it will appear to have the type of the base
|
|
|
|
* type, not the domain. This seems correct since within the check
|
|
|
|
* expression, we should not assume the input value can be considered a
|
|
|
|
* member of the domain.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
2003-02-03 22:15:45 +01:00
|
|
|
domVal = makeNode(CoerceToDomainValue);
|
2002-12-06 06:00:34 +01:00
|
|
|
domVal->typeId = baseTypeOid;
|
|
|
|
domVal->typeMod = typMod;
|
|
|
|
|
2002-12-12 21:35:16 +01:00
|
|
|
pstate->p_value_substitute = (Node *) domVal;
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-12-12 21:35:16 +01:00
|
|
|
expr = transformExpr(pstate, constr->raw_expr);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure it yields a boolean result.
|
|
|
|
*/
|
2003-04-30 00:13:11 +02:00
|
|
|
expr = coerce_to_boolean(pstate, expr, "CHECK");
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
2003-07-20 23:56:35 +02:00
|
|
|
* Make sure no outside relations are referred to.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
2004-05-26 06:41:50 +02:00
|
|
|
if (list_length(pstate->p_rtable) != 0)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("cannot use table references in domain check constraint")));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-12-12 21:35:16 +01:00
|
|
|
/*
|
|
|
|
* Domains don't allow var clauses (this should be redundant with the
|
|
|
|
* above check, but make it anyway)
|
|
|
|
*/
|
|
|
|
if (contain_var_clause(expr))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("cannot use table references in domain check constraint")));
|
2002-12-12 21:35:16 +01:00
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
/*
|
|
|
|
* No subplans or aggregates, either...
|
|
|
|
*/
|
2003-06-06 17:04:03 +02:00
|
|
|
if (pstate->p_hasSubLinks)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("cannot use subquery in check constraint")));
|
2003-06-06 17:04:03 +02:00
|
|
|
if (pstate->p_hasAggs)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
2006-06-21 20:09:53 +02:00
|
|
|
errmsg("cannot use aggregate function in check constraint")));
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
2002-12-12 16:49:42 +01:00
|
|
|
* Convert to string form for storage.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
ccbin = nodeToString(expr);
|
|
|
|
|
|
|
|
/*
|
2002-12-12 16:49:42 +01:00
|
|
|
* Deparse it to produce text for consrc.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Since VARNOs aren't allowed in domain constraints, relation context
|
|
|
|
* isn't required as anything other than a shell.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
ccsrc = deparse_expression(expr,
|
2002-12-12 16:49:42 +01:00
|
|
|
deparse_context_for(domainName,
|
|
|
|
InvalidOid),
|
|
|
|
false, false);
|
2002-12-06 06:00:34 +01:00
|
|
|
|
2002-12-12 21:35:16 +01:00
|
|
|
/*
|
|
|
|
* Store the constraint in pg_constraint
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
CreateConstraintEntry(constr->name, /* Constraint Name */
|
|
|
|
domainNamespace, /* namespace */
|
2002-12-06 06:00:34 +01:00
|
|
|
CONSTRAINT_CHECK, /* Constraint Type */
|
|
|
|
false, /* Is Deferrable */
|
|
|
|
false, /* Is Deferred */
|
2003-08-04 02:43:34 +02:00
|
|
|
InvalidOid, /* not a relation constraint */
|
|
|
|
NULL,
|
2002-12-06 06:00:34 +01:00
|
|
|
0,
|
|
|
|
domainOid, /* domain constraint */
|
|
|
|
InvalidOid, /* Foreign key fields */
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
' ',
|
|
|
|
' ',
|
|
|
|
' ',
|
|
|
|
InvalidOid,
|
2003-08-04 02:43:34 +02:00
|
|
|
expr, /* Tree form check constraint */
|
2002-12-06 06:00:34 +01:00
|
|
|
ccbin, /* Binary form check constraint */
|
2003-08-04 02:43:34 +02:00
|
|
|
ccsrc); /* Source form check constraint */
|
2002-12-06 06:00:34 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Return the compiled constraint expression so the calling routine can
|
|
|
|
* perform any additional required tests.
|
2002-12-06 06:00:34 +01:00
|
|
|
*/
|
|
|
|
return ccbin;
|
|
|
|
}
|
2003-01-06 01:31:45 +01:00
|
|
|
|
2003-02-03 22:15:45 +01:00
|
|
|
/*
|
|
|
|
* GetDomainConstraints - get a list of the current constraints of domain
|
|
|
|
*
|
|
|
|
* Returns a possibly-empty list of DomainConstraintState nodes.
|
|
|
|
*
|
|
|
|
* This is called by the executor during plan startup for a CoerceToDomain
|
|
|
|
* expression node. The given constraints will be checked for each value
|
|
|
|
* passed through the node.
|
2005-01-25 00:21:57 +01:00
|
|
|
*
|
|
|
|
* We allow this to be called for non-domain types, in which case the result
|
|
|
|
* is always NIL.
|
2003-02-03 22:15:45 +01:00
|
|
|
*/
|
|
|
|
List *
|
|
|
|
GetDomainConstraints(Oid typeOid)
|
|
|
|
{
|
|
|
|
List *result = NIL;
|
|
|
|
bool notNull = false;
|
|
|
|
Relation conRel;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
conRel = heap_open(ConstraintRelationId, AccessShareLock);
|
2003-02-03 22:15:45 +01:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
HeapTuple conTup;
|
|
|
|
Form_pg_type typTup;
|
|
|
|
ScanKeyData key[1];
|
|
|
|
SysScanDesc scan;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-02-03 22:15:45 +01:00
|
|
|
tup = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(typeOid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeOid);
|
2003-02-03 22:15:45 +01:00
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
|
2005-01-25 00:21:57 +01:00
|
|
|
if (typTup->typtype != 'd')
|
|
|
|
{
|
|
|
|
/* Not a domain, so done */
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-02-03 22:15:45 +01:00
|
|
|
/* Test for NOT NULL Constraint */
|
|
|
|
if (typTup->typnotnull)
|
|
|
|
notNull = true;
|
|
|
|
|
|
|
|
/* Look for CHECK Constraints on this domain */
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_constraint_contypid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(typeOid));
|
2003-02-03 22:15:45 +01:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(conRel, ConstraintTypidIndexId, true,
|
2003-02-03 22:15:45 +01:00
|
|
|
SnapshotNow, 1, key);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(conTup = systable_getnext(scan)))
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_constraint c = (Form_pg_constraint) GETSTRUCT(conTup);
|
|
|
|
Datum val;
|
|
|
|
bool isNull;
|
|
|
|
Expr *check_expr;
|
2003-02-03 22:15:45 +01:00
|
|
|
DomainConstraintState *r;
|
|
|
|
|
|
|
|
/* Ignore non-CHECK constraints (presently, shouldn't be any) */
|
|
|
|
if (c->contype != CONSTRAINT_CHECK)
|
|
|
|
continue;
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Not expecting conbin to be NULL, but we'll test for it anyway
|
2003-08-04 02:43:34 +02:00
|
|
|
*/
|
2003-02-03 22:15:45 +01:00
|
|
|
val = fastgetattr(conTup, Anum_pg_constraint_conbin,
|
|
|
|
conRel->rd_att, &isNull);
|
|
|
|
if (isNull)
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "domain \"%s\" constraint \"%s\" has NULL conbin",
|
2003-02-03 22:15:45 +01:00
|
|
|
NameStr(typTup->typname), NameStr(c->conname));
|
|
|
|
|
|
|
|
check_expr = (Expr *)
|
|
|
|
stringToNode(DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
val)));
|
|
|
|
|
|
|
|
/* ExecInitExpr assumes we already fixed opfuncids */
|
|
|
|
fix_opfuncids((Node *) check_expr);
|
|
|
|
|
|
|
|
r = makeNode(DomainConstraintState);
|
|
|
|
r->constrainttype = DOM_CONSTRAINT_CHECK;
|
|
|
|
r->name = pstrdup(NameStr(c->conname));
|
|
|
|
r->check_expr = ExecInitExpr(check_expr, NULL);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* use lcons() here because constraints of lower domains should be
|
|
|
|
* applied earlier.
|
2003-02-03 22:15:45 +01:00
|
|
|
*/
|
|
|
|
result = lcons(r, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
2005-01-25 00:21:57 +01:00
|
|
|
/* loop to next domain in stack */
|
2003-02-03 22:15:45 +01:00
|
|
|
typeOid = typTup->typbasetype;
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_close(conRel, AccessShareLock);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Only need to add one NOT NULL check regardless of how many domains in
|
|
|
|
* the stack request it.
|
2003-02-03 22:15:45 +01:00
|
|
|
*/
|
|
|
|
if (notNull)
|
|
|
|
{
|
|
|
|
DomainConstraintState *r = makeNode(DomainConstraintState);
|
|
|
|
|
|
|
|
r->constrainttype = DOM_CONSTRAINT_NOTNULL;
|
|
|
|
r->name = pstrdup("NOT NULL");
|
|
|
|
r->check_expr = NULL;
|
|
|
|
|
|
|
|
/* lcons to apply the nullness check FIRST */
|
|
|
|
result = lcons(r, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-01-06 01:31:45 +01:00
|
|
|
/*
|
2004-06-25 23:55:59 +02:00
|
|
|
* Change the owner of a type.
|
2003-01-06 01:31:45 +01:00
|
|
|
*/
|
|
|
|
void
|
2005-06-28 07:09:14 +02:00
|
|
|
AlterTypeOwner(List *names, Oid newOwnerId)
|
2003-01-06 01:31:45 +01:00
|
|
|
{
|
|
|
|
TypeName *typename;
|
|
|
|
Oid typeOid;
|
|
|
|
Relation rel;
|
|
|
|
HeapTuple tup;
|
2003-08-04 02:43:34 +02:00
|
|
|
Form_pg_type typTup;
|
2005-07-14 23:46:30 +02:00
|
|
|
AclResult aclresult;
|
2003-01-06 01:31:45 +01:00
|
|
|
|
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
2006-03-14 23:48:25 +01:00
|
|
|
typename = makeTypeNameFromNameList(names);
|
2003-01-06 01:31:45 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Use LookupTypeName here so that shell types can be processed */
|
|
|
|
typeOid = LookupTypeName(NULL, typename);
|
2003-01-06 01:31:45 +01:00
|
|
|
if (!OidIsValid(typeOid))
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("type \"%s\" does not exist",
|
|
|
|
TypeNameToString(typename))));
|
2003-01-06 01:31:45 +01:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Look up the type in the type table */
|
|
|
|
rel = heap_open(TypeRelationId, RowExclusiveLock);
|
|
|
|
|
2003-01-06 01:31:45 +01:00
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(typeOid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeOid);
|
2003-01-06 01:31:45 +01:00
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* If it's a composite type, we need to check that it really is a
|
|
|
|
* free-standing composite type, and not a table's underlying type. We
|
|
|
|
* want people to use ALTER TABLE not ALTER TYPE for that case.
|
2004-06-25 23:55:59 +02:00
|
|
|
*/
|
2005-08-04 03:09:29 +02:00
|
|
|
if (typTup->typtype == 'c' &&
|
|
|
|
get_rel_relkind(typTup->typrelid) != RELKIND_COMPOSITE_TYPE)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2004-06-25 23:55:59 +02:00
|
|
|
errmsg("\"%s\" is a table's row type",
|
2003-07-20 23:56:35 +02:00
|
|
|
TypeNameToString(typename))));
|
2003-01-06 01:31:45 +01:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2004-06-25 23:55:59 +02:00
|
|
|
* If the new owner is the same as the existing owner, consider the
|
|
|
|
* command to have succeeded. This is for dump restoration purposes.
|
|
|
|
*/
|
2005-06-28 07:09:14 +02:00
|
|
|
if (typTup->typowner != newOwnerId)
|
2004-06-25 23:55:59 +02:00
|
|
|
{
|
2005-08-22 19:38:20 +02:00
|
|
|
/* Superusers can always do it */
|
|
|
|
if (!superuser())
|
|
|
|
{
|
|
|
|
/* Otherwise, must be owner of the existing object */
|
2005-10-15 04:49:52 +02:00
|
|
|
if (!pg_type_ownercheck(HeapTupleGetOid(tup), GetUserId()))
|
2005-08-22 19:38:20 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
|
|
|
|
TypeNameToString(typename));
|
|
|
|
|
|
|
|
/* Must be able to become new owner */
|
|
|
|
check_is_member_of_role(GetUserId(), newOwnerId);
|
|
|
|
|
|
|
|
/* New owner must have CREATE privilege on namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(typTup->typnamespace,
|
|
|
|
newOwnerId,
|
|
|
|
ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
get_namespace_name(typTup->typnamespace));
|
|
|
|
}
|
2004-06-25 23:55:59 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Modify the owner --- okay to scribble on typTup because it's a copy
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2005-06-28 07:09:14 +02:00
|
|
|
typTup->typowner = newOwnerId;
|
2003-01-06 01:31:45 +01:00
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
simple_heap_update(rel, &tup->t_self, tup);
|
2003-01-06 01:31:45 +01:00
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
CatalogUpdateIndexes(rel, tup);
|
2005-07-07 22:40:02 +02:00
|
|
|
|
|
|
|
/* Update owner dependency reference */
|
|
|
|
changeDependencyOnOwner(TypeRelationId, typeOid, newOwnerId);
|
2004-06-25 23:55:59 +02:00
|
|
|
}
|
2003-01-06 01:31:45 +01:00
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
heap_close(rel, RowExclusiveLock);
|
|
|
|
}
|
2005-08-01 06:03:59 +02:00
|
|
|
|
2005-08-04 03:09:29 +02:00
|
|
|
/*
|
|
|
|
* AlterTypeOwnerInternal - change type owner unconditionally
|
|
|
|
*
|
|
|
|
* This is currently only used to propagate ALTER TABLE OWNER to the
|
2005-11-21 13:49:33 +01:00
|
|
|
* table's rowtype, and to implement REASSIGN OWNED BY. It assumes the
|
|
|
|
* caller has done all needed checks.
|
2005-08-04 03:09:29 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_type typTup;
|
|
|
|
|
|
|
|
rel = heap_open(TypeRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(typeOid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeOid);
|
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Modify the owner --- okay to scribble on typTup because it's a copy
|
2005-08-04 03:09:29 +02:00
|
|
|
*/
|
|
|
|
typTup->typowner = newOwnerId;
|
|
|
|
|
|
|
|
simple_heap_update(rel, &tup->t_self, tup);
|
|
|
|
|
|
|
|
CatalogUpdateIndexes(rel, tup);
|
|
|
|
|
|
|
|
/* Update owner dependency reference */
|
|
|
|
changeDependencyOnOwner(TypeRelationId, typeOid, newOwnerId);
|
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
heap_close(rel, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
|
2005-08-01 06:03:59 +02:00
|
|
|
/*
|
|
|
|
* Execute ALTER TYPE SET SCHEMA
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterTypeNamespace(List *names, const char *newschema)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
TypeName *typename;
|
|
|
|
Oid typeOid;
|
|
|
|
Oid nspOid;
|
2005-08-01 06:03:59 +02:00
|
|
|
|
2006-03-14 23:48:25 +01:00
|
|
|
/* Make a TypeName so we can use standard type lookup machinery */
|
|
|
|
typename = makeTypeNameFromNameList(names);
|
|
|
|
typeOid = typenameTypeId(NULL, typename);
|
2005-08-01 06:03:59 +02:00
|
|
|
|
|
|
|
/* check permissions on type */
|
|
|
|
if (!pg_type_ownercheck(typeOid, GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
|
|
|
|
format_type_be(typeOid));
|
|
|
|
|
|
|
|
/* get schema OID and check its permissions */
|
|
|
|
nspOid = LookupCreationNamespace(newschema);
|
|
|
|
|
|
|
|
/* and do the work */
|
|
|
|
AlterTypeNamespaceInternal(typeOid, nspOid, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move specified type to new namespace.
|
|
|
|
*
|
|
|
|
* Caller must have already checked privileges.
|
|
|
|
*
|
|
|
|
* If errorOnTableType is TRUE, the function errors out if the type is
|
|
|
|
* a table type. ALTER TABLE has to be used to move a table to a new
|
|
|
|
* namespace.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
|
|
|
|
bool errorOnTableType)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_type typform;
|
|
|
|
Oid oldNspOid;
|
|
|
|
bool isCompositeType;
|
|
|
|
|
|
|
|
rel = heap_open(TypeRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
tup = SearchSysCacheCopy(TYPEOID,
|
|
|
|
ObjectIdGetDatum(typeOid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup failed for type %u", typeOid);
|
|
|
|
typform = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
oldNspOid = typform->typnamespace;
|
|
|
|
|
|
|
|
if (oldNspOid == nspOid)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("type %s is already in schema \"%s\"",
|
|
|
|
format_type_be(typeOid),
|
|
|
|
get_namespace_name(nspOid))));
|
|
|
|
|
|
|
|
/* disallow renaming into or out of temp schemas */
|
|
|
|
if (isAnyTempNamespace(nspOid) || isAnyTempNamespace(oldNspOid))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("cannot move objects into or out of temporary schemas")));
|
2005-08-01 06:03:59 +02:00
|
|
|
|
|
|
|
/* same for TOAST schema */
|
|
|
|
if (nspOid == PG_TOAST_NAMESPACE || oldNspOid == PG_TOAST_NAMESPACE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot move objects into or out of TOAST schema")));
|
|
|
|
|
|
|
|
/* check for duplicate name (more friendly than unique-index failure) */
|
|
|
|
if (SearchSysCacheExists(TYPENAMENSP,
|
|
|
|
CStringGetDatum(NameStr(typform->typname)),
|
|
|
|
ObjectIdGetDatum(nspOid),
|
|
|
|
0, 0))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("type \"%s\" already exists in schema \"%s\"",
|
|
|
|
NameStr(typform->typname),
|
|
|
|
get_namespace_name(nspOid))));
|
|
|
|
|
|
|
|
/* Detect whether type is a composite type (but not a table rowtype) */
|
|
|
|
isCompositeType =
|
|
|
|
(typform->typtype == 'c' &&
|
|
|
|
get_rel_relkind(typform->typrelid) == RELKIND_COMPOSITE_TYPE);
|
|
|
|
|
|
|
|
/* Enforce not-table-type if requested */
|
|
|
|
if (typform->typtype == 'c' && !isCompositeType && errorOnTableType)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("%s is a table's row type",
|
|
|
|
format_type_be(typeOid)),
|
|
|
|
errhint("Use ALTER TABLE SET SCHEMA instead.")));
|
|
|
|
|
|
|
|
/* OK, modify the pg_type row */
|
|
|
|
|
|
|
|
/* tup is a copy, so we can scribble directly on it */
|
|
|
|
typform->typnamespace = nspOid;
|
|
|
|
|
|
|
|
simple_heap_update(rel, &tup->t_self, tup);
|
|
|
|
CatalogUpdateIndexes(rel, tup);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Composite types have pg_class entries.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We need to modify the pg_class tuple as well to reflect the change of
|
|
|
|
* schema.
|
2005-08-01 06:03:59 +02:00
|
|
|
*/
|
|
|
|
if (isCompositeType)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Relation classRel;
|
2005-08-01 06:03:59 +02:00
|
|
|
|
|
|
|
classRel = heap_open(RelationRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The dependency on the schema is listed under the pg_class entry, so
|
|
|
|
* tell AlterRelationNamespaceInternal to fix it.
|
2005-08-01 06:03:59 +02:00
|
|
|
*/
|
|
|
|
AlterRelationNamespaceInternal(classRel, typform->typrelid,
|
|
|
|
oldNspOid, nspOid,
|
|
|
|
true);
|
|
|
|
|
|
|
|
heap_close(classRel, RowExclusiveLock);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check for constraints associated with the composite type (we don't
|
|
|
|
* currently support this, but probably will someday).
|
2005-08-01 06:03:59 +02:00
|
|
|
*/
|
|
|
|
AlterConstraintNamespaces(typform->typrelid, oldNspOid,
|
|
|
|
nspOid, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If it's a domain, it might have constraints */
|
|
|
|
if (typform->typtype == 'd')
|
|
|
|
AlterConstraintNamespaces(typeOid, oldNspOid, nspOid, true);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Update dependency on schema, if any --- a table rowtype has not got
|
|
|
|
* one.
|
2005-08-01 06:03:59 +02:00
|
|
|
*/
|
|
|
|
if (typform->typtype != 'c')
|
|
|
|
if (changeDependencyFor(TypeRelationId, typeOid,
|
2005-10-15 04:49:52 +02:00
|
|
|
NamespaceRelationId, oldNspOid, nspOid) != 1)
|
2005-08-01 06:03:59 +02:00
|
|
|
elog(ERROR, "failed to change schema dependency for type %s",
|
|
|
|
format_type_be(typeOid));
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_freetuple(tup);
|
|
|
|
|
|
|
|
heap_close(rel, RowExclusiveLock);
|
|
|
|
}
|