LCOV - code coverage report
Current view: top level - src/backend/access/table - table.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 30 38 78.9 %
Date: 2021-12-05 01:09:12 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * table.c
       4             :  *    Generic routines for table related code.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/access/table/table.c
      12             :  *
      13             :  *
      14             :  * NOTES
      15             :  *    This file contains table_ routines that implement access to tables (in
      16             :  *    contrast to other relation types like indexes) that are independent of
      17             :  *    individual table access methods.
      18             :  *
      19             :  *-------------------------------------------------------------------------
      20             :  */
      21             : 
      22             : #include "postgres.h"
      23             : 
      24             : #include "access/relation.h"
      25             : #include "access/table.h"
      26             : #include "storage/lmgr.h"
      27             : 
      28             : 
      29             : /* ----------------
      30             :  *      table_open - open a table relation by relation OID
      31             :  *
      32             :  *      This is essentially relation_open plus check that the relation
      33             :  *      is not an index nor a composite type.  (The caller should also
      34             :  *      check that it's not a view or foreign table before assuming it has
      35             :  *      storage.)
      36             :  * ----------------
      37             :  */
      38             : Relation
      39    17968192 : table_open(Oid relationId, LOCKMODE lockmode)
      40             : {
      41             :     Relation    r;
      42             : 
      43    17968192 :     r = relation_open(relationId, lockmode);
      44             : 
      45    17968184 :     if (r->rd_rel->relkind == RELKIND_INDEX ||
      46    17968184 :         r->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
      47           0 :         ereport(ERROR,
      48             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
      49             :                  errmsg("\"%s\" is an index",
      50             :                         RelationGetRelationName(r))));
      51    17968184 :     else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
      52           0 :         ereport(ERROR,
      53             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
      54             :                  errmsg("\"%s\" is a composite type",
      55             :                         RelationGetRelationName(r))));
      56             : 
      57    17968184 :     return r;
      58             : }
      59             : 
      60             : 
      61             : /* ----------------
      62             :  *      try_table_open - open a table relation by relation OID
      63             :  *
      64             :  *      Same as table_open, except return NULL instead of failing
      65             :  *      if the relation does not exist.
      66             :  * ----------------
      67             :  */
      68             : Relation
      69      221754 : try_table_open(Oid relationId, LOCKMODE lockmode)
      70             : {
      71             :     Relation    r;
      72             : 
      73      221754 :     r = try_relation_open(relationId, lockmode);
      74             : 
      75             :     /* leave if table does not exist */
      76      221754 :     if (!r)
      77           0 :         return NULL;
      78             : 
      79      221754 :     if (r->rd_rel->relkind == RELKIND_INDEX ||
      80      221754 :         r->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
      81           0 :         ereport(ERROR,
      82             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
      83             :                  errmsg("\"%s\" is an index",
      84             :                         RelationGetRelationName(r))));
      85      221754 :     else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
      86           0 :         ereport(ERROR,
      87             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
      88             :                  errmsg("\"%s\" is a composite type",
      89             :                         RelationGetRelationName(r))));
      90             : 
      91      221754 :     return r;
      92             : }
      93             : 
      94             : /* ----------------
      95             :  *      table_openrv - open a table relation specified
      96             :  *      by a RangeVar node
      97             :  *
      98             :  *      As above, but relation is specified by a RangeVar.
      99             :  * ----------------
     100             :  */
     101             : Relation
     102       56964 : table_openrv(const RangeVar *relation, LOCKMODE lockmode)
     103             : {
     104             :     Relation    r;
     105             : 
     106       56964 :     r = relation_openrv(relation, lockmode);
     107             : 
     108       56950 :     if (r->rd_rel->relkind == RELKIND_INDEX ||
     109       56946 :         r->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
     110           4 :         ereport(ERROR,
     111             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     112             :                  errmsg("\"%s\" is an index",
     113             :                         RelationGetRelationName(r))));
     114       56946 :     else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
     115           0 :         ereport(ERROR,
     116             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     117             :                  errmsg("\"%s\" is a composite type",
     118             :                         RelationGetRelationName(r))));
     119             : 
     120       56946 :     return r;
     121             : }
     122             : 
     123             : /* ----------------
     124             :  *      table_openrv_extended - open a table relation specified
     125             :  *      by a RangeVar node
     126             :  *
     127             :  *      As above, but optionally return NULL instead of failing for
     128             :  *      relation-not-found.
     129             :  * ----------------
     130             :  */
     131             : Relation
     132      643710 : table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
     133             :                       bool missing_ok)
     134             : {
     135             :     Relation    r;
     136             : 
     137      643710 :     r = relation_openrv_extended(relation, lockmode, missing_ok);
     138             : 
     139      643622 :     if (r)
     140             :     {
     141      643482 :         if (r->rd_rel->relkind == RELKIND_INDEX ||
     142      643482 :             r->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
     143           0 :             ereport(ERROR,
     144             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     145             :                      errmsg("\"%s\" is an index",
     146             :                             RelationGetRelationName(r))));
     147      643482 :         else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
     148           0 :             ereport(ERROR,
     149             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     150             :                      errmsg("\"%s\" is a composite type",
     151             :                             RelationGetRelationName(r))));
     152             :     }
     153             : 
     154      643622 :     return r;
     155             : }
     156             : 
     157             : /* ----------------
     158             :  *      table_close - close a table
     159             :  *
     160             :  *      If lockmode is not "NoLock", we then release the specified lock.
     161             :  *
     162             :  *      Note that it is often sensible to hold a lock beyond relation_close;
     163             :  *      in that case, the lock is released automatically at xact end.
     164             :  *      ----------------
     165             :  */
     166             : void
     167    19088530 : table_close(Relation relation, LOCKMODE lockmode)
     168             : {
     169    19088530 :     relation_close(relation, lockmode);
     170    19088530 : }

Generated by: LCOV version 1.14