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) ...@@ -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)
{ {
......
...@@ -291,7 +291,11 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, ...@@ -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,7 +310,10 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, ...@@ -306,7 +310,10 @@ 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);
if (missing_ok && !OidIsValid(namespaceId))
relId = InvalidOid;
else
relId = get_relname_relid(relation->relname, namespaceId); relId = get_relname_relid(relation->relname, namespaceId);
} }
else else
...@@ -919,7 +926,7 @@ FuncnameGetCandidates(List *names, int nargs, List *argnames, ...@@ -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) ...@@ -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) ...@@ -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,7 +2100,10 @@ get_ts_parser_oid(List *names, bool missing_ok) ...@@ -2093,7 +2100,10 @@ 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);
if (missing_ok && !OidIsValid(namespaceId))
prsoid = InvalidOid;
else
prsoid = GetSysCacheOid2(TSPARSERNAMENSP, prsoid = GetSysCacheOid2(TSPARSERNAMENSP,
PointerGetDatum(parser_name), PointerGetDatum(parser_name),
ObjectIdGetDatum(namespaceId)); ObjectIdGetDatum(namespaceId));
...@@ -2216,7 +2226,10 @@ get_ts_dict_oid(List *names, bool missing_ok) ...@@ -2216,7 +2226,10 @@ 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);
if (missing_ok && !OidIsValid(namespaceId))
dictoid = InvalidOid;
else
dictoid = GetSysCacheOid2(TSDICTNAMENSP, dictoid = GetSysCacheOid2(TSDICTNAMENSP,
PointerGetDatum(dict_name), PointerGetDatum(dict_name),
ObjectIdGetDatum(namespaceId)); ObjectIdGetDatum(namespaceId));
...@@ -2340,7 +2353,10 @@ get_ts_template_oid(List *names, bool missing_ok) ...@@ -2340,7 +2353,10 @@ 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);
if (missing_ok && !OidIsValid(namespaceId))
tmploid = InvalidOid;
else
tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP, tmploid = GetSysCacheOid2(TSTEMPLATENAMENSP,
PointerGetDatum(template_name), PointerGetDatum(template_name),
ObjectIdGetDatum(namespaceId)); ObjectIdGetDatum(namespaceId));
...@@ -2463,7 +2479,10 @@ get_ts_config_oid(List *names, bool missing_ok) ...@@ -2463,7 +2479,10 @@ 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);
if (missing_ok && !OidIsValid(namespaceId))
cfgoid = InvalidOid;
else
cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP, cfgoid = GetSysCacheOid2(TSCONFIGNAMENSP,
PointerGetDatum(config_name), PointerGetDatum(config_name),
ObjectIdGetDatum(namespaceId)); ObjectIdGetDatum(namespaceId));
...@@ -2657,7 +2676,7 @@ LookupNamespaceNoError(const char *nspname) ...@@ -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,7 +2695,9 @@ LookupExplicitNamespace(const char *nspname) ...@@ -2676,7 +2695,9 @@ 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)
...@@ -3248,7 +3269,9 @@ get_collation_oid(List *name, bool missing_ok) ...@@ -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,7 +3341,10 @@ get_conversion_oid(List *name, bool missing_ok) ...@@ -3318,7 +3341,10 @@ 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);
if (missing_ok && !OidIsValid(namespaceId))
conoid = InvalidOid;
else
conoid = GetSysCacheOid2(CONNAMENSP, conoid = GetSysCacheOid2(CONNAMENSP,
PointerGetDatum(conversion_name), PointerGetDatum(conversion_name),
ObjectIdGetDatum(namespaceId)); ObjectIdGetDatum(namespaceId));
......
...@@ -1264,7 +1264,7 @@ GetIndexOpClass(List *opclass, Oid attrType, ...@@ -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),
......
...@@ -103,7 +103,7 @@ OpFamilyCacheLookup(Oid amID, List *opfamilyname, bool missing_ok) ...@@ -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) ...@@ -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),
......
...@@ -4226,7 +4226,8 @@ AfterTriggerSetState(ConstraintsSetStmt *stmt) ...@@ -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);
} }
......
...@@ -1027,7 +1027,7 @@ make_oper_cache_key(OprCacheKey *key, List *opname, Oid ltypeId, Oid rtypeId) ...@@ -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
{ {
......
...@@ -149,7 +149,7 @@ LookupTypeName(ParseState *pstate, const TypeName *typeName, ...@@ -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));
......
...@@ -2678,7 +2678,7 @@ schema_to_xml(PG_FUNCTION_ARGS) ...@@ -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, ...@@ -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) ...@@ -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);
......
...@@ -106,7 +106,7 @@ extern void DeconstructQualifiedName(List *names, ...@@ -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);
......
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