LCOV - code coverage report
Current view: top level - src/backend/commands - amcmds.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 90.6 % 96 87
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 45.0 % 60 27

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * amcmds.c
       4                 :             :  *        Routines for SQL commands that manipulate access methods.
       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/amcmds.c
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include "access/htup_details.h"
      17                 :             : #include "access/table.h"
      18                 :             : #include "catalog/catalog.h"
      19                 :             : #include "catalog/dependency.h"
      20                 :             : #include "catalog/indexing.h"
      21                 :             : #include "catalog/objectaccess.h"
      22                 :             : #include "catalog/pg_am.h"
      23                 :             : #include "catalog/pg_proc.h"
      24                 :             : #include "catalog/pg_type.h"
      25                 :             : #include "commands/defrem.h"
      26                 :             : #include "miscadmin.h"
      27                 :             : #include "parser/parse_func.h"
      28                 :             : #include "utils/builtins.h"
      29                 :             : #include "utils/lsyscache.h"
      30                 :             : #include "utils/rel.h"
      31                 :             : #include "utils/syscache.h"
      32                 :             : 
      33                 :             : 
      34                 :             : static Oid      lookup_am_handler_func(List *handler_name, char amtype);
      35                 :             : static const char *get_am_type_string(char amtype);
      36                 :             : 
      37                 :             : 
      38                 :             : /*
      39                 :             :  * CreateAccessMethod
      40                 :             :  *              Registers a new access method.
      41                 :             :  */
      42                 :             : ObjectAddress
      43                 :           5 : CreateAccessMethod(CreateAmStmt *stmt)
      44                 :             : {
      45                 :           5 :         Relation        rel;
      46                 :             :         ObjectAddress myself;
      47                 :           5 :         ObjectAddress referenced;
      48                 :           5 :         Oid                     amoid;
      49                 :           5 :         Oid                     amhandler;
      50                 :           5 :         bool            nulls[Natts_pg_am];
      51                 :           5 :         Datum           values[Natts_pg_am];
      52                 :           5 :         HeapTuple       tup;
      53                 :             : 
      54                 :           5 :         rel = table_open(AccessMethodRelationId, RowExclusiveLock);
      55                 :             : 
      56                 :             :         /* Must be superuser */
      57         [ +  - ]:           5 :         if (!superuser())
      58   [ #  #  #  # ]:           0 :                 ereport(ERROR,
      59                 :             :                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      60                 :             :                                  errmsg("permission denied to create access method \"%s\"",
      61                 :             :                                                 stmt->amname),
      62                 :             :                                  errhint("Must be superuser to create an access method.")));
      63                 :             : 
      64                 :             :         /* Check if name is used */
      65                 :           5 :         amoid = GetSysCacheOid1(AMNAME, Anum_pg_am_oid,
      66                 :             :                                                         CStringGetDatum(stmt->amname));
      67         [ +  - ]:           5 :         if (OidIsValid(amoid))
      68                 :             :         {
      69   [ #  #  #  # ]:           0 :                 ereport(ERROR,
      70                 :             :                                 (errcode(ERRCODE_DUPLICATE_OBJECT),
      71                 :             :                                  errmsg("access method \"%s\" already exists",
      72                 :             :                                                 stmt->amname)));
      73                 :           0 :         }
      74                 :             : 
      75                 :             :         /*
      76                 :             :          * Get the handler function oid, verifying the AM type while at it.
      77                 :             :          */
      78                 :           5 :         amhandler = lookup_am_handler_func(stmt->handler_name, stmt->amtype);
      79                 :             : 
      80                 :             :         /*
      81                 :             :          * Insert tuple into pg_am.
      82                 :             :          */
      83                 :           5 :         memset(values, 0, sizeof(values));
      84                 :           5 :         memset(nulls, false, sizeof(nulls));
      85                 :             : 
      86                 :           5 :         amoid = GetNewOidWithIndex(rel, AmOidIndexId, Anum_pg_am_oid);
      87                 :           5 :         values[Anum_pg_am_oid - 1] = ObjectIdGetDatum(amoid);
      88                 :           5 :         values[Anum_pg_am_amname - 1] =
      89                 :           5 :                 DirectFunctionCall1(namein, CStringGetDatum(stmt->amname));
      90                 :           5 :         values[Anum_pg_am_amhandler - 1] = ObjectIdGetDatum(amhandler);
      91                 :           5 :         values[Anum_pg_am_amtype - 1] = CharGetDatum(stmt->amtype);
      92                 :             : 
      93                 :           5 :         tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
      94                 :             : 
      95                 :           5 :         CatalogTupleInsert(rel, tup);
      96                 :           5 :         heap_freetuple(tup);
      97                 :             : 
      98                 :           5 :         myself.classId = AccessMethodRelationId;
      99                 :           5 :         myself.objectId = amoid;
     100                 :           5 :         myself.objectSubId = 0;
     101                 :             : 
     102                 :             :         /* Record dependency on handler function */
     103                 :           5 :         referenced.classId = ProcedureRelationId;
     104                 :           5 :         referenced.objectId = amhandler;
     105                 :           5 :         referenced.objectSubId = 0;
     106                 :             : 
     107                 :           5 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
     108                 :             : 
     109                 :           5 :         recordDependencyOnCurrentExtension(&myself, false);
     110                 :             : 
     111         [ +  - ]:           5 :         InvokeObjectPostCreateHook(AccessMethodRelationId, amoid, 0);
     112                 :             : 
     113                 :           5 :         table_close(rel, RowExclusiveLock);
     114                 :             : 
     115                 :             :         return myself;
     116                 :           5 : }
     117                 :             : 
     118                 :             : /*
     119                 :             :  * get_am_type_oid
     120                 :             :  *              Worker for various get_am_*_oid variants
     121                 :             :  *
     122                 :             :  * If missing_ok is false, throw an error if access method not found.  If
     123                 :             :  * true, just return InvalidOid.
     124                 :             :  *
     125                 :             :  * If amtype is not '\0', an error is raised if the AM found is not of the
     126                 :             :  * given type.
     127                 :             :  */
     128                 :             : static Oid
     129                 :        4082 : get_am_type_oid(const char *amname, char amtype, bool missing_ok)
     130                 :             : {
     131                 :        4082 :         HeapTuple       tup;
     132                 :        4082 :         Oid                     oid = InvalidOid;
     133                 :             : 
     134                 :        4082 :         tup = SearchSysCache1(AMNAME, CStringGetDatum(amname));
     135         [ +  + ]:        4082 :         if (HeapTupleIsValid(tup))
     136                 :             :         {
     137                 :        4064 :                 Form_pg_am      amform = (Form_pg_am) GETSTRUCT(tup);
     138                 :             : 
     139   [ +  +  +  + ]:        4064 :                 if (amtype != '\0' &&
     140                 :        4055 :                         amform->amtype != amtype)
     141   [ +  -  +  - ]:           2 :                         ereport(ERROR,
     142                 :             :                                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     143                 :             :                                          errmsg("access method \"%s\" is not of type %s",
     144                 :             :                                                         NameStr(amform->amname),
     145                 :             :                                                         get_am_type_string(amtype))));
     146                 :             : 
     147                 :        4062 :                 oid = amform->oid;
     148                 :        4062 :                 ReleaseSysCache(tup);
     149                 :        4062 :         }
     150                 :             : 
     151   [ +  +  +  + ]:        4080 :         if (!OidIsValid(oid) && !missing_ok)
     152   [ +  -  +  - ]:          16 :                 ereport(ERROR,
     153                 :             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
     154                 :             :                                  errmsg("access method \"%s\" does not exist", amname)));
     155                 :        8128 :         return oid;
     156                 :        4064 : }
     157                 :             : 
     158                 :             : /*
     159                 :             :  * get_index_am_oid - given an access method name, look up its OID
     160                 :             :  *              and verify it corresponds to an index AM.
     161                 :             :  */
     162                 :             : Oid
     163                 :         228 : get_index_am_oid(const char *amname, bool missing_ok)
     164                 :             : {
     165                 :         228 :         return get_am_type_oid(amname, AMTYPE_INDEX, missing_ok);
     166                 :             : }
     167                 :             : 
     168                 :             : /*
     169                 :             :  * get_table_am_oid - given an access method name, look up its OID
     170                 :             :  *              and verify it corresponds to a table AM.
     171                 :             :  */
     172                 :             : Oid
     173                 :        3842 : get_table_am_oid(const char *amname, bool missing_ok)
     174                 :             : {
     175                 :        3842 :         return get_am_type_oid(amname, AMTYPE_TABLE, missing_ok);
     176                 :             : }
     177                 :             : 
     178                 :             : /*
     179                 :             :  * get_am_oid - given an access method name, look up its OID.
     180                 :             :  *              The type is not checked.
     181                 :             :  */
     182                 :             : Oid
     183                 :          12 : get_am_oid(const char *amname, bool missing_ok)
     184                 :             : {
     185                 :          12 :         return get_am_type_oid(amname, '\0', missing_ok);
     186                 :             : }
     187                 :             : 
     188                 :             : /*
     189                 :             :  * get_am_name - given an access method OID, look up its name.
     190                 :             :  */
     191                 :             : char *
     192                 :          14 : get_am_name(Oid amOid)
     193                 :             : {
     194                 :          14 :         HeapTuple       tup;
     195                 :          14 :         char       *result = NULL;
     196                 :             : 
     197                 :          14 :         tup = SearchSysCache1(AMOID, ObjectIdGetDatum(amOid));
     198         [ +  + ]:          14 :         if (HeapTupleIsValid(tup))
     199                 :             :         {
     200                 :          12 :                 Form_pg_am      amform = (Form_pg_am) GETSTRUCT(tup);
     201                 :             : 
     202                 :          12 :                 result = pstrdup(NameStr(amform->amname));
     203                 :          12 :                 ReleaseSysCache(tup);
     204                 :          12 :         }
     205                 :          28 :         return result;
     206                 :          14 : }
     207                 :             : 
     208                 :             : /*
     209                 :             :  * Convert single-character access method type into string for error reporting.
     210                 :             :  */
     211                 :             : static const char *
     212                 :           2 : get_am_type_string(char amtype)
     213                 :             : {
     214      [ +  -  - ]:           2 :         switch (amtype)
     215                 :             :         {
     216                 :             :                 case AMTYPE_INDEX:
     217                 :           0 :                         return "INDEX";
     218                 :             :                 case AMTYPE_TABLE:
     219                 :           2 :                         return "TABLE";
     220                 :             :                 default:
     221                 :             :                         /* shouldn't happen */
     222   [ #  #  #  # ]:           0 :                         elog(ERROR, "invalid access method type '%c'", amtype);
     223                 :           0 :                         return NULL;            /* keep compiler quiet */
     224                 :             :         }
     225                 :           2 : }
     226                 :             : 
     227                 :             : /*
     228                 :             :  * Convert a handler function name to an Oid.  If the return type of the
     229                 :             :  * function doesn't match the given AM type, an error is raised.
     230                 :             :  *
     231                 :             :  * This function either return valid function Oid or throw an error.
     232                 :             :  */
     233                 :             : static Oid
     234                 :           7 : lookup_am_handler_func(List *handler_name, char amtype)
     235                 :             : {
     236                 :           7 :         Oid                     handlerOid;
     237                 :           7 :         Oid                     funcargtypes[1] = {INTERNALOID};
     238                 :           7 :         Oid                     expectedType = InvalidOid;
     239                 :             : 
     240         [ +  - ]:           7 :         if (handler_name == NIL)
     241   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     242                 :             :                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
     243                 :             :                                  errmsg("handler function is not specified")));
     244                 :             : 
     245                 :             :         /* handlers have one argument of type internal */
     246                 :           7 :         handlerOid = LookupFuncName(handler_name, 1, funcargtypes, false);
     247                 :             : 
     248                 :             :         /* check that handler has the correct return type */
     249      [ +  +  - ]:           7 :         switch (amtype)
     250                 :             :         {
     251                 :             :                 case AMTYPE_INDEX:
     252                 :           2 :                         expectedType = INDEX_AM_HANDLEROID;
     253                 :           2 :                         break;
     254                 :             :                 case AMTYPE_TABLE:
     255                 :           5 :                         expectedType = TABLE_AM_HANDLEROID;
     256                 :           5 :                         break;
     257                 :             :                 default:
     258   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized access method type \"%c\"", amtype);
     259                 :           0 :         }
     260                 :             : 
     261         [ +  + ]:           7 :         if (get_func_rettype(handlerOid) != expectedType)
     262   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     263                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     264                 :             :                                  errmsg("function %s must return type %s",
     265                 :             :                                                 get_func_name(handlerOid),
     266                 :             :                                                 format_type_extended(expectedType, -1, 0))));
     267                 :             : 
     268                 :          10 :         return handlerOid;
     269                 :           5 : }
        

Generated by: LCOV version 2.3.2-1