LCOV - differential code coverage report
Current view: top level - src/backend/catalog - toasting.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 95.8 % 120 115 5 115
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 7 7 7
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 95.8 % 120 115 5 115
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 100.0 % 7 7 7

 Age         Owner                  TLA  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-2023, 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/heapam.h"
                                 18                 : #include "access/toast_compression.h"
                                 19                 : #include "access/xact.h"
                                 20                 : #include "catalog/binary_upgrade.h"
                                 21                 : #include "catalog/catalog.h"
                                 22                 : #include "catalog/dependency.h"
                                 23                 : #include "catalog/heap.h"
                                 24                 : #include "catalog/index.h"
                                 25                 : #include "catalog/namespace.h"
                                 26                 : #include "catalog/pg_am.h"
                                 27                 : #include "catalog/pg_namespace.h"
                                 28                 : #include "catalog/pg_opclass.h"
                                 29                 : #include "catalog/pg_type.h"
                                 30                 : #include "catalog/toasting.h"
                                 31                 : #include "miscadmin.h"
                                 32                 : #include "nodes/makefuncs.h"
                                 33                 : #include "storage/lock.h"
                                 34                 : #include "utils/builtins.h"
                                 35                 : #include "utils/rel.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
 3290 simon                      60 CBC       43739 : AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
                                 61                 : {
  592 akapila                    62           43739 :     CheckAndCreateToastTable(relOid, reloptions, lockmode, true, InvalidOid);
 3290 simon                      63           43739 : }
                                 64                 : 
                                 65                 : void
  592 akapila                    66             294 : NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
                                 67                 :                         Oid OIDOldToast)
                                 68                 : {
                                 69             294 :     CheckAndCreateToastTable(relOid, reloptions, lockmode, false, OIDOldToast);
 3290 simon                      70             294 : }
                                 71                 : 
                                 72                 : void
                                 73           17238 : NewRelationCreateToastTable(Oid relOid, Datum reloptions)
                                 74                 : {
  592 akapila                    75           17238 :     CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false,
                                 76                 :                              InvalidOid);
 3290 simon                      77           17238 : }
                                 78                 : 
                                 79                 : static void
  592 akapila                    80           61271 : CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
                                 81                 :                          bool check, Oid OIDOldToast)
                                 82                 : {
                                 83                 :     Relation    rel;
                                 84                 : 
 1539 andres                     85           61271 :     rel = table_open(relOid, lockmode);
                                 86                 : 
                                 87                 :     /* create_toast_table does all the work */
  592 akapila                    88           61271 :     (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode,
                                 89                 :                               check, OIDOldToast);
                                 90                 : 
 1539 andres                     91           61271 :     table_close(rel, NoLock);
 6096 tgl                        92           61271 : }
                                 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           10980 : BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
                                101                 : {
                                102                 :     Relation    rel;
                                103                 : 
 1539 andres                    104           10980 :     rel = table_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
                                105                 : 
 3689 kgrittn                   106           10980 :     if (rel->rd_rel->relkind != RELKIND_RELATION &&
 3689 kgrittn                   107 UBC           0 :         rel->rd_rel->relkind != RELKIND_MATVIEW)
  640 peter                     108               0 :         elog(ERROR, "\"%s\" is not a table or materialized view",
                                109                 :              relName);
                                110                 : 
                                111                 :     /* create_toast_table does all the work */
 3290 simon                     112 CBC       10980 :     if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
                                113                 :                             AccessExclusiveLock, false, InvalidOid))
 6096 tgl                       114 UBC           0 :         elog(ERROR, "\"%s\" does not require a toast table",
                                115                 :              relName);
                                116                 : 
 1539 andres                    117 CBC       10980 :     table_close(rel, NoLock);
 6096 tgl                       118           10980 : }
                                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
 3290 simon                     129           72251 : create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
                                130                 :                    Datum reloptions, LOCKMODE lockmode, bool check,
                                131                 :                    Oid OIDOldToast)
                                132                 : {
 6096 tgl                       133           72251 :     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         collationObjectId[2];
                                146                 :     Oid         classObjectId[2];
                                147                 :     int16       coloptions[2];
                                148                 :     ObjectAddress baseobject,
                                149                 :                 toastobject;
                                150                 : 
                                151                 :     /*
                                152                 :      * Is it already toasted?
                                153                 :      */
                                154           72251 :     if (rel->rd_rel->reltoastrelid != InvalidOid)
                                155           21692 :         return false;
                                156                 : 
                                157                 :     /*
                                158                 :      * Check to see whether the table actually needs a TOAST table.
                                159                 :      */
 3167 bruce                     160           50559 :     if (!IsBinaryUpgrade)
                                161                 :     {
                                162                 :         /* Normal mode, normal check */
                                163           49166 :         if (!needs_toast_table(rel))
                                164           31384 :             return false;
                                165                 :     }
                                166                 :     else
                                167                 :     {
                                168                 :         /*
                                169                 :          * In binary-upgrade mode, create a TOAST table if and only if
                                170                 :          * pg_upgrade told us to (ie, a TOAST table OID has been provided).
                                171                 :          *
                                172                 :          * This indicates that the old cluster had a TOAST table for the
                                173                 :          * current table.  We must create a TOAST table to receive the old
                                174                 :          * TOAST file, even if the table seems not to need one.
                                175                 :          *
                                176                 :          * Contrariwise, if the old cluster did not have a TOAST table, we
                                177                 :          * should be able to get along without one even if the new version's
                                178                 :          * needs_toast_table rules suggest we should have one.  There is a lot
                                179                 :          * of daylight between where we will create a TOAST table and where
                                180                 :          * one is really necessary to avoid failures, so small cross-version
                                181                 :          * differences in the when-to-create heuristic shouldn't be a problem.
                                182                 :          * If we tried to create a TOAST table anyway, we would have the
                                183                 :          * problem that it might take up an OID that will conflict with some
                                184                 :          * old-cluster table we haven't seen yet.
                                185                 :          */
 1006 tgl                       186            1393 :         if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
 3167 bruce                     187            1149 :             return false;
                                188                 :     }
                                189                 : 
                                190                 :     /*
                                191                 :      * If requested check lockmode is sufficient. This is a cross check in
                                192                 :      * case of errors or conflicting decisions in earlier code.
                                193                 :      */
 3290 simon                     194           18026 :     if (check && lockmode != AccessExclusiveLock)
 3290 simon                     195 UBC           0 :         elog(ERROR, "AccessExclusiveLock required to add toast table.");
                                196                 : 
                                197                 :     /*
                                198                 :      * Create the toast table and its index
                                199                 :      */
 6096 tgl                       200 CBC       18026 :     snprintf(toast_relname, sizeof(toast_relname),
                                201                 :              "pg_toast_%u", relOid);
                                202           18026 :     snprintf(toast_idxname, sizeof(toast_idxname),
                                203                 :              "pg_toast_%u_index", relOid);
                                204                 : 
                                205                 :     /* this is pretty painful...  need a tuple descriptor */
 1601 andres                    206           18026 :     tupdesc = CreateTemplateTupleDesc(3);
 6096 tgl                       207           18026 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1,
                                208                 :                        "chunk_id",
                                209                 :                        OIDOID,
                                210                 :                        -1, 0);
                                211           18026 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2,
                                212                 :                        "chunk_seq",
                                213                 :                        INT4OID,
                                214                 :                        -1, 0);
                                215           18026 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3,
                                216                 :                        "chunk_data",
                                217                 :                        BYTEAOID,
                                218                 :                        -1, 0);
                                219                 : 
                                220                 :     /*
                                221                 :      * Ensure that the toast table doesn't itself get toasted, or we'll be
                                222                 :      * toast :-(.  This is essential for chunk_data because type bytea is
                                223                 :      * toastable; hit the other two just to be sure.
                                224                 :      */
 1131                           225           18026 :     TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
                                226           18026 :     TupleDescAttr(tupdesc, 1)->attstorage = TYPSTORAGE_PLAIN;
                                227           18026 :     TupleDescAttr(tupdesc, 2)->attstorage = TYPSTORAGE_PLAIN;
                                228                 : 
                                229                 :     /* Toast field should not be compressed */
  751 rhaas                     230           18026 :     TupleDescAttr(tupdesc, 0)->attcompression = InvalidCompressionMethod;
                                231           18026 :     TupleDescAttr(tupdesc, 1)->attcompression = InvalidCompressionMethod;
                                232           18026 :     TupleDescAttr(tupdesc, 2)->attcompression = InvalidCompressionMethod;
                                233                 : 
                                234                 :     /*
                                235                 :      * Toast tables for regular relations go in pg_toast; those for temp
                                236                 :      * relations go into the per-backend temp-toast-table namespace.
                                237                 :      */
 3149 bruce                     238           18026 :     if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
 5737 tgl                       239             430 :         namespaceid = GetTempToastNamespace();
                                240                 :     else
                                241           17596 :         namespaceid = PG_TOAST_NAMESPACE;
                                242                 : 
                                243                 :     /* Toast table is shared if and only if its parent is. */
 1482 peter                     244           18026 :     shared_relation = rel->rd_rel->relisshared;
                                245                 : 
                                246                 :     /* It's mapped if and only if its parent is, too */
                                247           18026 :     mapped_relation = RelationIsMapped(rel);
                                248                 : 
 6096 tgl                       249           36052 :     toast_relid = heap_create_with_catalog(toast_relname,
                                250                 :                                            namespaceid,
                                251           18026 :                                            rel->rd_rel->reltablespace,
                                252                 :                                            toastOid,
                                253                 :                                            InvalidOid,
                                254                 :                                            InvalidOid,
                                255           18026 :                                            rel->rd_rel->relowner,
                                256                 :                                            table_relation_toast_am(rel),
                                257                 :                                            tupdesc,
                                258                 :                                            NIL,
                                259                 :                                            RELKIND_TOASTVALUE,
 4500 rhaas                     260           18026 :                                            rel->rd_rel->relpersistence,
                                261                 :                                            shared_relation,
                                262                 :                                            mapped_relation,
                                263                 :                                            ONCOMMIT_NOOP,
                                264                 :                                            reloptions,
                                265                 :                                            false,
                                266                 :                                            true,
                                267                 :                                            true,
                                268                 :                                            OIDOldToast,
                                269                 :                                            NULL);
 4641                           270           18026 :     Assert(toast_relid != InvalidOid);
                                271                 : 
                                272                 :     /* make the toast relation visible, else table_open will fail */
 6096 tgl                       273           18026 :     CommandCounterIncrement();
                                274                 : 
                                275                 :     /* ShareLock is not really needed here, but take it anyway */
 1539 andres                    276           18026 :     toast_rel = table_open(toast_relid, ShareLock);
                                277                 : 
                                278                 :     /*
                                279                 :      * Create unique index on chunk_id, chunk_seq.
                                280                 :      *
                                281                 :      * NOTE: the normal TOAST access routines could actually function with a
                                282                 :      * single-column index on chunk_id only. However, the slice access
                                283                 :      * routines use both columns for faster access to an individual chunk. In
                                284                 :      * addition, we want it to be unique as a check against the possibility of
                                285                 :      * duplicate TOAST chunk OIDs. The index might also be a little more
                                286                 :      * efficient this way, since btree isn't all that happy with large numbers
                                287                 :      * of equal keys.
                                288                 :      */
                                289                 : 
 6096 tgl                       290           18026 :     indexInfo = makeNode(IndexInfo);
                                291           18026 :     indexInfo->ii_NumIndexAttrs = 2;
 1828 teodor                    292           18026 :     indexInfo->ii_NumIndexKeyAttrs = 2;
 1823                           293           18026 :     indexInfo->ii_IndexAttrNumbers[0] = 1;
                                294           18026 :     indexInfo->ii_IndexAttrNumbers[1] = 2;
 6096 tgl                       295           18026 :     indexInfo->ii_Expressions = NIL;
                                296           18026 :     indexInfo->ii_ExpressionsState = NIL;
                                297           18026 :     indexInfo->ii_Predicate = NIL;
 2217 andres                    298           18026 :     indexInfo->ii_PredicateState = NULL;
 4871 tgl                       299           18026 :     indexInfo->ii_ExclusionOps = NULL;
                                300           18026 :     indexInfo->ii_ExclusionProcs = NULL;
                                301           18026 :     indexInfo->ii_ExclusionStrats = NULL;
 1105 akorotkov                 302           18026 :     indexInfo->ii_OpclassOptions = NULL;
 6096 tgl                       303           18026 :     indexInfo->ii_Unique = true;
  430 peter                     304           18026 :     indexInfo->ii_NullsNotDistinct = false;
 5680 tgl                       305           18026 :     indexInfo->ii_ReadyForInserts = true;
  452 pg                        306           18026 :     indexInfo->ii_CheckedUnchanged = false;
                                307           18026 :     indexInfo->ii_IndexUnchanged = false;
 6071 tgl                       308           18026 :     indexInfo->ii_Concurrent = false;
 5680                           309           18026 :     indexInfo->ii_BrokenHotChain = false;
 1892 rhaas                     310           18026 :     indexInfo->ii_ParallelWorkers = 0;
 1906 alvherre                  311           18026 :     indexInfo->ii_Am = BTREE_AM_OID;
 2250 tgl                       312           18026 :     indexInfo->ii_AmCache = NULL;
                                313           18026 :     indexInfo->ii_Context = CurrentMemoryContext;
                                314                 : 
 4443 peter_e                   315           18026 :     collationObjectId[0] = InvalidOid;
                                316           18026 :     collationObjectId[1] = InvalidOid;
                                317                 : 
 6096 tgl                       318           18026 :     classObjectId[0] = OID_BTREE_OPS_OID;
                                319           18026 :     classObjectId[1] = INT4_BTREE_OPS_OID;
                                320                 : 
 5934                           321           18026 :     coloptions[0] = 0;
                                322           18026 :     coloptions[1] = 0;
                                323                 : 
 4283 rhaas                     324           18026 :     index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
                                325                 :                  InvalidOid, InvalidOid,
                                326                 :                  indexInfo,
 4322 bruce                     327           18026 :                  list_make2("chunk_id", "chunk_seq"),
                                328                 :                  BTREE_AM_OID,
                                329           18026 :                  rel->rd_rel->reltablespace,
                                330                 :                  collationObjectId, classObjectId, coloptions, (Datum) 0,
                                331                 :                  INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL);
                                332                 : 
 1539 andres                    333           18026 :     table_close(toast_rel, NoLock);
                                334                 : 
                                335                 :     /*
                                336                 :      * Store the toast table's OID in the parent relation's pg_class row
                                337                 :      */
                                338           18026 :     class_rel = table_open(RelationRelationId, RowExclusiveLock);
                                339                 : 
 4802 rhaas                     340           18026 :     reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
 6096 tgl                       341           18026 :     if (!HeapTupleIsValid(reltup))
 6096 tgl                       342 UBC           0 :         elog(ERROR, "cache lookup failed for relation %u", relOid);
                                343                 : 
 6096 tgl                       344 CBC       18026 :     ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
                                345                 : 
                                346           18026 :     if (!IsBootstrapProcessingMode())
                                347                 :     {
                                348                 :         /* normal case, use a transactional update */
 2259 alvherre                  349            7046 :         CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
                                350                 :     }
                                351                 :     else
                                352                 :     {
                                353                 :         /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
 6096 tgl                       354           10980 :         heap_inplace_update(class_rel, reltup);
                                355                 :     }
                                356                 : 
                                357           18026 :     heap_freetuple(reltup);
                                358                 : 
 1539 andres                    359           18026 :     table_close(class_rel, RowExclusiveLock);
                                360                 : 
                                361                 :     /*
                                362                 :      * Register dependency from the toast table to the main, so that the toast
                                363                 :      * table will be deleted if the main is.  Skip this in bootstrap mode.
                                364                 :      */
 6096 tgl                       365           18026 :     if (!IsBootstrapProcessingMode())
                                366                 :     {
                                367            7046 :         baseobject.classId = RelationRelationId;
                                368            7046 :         baseobject.objectId = relOid;
                                369            7046 :         baseobject.objectSubId = 0;
                                370            7046 :         toastobject.classId = RelationRelationId;
                                371            7046 :         toastobject.objectId = toast_relid;
                                372            7046 :         toastobject.objectSubId = 0;
                                373                 : 
                                374            7046 :         recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
                                375                 :     }
                                376                 : 
                                377                 :     /*
                                378                 :      * Make changes visible
                                379                 :      */
                                380           18026 :     CommandCounterIncrement();
                                381                 : 
                                382           18026 :     return true;
                                383                 : }
                                384                 : 
                                385                 : /*
                                386                 :  * Check to see whether the table needs a TOAST table.
                                387                 :  */
                                388                 : static bool
                                389           49166 : needs_toast_table(Relation rel)
                                390                 : {
                                391                 :     /*
                                392                 :      * No need to create a TOAST table for partitioned tables.
                                393                 :      */
 1844 rhaas                     394           49166 :     if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
                                395            3736 :         return false;
                                396                 : 
                                397                 :     /*
                                398                 :      * We cannot allow toasting a shared relation after initdb (because
                                399                 :      * there's no way to mark it toasted in other databases' pg_class).
                                400                 :      */
 1482 peter                     401           45430 :     if (rel->rd_rel->relisshared && !IsBootstrapProcessingMode())
                                402             909 :         return false;
                                403                 : 
                                404                 :     /*
                                405                 :      * Ignore attempts to create toast tables on catalog tables after initdb.
                                406                 :      * Which catalogs get toast tables is explicitly chosen in catalog/pg_*.h.
                                407                 :      * (We could get here via some ALTER TABLE command if the catalog doesn't
                                408                 :      * have a toast table.)
                                409                 :      */
                                410           44521 :     if (IsCatalogRelation(rel) && !IsBootstrapProcessingMode())
                                411           13938 :         return false;
                                412                 : 
                                413                 :     /* Otherwise, let the AM decide. */
 1419 rhaas                     414           30583 :     return table_relation_needs_toast_table(rel);
                                415                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a