LCOV - code coverage report
Current view: top level - src/backend/access/rmgrdesc - heapdesc.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 192 245 78.4 %
Date: 2024-04-25 19:11:21 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * heapdesc.c
       4             :  *    rmgr descriptor routines for access/heap/heapam.c
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/access/rmgrdesc/heapdesc.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/heapam_xlog.h"
      18             : #include "access/rmgrdesc_utils.h"
      19             : 
      20             : /*
      21             :  * NOTE: "keyname" argument cannot have trailing spaces or punctuation
      22             :  * characters
      23             :  */
      24             : static void
      25         100 : infobits_desc(StringInfo buf, uint8 infobits, const char *keyname)
      26             : {
      27         100 :     appendStringInfo(buf, "%s: [", keyname);
      28             : 
      29             :     Assert(buf->data[buf->len - 1] != ' ');
      30             : 
      31         100 :     if (infobits & XLHL_XMAX_IS_MULTI)
      32           0 :         appendStringInfoString(buf, "IS_MULTI, ");
      33         100 :     if (infobits & XLHL_XMAX_LOCK_ONLY)
      34          14 :         appendStringInfoString(buf, "LOCK_ONLY, ");
      35         100 :     if (infobits & XLHL_XMAX_EXCL_LOCK)
      36          14 :         appendStringInfoString(buf, "EXCL_LOCK, ");
      37         100 :     if (infobits & XLHL_XMAX_KEYSHR_LOCK)
      38           0 :         appendStringInfoString(buf, "KEYSHR_LOCK, ");
      39         100 :     if (infobits & XLHL_KEYS_UPDATED)
      40          40 :         appendStringInfoString(buf, "KEYS_UPDATED, ");
      41             : 
      42         100 :     if (buf->data[buf->len - 1] == ' ')
      43             :     {
      44             :         /* Truncate-away final unneeded ", "  */
      45             :         Assert(buf->data[buf->len - 2] == ',');
      46          54 :         buf->len -= 2;
      47          54 :         buf->data[buf->len] = '\0';
      48             :     }
      49             : 
      50         100 :     appendStringInfoChar(buf, ']');
      51         100 : }
      52             : 
      53             : static void
      54           0 : truncate_flags_desc(StringInfo buf, uint8 flags)
      55             : {
      56           0 :     appendStringInfoString(buf, "flags: [");
      57             : 
      58           0 :     if (flags & XLH_TRUNCATE_CASCADE)
      59           0 :         appendStringInfoString(buf, "CASCADE, ");
      60           0 :     if (flags & XLH_TRUNCATE_RESTART_SEQS)
      61           0 :         appendStringInfoString(buf, "RESTART_SEQS, ");
      62             : 
      63           0 :     if (buf->data[buf->len - 1] == ' ')
      64             :     {
      65             :         /* Truncate-away final unneeded ", "  */
      66             :         Assert(buf->data[buf->len - 2] == ',');
      67           0 :         buf->len -= 2;
      68           0 :         buf->data[buf->len] = '\0';
      69             :     }
      70             : 
      71           0 :     appendStringInfoChar(buf, ']');
      72           0 : }
      73             : 
      74             : static void
      75          16 : plan_elem_desc(StringInfo buf, void *plan, void *data)
      76             : {
      77          16 :     xlhp_freeze_plan *new_plan = (xlhp_freeze_plan *) plan;
      78          16 :     OffsetNumber **offsets = data;
      79             : 
      80          16 :     appendStringInfo(buf, "{ xmax: %u, infomask: %u, infomask2: %u, ntuples: %u",
      81             :                      new_plan->xmax,
      82          16 :                      new_plan->t_infomask, new_plan->t_infomask2,
      83          16 :                      new_plan->ntuples);
      84             : 
      85          16 :     appendStringInfoString(buf, ", offsets:");
      86          16 :     array_desc(buf, *offsets, sizeof(OffsetNumber), new_plan->ntuples,
      87             :                &offset_elem_desc, NULL);
      88             : 
      89          16 :     *offsets += new_plan->ntuples;
      90             : 
      91          16 :     appendStringInfoString(buf, " }");
      92          16 : }
      93             : 
      94             : 
      95             : /*
      96             :  * Given a MAXALIGNed buffer returned by XLogRecGetBlockData() and pointed to
      97             :  * by cursor and any xl_heap_prune flags, deserialize the arrays of
      98             :  * OffsetNumbers contained in an XLOG_HEAP2_PRUNE_* record.
      99             :  *
     100             :  * This is in heapdesc.c so it can be shared between heap2_redo and heap2_desc
     101             :  * code, the latter of which is used in frontend (pg_waldump) code.
     102             :  */
     103             : void
     104       15054 : heap_xlog_deserialize_prune_and_freeze(char *cursor, uint8 flags,
     105             :                                        int *nplans, xlhp_freeze_plan **plans,
     106             :                                        OffsetNumber **frz_offsets,
     107             :                                        int *nredirected, OffsetNumber **redirected,
     108             :                                        int *ndead, OffsetNumber **nowdead,
     109             :                                        int *nunused, OffsetNumber **nowunused)
     110             : {
     111       15054 :     if (flags & XLHP_HAS_FREEZE_PLANS)
     112             :     {
     113          98 :         xlhp_freeze_plans *freeze_plans = (xlhp_freeze_plans *) cursor;
     114             : 
     115          98 :         *nplans = freeze_plans->nplans;
     116             :         Assert(*nplans > 0);
     117          98 :         *plans = freeze_plans->plans;
     118             : 
     119          98 :         cursor += offsetof(xlhp_freeze_plans, plans);
     120          98 :         cursor += sizeof(xlhp_freeze_plan) * *nplans;
     121             :     }
     122             :     else
     123             :     {
     124       14956 :         *nplans = 0;
     125       14956 :         *plans = NULL;
     126             :     }
     127             : 
     128       15054 :     if (flags & XLHP_HAS_REDIRECTIONS)
     129             :     {
     130        3634 :         xlhp_prune_items *subrecord = (xlhp_prune_items *) cursor;
     131             : 
     132        3634 :         *nredirected = subrecord->ntargets;
     133             :         Assert(*nredirected > 0);
     134        3634 :         *redirected = &subrecord->data[0];
     135             : 
     136        3634 :         cursor += offsetof(xlhp_prune_items, data);
     137        3634 :         cursor += sizeof(OffsetNumber[2]) * *nredirected;
     138             :     }
     139             :     else
     140             :     {
     141       11420 :         *nredirected = 0;
     142       11420 :         *redirected = NULL;
     143             :     }
     144             : 
     145       15054 :     if (flags & XLHP_HAS_DEAD_ITEMS)
     146             :     {
     147       10172 :         xlhp_prune_items *subrecord = (xlhp_prune_items *) cursor;
     148             : 
     149       10172 :         *ndead = subrecord->ntargets;
     150             :         Assert(*ndead > 0);
     151       10172 :         *nowdead = subrecord->data;
     152             : 
     153       10172 :         cursor += offsetof(xlhp_prune_items, data);
     154       10172 :         cursor += sizeof(OffsetNumber) * *ndead;
     155             :     }
     156             :     else
     157             :     {
     158        4882 :         *ndead = 0;
     159        4882 :         *nowdead = NULL;
     160             :     }
     161             : 
     162       15054 :     if (flags & XLHP_HAS_NOW_UNUSED_ITEMS)
     163             :     {
     164        6170 :         xlhp_prune_items *subrecord = (xlhp_prune_items *) cursor;
     165             : 
     166        6170 :         *nunused = subrecord->ntargets;
     167             :         Assert(*nunused > 0);
     168        6170 :         *nowunused = subrecord->data;
     169             : 
     170        6170 :         cursor += offsetof(xlhp_prune_items, data);
     171        6170 :         cursor += sizeof(OffsetNumber) * *nunused;
     172             :     }
     173             :     else
     174             :     {
     175        8884 :         *nunused = 0;
     176        8884 :         *nowunused = NULL;
     177             :     }
     178             : 
     179       15054 :     *frz_offsets = (OffsetNumber *) cursor;
     180       15054 : }
     181             : 
     182             : void
     183       61902 : heap_desc(StringInfo buf, XLogReaderState *record)
     184             : {
     185       61902 :     char       *rec = XLogRecGetData(record);
     186       61902 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
     187             : 
     188       61902 :     info &= XLOG_HEAP_OPMASK;
     189       61902 :     if (info == XLOG_HEAP_INSERT)
     190             :     {
     191       61798 :         xl_heap_insert *xlrec = (xl_heap_insert *) rec;
     192             : 
     193       61798 :         appendStringInfo(buf, "off: %u, flags: 0x%02X",
     194       61798 :                          xlrec->offnum,
     195       61798 :                          xlrec->flags);
     196             :     }
     197         104 :     else if (info == XLOG_HEAP_DELETE)
     198             :     {
     199          40 :         xl_heap_delete *xlrec = (xl_heap_delete *) rec;
     200             : 
     201          40 :         appendStringInfo(buf, "xmax: %u, off: %u, ",
     202          40 :                          xlrec->xmax, xlrec->offnum);
     203          40 :         infobits_desc(buf, xlrec->infobits_set, "infobits");
     204          40 :         appendStringInfo(buf, ", flags: 0x%02X", xlrec->flags);
     205             :     }
     206          64 :     else if (info == XLOG_HEAP_UPDATE)
     207             :     {
     208          16 :         xl_heap_update *xlrec = (xl_heap_update *) rec;
     209             : 
     210          16 :         appendStringInfo(buf, "old_xmax: %u, old_off: %u, ",
     211          16 :                          xlrec->old_xmax, xlrec->old_offnum);
     212          16 :         infobits_desc(buf, xlrec->old_infobits_set, "old_infobits");
     213          16 :         appendStringInfo(buf, ", flags: 0x%02X, new_xmax: %u, new_off: %u",
     214          16 :                          xlrec->flags, xlrec->new_xmax, xlrec->new_offnum);
     215             :     }
     216          48 :     else if (info == XLOG_HEAP_HOT_UPDATE)
     217             :     {
     218          30 :         xl_heap_update *xlrec = (xl_heap_update *) rec;
     219             : 
     220          30 :         appendStringInfo(buf, "old_xmax: %u, old_off: %u, ",
     221          30 :                          xlrec->old_xmax, xlrec->old_offnum);
     222          30 :         infobits_desc(buf, xlrec->old_infobits_set, "old_infobits");
     223          30 :         appendStringInfo(buf, ", flags: 0x%02X, new_xmax: %u, new_off: %u",
     224          30 :                          xlrec->flags, xlrec->new_xmax, xlrec->new_offnum);
     225             :     }
     226          18 :     else if (info == XLOG_HEAP_TRUNCATE)
     227             :     {
     228           0 :         xl_heap_truncate *xlrec = (xl_heap_truncate *) rec;
     229             : 
     230           0 :         truncate_flags_desc(buf, xlrec->flags);
     231           0 :         appendStringInfo(buf, ", nrelids: %u", xlrec->nrelids);
     232           0 :         appendStringInfoString(buf, ", relids:");
     233           0 :         array_desc(buf, xlrec->relids, sizeof(Oid), xlrec->nrelids,
     234             :                    &oid_elem_desc, NULL);
     235             :     }
     236          18 :     else if (info == XLOG_HEAP_CONFIRM)
     237             :     {
     238           0 :         xl_heap_confirm *xlrec = (xl_heap_confirm *) rec;
     239             : 
     240           0 :         appendStringInfo(buf, "off: %u", xlrec->offnum);
     241             :     }
     242          18 :     else if (info == XLOG_HEAP_LOCK)
     243             :     {
     244          14 :         xl_heap_lock *xlrec = (xl_heap_lock *) rec;
     245             : 
     246          14 :         appendStringInfo(buf, "xmax: %u, off: %u, ",
     247          14 :                          xlrec->xmax, xlrec->offnum);
     248          14 :         infobits_desc(buf, xlrec->infobits_set, "infobits");
     249          14 :         appendStringInfo(buf, ", flags: 0x%02X", xlrec->flags);
     250             :     }
     251           4 :     else if (info == XLOG_HEAP_INPLACE)
     252             :     {
     253           4 :         xl_heap_inplace *xlrec = (xl_heap_inplace *) rec;
     254             : 
     255           4 :         appendStringInfo(buf, "off: %u", xlrec->offnum);
     256             :     }
     257       61902 : }
     258             : 
     259             : void
     260         134 : heap2_desc(StringInfo buf, XLogReaderState *record)
     261             : {
     262         134 :     char       *rec = XLogRecGetData(record);
     263         134 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
     264             : 
     265         134 :     info &= XLOG_HEAP_OPMASK;
     266         134 :     if (info == XLOG_HEAP2_PRUNE_ON_ACCESS ||
     267          48 :         info == XLOG_HEAP2_PRUNE_VACUUM_SCAN ||
     268             :         info == XLOG_HEAP2_PRUNE_VACUUM_CLEANUP)
     269          90 :     {
     270          90 :         xl_heap_prune *xlrec = (xl_heap_prune *) rec;
     271             : 
     272          90 :         if (xlrec->flags & XLHP_HAS_CONFLICT_HORIZON)
     273             :         {
     274             :             TransactionId conflict_xid;
     275             : 
     276          36 :             memcpy(&conflict_xid, rec + SizeOfHeapPrune, sizeof(TransactionId));
     277             : 
     278          36 :             appendStringInfo(buf, "snapshotConflictHorizon: %u",
     279             :                              conflict_xid);
     280             :         }
     281             : 
     282          90 :         appendStringInfo(buf, ", isCatalogRel: %c",
     283          90 :                          xlrec->flags & XLHP_IS_CATALOG_REL ? 'T' : 'F');
     284             : 
     285          90 :         if (XLogRecHasBlockData(record, 0))
     286             :         {
     287             :             Size        datalen;
     288             :             OffsetNumber *redirected;
     289             :             OffsetNumber *nowdead;
     290             :             OffsetNumber *nowunused;
     291             :             int         nredirected;
     292             :             int         nunused;
     293             :             int         ndead;
     294             :             int         nplans;
     295             :             xlhp_freeze_plan *plans;
     296             :             OffsetNumber *frz_offsets;
     297             : 
     298          90 :             char       *cursor = XLogRecGetBlockData(record, 0, &datalen);
     299             : 
     300          90 :             heap_xlog_deserialize_prune_and_freeze(cursor, xlrec->flags,
     301             :                                                    &nplans, &plans, &frz_offsets,
     302             :                                                    &nredirected, &redirected,
     303             :                                                    &ndead, &nowdead,
     304             :                                                    &nunused, &nowunused);
     305             : 
     306          90 :             appendStringInfo(buf, ", nplans: %u, nredirected: %u, ndead: %u, nunused: %u",
     307             :                              nplans, nredirected, ndead, nunused);
     308             : 
     309          90 :             if (nplans > 0)
     310             :             {
     311          12 :                 appendStringInfoString(buf, ", plans:");
     312          12 :                 array_desc(buf, plans, sizeof(xlhp_freeze_plan), nplans,
     313             :                            &plan_elem_desc, &frz_offsets);
     314             :             }
     315             : 
     316          90 :             if (nredirected > 0)
     317             :             {
     318          14 :                 appendStringInfoString(buf, ", redirected:");
     319          14 :                 array_desc(buf, redirected, sizeof(OffsetNumber) * 2,
     320             :                            nredirected, &redirect_elem_desc, NULL);
     321             :             }
     322             : 
     323          90 :             if (ndead > 0)
     324             :             {
     325          66 :                 appendStringInfoString(buf, ", dead:");
     326          66 :                 array_desc(buf, nowdead, sizeof(OffsetNumber), ndead,
     327             :                            &offset_elem_desc, NULL);
     328             :             }
     329             : 
     330          90 :             if (nunused > 0)
     331             :             {
     332          18 :                 appendStringInfoString(buf, ", unused:");
     333          18 :                 array_desc(buf, nowunused, sizeof(OffsetNumber), nunused,
     334             :                            &offset_elem_desc, NULL);
     335             :             }
     336             :         }
     337             :     }
     338          44 :     else if (info == XLOG_HEAP2_VISIBLE)
     339             :     {
     340           4 :         xl_heap_visible *xlrec = (xl_heap_visible *) rec;
     341             : 
     342           4 :         appendStringInfo(buf, "snapshotConflictHorizon: %u, flags: 0x%02X",
     343           4 :                          xlrec->snapshotConflictHorizon, xlrec->flags);
     344             :     }
     345          40 :     else if (info == XLOG_HEAP2_MULTI_INSERT)
     346             :     {
     347          40 :         xl_heap_multi_insert *xlrec = (xl_heap_multi_insert *) rec;
     348          40 :         bool        isinit = (XLogRecGetInfo(record) & XLOG_HEAP_INIT_PAGE) != 0;
     349             : 
     350          40 :         appendStringInfo(buf, "ntuples: %d, flags: 0x%02X", xlrec->ntuples,
     351          40 :                          xlrec->flags);
     352             : 
     353          40 :         if (XLogRecHasBlockData(record, 0) && !isinit)
     354             :         {
     355          34 :             appendStringInfoString(buf, ", offsets:");
     356          34 :             array_desc(buf, xlrec->offsets, sizeof(OffsetNumber),
     357          34 :                        xlrec->ntuples, &offset_elem_desc, NULL);
     358             :         }
     359             :     }
     360           0 :     else if (info == XLOG_HEAP2_LOCK_UPDATED)
     361             :     {
     362           0 :         xl_heap_lock_updated *xlrec = (xl_heap_lock_updated *) rec;
     363             : 
     364           0 :         appendStringInfo(buf, "xmax: %u, off: %u, ",
     365           0 :                          xlrec->xmax, xlrec->offnum);
     366           0 :         infobits_desc(buf, xlrec->infobits_set, "infobits");
     367           0 :         appendStringInfo(buf, ", flags: 0x%02X", xlrec->flags);
     368             :     }
     369           0 :     else if (info == XLOG_HEAP2_NEW_CID)
     370             :     {
     371           0 :         xl_heap_new_cid *xlrec = (xl_heap_new_cid *) rec;
     372             : 
     373           0 :         appendStringInfo(buf, "rel: %u/%u/%u, tid: %u/%u",
     374             :                          xlrec->target_locator.spcOid,
     375             :                          xlrec->target_locator.dbOid,
     376             :                          xlrec->target_locator.relNumber,
     377           0 :                          ItemPointerGetBlockNumber(&(xlrec->target_tid)),
     378           0 :                          ItemPointerGetOffsetNumber(&(xlrec->target_tid)));
     379           0 :         appendStringInfo(buf, ", cmin: %u, cmax: %u, combo: %u",
     380             :                          xlrec->cmin, xlrec->cmax, xlrec->combocid);
     381             :     }
     382         134 : }
     383             : 
     384             : const char *
     385       61902 : heap_identify(uint8 info)
     386             : {
     387       61902 :     const char *id = NULL;
     388             : 
     389       61902 :     switch (info & ~XLR_INFO_MASK)
     390             :     {
     391       58976 :         case XLOG_HEAP_INSERT:
     392       58976 :             id = "INSERT";
     393       58976 :             break;
     394        2822 :         case XLOG_HEAP_INSERT | XLOG_HEAP_INIT_PAGE:
     395        2822 :             id = "INSERT+INIT";
     396        2822 :             break;
     397          40 :         case XLOG_HEAP_DELETE:
     398          40 :             id = "DELETE";
     399          40 :             break;
     400          14 :         case XLOG_HEAP_UPDATE:
     401          14 :             id = "UPDATE";
     402          14 :             break;
     403           2 :         case XLOG_HEAP_UPDATE | XLOG_HEAP_INIT_PAGE:
     404           2 :             id = "UPDATE+INIT";
     405           2 :             break;
     406          30 :         case XLOG_HEAP_HOT_UPDATE:
     407          30 :             id = "HOT_UPDATE";
     408          30 :             break;
     409           0 :         case XLOG_HEAP_HOT_UPDATE | XLOG_HEAP_INIT_PAGE:
     410           0 :             id = "HOT_UPDATE+INIT";
     411           0 :             break;
     412           0 :         case XLOG_HEAP_TRUNCATE:
     413           0 :             id = "TRUNCATE";
     414           0 :             break;
     415           0 :         case XLOG_HEAP_CONFIRM:
     416           0 :             id = "HEAP_CONFIRM";
     417           0 :             break;
     418          14 :         case XLOG_HEAP_LOCK:
     419          14 :             id = "LOCK";
     420          14 :             break;
     421           4 :         case XLOG_HEAP_INPLACE:
     422           4 :             id = "INPLACE";
     423           4 :             break;
     424             :     }
     425             : 
     426       61902 :     return id;
     427             : }
     428             : 
     429             : const char *
     430         134 : heap2_identify(uint8 info)
     431             : {
     432         134 :     const char *id = NULL;
     433             : 
     434         134 :     switch (info & ~XLR_INFO_MASK)
     435             :     {
     436          64 :         case XLOG_HEAP2_PRUNE_ON_ACCESS:
     437          64 :             id = "PRUNE_ON_ACCESS";
     438          64 :             break;
     439          22 :         case XLOG_HEAP2_PRUNE_VACUUM_SCAN:
     440          22 :             id = "PRUNE_VACUUM_SCAN";
     441          22 :             break;
     442           4 :         case XLOG_HEAP2_PRUNE_VACUUM_CLEANUP:
     443           4 :             id = "PRUNE_VACUUM_CLEANUP";
     444           4 :             break;
     445           4 :         case XLOG_HEAP2_VISIBLE:
     446           4 :             id = "VISIBLE";
     447           4 :             break;
     448          40 :         case XLOG_HEAP2_MULTI_INSERT:
     449          40 :             id = "MULTI_INSERT";
     450          40 :             break;
     451           0 :         case XLOG_HEAP2_MULTI_INSERT | XLOG_HEAP_INIT_PAGE:
     452           0 :             id = "MULTI_INSERT+INIT";
     453           0 :             break;
     454           0 :         case XLOG_HEAP2_LOCK_UPDATED:
     455           0 :             id = "LOCK_UPDATED";
     456           0 :             break;
     457           0 :         case XLOG_HEAP2_NEW_CID:
     458           0 :             id = "NEW_CID";
     459           0 :             break;
     460           0 :         case XLOG_HEAP2_REWRITE:
     461           0 :             id = "REWRITE";
     462           0 :             break;
     463             :     }
     464             : 
     465         134 :     return id;
     466             : }

Generated by: LCOV version 1.14