Commit 7e2322df authored by Bruce Momjian's avatar Bruce Momjian

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.
parent 7c83619b
......@@ -755,7 +755,7 @@ objectsInSchemaToOids(GrantObjectType objtype, List *nspnames)
Oid namespaceId;
List *objs;
namespaceId = LookupExplicitNamespace(nspname);
namespaceId = LookupExplicitNamespace(nspname, false);
switch (objtype)
{
......
......@@ -291,7 +291,11 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
{
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)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
......@@ -306,8 +310,11 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
Oid namespaceId;
/* use exact schema given */
namespaceId = LookupExplicitNamespace(relation->schemaname);
relId = get_relname_relid(relation->relname, namespaceId);
namespaceId = LookupExplicitNamespace(relation->schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
relId = InvalidOid;
else
relId = get_relname_relid(relation->relname, namespaceId);
}
else
{
......@@ -919,7 +926,7 @@ FuncnameGetCandidates(List *names, int nargs, List *argnames,
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
}
else
{
......@@ -1453,7 +1460,7 @@ OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
Oid namespaceId;
HeapTuple opertup;
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
opertup = SearchSysCache4(OPERNAMENSP,
CStringGetDatum(opername),
ObjectIdGetDatum(oprleft),
......@@ -1551,7 +1558,7 @@ OpernameGetCandidates(List *names, char oprkind)
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
}
else
{
......@@ -2093,10 +2100,13 @@ get_ts_parser_oid(List *names, bool missing_ok)
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
prsoid = GetSysCacheOid2(TSPARSERNAMENSP,
PointerGetDatum(parser_name),
ObjectIdGetDatum(namespaceId));
namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
prsoid = InvalidOid;
else
prsoid = GetSysCacheOid2(TSPARSERNAMENSP,
PointerGetDatum(parser_name),
ObjectIdGetDatum(namespaceId));
}
else
{
......@@ -2216,10 +2226,13 @@ get_ts_dict_oid(List *names, bool missing_ok)
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
dictoid = GetSysCacheOid2(TSDICTNAMENSP,
PointerGetDatum(dict_name),
ObjectIdGetDatum(namespaceId));
namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
dictoid = InvalidOid;
else
dictoid = GetSysCacheOid2(TSDICTNAMENSP,
PointerGetDatum(dict_name),
ObjectIdGetDatum(namespaceId));
}
else
{
......@@ -2340,10 +2353,13 @@ get_ts_template_oid(List *names, bool missing_ok)
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP,
PointerGetDatum(template_name),
ObjectIdGetDatum(namespaceId));
namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
tmploid = InvalidOid;
else
tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP,
PointerGetDatum(template_name),
ObjectIdGetDatum(namespaceId));
}
else
{
......@@ -2463,10 +2479,13 @@ get_ts_config_oid(List *names, bool missing_ok)
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP,
PointerGetDatum(config_name),
ObjectIdGetDatum(namespaceId));
namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
cfgoid = InvalidOid;
else
cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP,
PointerGetDatum(config_name),
ObjectIdGetDatum(namespaceId));
}
else
{
......@@ -2657,7 +2676,7 @@ LookupNamespaceNoError(const char *nspname)
* Returns the namespace OID. Raises ereport if any problem.
*/
Oid
LookupExplicitNamespace(const char *nspname)
LookupExplicitNamespace(const char *nspname, bool missing_ok)
{
Oid namespaceId;
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);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
......@@ -3248,7 +3269,9 @@ get_collation_oid(List *name, bool missing_ok)
if (schemaname)
{
/* 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 */
colloid = GetSysCacheOid3(COLLNAMEENCNSP,
......@@ -3318,10 +3341,13 @@ get_conversion_oid(List *name, bool missing_ok)
if (schemaname)
{
/* use exact schema given */
namespaceId = LookupExplicitNamespace(schemaname);
conoid = GetSysCacheOid2(CONNAMENSP,
PointerGetDatum(conversion_name),
ObjectIdGetDatum(namespaceId));
namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
if (missing_ok && !OidIsValid(namespaceId))
conoid = InvalidOid;
else
conoid = GetSysCacheOid2(CONNAMENSP,
PointerGetDatum(conversion_name),
ObjectIdGetDatum(namespaceId));
}
else
{
......
......@@ -1264,7 +1264,7 @@ GetIndexOpClass(List *opclass, Oid attrType,
/* Look in specific schema only */
Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
tuple = SearchSysCache3(CLAAMNAMENSP,
ObjectIdGetDatum(accessMethodId),
PointerGetDatum(opcname),
......
......@@ -103,7 +103,7 @@ OpFamilyCacheLookup(Oid amID, List *opfamilyname, bool missing_ok)
/* Look in specific schema only */
Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
htup = SearchSysCache3(OPFAMILYAMNAMENSP,
ObjectIdGetDatum(amID),
PointerGetDatum(opfname),
......@@ -179,7 +179,7 @@ OpClassCacheLookup(Oid amID, List *opclassname, bool missing_ok)
/* Look in specific schema only */
Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
htup = SearchSysCache3(CLAAMNAMENSP,
ObjectIdGetDatum(amID),
PointerGetDatum(opcname),
......
......@@ -4226,7 +4226,8 @@ AfterTriggerSetState(ConstraintsSetStmt *stmt)
*/
if (constraint->schemaname)
{
Oid namespaceId = LookupExplicitNamespace(constraint->schemaname);
Oid namespaceId = LookupExplicitNamespace(constraint->schemaname,
false);
namespacelist = list_make1_oid(namespaceId);
}
......
......@@ -1027,7 +1027,7 @@ make_oper_cache_key(OprCacheKey *key, List *opname, Oid ltypeId, Oid rtypeId)
if (schemaname)
{
/* search only in exact schema given */
key->search_path[0] = LookupExplicitNamespace(schemaname);
key->search_path[0] = LookupExplicitNamespace(schemaname, false);
}
else
{
......
......@@ -149,7 +149,7 @@ LookupTypeName(ParseState *pstate, const TypeName *typeName,
/* Look in specific schema only */
Oid namespaceId;
namespaceId = LookupExplicitNamespace(schemaname);
namespaceId = LookupExplicitNamespace(schemaname, false);
typoid = GetSysCacheOid2(TYPENAMENSP,
PointerGetDatum(typname),
ObjectIdGetDatum(namespaceId));
......
......@@ -2678,7 +2678,7 @@ schema_to_xml(PG_FUNCTION_ARGS)
Oid nspid;
schemaname = NameStr(*name);
nspid = LookupExplicitNamespace(schemaname);
nspid = LookupExplicitNamespace(schemaname, false);
PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
nulls, tableforest, targetns, true)));
......@@ -2724,7 +2724,7 @@ schema_to_xmlschema_internal(const char *schemaname, bool nulls,
result = makeStringInfo();
nspid = LookupExplicitNamespace(schemaname);
nspid = LookupExplicitNamespace(schemaname, false);
xsd_schema_element_start(result, targetns);
......@@ -2782,7 +2782,7 @@ schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
StringInfo xmlschema;
schemaname = NameStr(*name);
nspid = LookupExplicitNamespace(schemaname);
nspid = LookupExplicitNamespace(schemaname, false);
xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
tableforest, targetns);
......
......@@ -106,7 +106,7 @@ extern void DeconstructQualifiedName(List *names,
char **nspname_p,
char **objname_p);
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 LookupCreationNamespace(const char *nspname);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment