LCOV - differential code coverage report
Current view: top level - contrib/pg_visibility - pg_visibility.c (source / functions) Coverage Total Hit UNC LBC UBC GNC CBC DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 92.9 % 326 303 3 20 10 293 2 8
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 25 25 8 17 1
Baseline: lcov-20261128-baseline Branches: 72.5 % 178 129 6 1 42 6 123
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 81.8 % 11 9 2 9
(30,360] days: 81.8 % 11 9 1 1 1 8
(360..) days: 93.8 % 304 285 19 285
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(360..) days: 100.0 % 24 24 7 17
Branch coverage date bins:
(7,30] days: 50.0 % 12 6 6 6
(360..) days: 74.1 % 166 123 1 42 123

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_visibility.c
                                  4                 :                :  *    display visibility map information and page-level visibility bits
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2016-2026, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  *    contrib/pg_visibility/pg_visibility.c
                                  9                 :                :  *-------------------------------------------------------------------------
                                 10                 :                :  */
                                 11                 :                : #include "postgres.h"
                                 12                 :                : 
                                 13                 :                : #include "access/heapam.h"
                                 14                 :                : #include "access/htup_details.h"
                                 15                 :                : #include "access/visibilitymap.h"
                                 16                 :                : #include "access/xlog.h"
                                 17                 :                : #include "access/xloginsert.h"
                                 18                 :                : #include "catalog/pg_type.h"
                                 19                 :                : #include "catalog/storage_xlog.h"
                                 20                 :                : #include "funcapi.h"
                                 21                 :                : #include "miscadmin.h"
                                 22                 :                : #include "storage/bufmgr.h"
                                 23                 :                : #include "storage/proc.h"
                                 24                 :                : #include "storage/procarray.h"
                                 25                 :                : #include "storage/read_stream.h"
                                 26                 :                : #include "storage/smgr.h"
                                 27                 :                : #include "utils/rel.h"
                                 28                 :                : 
  612 tgl@sss.pgh.pa.us          29                 :CBC          43 : PG_MODULE_MAGIC_EXT(
                                 30                 :                :                     .name = "pg_visibility",
                                 31                 :                :                     .version = PG_VERSION
                                 32                 :                : );
                                 33                 :                : 
                                 34                 :                : typedef struct vbits
                                 35                 :                : {
                                 36                 :                :     BlockNumber next;
                                 37                 :                :     BlockNumber count;
                                 38                 :                :     pg_attribute_counted_by(count) uint8 bits[FLEXIBLE_ARRAY_MEMBER];
                                 39                 :                : } vbits;
                                 40                 :                : 
                                 41                 :                : typedef struct corrupt_items
                                 42                 :                : {
                                 43                 :                :     BlockNumber next;
                                 44                 :                :     BlockNumber count;
                                 45                 :                :     ItemPointer tids;
                                 46                 :                : } corrupt_items;
                                 47                 :                : 
                                 48                 :                : /* for collect_corrupt_items_read_stream_next_block */
                                 49                 :                : struct collect_corrupt_items_read_stream_private
                                 50                 :                : {
                                 51                 :                :     bool        all_frozen;
                                 52                 :                :     bool        all_visible;
                                 53                 :                :     BlockNumber current_blocknum;
                                 54                 :                :     BlockNumber last_exclusive;
                                 55                 :                :     Relation    rel;
                                 56                 :                :     Buffer      vmbuffer;
                                 57                 :                : };
                                 58                 :                : 
 3917 rhaas@postgresql.org       59                 :             11 : PG_FUNCTION_INFO_V1(pg_visibility_map);
                                 60                 :              9 : PG_FUNCTION_INFO_V1(pg_visibility_map_rel);
                                 61                 :              7 : PG_FUNCTION_INFO_V1(pg_visibility);
                                 62                 :              7 : PG_FUNCTION_INFO_V1(pg_visibility_rel);
                                 63                 :             27 : PG_FUNCTION_INFO_V1(pg_visibility_map_summary);
 3818                            64                 :              8 : PG_FUNCTION_INFO_V1(pg_check_frozen);
                                 65                 :             15 : PG_FUNCTION_INFO_V1(pg_check_visible);
 3816                            66                 :              8 : PG_FUNCTION_INFO_V1(pg_truncate_visibility_map);
                                 67                 :                : 
                                 68                 :                : static TupleDesc pg_visibility_tupdesc(bool include_blkno, bool include_pd);
                                 69                 :                : static vbits *collect_visibility_data(Oid relid, bool include_pd);
                                 70                 :                : static corrupt_items *collect_corrupt_items(Oid relid, bool all_visible,
                                 71                 :                :                                             bool all_frozen);
                                 72                 :                : static void record_corrupt_item(corrupt_items *items, ItemPointer tid);
                                 73                 :                : static bool tuple_all_visible(HeapTuple tup, TransactionId OldestXmin,
                                 74                 :                :                               Buffer buffer);
                                 75                 :                : static void check_relation_supported(Relation rel);
                                 76                 :                : 
                                 77                 :                : /*
                                 78                 :                :  * Visibility map information for a single block of a relation.
                                 79                 :                :  *
                                 80                 :                :  * Note: the VM code will silently return zeroes for pages past the end
                                 81                 :                :  * of the map, so we allow probes up to MaxBlockNumber regardless of the
                                 82                 :                :  * actual relation size.
                                 83                 :                :  */
                                 84                 :                : Datum
 3917                            85                 :             26 : pg_visibility_map(PG_FUNCTION_ARGS)
                                 86                 :                : {
                                 87                 :             26 :     Oid         relid = PG_GETARG_OID(0);
                                 88                 :             26 :     int64       blkno = PG_GETARG_INT64(1);
                                 89                 :                :     int32       mapbits;
                                 90                 :                :     Relation    rel;
                                 91                 :             26 :     Buffer      vmbuffer = InvalidBuffer;
                                 92                 :                :     TupleDesc   tupdesc;
                                 93                 :                :     Datum       values[2];
 1596 peter@eisentraut.org       94                 :             26 :     bool        nulls[2] = {0};
                                 95                 :                : 
 3917 rhaas@postgresql.org       96                 :             26 :     rel = relation_open(relid, AccessShareLock);
                                 97                 :                : 
                                 98                 :                :     /* Only some relkinds have a visibility map */
   22 michael@paquier.xyz        99                 :GNC          26 :     check_relation_supported(rel);
                                100                 :                : 
 3917 rhaas@postgresql.org      101   [ +  -  -  + ]:CBC          26 :     if (blkno < 0 || blkno > MaxBlockNumber)
 3917 rhaas@postgresql.org      102         [ #  # ]:UBC           0 :         ereport(ERROR,
                                103                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                104                 :                :                  errmsg("invalid block number")));
                                105                 :                : 
 3917 rhaas@postgresql.org      106                 :CBC          26 :     tupdesc = pg_visibility_tupdesc(false, false);
                                107                 :                : 
                                108                 :             26 :     mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
                                109         [ +  - ]:             26 :     if (vmbuffer != InvalidBuffer)
                                110                 :             26 :         ReleaseBuffer(vmbuffer);
                                111                 :             26 :     values[0] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0);
                                112                 :             26 :     values[1] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0);
                                113                 :                : 
                                114                 :             26 :     relation_close(rel, AccessShareLock);
                                115                 :                : 
                                116                 :             26 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
                                117                 :                : }
                                118                 :                : 
                                119                 :                : /*
                                120                 :                :  * Visibility map information for a single block of a relation, plus the
                                121                 :                :  * page-level information for the same block.
                                122                 :                :  */
                                123                 :                : Datum
                                124                 :              6 : pg_visibility(PG_FUNCTION_ARGS)
                                125                 :                : {
                                126                 :              6 :     Oid         relid = PG_GETARG_OID(0);
                                127                 :              6 :     int64       blkno = PG_GETARG_INT64(1);
                                128                 :                :     int32       mapbits;
                                129                 :                :     Relation    rel;
                                130                 :              6 :     Buffer      vmbuffer = InvalidBuffer;
                                131                 :                :     Buffer      buffer;
                                132                 :                :     Page        page;
                                133                 :                :     TupleDesc   tupdesc;
                                134                 :                :     Datum       values[3];
 1596 peter@eisentraut.org      135                 :              6 :     bool        nulls[3] = {0};
                                136                 :                : 
 3917 rhaas@postgresql.org      137                 :              6 :     rel = relation_open(relid, AccessShareLock);
                                138                 :                : 
                                139                 :                :     /* Only some relkinds have a visibility map */
   22 michael@paquier.xyz       140                 :GNC           6 :     check_relation_supported(rel);
                                141                 :                : 
 3917 rhaas@postgresql.org      142   [ +  -  -  + ]:CBC           1 :     if (blkno < 0 || blkno > MaxBlockNumber)
 3917 rhaas@postgresql.org      143         [ #  # ]:UBC           0 :         ereport(ERROR,
                                144                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                145                 :                :                  errmsg("invalid block number")));
                                146                 :                : 
 3917 rhaas@postgresql.org      147                 :CBC           1 :     tupdesc = pg_visibility_tupdesc(false, true);
                                148                 :                : 
                                149                 :              1 :     mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
                                150         [ +  - ]:              1 :     if (vmbuffer != InvalidBuffer)
                                151                 :              1 :         ReleaseBuffer(vmbuffer);
                                152                 :              1 :     values[0] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0);
                                153                 :              1 :     values[1] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0);
                                154                 :                : 
                                155                 :                :     /* Here we have to explicitly check rel size ... */
 3710 tgl@sss.pgh.pa.us         156         [ +  - ]:              1 :     if (blkno < RelationGetNumberOfBlocks(rel))
                                157                 :                :     {
                                158                 :              1 :         buffer = ReadBuffer(rel, blkno);
                                159                 :              1 :         LockBuffer(buffer, BUFFER_LOCK_SHARE);
                                160                 :                : 
                                161                 :              1 :         page = BufferGetPage(buffer);
                                162                 :              1 :         values[2] = BoolGetDatum(PageIsAllVisible(page));
                                163                 :                : 
                                164                 :              1 :         UnlockReleaseBuffer(buffer);
                                165                 :                :     }
                                166                 :                :     else
                                167                 :                :     {
                                168                 :                :         /* As with the vismap, silently return 0 for pages past EOF */
 3710 tgl@sss.pgh.pa.us         169                 :UBC           0 :         values[2] = BoolGetDatum(false);
                                170                 :                :     }
                                171                 :                : 
 3917 rhaas@postgresql.org      172                 :CBC           1 :     relation_close(rel, AccessShareLock);
                                173                 :                : 
                                174                 :              1 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
                                175                 :                : }
                                176                 :                : 
                                177                 :                : /*
                                178                 :                :  * Visibility map information for every block in a relation.
                                179                 :                :  */
                                180                 :                : Datum
                                181                 :            112 : pg_visibility_map_rel(PG_FUNCTION_ARGS)
                                182                 :                : {
                                183                 :                :     FuncCallContext *funcctx;
                                184                 :                :     vbits      *info;
                                185                 :                : 
                                186         [ +  + ]:            112 :     if (SRF_IS_FIRSTCALL())
                                187                 :                :     {
                                188                 :             13 :         Oid         relid = PG_GETARG_OID(0);
                                189                 :                :         MemoryContext oldcontext;
                                190                 :                : 
                                191                 :             13 :         funcctx = SRF_FIRSTCALL_INIT();
                                192                 :             13 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                193                 :             13 :         funcctx->tuple_desc = pg_visibility_tupdesc(true, false);
                                194                 :                :         /* collect_visibility_data will verify the relkind */
                                195                 :             13 :         funcctx->user_fctx = collect_visibility_data(relid, false);
                                196                 :              6 :         MemoryContextSwitchTo(oldcontext);
                                197                 :                :     }
                                198                 :                : 
                                199                 :            105 :     funcctx = SRF_PERCALL_SETUP();
                                200                 :            105 :     info = (vbits *) funcctx->user_fctx;
                                201                 :                : 
                                202         [ +  + ]:            105 :     if (info->next < info->count)
                                203                 :                :     {
                                204                 :                :         Datum       values[3];
 1596 peter@eisentraut.org      205                 :             99 :         bool        nulls[3] = {0};
                                206                 :                :         HeapTuple   tuple;
                                207                 :                : 
 3917 rhaas@postgresql.org      208                 :             99 :         values[0] = Int64GetDatum(info->next);
                                209                 :             99 :         values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
                                210                 :             99 :         values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
                                211                 :             99 :         info->next++;
                                212                 :                : 
                                213                 :             99 :         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
                                214                 :             99 :         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
                                215                 :                :     }
                                216                 :                : 
                                217                 :              6 :     SRF_RETURN_DONE(funcctx);
                                218                 :                : }
                                219                 :                : 
                                220                 :                : /*
                                221                 :                :  * Visibility map information for every block in a relation, plus the page
                                222                 :                :  * level information for each block.
                                223                 :                :  */
                                224                 :                : Datum
                                225                 :              9 : pg_visibility_rel(PG_FUNCTION_ARGS)
                                226                 :                : {
                                227                 :                :     FuncCallContext *funcctx;
                                228                 :                :     vbits      *info;
                                229                 :                : 
                                230         [ +  + ]:              9 :     if (SRF_IS_FIRSTCALL())
                                231                 :                :     {
                                232                 :              6 :         Oid         relid = PG_GETARG_OID(0);
                                233                 :                :         MemoryContext oldcontext;
                                234                 :                : 
                                235                 :              6 :         funcctx = SRF_FIRSTCALL_INIT();
                                236                 :              6 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                237                 :              6 :         funcctx->tuple_desc = pg_visibility_tupdesc(true, true);
                                238                 :                :         /* collect_visibility_data will verify the relkind */
                                239                 :              6 :         funcctx->user_fctx = collect_visibility_data(relid, true);
                                240                 :              6 :         MemoryContextSwitchTo(oldcontext);
                                241                 :                :     }
                                242                 :                : 
                                243                 :              9 :     funcctx = SRF_PERCALL_SETUP();
                                244                 :              9 :     info = (vbits *) funcctx->user_fctx;
                                245                 :                : 
                                246         [ +  + ]:              9 :     if (info->next < info->count)
                                247                 :                :     {
                                248                 :                :         Datum       values[4];
 1596 peter@eisentraut.org      249                 :              3 :         bool        nulls[4] = {0};
                                250                 :                :         HeapTuple   tuple;
                                251                 :                : 
 3917 rhaas@postgresql.org      252                 :              3 :         values[0] = Int64GetDatum(info->next);
                                253                 :              3 :         values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
                                254                 :              3 :         values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
                                255                 :              3 :         values[3] = BoolGetDatum((info->bits[info->next] & (1 << 2)) != 0);
                                256                 :              3 :         info->next++;
                                257                 :                : 
                                258                 :              3 :         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
                                259                 :              3 :         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
                                260                 :                :     }
                                261                 :                : 
                                262                 :              6 :     SRF_RETURN_DONE(funcctx);
                                263                 :                : }
                                264                 :                : 
                                265                 :                : /*
                                266                 :                :  * Count the number of all-visible and all-frozen pages in the visibility
                                267                 :                :  * map for a particular relation.
                                268                 :                :  */
                                269                 :                : Datum
                                270                 :             34 : pg_visibility_map_summary(PG_FUNCTION_ARGS)
                                271                 :                : {
                                272                 :             34 :     Oid         relid = PG_GETARG_OID(0);
                                273                 :                :     Relation    rel;
  340 msawada@postgresql.o      274                 :             34 :     BlockNumber all_visible = 0;
                                275                 :             34 :     BlockNumber all_frozen = 0;
                                276                 :                :     TupleDesc   tupdesc;
                                277                 :                :     Datum       values[2];
 1596 peter@eisentraut.org      278                 :             34 :     bool        nulls[2] = {0};
                                279                 :                : 
 3917 rhaas@postgresql.org      280                 :             34 :     rel = relation_open(relid, AccessShareLock);
                                281                 :                : 
                                282                 :                :     /* Only some relkinds have a visibility map */
   22 michael@paquier.xyz       283                 :GNC          34 :     check_relation_supported(rel);
                                284                 :                : 
  340 msawada@postgresql.o      285                 :CBC          29 :     visibilitymap_count(rel, &all_visible, &all_frozen);
                                286                 :                : 
 3917 rhaas@postgresql.org      287                 :             29 :     relation_close(rel, AccessShareLock);
                                288                 :                : 
 1438 michael@paquier.xyz       289         [ -  + ]:             29 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 1438 michael@paquier.xyz       290         [ #  # ]:UBC           0 :         elog(ERROR, "return type must be a row type");
                                291                 :                : 
  340 msawada@postgresql.o      292                 :CBC          29 :     values[0] = Int64GetDatum((int64) all_visible);
                                293                 :             29 :     values[1] = Int64GetDatum((int64) all_frozen);
                                294                 :                : 
 3917 rhaas@postgresql.org      295                 :             29 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
                                296                 :                : }
                                297                 :                : 
                                298                 :                : /*
                                299                 :                :  * Return the TIDs of non-frozen tuples present in pages marked all-frozen
                                300                 :                :  * in the visibility map.  We hope no one will ever find any, but there could
                                301                 :                :  * be bugs, database corruption, etc.
                                302                 :                :  */
                                303                 :                : Datum
 3818                           304                 :             15 : pg_check_frozen(PG_FUNCTION_ARGS)
                                305                 :                : {
                                306                 :                :     FuncCallContext *funcctx;
                                307                 :                :     corrupt_items *items;
                                308                 :                : 
                                309         [ +  + ]:             15 :     if (SRF_IS_FIRSTCALL())
                                310                 :                :     {
                                311                 :             10 :         Oid         relid = PG_GETARG_OID(0);
                                312                 :                :         MemoryContext oldcontext;
                                313                 :                : 
                                314                 :             10 :         funcctx = SRF_FIRSTCALL_INIT();
                                315                 :             10 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                316                 :                :         /* collect_corrupt_items will verify the relkind */
                                317                 :             10 :         funcctx->user_fctx = collect_corrupt_items(relid, false, true);
                                318                 :              5 :         MemoryContextSwitchTo(oldcontext);
                                319                 :                :     }
                                320                 :                : 
                                321                 :             10 :     funcctx = SRF_PERCALL_SETUP();
                                322                 :             10 :     items = (corrupt_items *) funcctx->user_fctx;
                                323                 :                : 
                                324         [ +  + ]:             10 :     if (items->next < items->count)
                                325                 :              5 :         SRF_RETURN_NEXT(funcctx, PointerGetDatum(&items->tids[items->next++]));
                                326                 :                : 
                                327                 :              5 :     SRF_RETURN_DONE(funcctx);
                                328                 :                : }
                                329                 :                : 
                                330                 :                : /*
                                331                 :                :  * Return the TIDs of not-all-visible tuples in pages marked all-visible
                                332                 :                :  * in the visibility map.  We hope no one will ever find any, but there could
                                333                 :                :  * be bugs, database corruption, etc.
                                334                 :                :  */
                                335                 :                : Datum
                                336                 :             15 : pg_check_visible(PG_FUNCTION_ARGS)
                                337                 :                : {
                                338                 :                :     FuncCallContext *funcctx;
                                339                 :                :     corrupt_items *items;
                                340                 :                : 
                                341         [ +  + ]:             15 :     if (SRF_IS_FIRSTCALL())
                                342                 :                :     {
                                343                 :             10 :         Oid         relid = PG_GETARG_OID(0);
                                344                 :                :         MemoryContext oldcontext;
                                345                 :                : 
                                346                 :             10 :         funcctx = SRF_FIRSTCALL_INIT();
                                347                 :             10 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                348                 :                :         /* collect_corrupt_items will verify the relkind */
                                349                 :             10 :         funcctx->user_fctx = collect_corrupt_items(relid, true, false);
                                350                 :             10 :         MemoryContextSwitchTo(oldcontext);
                                351                 :                :     }
                                352                 :                : 
                                353                 :             15 :     funcctx = SRF_PERCALL_SETUP();
                                354                 :             15 :     items = (corrupt_items *) funcctx->user_fctx;
                                355                 :                : 
                                356         [ +  + ]:             15 :     if (items->next < items->count)
                                357                 :              5 :         SRF_RETURN_NEXT(funcctx, PointerGetDatum(&items->tids[items->next++]));
                                358                 :                : 
                                359                 :             10 :     SRF_RETURN_DONE(funcctx);
                                360                 :                : }
                                361                 :                : 
                                362                 :                : /*
                                363                 :                :  * Remove the visibility map fork for a relation.  If there turn out to be
                                364                 :                :  * any bugs in the visibility map code that require rebuilding the VM, this
                                365                 :                :  * provides users with a way to do it that is cleaner than shutting down the
                                366                 :                :  * server and removing files by hand.
                                367                 :                :  *
                                368                 :                :  * This is a cut-down version of RelationTruncate.
                                369                 :                :  */
                                370                 :                : Datum
 3816                           371                 :              8 : pg_truncate_visibility_map(PG_FUNCTION_ARGS)
                                372                 :                : {
                                373                 :              8 :     Oid         relid = PG_GETARG_OID(0);
                                374                 :                :     Relation    rel;
                                375                 :                :     ForkNumber  fork;
                                376                 :                :     BlockNumber block;
                                377                 :                :     BlockNumber old_block;
                                378                 :                : 
                                379                 :              8 :     rel = relation_open(relid, AccessExclusiveLock);
                                380                 :                : 
                                381                 :                :     /* Only some relkinds have a visibility map */
   22 michael@paquier.xyz       382                 :GNC           8 :     check_relation_supported(rel);
                                383                 :                : 
                                384                 :                :     /* Forcibly reset cached file size */
 1965 tgl@sss.pgh.pa.us         385                 :CBC           3 :     RelationGetSmgr(rel)->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] = InvalidBlockNumber;
                                386                 :                : 
                                387                 :                :     /* Compute new and old size before entering critical section. */
  708 tmunro@postgresql.or      388                 :              3 :     fork = VISIBILITYMAP_FORKNUM;
 2622 fujii@postgresql.org      389                 :              3 :     block = visibilitymap_prepare_truncate(rel, 0);
  708 tmunro@postgresql.or      390         [ +  - ]:              3 :     old_block = BlockNumberIsValid(block) ? smgrnblocks(RelationGetSmgr(rel), fork) : 0;
                                391                 :                : 
                                392                 :                :     /*
                                393                 :                :      * WAL-logging, buffer dropping, file truncation must be atomic and all on
                                394                 :                :      * one side of a checkpoint.  See RelationTruncate() for discussion.
                                395                 :                :      */
                                396         [ -  + ]:              3 :     Assert((MyProc->delayChkptFlags & (DELAY_CHKPT_START | DELAY_CHKPT_COMPLETE)) == 0);
                                397                 :              3 :     MyProc->delayChkptFlags |= DELAY_CHKPT_START | DELAY_CHKPT_COMPLETE;
                                398                 :              3 :     START_CRIT_SECTION();
                                399                 :                : 
 3816 rhaas@postgresql.org      400   [ +  +  -  +  :              3 :     if (RelationNeedsWAL(rel))
                                        -  -  -  - ]
                                401                 :                :     {
                                402                 :                :         XLogRecPtr  lsn;
                                403                 :                :         xl_smgr_truncate xlrec;
                                404                 :                : 
                                405                 :              2 :         xlrec.blkno = 0;
 1606                           406                 :              2 :         xlrec.rlocator = rel->rd_locator;
 3816                           407                 :              2 :         xlrec.flags = SMGR_TRUNCATE_VM;
                                408                 :                : 
                                409                 :              2 :         XLogBeginInsert();
  655 peter@eisentraut.org      410                 :              2 :         XLogRegisterData(&xlrec, sizeof(xlrec));
                                411                 :                : 
  708 tmunro@postgresql.or      412                 :              2 :         lsn = XLogInsert(RM_SMGR_ID,
                                413                 :                :                          XLOG_SMGR_TRUNCATE | XLR_SPECIAL_REL_UPDATE);
                                414                 :              2 :         XLogFlush(lsn);
                                415                 :                :     }
                                416                 :                : 
                                417         [ +  - ]:              3 :     if (BlockNumberIsValid(block))
                                418                 :              3 :         smgrtruncate(RelationGetSmgr(rel), &fork, 1, &old_block, &block);
                                419                 :                : 
                                420         [ -  + ]:              3 :     END_CRIT_SECTION();
                                421                 :              3 :     MyProc->delayChkptFlags &= ~(DELAY_CHKPT_START | DELAY_CHKPT_COMPLETE);
                                422                 :                : 
                                423                 :                :     /*
                                424                 :                :      * Release the lock right away, not at commit time.
                                425                 :                :      *
                                426                 :                :      * It would be a problem to release the lock prior to commit if this
                                427                 :                :      * truncate operation sends any transactional invalidation messages. Other
                                428                 :                :      * backends would potentially be able to lock the relation without
                                429                 :                :      * processing them in the window of time between when we release the lock
                                430                 :                :      * here and when we sent the messages at our eventual commit.  However,
                                431                 :                :      * we're currently only sending a non-transactional smgr invalidation,
                                432                 :                :      * which will have been posted to shared memory immediately from within
                                433                 :                :      * smgr_truncate.  Therefore, there should be no race here.
                                434                 :                :      *
                                435                 :                :      * The reason why it's desirable to release the lock early here is because
                                436                 :                :      * of the possibility that someone will need to use this to blow away many
                                437                 :                :      * visibility map forks at once.  If we can't release the lock until
                                438                 :                :      * commit time, the transaction doing this will accumulate
                                439                 :                :      * AccessExclusiveLocks on all of those relations at the same time, which
                                440                 :                :      * is undesirable. However, if this turns out to be unsafe we may have no
                                441                 :                :      * choice...
                                442                 :                :      */
 3816 rhaas@postgresql.org      443                 :              3 :     relation_close(rel, AccessExclusiveLock);
                                444                 :                : 
                                445                 :                :     /* Nothing to return. */
                                446                 :              3 :     PG_RETURN_VOID();
                                447                 :                : }
                                448                 :                : 
                                449                 :                : /*
                                450                 :                :  * Helper function to construct whichever TupleDesc we need for a particular
                                451                 :                :  * call.
                                452                 :                :  */
                                453                 :                : static TupleDesc
 3917                           454                 :             46 : pg_visibility_tupdesc(bool include_blkno, bool include_pd)
                                455                 :                : {
                                456                 :                :     TupleDesc   tupdesc;
                                457                 :             46 :     AttrNumber  maxattr = 2;
                                458                 :             46 :     AttrNumber  a = 0;
                                459                 :                : 
                                460         [ +  + ]:             46 :     if (include_blkno)
                                461                 :             19 :         ++maxattr;
                                462         [ +  + ]:             46 :     if (include_pd)
                                463                 :              7 :         ++maxattr;
 2930 andres@anarazel.de        464                 :             46 :     tupdesc = CreateTemplateTupleDesc(maxattr);
 3917 rhaas@postgresql.org      465         [ +  + ]:             46 :     if (include_blkno)
                                466                 :             19 :         TupleDescInitEntry(tupdesc, ++a, "blkno", INT8OID, -1, 0);
                                467                 :             46 :     TupleDescInitEntry(tupdesc, ++a, "all_visible", BOOLOID, -1, 0);
                                468                 :             46 :     TupleDescInitEntry(tupdesc, ++a, "all_frozen", BOOLOID, -1, 0);
                                469         [ +  + ]:             46 :     if (include_pd)
                                470                 :              7 :         TupleDescInitEntry(tupdesc, ++a, "pd_all_visible", BOOLOID, -1, 0);
                                471         [ -  + ]:             46 :     Assert(a == maxattr);
                                472                 :                : 
  257 drowley@postgresql.o      473                 :             46 :     TupleDescFinalize(tupdesc);
                                474                 :                : 
 3917 rhaas@postgresql.org      475                 :             46 :     return BlessTupleDesc(tupdesc);
                                476                 :                : }
                                477                 :                : 
                                478                 :                : /*
                                479                 :                :  * Collect visibility data about a relation.
                                480                 :                :  *
                                481                 :                :  * Checks relkind of relid and will throw an error if the relation does not
                                482                 :                :  * have a VM.
                                483                 :                :  */
                                484                 :                : static vbits *
                                485                 :             19 : collect_visibility_data(Oid relid, bool include_pd)
                                486                 :                : {
                                487                 :                :     Relation    rel;
                                488                 :                :     BlockNumber nblocks;
                                489                 :                :     vbits      *info;
                                490                 :                :     BlockNumber blkno;
                                491                 :             19 :     Buffer      vmbuffer = InvalidBuffer;
 3824                           492                 :             19 :     BufferAccessStrategy bstrategy = GetAccessStrategy(BAS_BULKREAD);
                                493                 :                :     BlockRangeReadStreamPrivate p;
  809 noah@leadboat.com         494                 :             19 :     ReadStream *stream = NULL;
                                495                 :                : 
 3917 rhaas@postgresql.org      496                 :             19 :     rel = relation_open(relid, AccessShareLock);
                                497                 :                : 
                                498                 :                :     /* Only some relkinds have a visibility map */
   22 michael@paquier.xyz       499                 :GNC          17 :     check_relation_supported(rel);
                                500                 :                : 
 3917 rhaas@postgresql.org      501                 :CBC          12 :     nblocks = RelationGetNumberOfBlocks(rel);
 3447 tgl@sss.pgh.pa.us         502                 :             12 :     info = palloc0(offsetof(vbits, bits) + nblocks);
 3917 rhaas@postgresql.org      503                 :             12 :     info->next = 0;
                                504                 :             12 :     info->count = nblocks;
                                505                 :                : 
                                506                 :                :     /* Create a stream if reading main fork. */
  809 noah@leadboat.com         507         [ +  + ]:             12 :     if (include_pd)
                                508                 :                :     {
                                509                 :              6 :         p.current_blocknum = 0;
                                510                 :              6 :         p.last_exclusive = nblocks;
                                511                 :                : 
                                512                 :                :         /*
                                513                 :                :          * It is safe to use batchmode as block_range_read_stream_cb takes no
                                514                 :                :          * locks.
                                515                 :                :          */
  608 andres@anarazel.de        516                 :              6 :         stream = read_stream_begin_relation(READ_STREAM_FULL |
                                517                 :                :                                             READ_STREAM_USE_BATCHING,
                                518                 :                :                                             bstrategy,
                                519                 :                :                                             rel,
                                520                 :                :                                             MAIN_FORKNUM,
                                521                 :                :                                             block_range_read_stream_cb,
                                522                 :                :                                             &p,
                                523                 :                :                                             0);
                                524                 :                :     }
                                525                 :                : 
 3917 rhaas@postgresql.org      526         [ +  + ]:            114 :     for (blkno = 0; blkno < nblocks; ++blkno)
                                527                 :                :     {
                                528                 :                :         int32       mapbits;
                                529                 :                : 
                                530                 :                :         /* Make sure we are interruptible. */
                                531         [ -  + ]:            102 :         CHECK_FOR_INTERRUPTS();
                                532                 :                : 
                                533                 :                :         /* Get map info. */
                                534                 :            102 :         mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
                                535         [ +  + ]:            102 :         if ((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0)
                                536                 :             98 :             info->bits[blkno] |= (1 << 0);
                                537         [ +  + ]:            102 :         if ((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0)
                                538                 :             95 :             info->bits[blkno] |= (1 << 1);
                                539                 :                : 
                                540                 :                :         /*
                                541                 :                :          * Page-level data requires reading every block, so only get it if the
                                542                 :                :          * caller needs it.  Use a buffer access strategy, too, to prevent
                                543                 :                :          * cache-trashing.
                                544                 :                :          */
                                545         [ +  + ]:            102 :         if (include_pd)
                                546                 :                :         {
                                547                 :                :             Buffer      buffer;
                                548                 :                :             Page        page;
                                549                 :                : 
  809 noah@leadboat.com         550                 :              3 :             buffer = read_stream_next_buffer(stream, NULL);
 3917 rhaas@postgresql.org      551                 :              3 :             LockBuffer(buffer, BUFFER_LOCK_SHARE);
                                552                 :                : 
 3874 kgrittn@postgresql.o      553                 :              3 :             page = BufferGetPage(buffer);
 3917 rhaas@postgresql.org      554         [ +  + ]:              3 :             if (PageIsAllVisible(page))
                                555                 :              2 :                 info->bits[blkno] |= (1 << 2);
                                556                 :                : 
                                557                 :              3 :             UnlockReleaseBuffer(buffer);
                                558                 :                :         }
                                559                 :                :     }
                                560                 :                : 
  809 noah@leadboat.com         561         [ +  + ]:             12 :     if (include_pd)
                                562                 :                :     {
                                563         [ -  + ]:              6 :         Assert(read_stream_next_buffer(stream, NULL) == InvalidBuffer);
                                564                 :              6 :         read_stream_end(stream);
                                565                 :                :     }
                                566                 :                : 
                                567                 :                :     /* Clean up. */
 3917 rhaas@postgresql.org      568         [ +  + ]:             12 :     if (vmbuffer != InvalidBuffer)
                                569                 :              9 :         ReleaseBuffer(vmbuffer);
                                570                 :             12 :     relation_close(rel, AccessShareLock);
                                571                 :                : 
                                572                 :             12 :     return info;
                                573                 :                : }
                                574                 :                : 
                                575                 :                : /*
                                576                 :                :  * The "strict" version of GetOldestNonRemovableTransactionId().  The
                                577                 :                :  * pg_visibility check can tolerate false positives (don't report some of the
                                578                 :                :  * errors), but can't tolerate false negatives (report false errors). Normally,
                                579                 :                :  * horizons move forwards, but there are cases when it could move backward
                                580                 :                :  * (see comment for ComputeXidHorizons()).
                                581                 :                :  *
                                582                 :                :  * This is why we have to implement our own function for xid horizon, which
                                583                 :                :  * would be guaranteed to be newer or equal to any xid horizon computed before.
                                584                 :                :  * We have to do the following to achieve this.
                                585                 :                :  *
                                586                 :                :  * 1. Ignore processes xmin's, because they consider connection to other
                                587                 :                :  *    databases that were ignored before.
                                588                 :                :  * 2. Ignore KnownAssignedXids, as they are not database-aware. Although we
                                589                 :                :  *    now perform minimal checking on a standby by always using nextXid, this
                                590                 :                :  *    approach is better than nothing and will at least catch extremely broken
                                591                 :                :  *    cases where a xid is in the future.
                                592                 :                :  * 3. Ignore walsender xmin, because it could go backward if some replication
                                593                 :                :  *    connections don't use replication slots.
                                594                 :                :  *
                                595                 :                :  * While it might seem like we could use KnownAssignedXids for shared
                                596                 :                :  * catalogs, since shared catalogs rely on a global horizon rather than a
                                597                 :                :  * database-specific one - there are potential edge cases.  For example, a
                                598                 :                :  * transaction may crash on the primary without writing a commit/abort record.
                                599                 :                :  * This would lead to a situation where it appears to still be running on the
                                600                 :                :  * standby, even though it has already ended on the primary.  For this reason,
                                601                 :                :  * it's safer to ignore KnownAssignedXids, even for shared catalogs.
                                602                 :                :  *
                                603                 :                :  * As a result, we're using only currently running xids to compute the horizon.
                                604                 :                :  * Surely these would significantly sacrifice accuracy.  But we have to do so
                                605                 :                :  * to avoid reporting false errors.
                                606                 :                :  */
                                607                 :                : static TransactionId
  989 akorotkov@postgresql      608                 :             15 : GetStrictOldestNonRemovableTransactionId(Relation rel)
                                609                 :                : {
                                610                 :                :     RunningTransactions runningTransactions;
                                611                 :                : 
  834                           612         [ +  + ]:             15 :     if (RecoveryInProgress())
                                613                 :                :     {
                                614                 :                :         TransactionId result;
                                615                 :                : 
                                616                 :                :         /* As we ignore KnownAssignedXids on standby, just pick nextXid */
                                617                 :              1 :         LWLockAcquire(XidGenLock, LW_SHARED);
                                618                 :              1 :         result = XidFromFullTransactionId(TransamVariables->nextXid);
                                619                 :              1 :         LWLockRelease(XidGenLock);
                                620                 :              1 :         return result;
                                621                 :                :     }
                                622   [ +  -  -  + ]:             14 :     else if (rel == NULL || rel->rd_rel->relisshared)
                                623                 :                :     {
                                624                 :                :         /* Shared relation: take into account all running xids */
  189 alvherre@kurilemu.de      625                 :UBC           0 :         runningTransactions = GetRunningTransactionData();
  989 akorotkov@postgresql      626                 :              0 :         LWLockRelease(ProcArrayLock);
                                627                 :              0 :         LWLockRelease(XidGenLock);
                                628                 :              0 :         return runningTransactions->oldestRunningXid;
                                629                 :                :     }
  989 akorotkov@postgresql      630   [ +  +  +  - ]:CBC          14 :     else if (!RELATION_IS_LOCAL(rel))
                                631                 :                :     {
                                632                 :                :         /*
                                633                 :                :          * Normal relation: take into account xids running within the current
                                634                 :                :          * database
                                635                 :                :          */
  189 alvherre@kurilemu.de      636                 :             12 :         runningTransactions = GetRunningTransactionData();
  989 akorotkov@postgresql      637                 :             12 :         LWLockRelease(ProcArrayLock);
                                638                 :             12 :         LWLockRelease(XidGenLock);
                                639                 :             12 :         return runningTransactions->oldestDatabaseRunningXid;
                                640                 :                :     }
                                641                 :                :     else
                                642                 :                :     {
                                643                 :                :         /*
                                644                 :                :          * For temporary relations, ComputeXidHorizons() uses only
                                645                 :                :          * TransamVariables->latestCompletedXid and MyProc->xid.  These two
                                646                 :                :          * shouldn't go backwards.  So we're fine with this horizon.
                                647                 :                :          */
                                648                 :              2 :         return GetOldestNonRemovableTransactionId(rel);
                                649                 :                :     }
                                650                 :                : }
                                651                 :                : 
                                652                 :                : /*
                                653                 :                :  * Callback function to get next block for read stream object used in
                                654                 :                :  * collect_corrupt_items() function.
                                655                 :                :  */
                                656                 :                : static BlockNumber
  809 noah@leadboat.com         657                 :            129 : collect_corrupt_items_read_stream_next_block(ReadStream *stream,
                                658                 :                :                                              void *callback_private_data,
                                659                 :                :                                              void *per_buffer_data)
                                660                 :                : {
                                661                 :            129 :     struct collect_corrupt_items_read_stream_private *p = callback_private_data;
                                662                 :                : 
                                663         [ +  + ]:            142 :     for (; p->current_blocknum < p->last_exclusive; p->current_blocknum++)
                                664                 :                :     {
                                665                 :            127 :         bool        check_frozen = false;
                                666                 :            127 :         bool        check_visible = false;
                                667                 :                : 
                                668                 :                :         /* Make sure we are interruptible. */
                                669         [ -  + ]:            127 :         CHECK_FOR_INTERRUPTS();
                                670                 :                : 
                                671   [ +  +  +  + ]:            127 :         if (p->all_frozen && VM_ALL_FROZEN(p->rel, p->current_blocknum, &p->vmbuffer))
                                672                 :             49 :             check_frozen = true;
                                673   [ +  +  +  + ]:            127 :         if (p->all_visible && VM_ALL_VISIBLE(p->rel, p->current_blocknum, &p->vmbuffer))
                                674                 :             65 :             check_visible = true;
                                675   [ +  +  +  + ]:            127 :         if (!check_visible && !check_frozen)
                                676                 :             13 :             continue;
                                677                 :                : 
                                678                 :            114 :         return p->current_blocknum++;
                                679                 :                :     }
                                680                 :                : 
                                681                 :             15 :     return InvalidBlockNumber;
                                682                 :                : }
                                683                 :                : 
                                684                 :                : /*
                                685                 :                :  * Returns a list of items whose visibility map information does not match
                                686                 :                :  * the status of the tuples on the page.
                                687                 :                :  *
                                688                 :                :  * If all_visible is passed as true, this will include all items which are
                                689                 :                :  * on pages marked as all-visible in the visibility map but which do not
                                690                 :                :  * seem to in fact be all-visible.
                                691                 :                :  *
                                692                 :                :  * If all_frozen is passed as true, this will include all items which are
                                693                 :                :  * on pages marked as all-frozen but which do not seem to in fact be frozen.
                                694                 :                :  *
                                695                 :                :  * Checks relkind of relid and will throw an error if the relation does not
                                696                 :                :  * have a VM.
                                697                 :                :  */
                                698                 :                : static corrupt_items *
 3818 rhaas@postgresql.org      699                 :             20 : collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
                                700                 :                : {
                                701                 :                :     Relation    rel;
                                702                 :                :     corrupt_items *items;
                                703                 :             20 :     Buffer      vmbuffer = InvalidBuffer;
                                704                 :             20 :     BufferAccessStrategy bstrategy = GetAccessStrategy(BAS_BULKREAD);
                                705                 :             20 :     TransactionId OldestXmin = InvalidTransactionId;
                                706                 :                :     struct collect_corrupt_items_read_stream_private p;
                                707                 :                :     ReadStream *stream;
                                708                 :                :     Buffer      buffer;
                                709                 :                : 
                                710                 :             20 :     rel = relation_open(relid, AccessShareLock);
                                711                 :                : 
                                712                 :                :     /* Only some relkinds have a visibility map */
   22 michael@paquier.xyz       713                 :GNC          20 :     check_relation_supported(rel);
                                714                 :                : 
 2299 andres@anarazel.de        715         [ +  + ]:CBC          15 :     if (all_visible)
  989 akorotkov@postgresql      716                 :             10 :         OldestXmin = GetStrictOldestNonRemovableTransactionId(rel);
                                717                 :                : 
                                718                 :                :     /*
                                719                 :                :      * Guess an initial array size. We don't expect many corrupted tuples, so
                                720                 :                :      * start with a small array.  This function uses the "next" field to track
                                721                 :                :      * the next offset where we can store an item (which is the same thing as
                                722                 :                :      * the number of items found so far) and the "count" field to track the
                                723                 :                :      * number of entries allocated.  We'll repurpose these fields before
                                724                 :                :      * returning.
                                725                 :                :      */
  358 michael@paquier.xyz       726                 :             15 :     items = palloc0_object(corrupt_items);
 3818 rhaas@postgresql.org      727                 :             15 :     items->next = 0;
                                728                 :             15 :     items->count = 64;
  103 michael@paquier.xyz       729                 :GNC          15 :     items->tids = palloc_array(ItemPointerData, items->count);
                                730                 :                : 
  809 noah@leadboat.com         731                 :CBC          15 :     p.current_blocknum = 0;
                                732                 :             15 :     p.last_exclusive = RelationGetNumberOfBlocks(rel);
                                733                 :             15 :     p.rel = rel;
                                734                 :             15 :     p.vmbuffer = InvalidBuffer;
                                735                 :             15 :     p.all_frozen = all_frozen;
                                736                 :             15 :     p.all_visible = all_visible;
                                737                 :             15 :     stream = read_stream_begin_relation(READ_STREAM_FULL,
                                738                 :                :                                         bstrategy,
                                739                 :                :                                         rel,
                                740                 :                :                                         MAIN_FORKNUM,
                                741                 :                :                                         collect_corrupt_items_read_stream_next_block,
                                742                 :                :                                         &p,
                                743                 :                :                                         0);
                                744                 :                : 
                                745                 :                :     /* Loop over every block in the relation. */
                                746         [ +  + ]:            129 :     while ((buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
                                747                 :                :     {
                                748                 :            114 :         bool        check_frozen = all_frozen;
                                749                 :            114 :         bool        check_visible = all_visible;
                                750                 :                :         Page        page;
                                751                 :                :         OffsetNumber offnum,
                                752                 :                :                     maxoff;
                                753                 :                :         BlockNumber blkno;
                                754                 :                : 
                                755                 :                :         /* Make sure we are interruptible. */
 3818 rhaas@postgresql.org      756         [ -  + ]:            114 :         CHECK_FOR_INTERRUPTS();
                                757                 :                : 
                                758                 :            114 :         LockBuffer(buffer, BUFFER_LOCK_SHARE);
                                759                 :                : 
                                760                 :            114 :         page = BufferGetPage(buffer);
                                761                 :            114 :         maxoff = PageGetMaxOffsetNumber(page);
  809 noah@leadboat.com         762                 :            114 :         blkno = BufferGetBlockNumber(buffer);
                                763                 :                : 
                                764                 :                :         /*
                                765                 :                :          * The visibility map bits might have changed while we were acquiring
                                766                 :                :          * the page lock.  Recheck to avoid returning spurious results.
                                767                 :                :          */
  815                           768   [ +  +  -  + ]:            114 :         if (check_frozen && !VM_ALL_FROZEN(rel, blkno, &vmbuffer))
 3818 rhaas@postgresql.org      769                 :UBC           0 :             check_frozen = false;
  815 noah@leadboat.com         770   [ +  +  -  + ]:CBC         114 :         if (check_visible && !VM_ALL_VISIBLE(rel, blkno, &vmbuffer))
 3818 rhaas@postgresql.org      771                 :UBC           0 :             check_visible = false;
 3818 rhaas@postgresql.org      772   [ +  +  -  + ]:CBC         114 :         if (!check_visible && !check_frozen)
                                773                 :                :         {
 3818 rhaas@postgresql.org      774                 :UBC           0 :             UnlockReleaseBuffer(buffer);
                                775                 :              0 :             continue;
                                776                 :                :         }
                                777                 :                : 
                                778                 :                :         /* Iterate over each tuple on the page. */
 3818 rhaas@postgresql.org      779                 :CBC         114 :         for (offnum = FirstOffsetNumber;
                                780         [ +  + ]:          17188 :              offnum <= maxoff;
                                781                 :          17074 :              offnum = OffsetNumberNext(offnum))
                                782                 :                :         {
                                783                 :                :             HeapTupleData tuple;
                                784                 :                :             ItemId      itemid;
                                785                 :                : 
                                786                 :          17074 :             itemid = PageGetItemId(page, offnum);
                                787                 :                : 
                                788                 :                :             /* Unused or redirect line pointers are of no interest. */
                                789   [ +  -  +  + ]:          17074 :             if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid))
                                790                 :             24 :                 continue;
                                791                 :                : 
                                792                 :                :             /* Dead line pointers are neither all-visible nor frozen. */
                                793         [ -  + ]:          17050 :             if (ItemIdIsDead(itemid))
                                794                 :                :             {
 3710 tgl@sss.pgh.pa.us         795                 :UBC           0 :                 ItemPointerSet(&(tuple.t_self), blkno, offnum);
                                796                 :              0 :                 record_corrupt_item(items, &tuple.t_self);
 3818 rhaas@postgresql.org      797                 :              0 :                 continue;
                                798                 :                :             }
                                799                 :                : 
                                800                 :                :             /* Initialize a HeapTupleData structure for checks below. */
 3710 tgl@sss.pgh.pa.us         801                 :CBC       17050 :             ItemPointerSet(&(tuple.t_self), blkno, offnum);
 3818 rhaas@postgresql.org      802                 :          17050 :             tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
                                803                 :          17050 :             tuple.t_len = ItemIdGetLength(itemid);
                                804                 :          17050 :             tuple.t_tableOid = relid;
                                805                 :                : 
                                806                 :                :             /*
                                807                 :                :              * If we're checking whether the page is all-visible, we expect
                                808                 :                :              * the tuple to be all-visible.
                                809                 :                :              */
                                810         [ +  + ]:          17050 :             if (check_visible &&
                                811         [ +  + ]:           9028 :                 !tuple_all_visible(&tuple, OldestXmin, buffer))
                                812                 :                :             {
                                813                 :                :                 TransactionId RecomputedOldestXmin;
                                814                 :                : 
                                815                 :                :                 /*
                                816                 :                :                  * Time has passed since we computed OldestXmin, so it's
                                817                 :                :                  * possible that this tuple is all-visible in reality even
                                818                 :                :                  * though it doesn't appear so based on our
                                819                 :                :                  * previously-computed value.  Let's compute a new value so we
                                820                 :                :                  * can be certain whether there is a problem.
                                821                 :                :                  *
                                822                 :                :                  * From a concurrency point of view, it sort of sucks to
                                823                 :                :                  * retake ProcArrayLock here while we're holding the buffer
                                824                 :                :                  * locked in shared mode, but it should be safe against
                                825                 :                :                  * deadlocks, because surely
                                826                 :                :                  * GetStrictOldestNonRemovableTransactionId() should never
                                827                 :                :                  * take a buffer lock. And this shouldn't happen often, so
                                828                 :                :                  * it's worth being careful so as to avoid false positives.
                                829                 :                :                  */
  989 akorotkov@postgresql      830                 :              5 :                 RecomputedOldestXmin = GetStrictOldestNonRemovableTransactionId(rel);
                                831                 :                : 
 3818 rhaas@postgresql.org      832         [ +  - ]:              5 :                 if (!TransactionIdPrecedes(OldestXmin, RecomputedOldestXmin))
 3710 tgl@sss.pgh.pa.us         833                 :              5 :                     record_corrupt_item(items, &tuple.t_self);
                                834                 :                :                 else
                                835                 :                :                 {
 3818 rhaas@postgresql.org      836                 :UBC           0 :                     OldestXmin = RecomputedOldestXmin;
                                837         [ #  # ]:              0 :                     if (!tuple_all_visible(&tuple, OldestXmin, buffer))
 3710 tgl@sss.pgh.pa.us         838                 :              0 :                         record_corrupt_item(items, &tuple.t_self);
                                839                 :                :                 }
                                840                 :                :             }
                                841                 :                : 
                                842                 :                :             /*
                                843                 :                :              * If we're checking whether the page is all-frozen, we expect the
                                844                 :                :              * tuple to be in a state where it will never need freezing.
                                845                 :                :              */
 3818 rhaas@postgresql.org      846         [ +  + ]:CBC       17050 :             if (check_frozen)
                                847                 :                :             {
                                848         [ +  + ]:           8022 :                 if (heap_tuple_needs_eventual_freeze(tuple.t_data))
 3710 tgl@sss.pgh.pa.us         849                 :              5 :                     record_corrupt_item(items, &tuple.t_self);
                                850                 :                :             }
                                851                 :                :         }
                                852                 :                : 
 3818 rhaas@postgresql.org      853                 :            114 :         UnlockReleaseBuffer(buffer);
                                854                 :                :     }
  809 noah@leadboat.com         855                 :             15 :     read_stream_end(stream);
                                856                 :                : 
                                857                 :                :     /* Clean up. */
 3818 rhaas@postgresql.org      858         [ +  + ]:             15 :     if (vmbuffer != InvalidBuffer)
                                859                 :             11 :         ReleaseBuffer(vmbuffer);
  809 noah@leadboat.com         860         [ +  - ]:             15 :     if (p.vmbuffer != InvalidBuffer)
                                861                 :             15 :         ReleaseBuffer(p.vmbuffer);
 3818 rhaas@postgresql.org      862                 :             15 :     relation_close(rel, AccessShareLock);
                                863                 :                : 
                                864                 :                :     /*
                                865                 :                :      * Before returning, repurpose the fields to match caller's expectations.
                                866                 :                :      * next is now the next item that should be read (rather than written) and
                                867                 :                :      * count is now the number of items we wrote (rather than the number we
                                868                 :                :      * allocated).
                                869                 :                :      */
                                870                 :             15 :     items->count = items->next;
                                871                 :             15 :     items->next = 0;
                                872                 :                : 
                                873                 :             15 :     return items;
                                874                 :                : }
                                875                 :                : 
                                876                 :                : /*
                                877                 :                :  * Remember one corrupt item.
                                878                 :                :  */
                                879                 :                : static void
                                880                 :             10 : record_corrupt_item(corrupt_items *items, ItemPointer tid)
                                881                 :                : {
                                882                 :                :     /* enlarge output array if needed. */
                                883         [ -  + ]:             10 :     if (items->next >= items->count)
                                884                 :                :     {
 3818 rhaas@postgresql.org      885                 :UBC           0 :         items->count *= 2;
  103 michael@paquier.xyz       886                 :UNC           0 :         items->tids = repalloc_array(items->tids, ItemPointerData, items->count);
                                887                 :                :     }
                                888                 :                :     /* and add the new item */
 3818 rhaas@postgresql.org      889                 :CBC          10 :     items->tids[items->next++] = *tid;
                                890                 :             10 : }
                                891                 :                : 
                                892                 :                : /*
                                893                 :                :  * Check whether a tuple is all-visible relative to a given OldestXmin value.
                                894                 :                :  * The buffer should contain the tuple and should be locked and pinned.
                                895                 :                :  */
                                896                 :                : static bool
                                897                 :           9028 : tuple_all_visible(HeapTuple tup, TransactionId OldestXmin, Buffer buffer)
                                898                 :                : {
                                899                 :                :     HTSV_Result state;
                                900                 :                :     TransactionId xmin;
                                901                 :                : 
                                902                 :           9028 :     state = HeapTupleSatisfiesVacuum(tup, OldestXmin, buffer);
                                903         [ +  + ]:           9028 :     if (state != HEAPTUPLE_LIVE)
                                904                 :              5 :         return false;           /* all-visible implies live */
                                905                 :                : 
                                906                 :                :     /*
                                907                 :                :      * Neither lazy_scan_heap nor heap_page_is_all_visible will mark a page
                                908                 :                :      * all-visible unless every tuple is hinted committed. However, those hint
                                909                 :                :      * bits could be lost after a crash, so we can't be certain that they'll
                                910                 :                :      * be set here.  So just check the xmin.
                                911                 :                :      */
                                912                 :                : 
                                913                 :           9023 :     xmin = HeapTupleHeaderGetXmin(tup->t_data);
                                914         [ -  + ]:           9023 :     if (!TransactionIdPrecedes(xmin, OldestXmin))
 3818 rhaas@postgresql.org      915                 :UBC           0 :         return false;           /* xmin not old enough for all to see */
                                916                 :                : 
 3818 rhaas@postgresql.org      917                 :CBC        9023 :     return true;
                                918                 :                : }
                                919                 :                : 
                                920                 :                : /*
                                921                 :                :  * check_relation_supported - convenience routine to check that relation
                                922                 :                :  * is supported by the callers.
                                923                 :                :  */
                                924                 :                : static void
   22 michael@paquier.xyz       925                 :GNC         111 : check_relation_supported(Relation rel)
                                926                 :                : {
 1821 peter@eisentraut.org      927   [ +  +  +  +  :CBC         111 :     if (!RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
                                              +  + ]
 3551 sfrost@snowman.net        928         [ +  - ]:             25 :         ereport(ERROR,
                                929                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                930                 :                :                  errmsg("relation \"%s\" is of wrong relation kind",
                                931                 :                :                         RelationGetRelationName(rel)),
                                932                 :                :                  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
                                933                 :                : 
   22 michael@paquier.xyz       934   [ +  +  -  + ]:GNC          86 :     if (RELATION_IS_OTHER_TEMP(rel))
   22 michael@paquier.xyz       935         [ #  # ]:UNC           0 :         ereport(ERROR,
                                936                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                937                 :                :                  errmsg("cannot access temporary tables of other sessions")));
                                938                 :                : 
   22 michael@paquier.xyz       939   [ +  +  -  + ]:GNC          86 :     if (!RelationIsPermanent(rel) && RecoveryInProgress())
   22 michael@paquier.xyz       940         [ #  # ]:UNC           0 :         ereport(ERROR,
                                941                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                942                 :                :                  errmsg("cannot access temporary or unlogged relations during recovery")));
 3551 sfrost@snowman.net        943                 :CBC          86 : }
        

Generated by: LCOV version 2.0-1