LCOV - differential code coverage report
Current view: top level - src/backend/commands - schemacmds.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 88.5 % 130 115 15 2 113 2
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 5 5 1 4
Baseline: lcov-20261128-baseline Branches: 61.8 % 76 47 29 47
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 80.0 % 5 4 1 4
(30,360] days: 100.0 % 4 4 2 2
(360..) days: 88.4 % 121 107 14 107
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 4 4 4
Branch coverage date bins:
(7,30] days: 0.0 % 2 0 2
(360..) days: 63.5 % 74 47 27 47

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * schemacmds.c
                                  4                 :                :  *    schema creation/manipulation commands
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/commands/schemacmds.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/htup_details.h"
                                 18                 :                : #include "access/table.h"
                                 19                 :                : #include "access/xact.h"
                                 20                 :                : #include "catalog/catalog.h"
                                 21                 :                : #include "catalog/dependency.h"
                                 22                 :                : #include "catalog/indexing.h"
                                 23                 :                : #include "catalog/namespace.h"
                                 24                 :                : #include "catalog/objectaccess.h"
                                 25                 :                : #include "catalog/pg_authid.h"
                                 26                 :                : #include "catalog/pg_database.h"
                                 27                 :                : #include "catalog/pg_namespace.h"
                                 28                 :                : #include "commands/event_trigger.h"
                                 29                 :                : #include "commands/schemacmds.h"
                                 30                 :                : #include "miscadmin.h"
                                 31                 :                : #include "parser/parse_utilcmd.h"
                                 32                 :                : #include "parser/scansup.h"
                                 33                 :                : #include "tcop/utility.h"
                                 34                 :                : #include "utils/acl.h"
                                 35                 :                : #include "utils/builtins.h"
                                 36                 :                : #include "utils/lsyscache.h"
                                 37                 :                : #include "utils/rel.h"
                                 38                 :                : #include "utils/syscache.h"
                                 39                 :                : 
                                 40                 :                : static void AlterSchemaOwner_internal(HeapTuple tup, Relation rel, Oid newOwnerId);
                                 41                 :                : 
                                 42                 :                : /*
                                 43                 :                :  * CREATE SCHEMA
                                 44                 :                :  *
                                 45                 :                :  * Note: caller should pass in location information for the whole
                                 46                 :                :  * CREATE SCHEMA statement, which in turn we pass down as the location
                                 47                 :                :  * of the component commands.  This comports with our general plan of
                                 48                 :                :  * reporting location/len for the whole command even when executing
                                 49                 :                :  * a subquery.
                                 50                 :                :  */
                                 51                 :                : Oid
  236 tgl@sss.pgh.pa.us          52                 :CBC         759 : CreateSchemaCommand(ParseState *pstate, CreateSchemaStmt *stmt,
                                 53                 :                :                     int stmt_location, int stmt_len)
                                 54                 :                : {
 4207 bruce@momjian.us           55                 :            759 :     const char *schemaName = stmt->schemaname;
                                 56                 :                :     Oid         namespaceId;
                                 57                 :                :     List       *parsetree_list;
                                 58                 :                :     ListCell   *parsetree_item;
                                 59                 :                :     Oid         owner_uid;
                                 60                 :                :     Oid         saved_uid;
                                 61                 :                :     int         save_sec_context;
                                 62                 :                :     int         save_nestlevel;
 1300 noah@leadboat.com          63                 :            759 :     char       *nsp = namespace_search_path;
                                 64                 :                :     AclResult   aclresult;
                                 65                 :                :     ObjectAddress address;
                                 66                 :                :     StringInfoData pathbuf;
                                 67                 :                : 
 6198 tgl@sss.pgh.pa.us          68                 :            759 :     GetUserIdAndSecContext(&saved_uid, &save_sec_context);
                                 69                 :                : 
                                 70                 :                :     /*
                                 71                 :                :      * Who is supposed to own the new schema?
                                 72                 :                :      */
 4282 alvherre@alvh.no-ip.       73         [ +  + ]:            759 :     if (stmt->authrole)
                                 74                 :            112 :         owner_uid = get_rolespec_oid(stmt->authrole, false);
                                 75                 :                :     else
 7823 tgl@sss.pgh.pa.us          76                 :            647 :         owner_uid = saved_uid;
                                 77                 :                : 
                                 78                 :                :     /* fill schema name with the user name if not specified */
 4282 alvherre@alvh.no-ip.       79         [ +  + ]:            753 :     if (!schemaName)
                                 80                 :                :     {
                                 81                 :                :         HeapTuple   tuple;
                                 82                 :                : 
                                 83                 :             48 :         tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(owner_uid));
                                 84         [ -  + ]:             48 :         if (!HeapTupleIsValid(tuple))
 4282 alvherre@alvh.no-ip.       85         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for role %u", owner_uid);
                                 86                 :                :         schemaName =
 4282 alvherre@alvh.no-ip.       87                 :CBC          48 :             pstrdup(NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname));
                                 88                 :             48 :         ReleaseSysCache(tuple);
                                 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                 :                :      */
 1476 peter@eisentraut.org       98                 :            753 :     aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, saved_uid, ACL_CREATE);
 8981 tgl@sss.pgh.pa.us          99         [ -  + ]:            753 :     if (aclresult != ACLCHECK_OK)
 3283 peter_e@gmx.net           100                 :UBC           0 :         aclcheck_error(aclresult, OBJECT_DATABASE,
 8520 tgl@sss.pgh.pa.us         101                 :              0 :                        get_database_name(MyDatabaseId));
                                102                 :                : 
 1471 rhaas@postgresql.org      103                 :CBC         753 :     check_can_set_role(saved_uid, owner_uid);
                                104                 :                : 
                                105                 :                :     /* Additional check to protect reserved schema names */
 8993 tgl@sss.pgh.pa.us         106   [ +  +  +  + ]:            749 :     if (!allowSystemTableMods && IsReservedName(schemaName))
 8534                           107         [ +  - ]:              1 :         ereport(ERROR,
                                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                 :                :      */
 1573                           119         [ +  + ]:            748 :     if (stmt->if_not_exists)
                                120                 :                :     {
                                121                 :             19 :         namespaceId = get_namespace_oid(schemaName, true);
                                122         [ +  + ]:             19 :         if (OidIsValid(namespaceId))
                                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                 :                :              */
                                128                 :             13 :             ObjectAddressSet(address, NamespaceRelationId, namespaceId);
                                129                 :             13 :             checkMembershipInCurrentExtension(&address);
                                130                 :                : 
                                131                 :                :             /* OK to skip */
                                132         [ +  + ]:             12 :             ereport(NOTICE,
                                133                 :                :                     (errcode(ERRCODE_DUPLICATE_SCHEMA),
                                134                 :                :                      errmsg("schema \"%s\" already exists, skipping",
                                135                 :                :                             schemaName)));
                                136                 :             12 :             return InvalidOid;
                                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                 :                :      */
 7807                           148         [ +  + ]:            735 :     if (saved_uid != owner_uid)
 6198                           149                 :             46 :         SetUserIdAndSecContext(owner_uid,
                                150                 :                :                                save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
                                151                 :                : 
                                152                 :                :     /* Create the schema's namespace */
 5378                           153                 :            735 :     namespaceId = NamespaceCreate(schemaName, owner_uid, false);
                                154                 :                : 
                                155                 :                :     /* Advance cmd counter to make the namespace visible */
 8993                           156                 :            731 :     CommandCounterIncrement();
                                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                 :                :      */
 1300 noah@leadboat.com         165                 :            731 :     save_nestlevel = NewGUCNestLevel();
                                166                 :                : 
                                167                 :            731 :     initStringInfo(&pathbuf);
                                168                 :            731 :     appendStringInfoString(&pathbuf, quote_identifier(schemaName));
                                169                 :                : 
                                170         [ +  + ]:            735 :     while (scanner_isspace(*nsp))
                                171                 :              4 :         nsp++;
                                172                 :                : 
                                173         [ +  + ]:            731 :     if (*nsp != '\0')
                                174                 :            712 :         appendStringInfo(&pathbuf, ", %s", nsp);
                                175                 :                : 
                                176                 :            731 :     (void) set_config_option("search_path", pathbuf.data,
                                177                 :                :                              PGC_USERSET, PGC_S_SESSION,
                                178                 :                :                              GUC_ACTION_SAVE, true, 0, false);
                                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                 :                :      */
 4219 alvherre@alvh.no-ip.      186                 :            731 :     ObjectAddressSet(address, NamespaceRelationId, namespaceId);
                                187                 :            731 :     EventTriggerCollectSimpleCommand(address, InvalidObjectAddress,
                                188                 :                :                                      (Node *) stmt);
                                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                 :                :      */
  236 tgl@sss.pgh.pa.us         197                 :            731 :     parsetree_list = transformCreateSchemaStmtElements(pstate,
                                198                 :                :                                                        stmt->schemaElts,
                                199                 :                :                                                        schemaName);
                                200                 :                : 
                                201                 :                :     /*
                                202                 :                :      * Execute each command contained in the CREATE SCHEMA.  Since the grammar
                                203                 :                :      * allows only utility commands in CREATE SCHEMA, there is no need to pass
                                204                 :                :      * them through parse_analyze_*() or the rewriter; we can just hand them
                                205                 :                :      * straight to ProcessUtility.
                                206                 :                :      */
 8993                           207   [ +  +  +  +  :           1099 :     foreach(parsetree_item, parsetree_list)
                                              +  + ]
                                208                 :                :     {
   81 peter@eisentraut.org      209                 :GNC         436 :         Node       *node = (Node *) lfirst(parsetree_item);
                                210                 :                :         PlannedStmt *wrapper;
                                211                 :                : 
                                212                 :                :         /* need to make a wrapper PlannedStmt */
 3605 tgl@sss.pgh.pa.us         213                 :CBC         436 :         wrapper = makeNode(PlannedStmt);
                                214                 :            436 :         wrapper->commandType = CMD_UTILITY;
                                215                 :            436 :         wrapper->canSetTag = false;
   81 peter@eisentraut.org      216                 :GNC         436 :         wrapper->utilityStmt = node;
 3605 tgl@sss.pgh.pa.us         217                 :CBC         436 :         wrapper->stmt_location = stmt_location;
                                218                 :            436 :         wrapper->stmt_len = stmt_len;
  485 michael@paquier.xyz       219                 :            436 :         wrapper->planOrigin = PLAN_STMT_INTERNAL;
                                220                 :                : 
                                221                 :                :         /* do this step */
 3605 tgl@sss.pgh.pa.us         222                 :            436 :         ProcessUtility(wrapper,
                                223                 :                :                        pstate->p_sourcetext,
                                224                 :                :                        false,
                                225                 :                :                        PROCESS_UTILITY_SUBCOMMAND,
                                226                 :                :                        NULL,
                                227                 :                :                        NULL,
                                228                 :                :                        None_Receiver,
                                229                 :                :                        NULL);
                                230                 :                : 
                                231                 :                :         /* make sure later steps can see the object created here */
 7098                           232                 :            432 :         CommandCounterIncrement();
                                233                 :                :     }
                                234                 :                : 
                                235                 :                :     /*
                                236                 :                :      * Restore the GUC variable search_path we set above.
                                237                 :                :      */
 1300 noah@leadboat.com         238                 :            663 :     AtEOXact_GUC(true, save_nestlevel);
                                239                 :                : 
                                240                 :                :     /* Reset current user and security context */
 6198 tgl@sss.pgh.pa.us         241                 :            663 :     SetUserIdAndSecContext(saved_uid, save_sec_context);
                                242                 :                : 
 5088 rhaas@postgresql.org      243                 :            663 :     return namespaceId;
                                244                 :                : }
                                245                 :                : 
                                246                 :                : 
                                247                 :                : /*
                                248                 :                :  * Rename schema
                                249                 :                :  */
                                250                 :                : ObjectAddress
 8555 peter_e@gmx.net           251                 :             15 : RenameSchema(const char *oldname, const char *newname)
                                252                 :                : {
                                253                 :                :     ObjectAddress address;
                                254                 :                :     Oid         nspOid;
                                255                 :                :     HeapTuple   tup;
                                256                 :                :     Relation    rel;
                                257                 :                :     AclResult   aclresult;
                                258                 :                :     Form_pg_namespace nspform;
                                259                 :                : 
                                260                 :                :     /* Locking before priv checks is bad, but DROP SCHEMA does the same. */
   19 noah@leadboat.com         261                 :             15 :     address = get_object_address(OBJECT_SCHEMA,
                                262                 :             15 :                                  (Node *) makeString(pstrdup(oldname)),
                                263                 :                :                                  NULL,
                                264                 :                :                                  AccessExclusiveLock, false);
                                265                 :             15 :     nspOid = address.objectId;
                                266                 :                : 
 2868 andres@anarazel.de        267                 :             15 :     rel = table_open(NamespaceRelationId, RowExclusiveLock);
                                268                 :                : 
   19 noah@leadboat.com         269                 :             15 :     tup = SearchSysCacheCopy1(NAMESPACEOID, ObjectIdGetDatum(nspOid));
 8555 peter_e@gmx.net           270         [ -  + ]:             15 :     if (!HeapTupleIsValid(tup))
   19 noah@leadboat.com         271         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for schema %u", nspOid);
                                272                 :                : 
 2930 andres@anarazel.de        273                 :CBC          15 :     nspform = (Form_pg_namespace) GETSTRUCT(tup);
                                274                 :                : 
                                275                 :                :     /* make sure the new name doesn't exist */
 5959 rhaas@postgresql.org      276         [ -  + ]:             15 :     if (OidIsValid(get_namespace_oid(newname, true)))
 8555 peter_e@gmx.net           277         [ #  # ]:UBC           0 :         ereport(ERROR,
                                278                 :                :                 (errcode(ERRCODE_DUPLICATE_SCHEMA),
                                279                 :                :                  errmsg("schema \"%s\" already exists", newname)));
                                280                 :                : 
                                281                 :                :     /* must be owner */
 1476 peter@eisentraut.org      282         [ -  + ]:CBC          15 :     if (!object_ownercheck(NamespaceRelationId, nspOid, GetUserId()))
 3283 peter_e@gmx.net           283                 :UBC           0 :         aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SCHEMA,
                                284                 :                :                        oldname);
                                285                 :                : 
                                286                 :                :     /* must have CREATE privilege on database */
 1476 peter@eisentraut.org      287                 :CBC          15 :     aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(), ACL_CREATE);
 8555 peter_e@gmx.net           288         [ -  + ]:             15 :     if (aclresult != ACLCHECK_OK)
 3283 peter_e@gmx.net           289                 :UBC           0 :         aclcheck_error(aclresult, OBJECT_DATABASE,
 8520 tgl@sss.pgh.pa.us         290                 :              0 :                        get_database_name(MyDatabaseId));
                                291                 :                : 
 8555 peter_e@gmx.net           292   [ +  -  -  + ]:CBC          15 :     if (!allowSystemTableMods && IsReservedName(newname))
 8534 tgl@sss.pgh.pa.us         293         [ #  # ]:UBC           0 :         ereport(ERROR,
                                294                 :                :                 (errcode(ERRCODE_RESERVED_NAME),
                                295                 :                :                  errmsg("unacceptable schema name \"%s\"", newname),
                                296                 :                :                  errdetail("The prefix \"pg_\" is reserved for system schemas.")));
                                297                 :                : 
                                298                 :                :     /* rename */
 2930 andres@anarazel.de        299                 :CBC          15 :     namestrcpy(&nspform->nspname, newname);
 3588 alvherre@alvh.no-ip.      300                 :             15 :     CatalogTupleUpdate(rel, &tup->t_self, tup);
                                301                 :                : 
 2930 andres@anarazel.de        302         [ -  + ]:             15 :     InvokeObjectPostAlterHook(NamespaceRelationId, nspOid, 0);
                                303                 :                : 
 2868                           304                 :             15 :     table_close(rel, NoLock);
 8555 peter_e@gmx.net           305                 :             15 :     heap_freetuple(tup);
                                306                 :                : 
 4288 alvherre@alvh.no-ip.      307                 :             15 :     return address;
                                308                 :                : }
                                309                 :                : 
                                310                 :                : void
 1530 pg@bowt.ie                311                 :              5 : AlterSchemaOwner_oid(Oid schemaoid, Oid newOwnerId)
                                312                 :                : {
                                313                 :                :     HeapTuple   tup;
                                314                 :                :     Relation    rel;
                                315                 :                : 
 2868 andres@anarazel.de        316                 :              5 :     rel = table_open(NamespaceRelationId, RowExclusiveLock);
                                317                 :                : 
 1530 pg@bowt.ie                318                 :              5 :     tup = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(schemaoid));
 7677 alvherre@alvh.no-ip.      319         [ -  + ]:              5 :     if (!HeapTupleIsValid(tup))
 1530 pg@bowt.ie                320         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for schema %u", schemaoid);
                                321                 :                : 
 7677 alvherre@alvh.no-ip.      322                 :CBC           5 :     AlterSchemaOwner_internal(tup, rel, newOwnerId);
                                323                 :                : 
                                324                 :              5 :     ReleaseSysCache(tup);
                                325                 :                : 
 2868 andres@anarazel.de        326                 :              5 :     table_close(rel, RowExclusiveLock);
 7677 alvherre@alvh.no-ip.      327                 :              5 : }
                                328                 :                : 
                                329                 :                : 
                                330                 :                : /*
                                331                 :                :  * Change schema owner
                                332                 :                :  */
                                333                 :                : ObjectAddress
 7823 tgl@sss.pgh.pa.us         334                 :             53 : AlterSchemaOwner(const char *name, Oid newOwnerId)
                                335                 :                : {
                                336                 :                :     Oid         nspOid;
                                337                 :                :     HeapTuple   tup;
                                338                 :                :     Relation    rel;
                                339                 :                :     ObjectAddress address;
                                340                 :                :     Form_pg_namespace nspform;
                                341                 :                : 
 2868 andres@anarazel.de        342                 :             53 :     rel = table_open(NamespaceRelationId, RowExclusiveLock);
                                343                 :                : 
 6131 rhaas@postgresql.org      344                 :             53 :     tup = SearchSysCache1(NAMESPACENAME, CStringGetDatum(name));
 8191 tgl@sss.pgh.pa.us         345         [ -  + ]:             53 :     if (!HeapTupleIsValid(tup))
 8191 tgl@sss.pgh.pa.us         346         [ #  # ]:UBC           0 :         ereport(ERROR,
                                347                 :                :                 (errcode(ERRCODE_UNDEFINED_SCHEMA),
                                348                 :                :                  errmsg("schema \"%s\" does not exist", name)));
                                349                 :                : 
 2930 andres@anarazel.de        350                 :CBC          53 :     nspform = (Form_pg_namespace) GETSTRUCT(tup);
                                351                 :             53 :     nspOid = nspform->oid;
                                352                 :                : 
 7677 alvherre@alvh.no-ip.      353                 :             53 :     AlterSchemaOwner_internal(tup, rel, newOwnerId);
                                354                 :                : 
 4288                           355                 :             53 :     ObjectAddressSet(address, NamespaceRelationId, nspOid);
                                356                 :                : 
 7677                           357                 :             53 :     ReleaseSysCache(tup);
                                358                 :                : 
 2868 andres@anarazel.de        359                 :             53 :     table_close(rel, RowExclusiveLock);
                                360                 :                : 
 4288 alvherre@alvh.no-ip.      361                 :             53 :     return address;
                                362                 :                : }
                                363                 :                : 
                                364                 :                : static void
 7677                           365                 :             58 : AlterSchemaOwner_internal(HeapTuple tup, Relation rel, Oid newOwnerId)
                                366                 :                : {
                                367                 :                :     Form_pg_namespace nspForm;
                                368                 :                : 
                                369         [ -  + ]:             58 :     Assert(tup->t_tableOid == NamespaceRelationId);
                                370         [ -  + ]:             58 :     Assert(RelationGetRelid(rel) == NamespaceRelationId);
                                371                 :                : 
 8191 tgl@sss.pgh.pa.us         372                 :             58 :     nspForm = (Form_pg_namespace) GETSTRUCT(tup);
                                373                 :                : 
                                374                 :                :     /*
                                375                 :                :      * If the new owner is the same as the existing owner, consider the
                                376                 :                :      * command to have succeeded.  This is for dump restoration purposes.
                                377                 :                :      */
 7823                           378         [ +  + ]:             58 :     if (nspForm->nspowner != newOwnerId)
                                379                 :                :     {
                                380                 :                :         Datum       repl_val[Natts_pg_namespace];
                                381                 :                :         bool        repl_null[Natts_pg_namespace];
                                382                 :                :         bool        repl_repl[Natts_pg_namespace];
                                383                 :                :         Acl        *newAcl;
                                384                 :                :         Datum       aclDatum;
                                385                 :                :         bool        isNull;
                                386                 :                :         HeapTuple   newtuple;
                                387                 :                :         AclResult   aclresult;
                                388                 :                : 
                                389                 :                :         /* Otherwise, must be owner of the existing object */
 1476 peter@eisentraut.org      390         [ -  + ]:             31 :         if (!object_ownercheck(NamespaceRelationId, nspForm->oid, GetUserId()))
 3283 peter_e@gmx.net           391                 :UBC           0 :             aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SCHEMA,
 7677 alvherre@alvh.no-ip.      392                 :              0 :                            NameStr(nspForm->nspname));
                                393                 :                : 
                                394                 :                :         /* Must be able to become new owner */
 1471 rhaas@postgresql.org      395                 :CBC          31 :         check_can_set_role(GetUserId(), newOwnerId);
                                396                 :                : 
                                397                 :                :         /*
                                398                 :                :          * must have create-schema rights
                                399                 :                :          *
                                400                 :                :          * NOTE: This is different from other alter-owner checks in that the
                                401                 :                :          * current user is checked for create privileges instead of the
                                402                 :                :          * destination owner.  This is consistent with the CREATE case for
                                403                 :                :          * schemas.  Because superusers will always have this right, we need
                                404                 :                :          * no special case for them.
                                405                 :                :          */
 1476 peter@eisentraut.org      406                 :             31 :         aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(),
                                407                 :                :                                     ACL_CREATE);
 7807 tgl@sss.pgh.pa.us         408         [ -  + ]:             31 :         if (aclresult != ACLCHECK_OK)
 3283 peter_e@gmx.net           409                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_DATABASE,
 7807 tgl@sss.pgh.pa.us         410                 :              0 :                            get_database_name(MyDatabaseId));
                                411                 :                : 
 6600 tgl@sss.pgh.pa.us         412                 :CBC          31 :         memset(repl_null, false, sizeof(repl_null));
                                413                 :             31 :         memset(repl_repl, false, sizeof(repl_repl));
                                414                 :                : 
                                415                 :             31 :         repl_repl[Anum_pg_namespace_nspowner - 1] = true;
 7823                           416                 :             31 :         repl_val[Anum_pg_namespace_nspowner - 1] = ObjectIdGetDatum(newOwnerId);
                                417                 :                : 
                                418                 :                :         /*
                                419                 :                :          * Determine the modified ACL for the new owner.  This is only
                                420                 :                :          * necessary when the ACL is non-null.
                                421                 :                :          */
 8154                           422                 :             31 :         aclDatum = SysCacheGetAttr(NAMESPACENAME, tup,
                                423                 :                :                                    Anum_pg_namespace_nspacl,
                                424                 :                :                                    &isNull);
                                425         [ +  + ]:             31 :         if (!isNull)
                                426                 :                :         {
                                427                 :              6 :             newAcl = aclnewowner(DatumGetAclP(aclDatum),
                                428                 :                :                                  nspForm->nspowner, newOwnerId);
 6600                           429                 :              6 :             repl_repl[Anum_pg_namespace_nspacl - 1] = true;
 8154                           430                 :              6 :             repl_val[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(newAcl);
                                431                 :                :         }
                                432                 :                : 
 6600                           433                 :             31 :         newtuple = heap_modify_tuple(tup, RelationGetDescr(rel), repl_val, repl_null, repl_repl);
                                434                 :                : 
 3588 alvherre@alvh.no-ip.      435                 :             31 :         CatalogTupleUpdate(rel, &newtuple->t_self, newtuple);
                                436                 :                : 
 8154 tgl@sss.pgh.pa.us         437                 :             31 :         heap_freetuple(newtuple);
                                438                 :                : 
                                439                 :                :         /* Update owner dependency reference */
 2930 andres@anarazel.de        440                 :             31 :         changeDependencyOnOwner(NamespaceRelationId, nspForm->oid,
                                441                 :                :                                 newOwnerId);
                                442                 :                :     }
                                443                 :                : 
 5004 rhaas@postgresql.org      444         [ -  + ]:             58 :     InvokeObjectPostAlterHook(NamespaceRelationId,
                                445                 :                :                               nspForm->oid, 0);
 8191 tgl@sss.pgh.pa.us         446                 :             58 : }
        

Generated by: LCOV version 2.0-1