Allow CREATE TABLE IF EXIST so succeed if the schema is nonexistent

Previously, CREATE TABLE IF EXIST threw an error if the schema was
nonexistent.  This was done by passing 'missing_ok' to the function that
looks up the schema oid.
This commit is contained in:
Bruce Momjian 2013-01-26 13:24:50 -05:00
parent 7c83619b50
commit 7e2322dff3
9 changed files with 68 additions and 41 deletions

View File

@ -755,7 +755,7 @@ objectsInSchemaToOids(GrantObjectType objtype, List *nspnames)
Oid namespaceId; Oid namespaceId;
List *objs; List *objs;
namespaceId = LookupExplicitNamespace(nspname); namespaceId = LookupExplicitNamespace(nspname, false);
switch (objtype) switch (objtype)
{ {

View File

@ -291,7 +291,11 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
{ {
Oid namespaceId; Oid namespaceId;
namespaceId = LookupExplicitNamespace(relation->schemaname); namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
/*
* For missing_ok, allow a non-existant schema name
* to throw the error below (namespaceId == InvalidOid).
*/
if (namespaceId != myTempNamespace) if (namespaceId != myTempNamespace)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
@ -306,8 +310,11 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
Oid namespaceId; Oid namespaceId;
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(relation->schemaname); namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
relId = get_relname_relid(relation->relname, namespaceId); if (missing_ok && !OidIsValid(namespaceId))
relId = InvalidOid;
else
relId = get_relname_relid(relation->relname, namespaceId);
} }
else else
{ {
@ -919,7 +926,7 @@ FuncnameGetCandidates(List *names, int nargs, List *argnames,
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
} }
else else
{ {
@ -1453,7 +1460,7 @@ OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
Oid namespaceId; Oid namespaceId;
HeapTuple opertup; HeapTuple opertup;
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
opertup = SearchSysCache4(OPERNAMENSP, opertup = SearchSysCache4(OPERNAMENSP,
CStringGetDatum(opername), CStringGetDatum(opername),
ObjectIdGetDatum(oprleft), ObjectIdGetDatum(oprleft),
@ -1551,7 +1558,7 @@ OpernameGetCandidates(List *names, char oprkind)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
} }
else else
{ {
@ -2093,10 +2100,13 @@ get_ts_parser_oid(List *names, bool missing_ok)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
prsoid = GetSysCacheOid2(TSPARSERNAMENSP, if (missing_ok && !OidIsValid(namespaceId))
PointerGetDatum(parser_name), prsoid = InvalidOid;
ObjectIdGetDatum(namespaceId)); else
prsoid = GetSysCacheOid2(TSPARSERNAMENSP,
PointerGetDatum(parser_name),
ObjectIdGetDatum(namespaceId));
} }
else else
{ {
@ -2216,10 +2226,13 @@ get_ts_dict_oid(List *names, bool missing_ok)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
dictoid = GetSysCacheOid2(TSDICTNAMENSP, if (missing_ok && !OidIsValid(namespaceId))
PointerGetDatum(dict_name), dictoid = InvalidOid;
ObjectIdGetDatum(namespaceId)); else
dictoid = GetSysCacheOid2(TSDICTNAMENSP,
PointerGetDatum(dict_name),
ObjectIdGetDatum(namespaceId));
} }
else else
{ {
@ -2340,10 +2353,13 @@ get_ts_template_oid(List *names, bool missing_ok)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP, if (missing_ok && !OidIsValid(namespaceId))
PointerGetDatum(template_name), tmploid = InvalidOid;
ObjectIdGetDatum(namespaceId)); else
tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP,
PointerGetDatum(template_name),
ObjectIdGetDatum(namespaceId));
} }
else else
{ {
@ -2463,10 +2479,13 @@ get_ts_config_oid(List *names, bool missing_ok)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP, if (missing_ok && !OidIsValid(namespaceId))
PointerGetDatum(config_name), cfgoid = InvalidOid;
ObjectIdGetDatum(namespaceId)); else
cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP,
PointerGetDatum(config_name),
ObjectIdGetDatum(namespaceId));
} }
else else
{ {
@ -2657,7 +2676,7 @@ LookupNamespaceNoError(const char *nspname)
* Returns the namespace OID. Raises ereport if any problem. * Returns the namespace OID. Raises ereport if any problem.
*/ */
Oid Oid
LookupExplicitNamespace(const char *nspname) LookupExplicitNamespace(const char *nspname, bool missing_ok)
{ {
Oid namespaceId; Oid namespaceId;
AclResult aclresult; AclResult aclresult;
@ -2676,8 +2695,10 @@ LookupExplicitNamespace(const char *nspname)
*/ */
} }
namespaceId = get_namespace_oid(nspname, false); namespaceId = get_namespace_oid(nspname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
return InvalidOid;
aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE); aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
if (aclresult != ACLCHECK_OK) if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE, aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
@ -3248,7 +3269,9 @@ get_collation_oid(List *name, bool missing_ok)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
return InvalidOid;
/* first try for encoding-specific entry, then any-encoding */ /* first try for encoding-specific entry, then any-encoding */
colloid = GetSysCacheOid3(COLLNAMEENCNSP, colloid = GetSysCacheOid3(COLLNAMEENCNSP,
@ -3318,10 +3341,13 @@ get_conversion_oid(List *name, bool missing_ok)
if (schemaname) if (schemaname)
{ {
/* use exact schema given */ /* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
conoid = GetSysCacheOid2(CONNAMENSP, if (missing_ok && !OidIsValid(namespaceId))
PointerGetDatum(conversion_name), conoid = InvalidOid;
ObjectIdGetDatum(namespaceId)); else
conoid = GetSysCacheOid2(CONNAMENSP,
PointerGetDatum(conversion_name),
ObjectIdGetDatum(namespaceId));
} }
else else
{ {

View File

@ -1264,7 +1264,7 @@ GetIndexOpClass(List *opclass, Oid attrType,
/* Look in specific schema only */ /* Look in specific schema only */
Oid namespaceId; Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
tuple = SearchSysCache3(CLAAMNAMENSP, tuple = SearchSysCache3(CLAAMNAMENSP,
ObjectIdGetDatum(accessMethodId), ObjectIdGetDatum(accessMethodId),
PointerGetDatum(opcname), PointerGetDatum(opcname),

View File

@ -103,7 +103,7 @@ OpFamilyCacheLookup(Oid amID, List *opfamilyname, bool missing_ok)
/* Look in specific schema only */ /* Look in specific schema only */
Oid namespaceId; Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
htup = SearchSysCache3(OPFAMILYAMNAMENSP, htup = SearchSysCache3(OPFAMILYAMNAMENSP,
ObjectIdGetDatum(amID), ObjectIdGetDatum(amID),
PointerGetDatum(opfname), PointerGetDatum(opfname),
@ -179,7 +179,7 @@ OpClassCacheLookup(Oid amID, List *opclassname, bool missing_ok)
/* Look in specific schema only */ /* Look in specific schema only */
Oid namespaceId; Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
htup = SearchSysCache3(CLAAMNAMENSP, htup = SearchSysCache3(CLAAMNAMENSP,
ObjectIdGetDatum(amID), ObjectIdGetDatum(amID),
PointerGetDatum(opcname), PointerGetDatum(opcname),

View File

@ -4226,7 +4226,8 @@ AfterTriggerSetState(ConstraintsSetStmt *stmt)
*/ */
if (constraint->schemaname) if (constraint->schemaname)
{ {
Oid namespaceId = LookupExplicitNamespace(constraint->schemaname); Oid namespaceId = LookupExplicitNamespace(constraint->schemaname,
false);
namespacelist = list_make1_oid(namespaceId); namespacelist = list_make1_oid(namespaceId);
} }

View File

@ -1027,7 +1027,7 @@ make_oper_cache_key(OprCacheKey *key, List *opname, Oid ltypeId, Oid rtypeId)
if (schemaname) if (schemaname)
{ {
/* search only in exact schema given */ /* search only in exact schema given */
key->search_path[0] = LookupExplicitNamespace(schemaname); key->search_path[0] = LookupExplicitNamespace(schemaname, false);
} }
else else
{ {

View File

@ -149,7 +149,7 @@ LookupTypeName(ParseState *pstate, const TypeName *typeName,
/* Look in specific schema only */ /* Look in specific schema only */
Oid namespaceId; Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname); namespaceId = LookupExplicitNamespace(schemaname, false);
typoid = GetSysCacheOid2(TYPENAMENSP, typoid = GetSysCacheOid2(TYPENAMENSP,
PointerGetDatum(typname), PointerGetDatum(typname),
ObjectIdGetDatum(namespaceId)); ObjectIdGetDatum(namespaceId));

View File

@ -2678,7 +2678,7 @@ schema_to_xml(PG_FUNCTION_ARGS)
Oid nspid; Oid nspid;
schemaname = NameStr(*name); schemaname = NameStr(*name);
nspid = LookupExplicitNamespace(schemaname); nspid = LookupExplicitNamespace(schemaname, false);
PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL, PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
nulls, tableforest, targetns, true))); nulls, tableforest, targetns, true)));
@ -2724,7 +2724,7 @@ schema_to_xmlschema_internal(const char *schemaname, bool nulls,
result = makeStringInfo(); result = makeStringInfo();
nspid = LookupExplicitNamespace(schemaname); nspid = LookupExplicitNamespace(schemaname, false);
xsd_schema_element_start(result, targetns); xsd_schema_element_start(result, targetns);
@ -2782,7 +2782,7 @@ schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
StringInfo xmlschema; StringInfo xmlschema;
schemaname = NameStr(*name); schemaname = NameStr(*name);
nspid = LookupExplicitNamespace(schemaname); nspid = LookupExplicitNamespace(schemaname, false);
xmlschema = schema_to_xmlschema_internal(schemaname, nulls, xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
tableforest, targetns); tableforest, targetns);

View File

@ -106,7 +106,7 @@ extern void DeconstructQualifiedName(List *names,
char **nspname_p, char **nspname_p,
char **objname_p); char **objname_p);
extern Oid LookupNamespaceNoError(const char *nspname); extern Oid LookupNamespaceNoError(const char *nspname);
extern Oid LookupExplicitNamespace(const char *nspname); extern Oid LookupExplicitNamespace(const char *nspname, bool missing_ok);
extern Oid get_namespace_oid(const char *nspname, bool missing_ok); extern Oid get_namespace_oid(const char *nspname, bool missing_ok);
extern Oid LookupCreationNamespace(const char *nspname); extern Oid LookupCreationNamespace(const char *nspname);