LCOV - code coverage report
Current view: top level - src/backend/catalog - toasting.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 95.3 % 127 121
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 7 7
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 65.0 % 60 39

             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/syscache.h"
      36                 :             : 
      37                 :             : static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
      38                 :             :                                      LOCKMODE lockmode, bool check,
      39                 :             :                                      Oid OIDOldToast);
      40                 :             : static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
      41                 :             :                                Datum reloptions, LOCKMODE lockmode, bool check,
      42                 :             :                                Oid OIDOldToast);
      43                 :             : static bool needs_toast_table(Relation rel);
      44                 :             : 
      45                 :             : 
      46                 :             : /*
      47                 :             :  * CreateToastTable variants
      48                 :             :  *      If the table needs a toast table, and doesn't already have one,
      49                 :             :  *      then create a toast table for it.
      50                 :             :  *
      51                 :             :  * reloptions for the toast table can be passed, too.  Pass (Datum) 0
      52                 :             :  * for default reloptions.
      53                 :             :  *
      54                 :             :  * We expect the caller to have verified that the relation is a table and have
      55                 :             :  * already done any necessary permission checks.  Callers expect this function
      56                 :             :  * to end with CommandCounterIncrement if it makes any changes.
      57                 :             :  */
      58                 :             : void
      59                 :       19734 : AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
      60                 :             : {
      61                 :       19734 :     CheckAndCreateToastTable(relOid, reloptions, lockmode, true, InvalidOid);
      62                 :       19734 : }
      63                 :             : 
      64                 :             : void
      65                 :         571 : NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
      66                 :             :                         Oid OIDOldToast)
      67                 :             : {
      68                 :         571 :     CheckAndCreateToastTable(relOid, reloptions, lockmode, false, OIDOldToast);
      69                 :         571 : }
      70                 :             : 
      71                 :             : void
      72                 :       26975 : NewRelationCreateToastTable(Oid relOid, Datum reloptions)
      73                 :             : {
      74                 :       26975 :     CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false,
      75                 :             :                              InvalidOid);
      76                 :       26975 : }
      77                 :             : 
      78                 :             : static void
      79                 :       47280 : CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
      80                 :             :                          bool check, Oid OIDOldToast)
      81                 :             : {
      82                 :             :     Relation    rel;
      83                 :             : 
      84                 :       47280 :     rel = table_open(relOid, lockmode);
      85                 :             : 
      86                 :             :     /* create_toast_table does all the work */
      87                 :       47280 :     (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode,
      88                 :             :                               check, OIDOldToast);
      89                 :             : 
      90                 :       47280 :     table_close(rel, NoLock);
      91                 :       47280 : }
      92                 :             : 
      93                 :             : /*
      94                 :             :  * Create a toast table during bootstrap
      95                 :             :  *
      96                 :             :  * Here we need to prespecify the OIDs of the toast table and its index
      97                 :             :  */
      98                 :             : void
      99                 :        2109 : BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
     100                 :             : {
     101                 :             :     Relation    rel;
     102                 :             : 
     103                 :        2109 :     rel = table_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
     104                 :             : 
     105         [ -  + ]:        2109 :     if (rel->rd_rel->relkind != RELKIND_RELATION &&
     106         [ #  # ]:           0 :         rel->rd_rel->relkind != RELKIND_MATVIEW)
     107         [ #  # ]:           0 :         elog(ERROR, "\"%s\" is not a table or materialized view",
     108                 :             :              relName);
     109                 :             : 
     110                 :             :     /* create_toast_table does all the work */
     111         [ -  + ]:        2109 :     if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
     112                 :             :                             AccessExclusiveLock, false, InvalidOid))
     113         [ #  # ]:           0 :         elog(ERROR, "\"%s\" does not require a toast table",
     114                 :             :              relName);
     115                 :             : 
     116                 :        2109 :     table_close(rel, NoLock);
     117                 :        2109 : }
     118                 :             : 
     119                 :             : 
     120                 :             : /*
     121                 :             :  * create_toast_table --- internal workhorse
     122                 :             :  *
     123                 :             :  * rel is already opened and locked
     124                 :             :  * toastOid and toastIndexOid are normally InvalidOid, but during
     125                 :             :  * bootstrap they can be nonzero to specify hand-assigned OIDs
     126                 :             :  */
     127                 :             : static bool
     128                 :       49389 : create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
     129                 :             :                    Datum reloptions, LOCKMODE lockmode, bool check,
     130                 :             :                    Oid OIDOldToast)
     131                 :             : {
     132                 :       49389 :     Oid         relOid = RelationGetRelid(rel);
     133                 :             :     HeapTuple   reltup;
     134                 :             :     TupleDesc   tupdesc;
     135                 :             :     bool        shared_relation;
     136                 :             :     bool        mapped_relation;
     137                 :             :     Relation    toast_rel;
     138                 :             :     Relation    class_rel;
     139                 :             :     Oid         toast_relid;
     140                 :             :     Oid         namespaceid;
     141                 :             :     char        toast_relname[NAMEDATALEN];
     142                 :             :     char        toast_idxname[NAMEDATALEN];
     143                 :             :     IndexInfo  *indexInfo;
     144                 :             :     Oid         collationIds[2];
     145                 :             :     Oid         opclassIds[2];
     146                 :             :     int16       coloptions[2];
     147                 :             :     ObjectAddress baseobject,
     148                 :             :                 toastobject;
     149                 :             : 
     150                 :             :     /*
     151                 :             :      * Is it already toasted?
     152                 :             :      */
     153         [ +  + ]:       49389 :     if (rel->rd_rel->reltoastrelid != InvalidOid)
     154                 :        6817 :         return false;
     155                 :             : 
     156                 :             :     /*
     157                 :             :      * Check to see whether the table actually needs a TOAST table.
     158                 :             :      */
     159         [ +  + ]:       42572 :     if (!IsBinaryUpgrade)
     160                 :             :     {
     161                 :             :         /* Normal mode, normal check */
     162         [ +  + ]:       40686 :         if (!needs_toast_table(rel))
     163                 :       29506 :             return false;
     164                 :             :     }
     165                 :             :     else
     166                 :             :     {
     167                 :             :         /*
     168                 :             :          * In binary-upgrade mode, create a TOAST table if and only if
     169                 :             :          * pg_upgrade told us to (ie, a TOAST table OID has been provided).
     170                 :             :          *
     171                 :             :          * This indicates that the old cluster had a TOAST table for the
     172                 :             :          * current table.  We must create a TOAST table to receive the old
     173                 :             :          * TOAST file, even if the table seems not to need one.
     174                 :             :          *
     175                 :             :          * Contrariwise, if the old cluster did not have a TOAST table, we
     176                 :             :          * should be able to get along without one even if the new version's
     177                 :             :          * needs_toast_table rules suggest we should have one.  There is a lot
     178                 :             :          * of daylight between where we will create a TOAST table and where
     179                 :             :          * one is really necessary to avoid failures, so small cross-version
     180                 :             :          * differences in the when-to-create heuristic shouldn't be a problem.
     181                 :             :          * If we tried to create a TOAST table anyway, we would have the
     182                 :             :          * problem that it might take up an OID that will conflict with some
     183                 :             :          * old-cluster table we haven't seen yet.
     184                 :             :          */
     185         [ +  + ]:        1886 :         if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
     186                 :        1597 :             return false;
     187                 :             :     }
     188                 :             : 
     189                 :             :     /*
     190                 :             :      * If requested check lockmode is sufficient. This is a cross check in
     191                 :             :      * case of errors or conflicting decisions in earlier code.
     192                 :             :      */
     193   [ +  +  -  + ]:       11469 :     if (check && lockmode != AccessExclusiveLock)
     194         [ #  # ]:           0 :         elog(ERROR, "AccessExclusiveLock required to add toast table.");
     195                 :             : 
     196                 :             :     /*
     197                 :             :      * Create the toast table and its index
     198                 :             :      */
     199                 :       11469 :     snprintf(toast_relname, sizeof(toast_relname),
     200                 :             :              "pg_toast_%u", relOid);
     201                 :       11469 :     snprintf(toast_idxname, sizeof(toast_idxname),
     202                 :             :              "pg_toast_%u_index", relOid);
     203                 :             : 
     204                 :             :     /* this is pretty painful...  need a tuple descriptor */
     205                 :       11469 :     tupdesc = CreateTemplateTupleDesc(3);
     206                 :       11469 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1,
     207                 :             :                        "chunk_id",
     208                 :             :                        OIDOID,
     209                 :             :                        -1, 0);
     210                 :       11469 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2,
     211                 :             :                        "chunk_seq",
     212                 :             :                        INT4OID,
     213                 :             :                        -1, 0);
     214                 :       11469 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3,
     215                 :             :                        "chunk_data",
     216                 :             :                        BYTEAOID,
     217                 :             :                        -1, 0);
     218                 :             : 
     219                 :             :     /*
     220                 :             :      * Ensure that the toast table doesn't itself get toasted, or we'll be
     221                 :             :      * toast :-(.  This is essential for chunk_data because type bytea is
     222                 :             :      * toastable; hit the other two just to be sure.
     223                 :             :      */
     224                 :       11469 :     TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
     225                 :       11469 :     TupleDescAttr(tupdesc, 1)->attstorage = TYPSTORAGE_PLAIN;
     226                 :       11469 :     TupleDescAttr(tupdesc, 2)->attstorage = TYPSTORAGE_PLAIN;
     227                 :             : 
     228                 :             :     /* Toast field should not be compressed */
     229                 :       11469 :     TupleDescAttr(tupdesc, 0)->attcompression = InvalidCompressionMethod;
     230                 :       11469 :     TupleDescAttr(tupdesc, 1)->attcompression = InvalidCompressionMethod;
     231                 :       11469 :     TupleDescAttr(tupdesc, 2)->attcompression = InvalidCompressionMethod;
     232                 :             : 
     233                 :       11469 :     populate_compact_attribute(tupdesc, 0);
     234                 :       11469 :     populate_compact_attribute(tupdesc, 1);
     235                 :       11469 :     populate_compact_attribute(tupdesc, 2);
     236                 :             : 
     237                 :       11469 :     TupleDescFinalize(tupdesc);
     238                 :             : 
     239                 :             :     /*
     240                 :             :      * Toast tables for regular relations go in pg_toast; those for temp
     241                 :             :      * relations go into the per-backend temp-toast-table namespace.
     242                 :             :      */
     243         [ +  + ]:       11469 :     if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
     244                 :         640 :         namespaceid = GetTempToastNamespace();
     245                 :             :     else
     246                 :       10829 :         namespaceid = PG_TOAST_NAMESPACE;
     247                 :             : 
     248                 :             :     /* Toast table is shared if and only if its parent is. */
     249                 :       11469 :     shared_relation = rel->rd_rel->relisshared;
     250                 :             : 
     251                 :             :     /* It's mapped if and only if its parent is, too */
     252   [ +  +  +  -  :       11469 :     mapped_relation = RelationIsMapped(rel);
          +  -  +  -  +  
                -  +  + ]
     253                 :             : 
     254                 :       22938 :     toast_relid = heap_create_with_catalog(toast_relname,
     255                 :             :                                            namespaceid,
     256                 :       11469 :                                            rel->rd_rel->reltablespace,
     257                 :             :                                            toastOid,
     258                 :             :                                            InvalidOid,
     259                 :             :                                            InvalidOid,
     260                 :       11469 :                                            rel->rd_rel->relowner,
     261                 :             :                                            table_relation_toast_am(rel),
     262                 :             :                                            tupdesc,
     263                 :             :                                            NIL,
     264                 :             :                                            RELKIND_TOASTVALUE,
     265                 :       11469 :                                            rel->rd_rel->relpersistence,
     266                 :             :                                            shared_relation,
     267                 :             :                                            mapped_relation,
     268                 :             :                                            ONCOMMIT_NOOP,
     269                 :             :                                            reloptions,
     270                 :             :                                            false,
     271                 :             :                                            true,
     272                 :             :                                            true,
     273                 :             :                                            OIDOldToast,
     274                 :             :                                            NULL);
     275                 :             :     Assert(toast_relid != InvalidOid);
     276                 :             : 
     277                 :             :     /* make the toast relation visible, else table_open will fail */
     278                 :       11469 :     CommandCounterIncrement();
     279                 :             : 
     280                 :             :     /* ShareLock is not really needed here, but take it anyway */
     281                 :       11469 :     toast_rel = table_open(toast_relid, ShareLock);
     282                 :             : 
     283                 :             :     /*
     284                 :             :      * Create unique index on chunk_id, chunk_seq.
     285                 :             :      *
     286                 :             :      * NOTE: the normal TOAST access routines could actually function with a
     287                 :             :      * single-column index on chunk_id only. However, the slice access
     288                 :             :      * routines use both columns for faster access to an individual chunk. In
     289                 :             :      * addition, we want it to be unique as a check against the possibility of
     290                 :             :      * duplicate TOAST chunk OIDs. The index might also be a little more
     291                 :             :      * efficient this way, since btree isn't all that happy with large numbers
     292                 :             :      * of equal keys.
     293                 :             :      */
     294                 :             : 
     295                 :       11469 :     indexInfo = makeNode(IndexInfo);
     296                 :       11469 :     indexInfo->ii_NumIndexAttrs = 2;
     297                 :       11469 :     indexInfo->ii_NumIndexKeyAttrs = 2;
     298                 :       11469 :     indexInfo->ii_IndexAttrNumbers[0] = 1;
     299                 :       11469 :     indexInfo->ii_IndexAttrNumbers[1] = 2;
     300                 :       11469 :     indexInfo->ii_Expressions = NIL;
     301                 :       11469 :     indexInfo->ii_ExpressionsState = NIL;
     302                 :       11469 :     indexInfo->ii_Predicate = NIL;
     303                 :       11469 :     indexInfo->ii_PredicateState = NULL;
     304                 :       11469 :     indexInfo->ii_ExclusionOps = NULL;
     305                 :       11469 :     indexInfo->ii_ExclusionProcs = NULL;
     306                 :       11469 :     indexInfo->ii_ExclusionStrats = NULL;
     307                 :       11469 :     indexInfo->ii_Unique = true;
     308                 :       11469 :     indexInfo->ii_NullsNotDistinct = false;
     309                 :       11469 :     indexInfo->ii_ReadyForInserts = true;
     310                 :       11469 :     indexInfo->ii_CheckedUnchanged = false;
     311                 :       11469 :     indexInfo->ii_IndexUnchanged = false;
     312                 :       11469 :     indexInfo->ii_Concurrent = false;
     313                 :       11469 :     indexInfo->ii_BrokenHotChain = false;
     314                 :       11469 :     indexInfo->ii_ParallelWorkers = 0;
     315                 :       11469 :     indexInfo->ii_Am = BTREE_AM_OID;
     316                 :       11469 :     indexInfo->ii_AmCache = NULL;
     317                 :       11469 :     indexInfo->ii_Context = CurrentMemoryContext;
     318                 :             : 
     319                 :       11469 :     collationIds[0] = InvalidOid;
     320                 :       11469 :     collationIds[1] = InvalidOid;
     321                 :             : 
     322                 :       11469 :     opclassIds[0] = OID_BTREE_OPS_OID;
     323                 :       11469 :     opclassIds[1] = INT4_BTREE_OPS_OID;
     324                 :             : 
     325                 :       11469 :     coloptions[0] = 0;
     326                 :       11469 :     coloptions[1] = 0;
     327                 :             : 
     328                 :       11469 :     index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
     329                 :             :                  InvalidOid, InvalidOid,
     330                 :             :                  indexInfo,
     331                 :       11469 :                  list_make2("chunk_id", "chunk_seq"),
     332                 :             :                  BTREE_AM_OID,
     333                 :       11469 :                  rel->rd_rel->reltablespace,
     334                 :             :                  collationIds, opclassIds, NULL, coloptions, NULL, (Datum) 0,
     335                 :             :                  INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL);
     336                 :             : 
     337                 :       11469 :     table_close(toast_rel, NoLock);
     338                 :             : 
     339                 :             :     /*
     340                 :             :      * Store the toast table's OID in the parent relation's pg_class row
     341                 :             :      */
     342                 :       11469 :     class_rel = table_open(RelationRelationId, RowExclusiveLock);
     343                 :             : 
     344         [ +  + ]:       11469 :     if (!IsBootstrapProcessingMode())
     345                 :             :     {
     346                 :             :         /* normal case, use a transactional update */
     347                 :        9360 :         reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
     348         [ -  + ]:        9360 :         if (!HeapTupleIsValid(reltup))
     349         [ #  # ]:           0 :             elog(ERROR, "cache lookup failed for relation %u", relOid);
     350                 :             : 
     351                 :        9360 :         ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
     352                 :             : 
     353                 :        9360 :         CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
     354                 :             :     }
     355                 :             :     else
     356                 :             :     {
     357                 :             :         /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
     358                 :             : 
     359                 :             :         ScanKeyData key[1];
     360                 :             :         void       *state;
     361                 :             : 
     362                 :        2109 :         ScanKeyInit(&key[0],
     363                 :             :                     Anum_pg_class_oid,
     364                 :             :                     BTEqualStrategyNumber, F_OIDEQ,
     365                 :             :                     ObjectIdGetDatum(relOid));
     366                 :        2109 :         systable_inplace_update_begin(class_rel, ClassOidIndexId, true,
     367                 :             :                                       NULL, 1, key, &reltup, &state);
     368         [ -  + ]:        2109 :         if (!HeapTupleIsValid(reltup))
     369         [ #  # ]:           0 :             elog(ERROR, "cache lookup failed for relation %u", relOid);
     370                 :             : 
     371                 :        2109 :         ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
     372                 :             : 
     373                 :        2109 :         systable_inplace_update_finish(state, reltup);
     374                 :             :     }
     375                 :             : 
     376                 :       11469 :     heap_freetuple(reltup);
     377                 :             : 
     378                 :       11469 :     table_close(class_rel, RowExclusiveLock);
     379                 :             : 
     380                 :             :     /*
     381                 :             :      * Register dependency from the toast table to the main, so that the toast
     382                 :             :      * table will be deleted if the main is.  Skip this in bootstrap mode.
     383                 :             :      */
     384         [ +  + ]:       11469 :     if (!IsBootstrapProcessingMode())
     385                 :             :     {
     386                 :        9360 :         baseobject.classId = RelationRelationId;
     387                 :        9360 :         baseobject.objectId = relOid;
     388                 :        9360 :         baseobject.objectSubId = 0;
     389                 :        9360 :         toastobject.classId = RelationRelationId;
     390                 :        9360 :         toastobject.objectId = toast_relid;
     391                 :        9360 :         toastobject.objectSubId = 0;
     392                 :             : 
     393                 :        9360 :         recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
     394                 :             :     }
     395                 :             : 
     396                 :             :     /*
     397                 :             :      * Make changes visible
     398                 :             :      */
     399                 :       11469 :     CommandCounterIncrement();
     400                 :             : 
     401                 :       11469 :     return true;
     402                 :             : }
     403                 :             : 
     404                 :             : /*
     405                 :             :  * Check to see whether the table needs a TOAST table.
     406                 :             :  */
     407                 :             : static bool
     408                 :       40686 : needs_toast_table(Relation rel)
     409                 :             : {
     410                 :             :     /*
     411                 :             :      * No need to create a TOAST table for partitioned tables.
     412                 :             :      */
     413         [ +  + ]:       40686 :     if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     414                 :        6218 :         return false;
     415                 :             : 
     416                 :             :     /*
     417                 :             :      * We cannot allow toasting a shared relation after initdb (because
     418                 :             :      * there's no way to mark it toasted in other databases' pg_class).
     419                 :             :      */
     420   [ +  +  +  + ]:       34468 :     if (rel->rd_rel->relisshared && !IsBootstrapProcessingMode())
     421                 :         385 :         return false;
     422                 :             : 
     423                 :             :     /*
     424                 :             :      * Ignore attempts to create toast tables on catalog tables after initdb.
     425                 :             :      * Which catalogs get toast tables is explicitly chosen in catalog/pg_*.h.
     426                 :             :      * (We could get here via some ALTER TABLE command if the catalog doesn't
     427                 :             :      * have a toast table.)
     428                 :             :      */
     429   [ +  +  +  + ]:       34083 :     if (IsCatalogRelation(rel) && !IsBootstrapProcessingMode())
     430                 :        2805 :         return false;
     431                 :             : 
     432                 :             :     /* Otherwise, let the AM decide. */
     433                 :       31278 :     return table_relation_needs_toast_table(rel);
     434                 :             : }
        

Generated by: LCOV version 2.0-1