54{
55 const char *schemaName =
stmt->schemaname;
61 int save_sec_context;
62 int save_nestlevel;
67
69
70 /*
71 * Who is supposed to own the new schema?
72 */
75 else
76 owner_uid = saved_uid;
77
78 /* fill schema name with the user name if not specified */
79 if (!schemaName)
80 {
82
85 elog(
ERROR,
"cache lookup failed for role %u", owner_uid);
86 schemaName =
89 }
90
91 /*
92 * To create a schema, must have schema-create privilege on the current
93 * database and must be able to become the target role (this does not
94 * imply that the target role itself must have create-schema privilege).
95 * The latter provision guards against "giveaway" attacks. Note that a
96 * superuser will always have both of these privileges a fortiori.
97 */
102
104
105 /* Additional check to protect reserved schema names */
108 (
errcode(ERRCODE_RESERVED_NAME),
109 errmsg(
"unacceptable schema name \"%s\"", schemaName),
110 errdetail(
"The prefix \"pg_\" is reserved for system schemas.")));
111
112 /*
113 * If if_not_exists was given and the schema already exists, bail out.
114 * (Note: we needn't check this when not if_not_exists, because
115 * NamespaceCreate will complain anyway.) We could do this before making
116 * the permissions checks, but since CREATE TABLE IF NOT EXISTS makes its
117 * creation-permission check first, we do likewise.
118 */
119 if (
stmt->if_not_exists)
120 {
123 {
124 /*
125 * If we are in an extension script, insist that the pre-existing
126 * object be a member of the extension, to avoid security risks.
127 */
130
131 /* OK to skip */
133 (
errcode(ERRCODE_DUPLICATE_SCHEMA),
134 errmsg(
"schema \"%s\" already exists, skipping",
135 schemaName)));
137 }
138 }
139
140 /*
141 * If the requested authorization is different from the current user,
142 * temporarily set the current user so that the object(s) will be created
143 * with the correct ownership.
144 *
145 * (The setting will be restored at the end of this routine, or in case of
146 * error, transaction abort will clean things up.)
147 */
148 if (saved_uid != owner_uid)
151
152 /* Create the schema's namespace */
154
155 /* Advance cmd counter to make the namespace visible */
157
158 /*
159 * Prepend the new schema to the current search path.
160 *
161 * We use the equivalent of a function SET option to allow the setting to
162 * persist for exactly the duration of the schema creation. guc.c also
163 * takes care of undoing the setting on error.
164 */
166
169
171 nsp++;
172
173 if (*nsp != '0円')
175
179
180 /*
181 * Report the new schema to possibly interested event triggers. Note we
182 * must do this here and not in ProcessUtilitySlow because otherwise the
183 * objects created below are reported before the schema, which would be
184 * wrong.
185 */
189
190 /*
191 * Examine the list of commands embedded in the CREATE SCHEMA command, and
192 * reorganize them into a sequentially executable order with no forward
193 * references. Note that the result is still a list of raw parsetrees ---
194 * we cannot, in general, run parse analysis on one statement until we
195 * have actually executed the prior ones.
196 */
198 schemaName);
199
200 /*
201 * Execute each command contained in the CREATE SCHEMA. Since the grammar
202 * allows only utility commands in CREATE SCHEMA, there is no need to pass
203 * them through parse_analyze_*() or the rewriter; we can just hand them
204 * straight to ProcessUtility.
205 */
206 foreach(parsetree_item, parsetree_list)
207 {
210
211 /* need to make a wrapper PlannedStmt */
219
220 /* do this step */
222 queryString,
223 false,
225 NULL,
226 NULL,
228 NULL);
229
230 /* make sure later steps can see the object created here */
232 }
233
234 /*
235 * Restore the GUC variable search_path we set above.
236 */
238
239 /* Reset current user and security context */
241
242 return namespaceId;
243}
void check_can_set_role(Oid member, Oid role)
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
#define OidIsValid(objectId)
bool IsReservedName(const char *name)
DestReceiver * None_Receiver
int errdetail(const char *fmt,...)
void EventTriggerCollectSimpleCommand(ObjectAddress address, ObjectAddress secondaryObject, Node *parsetree)
bool allowSystemTableMods
int NewGUCNestLevel(void)
void AtEOXact_GUC(bool isCommit, int nestLevel)
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
char * get_database_name(Oid dbid)
char * pstrdup(const char *in)
#define SECURITY_LOCAL_USERID_CHANGE
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
void SetUserIdAndSecContext(Oid userid, int sec_context)
Oid get_namespace_oid(const char *nspname, bool missing_ok)
char * namespace_search_path
const ObjectAddress InvalidObjectAddress
List * transformCreateSchemaStmtElements(List *schemaElts, const char *schemaName)
FormData_pg_authid * Form_pg_authid
void checkMembershipInCurrentExtension(const ObjectAddress *object)
Oid NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
const char * quote_identifier(const char *ident)
bool scanner_isspace(char ch)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void initStringInfo(StringInfo str)
PlannedStmtOrigin planOrigin
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
@ PROCESS_UTILITY_SUBCOMMAND
void CommandCounterIncrement(void)