LCOV - code coverage report
Current view: top level - src/backend/catalog - toasting.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 92.7 % 151 140
Test Date: 2026-09-26 09:15:51 Functions: 100.0 % 7 7
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 60.5 % 86 52

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * toasting.c
       4                 :             :  *    This file contains routines to support creation of toast tables
       5                 :             :  *
       6                 :             :  *
       7                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *    src/backend/catalog/toasting.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include "access/genam.h"
      18                 :             : #include "access/heapam.h"
      19                 :             : #include "access/toast_compression.h"
      20                 :             : #include "access/xact.h"
      21                 :             : #include "catalog/binary_upgrade.h"
      22                 :             : #include "catalog/catalog.h"
      23                 :             : #include "catalog/dependency.h"
      24                 :             : #include "catalog/heap.h"
      25                 :             : #include "catalog/index.h"
      26                 :             : #include "catalog/namespace.h"
      27                 :             : #include "catalog/pg_am.h"
      28                 :             : #include "catalog/pg_namespace.h"
      29                 :             : #include "catalog/pg_opclass.h"
      30                 :             : #include "catalog/toasting.h"
      31                 :             : #include "miscadmin.h"
      32                 :             : #include "nodes/makefuncs.h"
      33                 :             : #include "utils/fmgroids.h"
      34                 :             : #include "utils/rel.h"
      35                 :             : #include "utils/lsyscache.h"
      36                 :             : #include "utils/syscache.h"
      37                 :             : 
      38                 :             : static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
      39                 :             :                                      LOCKMODE lockmode, bool check,
      40                 :             :                                      Oid OIDOldToast);
      41                 :             : static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
      42                 :             :                                Datum reloptions, LOCKMODE lockmode, bool check,
      43                 :             :                                Oid OIDOldToast);
      44                 :             : static bool needs_toast_table(Relation rel);
      45                 :             : 
      46                 :             : 
      47                 :             : /*
      48                 :             :  * CreateToastTable variants
      49                 :             :  *      If the table needs a toast table, and doesn't already have one,
      50                 :             :  *      then create a toast table for it.
      51                 :             :  *
      52                 :             :  * reloptions for the toast table can be passed, too.  Pass (Datum) 0
      53                 :             :  * for default reloptions.
      54                 :             :  *
      55                 :             :  * We expect the caller to have verified that the relation is a table and have
      56                 :             :  * already done any necessary permission checks.  Callers expect this function
      57                 :             :  * to end with CommandCounterIncrement if it makes any changes.
      58                 :             :  */
      59                 :             : void
      60                 :       19076 : AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
      61                 :             : {
      62                 :       19076 :     CheckAndCreateToastTable(relOid, reloptions, lockmode, true, InvalidOid);
      63                 :       19076 : }
      64                 :             : 
      65                 :             : void
      66                 :         596 : NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
      67                 :             :                         Oid OIDOldToast)
      68                 :             : {
      69                 :         596 :     CheckAndCreateToastTable(relOid, reloptions, lockmode, false, OIDOldToast);
      70                 :         596 : }
      71                 :             : 
      72                 :             : void
      73                 :       25643 : NewRelationCreateToastTable(Oid relOid, Datum reloptions)
      74                 :             : {
      75                 :       25643 :     CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false,
      76                 :             :                              InvalidOid);
      77                 :       25643 : }
      78                 :             : 
      79                 :             : static void
      80                 :       45315 : CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
      81                 :             :                          bool check, Oid OIDOldToast)
      82                 :             : {
      83                 :             :     Relation    rel;
      84                 :             : 
      85                 :       45315 :     rel = table_open(relOid, lockmode);
      86                 :             : 
      87                 :             :     /* create_toast_table does all the work */
      88                 :       45315 :     (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode,
      89                 :             :                               check, OIDOldToast);
      90                 :             : 
      91                 :       45315 :     table_close(rel, NoLock);
      92                 :       45315 : }
      93                 :             : 
      94                 :             : /*
      95                 :             :  * Create a toast table during bootstrap
      96                 :             :  *
      97                 :             :  * Here we need to prespecify the OIDs of the toast table and its index
      98                 :             :  */
      99                 :             : void
     100                 :        2065 : BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
     101                 :             : {
     102                 :             :     Relation    rel;
     103                 :             : 
     104                 :        2065 :     rel = table_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
     105                 :             : 
     106         [ -  + ]:        2065 :     if (rel->rd_rel->relkind != RELKIND_RELATION &&
     107         [ #  # ]:           0 :         rel->rd_rel->relkind != RELKIND_MATVIEW)
     108         [ #  # ]:           0 :         elog(ERROR, "\"%s\" is not a table or materialized view",
     109                 :             :              relName);
     110                 :             : 
     111                 :             :     /* create_toast_table does all the work */
     112         [ -  + ]:        2065 :     if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
     113                 :             :                             AccessExclusiveLock, false, InvalidOid))
     114         [ #  # ]:           0 :         elog(ERROR, "\"%s\" does not require a toast table",
     115                 :             :              relName);
     116                 :             : 
     117                 :        2065 :     table_close(rel, NoLock);
     118                 :        2065 : }
     119                 :             : 
     120                 :             : 
     121                 :             : /*
     122                 :             :  * create_toast_table --- internal workhorse
     123                 :             :  *
     124                 :             :  * rel is already opened and locked
     125                 :             :  * toastOid and toastIndexOid are normally InvalidOid, but during
     126                 :             :  * bootstrap they can be nonzero to specify hand-assigned OIDs
     127                 :             :  */
     128                 :             : static bool
     129                 :       47380 : create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
     130                 :             :                    Datum reloptions, LOCKMODE lockmode, bool check,
     131                 :             :                    Oid OIDOldToast)
     132                 :             : {
     133                 :       47380 :     Oid         relOid = RelationGetRelid(rel);
     134                 :             :     HeapTuple   reltup;
     135                 :             :     TupleDesc   tupdesc;
     136                 :             :     bool        shared_relation;
     137                 :             :     bool        mapped_relation;
     138                 :             :     Relation    toast_rel;
     139                 :             :     Relation    class_rel;
     140                 :             :     Oid         toast_relid;
     141                 :             :     Oid         namespaceid;
     142                 :             :     char        toast_relname[NAMEDATALEN];
     143                 :             :     char        toast_idxname[NAMEDATALEN];
     144                 :             :     IndexInfo  *indexInfo;
     145                 :             :     Oid         collationIds[2];
     146                 :             :     Oid         opclassIds[2];
     147                 :             :     int16       coloptions[2];
     148                 :             :     ObjectAddress baseobject,
     149                 :             :                 toastobject;
     150                 :       47380 :     Oid         toast_chunkid_typid = OIDOID;
     151                 :             : 
     152                 :             :     /*
     153                 :             :      * Is it already toasted?
     154                 :             :      */
     155         [ +  + ]:       47380 :     if (rel->rd_rel->reltoastrelid != InvalidOid)
     156                 :        6730 :         return false;
     157                 :             : 
     158                 :             :     /*
     159                 :             :      * Check to see whether the table actually needs a TOAST table.
     160                 :             :      */
     161         [ +  + ]:       40650 :     if (!IsBinaryUpgrade)
     162                 :             :     {
     163                 :             :         StdRdOptToastValueType value_type;
     164                 :             : 
     165                 :             :         /* Normal mode, normal check */
     166         [ +  + ]:       38870 :         if (!needs_toast_table(rel))
     167                 :       27989 :             return false;
     168                 :             : 
     169         [ +  + ]:       10881 :         value_type = RelationGetToastValueType(rel, STDRD_OPTION_TOAST_VALUE_TYPE_OID);
     170                 :             : 
     171                 :             :         /* no default clause to catch new values added */
     172   [ +  +  -  - ]:       10881 :         switch (value_type)
     173                 :             :         {
     174                 :       10850 :             case STDRD_OPTION_TOAST_VALUE_TYPE_OID:
     175                 :       10850 :                 toast_chunkid_typid = OIDOID;
     176                 :       10850 :                 break;
     177                 :          31 :             case STDRD_OPTION_TOAST_VALUE_TYPE_OID8:
     178                 :          31 :                 toast_chunkid_typid = OID8OID;
     179                 :          31 :                 break;
     180                 :           0 :             case STDRD_OPTION_TOAST_VALUE_TYPE_INVALID:
     181         [ #  # ]:           0 :                 elog(ERROR, "unexpected toast_value_type value %d",
     182                 :             :                      value_type);
     183                 :             :                 break;
     184                 :             :         }
     185                 :             :     }
     186                 :             :     else
     187                 :             :     {
     188                 :             :         /*
     189                 :             :          * In binary-upgrade mode, create a TOAST table if and only if
     190                 :             :          * pg_upgrade told us to (ie, a TOAST table OID has been provided).
     191                 :             :          *
     192                 :             :          * This indicates that the old cluster had a TOAST table for the
     193                 :             :          * current table.  We must create a TOAST table to receive the old
     194                 :             :          * TOAST file, even if the table seems not to need one.
     195                 :             :          *
     196                 :             :          * Contrariwise, if the old cluster did not have a TOAST table, we
     197                 :             :          * should be able to get along without one even if the new version's
     198                 :             :          * needs_toast_table rules suggest we should have one.  There is a lot
     199                 :             :          * of daylight between where we will create a TOAST table and where
     200                 :             :          * one is really necessary to avoid failures, so small cross-version
     201                 :             :          * differences in the when-to-create heuristic shouldn't be a problem.
     202                 :             :          * If we tried to create a TOAST table anyway, we would have the
     203                 :             :          * problem that it might take up an OID that will conflict with some
     204                 :             :          * old-cluster table we haven't seen yet.
     205                 :             :          */
     206         [ +  + ]:        1780 :         if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
     207                 :        1502 :             return false;
     208                 :             : 
     209                 :             :         /*
     210                 :             :          * The attribute type for chunk_id should have been set when
     211                 :             :          * requesting a TOAST table creation.
     212                 :             :          */
     213         [ -  + ]:         278 :         if (!OidIsValid(binary_upgrade_next_toast_chunk_id_typoid))
     214         [ #  # ]:           0 :             ereport(ERROR,
     215                 :             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     216                 :             :                      errmsg("toast chunk_id type not set while in binary upgrade mode")));
     217         [ +  + ]:         278 :         if (binary_upgrade_next_toast_chunk_id_typoid != OIDOID &&
     218         [ -  + ]:           1 :             binary_upgrade_next_toast_chunk_id_typoid != OID8OID)
     219         [ #  # ]:           0 :             ereport(ERROR,
     220                 :             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     221                 :             :                      errmsg("cannot support toast chunk_id type %u in binary upgrade mode",
     222                 :             :                             binary_upgrade_next_toast_chunk_id_typoid)));
     223                 :             : 
     224                 :         278 :         toast_chunkid_typid = binary_upgrade_next_toast_chunk_id_typoid;
     225                 :         278 :         binary_upgrade_next_toast_chunk_id_typoid = InvalidOid;
     226                 :             :     }
     227                 :             : 
     228                 :             :     /*
     229                 :             :      * If requested check lockmode is sufficient. This is a cross check in
     230                 :             :      * case of errors or conflicting decisions in earlier code.
     231                 :             :      */
     232   [ +  +  -  + ]:       11159 :     if (check && lockmode != AccessExclusiveLock)
     233         [ #  # ]:           0 :         elog(ERROR, "AccessExclusiveLock required to add toast table.");
     234                 :             : 
     235                 :             :     /*
     236                 :             :      * Create the toast table and its index
     237                 :             :      */
     238                 :       11159 :     snprintf(toast_relname, sizeof(toast_relname),
     239                 :             :              "pg_toast_%u", relOid);
     240                 :       11159 :     snprintf(toast_idxname, sizeof(toast_idxname),
     241                 :             :              "pg_toast_%u_index", relOid);
     242                 :             : 
     243                 :             :     /*
     244                 :             :      * Special case here.  If OIDOldToast is defined, rely on the existing
     245                 :             :      * TOAST table and its chunk_id type, not the reloption value.  This
     246                 :             :      * guarantees that the same TOAST table is kept across rewrites of the
     247                 :             :      * parent.
     248                 :             :      */
     249         [ +  + ]:       11159 :     if (OidIsValid(OIDOldToast))
     250                 :             :     {
     251                 :         571 :         toast_chunkid_typid = get_atttype(OIDOldToast, 1);
     252         [ -  + ]:         571 :         if (!OidIsValid(toast_chunkid_typid))
     253         [ #  # ]:           0 :             elog(ERROR, "cache lookup failed for relation %u", OIDOldToast);
     254                 :             :     }
     255                 :             : 
     256                 :             :     /* this is pretty painful...  need a tuple descriptor */
     257                 :       11159 :     tupdesc = CreateTemplateTupleDesc(3);
     258                 :       11159 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1,
     259                 :             :                        "chunk_id",
     260                 :             :                        toast_chunkid_typid,
     261                 :             :                        -1, 0);
     262                 :       11159 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2,
     263                 :             :                        "chunk_seq",
     264                 :             :                        INT4OID,
     265                 :             :                        -1, 0);
     266                 :       11159 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3,
     267                 :             :                        "chunk_data",
     268                 :             :                        BYTEAOID,
     269                 :             :                        -1, 0);
     270                 :             : 
     271                 :             :     /*
     272                 :             :      * Ensure that the toast table doesn't itself get toasted, or we'll be
     273                 :             :      * toast :-(.  This is essential for chunk_data because type bytea is
     274                 :             :      * toastable; hit the other two just to be sure.
     275                 :             :      */
     276                 :       11159 :     TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
     277                 :       11159 :     TupleDescAttr(tupdesc, 1)->attstorage = TYPSTORAGE_PLAIN;
     278                 :       11159 :     TupleDescAttr(tupdesc, 2)->attstorage = TYPSTORAGE_PLAIN;
     279                 :             : 
     280                 :             :     /* Toast field should not be compressed */
     281                 :       11159 :     TupleDescAttr(tupdesc, 0)->attcompression = InvalidCompressionMethod;
     282                 :       11159 :     TupleDescAttr(tupdesc, 1)->attcompression = InvalidCompressionMethod;
     283                 :       11159 :     TupleDescAttr(tupdesc, 2)->attcompression = InvalidCompressionMethod;
     284                 :             : 
     285                 :       11159 :     populate_compact_attribute(tupdesc, 0);
     286                 :       11159 :     populate_compact_attribute(tupdesc, 1);
     287                 :       11159 :     populate_compact_attribute(tupdesc, 2);
     288                 :             : 
     289                 :       11159 :     TupleDescFinalize(tupdesc);
     290                 :             : 
     291                 :             :     /*
     292                 :             :      * Toast tables for regular relations go in pg_toast; those for temp
     293                 :             :      * relations go into the per-backend temp-toast-table namespace.
     294                 :             :      */
     295         [ +  + ]:       11159 :     if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
     296                 :         674 :         namespaceid = GetTempToastNamespace();
     297                 :             :     else
     298                 :       10485 :         namespaceid = PG_TOAST_NAMESPACE;
     299                 :             : 
     300                 :             :     /* Toast table is shared if and only if its parent is. */
     301                 :       11159 :     shared_relation = rel->rd_rel->relisshared;
     302                 :             : 
     303                 :             :     /* It's mapped if and only if its parent is, too */
     304   [ +  +  +  -  :       11159 :     mapped_relation = RelationIsMapped(rel);
          +  -  +  -  +  
                -  +  + ]
     305                 :             : 
     306                 :       22318 :     toast_relid = heap_create_with_catalog(toast_relname,
     307                 :             :                                            namespaceid,
     308                 :       11159 :                                            rel->rd_rel->reltablespace,
     309                 :             :                                            toastOid,
     310                 :             :                                            InvalidOid,
     311                 :             :                                            InvalidOid,
     312                 :       11159 :                                            rel->rd_rel->relowner,
     313                 :             :                                            table_relation_toast_am(rel),
     314                 :             :                                            tupdesc,
     315                 :             :                                            NIL,
     316                 :             :                                            RELKIND_TOASTVALUE,
     317                 :       11159 :                                            rel->rd_rel->relpersistence,
     318                 :             :                                            shared_relation,
     319                 :             :                                            mapped_relation,
     320                 :             :                                            ONCOMMIT_NOOP,
     321                 :             :                                            reloptions,
     322                 :             :                                            false,
     323                 :             :                                            true,
     324                 :             :                                            true,
     325                 :             :                                            OIDOldToast,
     326                 :             :                                            NULL);
     327                 :             :     Assert(toast_relid != InvalidOid);
     328                 :             : 
     329                 :             :     /* make the toast relation visible, else table_open will fail */
     330                 :       11159 :     CommandCounterIncrement();
     331                 :             : 
     332                 :             :     /* ShareLock is not really needed here, but take it anyway */
     333                 :       11159 :     toast_rel = table_open(toast_relid, ShareLock);
     334                 :             : 
     335                 :             :     /*
     336                 :             :      * Create unique index on chunk_id, chunk_seq.
     337                 :             :      *
     338                 :             :      * NOTE: the normal TOAST access routines could actually function with a
     339                 :             :      * single-column index on chunk_id only. However, the slice access
     340                 :             :      * routines use both columns for faster access to an individual chunk. In
     341                 :             :      * addition, we want it to be unique as a check against the possibility of
     342                 :             :      * duplicate TOAST chunk OIDs. The index might also be a little more
     343                 :             :      * efficient this way, since btree isn't all that happy with large numbers
     344                 :             :      * of equal keys.
     345                 :             :      */
     346                 :             : 
     347                 :       11159 :     indexInfo = makeNode(IndexInfo);
     348                 :       11159 :     indexInfo->ii_NumIndexAttrs = 2;
     349                 :       11159 :     indexInfo->ii_NumIndexKeyAttrs = 2;
     350                 :       11159 :     indexInfo->ii_IndexAttrNumbers[0] = 1;
     351                 :       11159 :     indexInfo->ii_IndexAttrNumbers[1] = 2;
     352                 :       11159 :     indexInfo->ii_Expressions = NIL;
     353                 :       11159 :     indexInfo->ii_ExpressionsState = NIL;
     354                 :       11159 :     indexInfo->ii_Predicate = NIL;
     355                 :       11159 :     indexInfo->ii_PredicateState = NULL;
     356                 :       11159 :     indexInfo->ii_ExclusionOps = NULL;
     357                 :       11159 :     indexInfo->ii_ExclusionProcs = NULL;
     358                 :       11159 :     indexInfo->ii_ExclusionStrats = NULL;
     359                 :       11159 :     indexInfo->ii_Unique = true;
     360                 :       11159 :     indexInfo->ii_NullsNotDistinct = false;
     361                 :       11159 :     indexInfo->ii_ReadyForInserts = true;
     362                 :       11159 :     indexInfo->ii_CheckedUnchanged = false;
     363                 :       11159 :     indexInfo->ii_IndexUnchanged = false;
     364                 :       11159 :     indexInfo->ii_Concurrent = false;
     365                 :       11159 :     indexInfo->ii_BrokenHotChain = false;
     366                 :       11159 :     indexInfo->ii_ParallelWorkers = 0;
     367                 :       11159 :     indexInfo->ii_Am = BTREE_AM_OID;
     368                 :       11159 :     indexInfo->ii_AmCache = NULL;
     369                 :       11159 :     indexInfo->ii_Context = CurrentMemoryContext;
     370                 :             : 
     371                 :       11159 :     collationIds[0] = InvalidOid;
     372                 :       11159 :     collationIds[1] = InvalidOid;
     373                 :             : 
     374         [ +  + ]:       11159 :     if (toast_chunkid_typid == OID8OID)
     375                 :          32 :         opclassIds[0] = OID8_BTREE_OPS_OID;
     376                 :             :     else
     377                 :       11127 :         opclassIds[0] = OID_BTREE_OPS_OID;
     378                 :       11159 :     opclassIds[1] = INT4_BTREE_OPS_OID;
     379                 :             : 
     380                 :       11159 :     coloptions[0] = 0;
     381                 :       11159 :     coloptions[1] = 0;
     382                 :             : 
     383                 :             :     /*
     384                 :             :      * Don't let index creation overwrite progress information for the command
     385                 :             :      * that caused the TOAST table to be created.
     386                 :             :      */
     387                 :       11159 :     index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
     388                 :             :                  InvalidOid, InvalidOid,
     389                 :             :                  indexInfo,
     390                 :       11159 :                  list_make2("chunk_id", "chunk_seq"),
     391                 :             :                  BTREE_AM_OID,
     392                 :       11159 :                  rel->rd_rel->reltablespace,
     393                 :             :                  collationIds, opclassIds, NULL, coloptions, NULL, (Datum) 0,
     394                 :             :                  INDEX_CREATE_IS_PRIMARY | INDEX_CREATE_SUPPRESS_PROGRESS,
     395                 :             :                  0, true, true, NULL);
     396                 :             : 
     397                 :       11159 :     table_close(toast_rel, NoLock);
     398                 :             : 
     399                 :             :     /*
     400                 :             :      * Store the toast table's OID in the parent relation's pg_class row
     401                 :             :      */
     402                 :       11159 :     class_rel = table_open(RelationRelationId, RowExclusiveLock);
     403                 :             : 
     404         [ +  + ]:       11159 :     if (!IsBootstrapProcessingMode())
     405                 :             :     {
     406                 :             :         /* normal case, use a transactional update */
     407                 :        9094 :         reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
     408         [ -  + ]:        9094 :         if (!HeapTupleIsValid(reltup))
     409         [ #  # ]:           0 :             elog(ERROR, "cache lookup failed for relation %u", relOid);
     410                 :             : 
     411                 :        9094 :         ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
     412                 :             : 
     413                 :        9094 :         CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
     414                 :             :     }
     415                 :             :     else
     416                 :             :     {
     417                 :             :         /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
     418                 :             : 
     419                 :             :         ScanKeyData key[1];
     420                 :             :         void       *state;
     421                 :             : 
     422                 :        2065 :         ScanKeyInit(&key[0],
     423                 :             :                     Anum_pg_class_oid,
     424                 :             :                     BTEqualStrategyNumber, F_OIDEQ,
     425                 :             :                     ObjectIdGetDatum(relOid));
     426                 :        2065 :         systable_inplace_update_begin(class_rel, ClassOidIndexId, true,
     427                 :             :                                       NULL, 1, key, &reltup, &state);
     428         [ -  + ]:        2065 :         if (!HeapTupleIsValid(reltup))
     429         [ #  # ]:           0 :             elog(ERROR, "cache lookup failed for relation %u", relOid);
     430                 :             : 
     431                 :        2065 :         ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
     432                 :             : 
     433                 :        2065 :         systable_inplace_update_finish(state, reltup);
     434                 :             :     }
     435                 :             : 
     436                 :       11159 :     heap_freetuple(reltup);
     437                 :             : 
     438                 :       11159 :     table_close(class_rel, RowExclusiveLock);
     439                 :             : 
     440                 :             :     /*
     441                 :             :      * Register dependency from the toast table to the main, so that the toast
     442                 :             :      * table will be deleted if the main is.  Skip this in bootstrap mode.
     443                 :             :      */
     444         [ +  + ]:       11159 :     if (!IsBootstrapProcessingMode())
     445                 :             :     {
     446                 :        9094 :         baseobject.classId = RelationRelationId;
     447                 :        9094 :         baseobject.objectId = relOid;
     448                 :        9094 :         baseobject.objectSubId = 0;
     449                 :        9094 :         toastobject.classId = RelationRelationId;
     450                 :        9094 :         toastobject.objectId = toast_relid;
     451                 :        9094 :         toastobject.objectSubId = 0;
     452                 :             : 
     453                 :        9094 :         recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
     454                 :             :     }
     455                 :             : 
     456                 :             :     /*
     457                 :             :      * Make changes visible
     458                 :             :      */
     459                 :       11159 :     CommandCounterIncrement();
     460                 :             : 
     461                 :       11159 :     return true;
     462                 :             : }
     463                 :             : 
     464                 :             : /*
     465                 :             :  * Check to see whether the table needs a TOAST table.
     466                 :             :  */
     467                 :             : static bool
     468                 :       38870 : needs_toast_table(Relation rel)
     469                 :             : {
     470                 :             :     /*
     471                 :             :      * No need to create a TOAST table for partitioned tables.
     472                 :             :      */
     473         [ +  + ]:       38870 :     if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     474                 :        5573 :         return false;
     475                 :             : 
     476                 :             :     /*
     477                 :             :      * We cannot allow toasting a shared relation after initdb (because
     478                 :             :      * there's no way to mark it toasted in other databases' pg_class).
     479                 :             :      */
     480   [ +  +  +  + ]:       33297 :     if (rel->rd_rel->relisshared && !IsBootstrapProcessingMode())
     481                 :         399 :         return false;
     482                 :             : 
     483                 :             :     /*
     484                 :             :      * Ignore attempts to create toast tables on catalog tables after initdb.
     485                 :             :      * Which catalogs get toast tables is explicitly chosen in catalog/pg_*.h.
     486                 :             :      * (We could get here via some ALTER TABLE command if the catalog doesn't
     487                 :             :      * have a toast table.)
     488                 :             :      */
     489   [ +  +  +  + ]:       32898 :     if (IsCatalogRelation(rel) && !IsBootstrapProcessingMode())
     490                 :        2565 :         return false;
     491                 :             : 
     492                 :             :     /* Otherwise, let the AM decide. */
     493                 :       30333 :     return table_relation_needs_toast_table(rel);
     494                 :             : }
        

Generated by: LCOV version 2.0-1