LCOV - differential code coverage report
Current view: top level - src/backend/replication/pgrepack - pgrepack.c (source / functions) Coverage Total Hit UNC GNC
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 93.3 % 90 84 6 84
Current Date: 2026-05-05 10:23:31 +0900 Functions: 100.0 % 8 8 8
Baseline: lcov-20260505-025707-baseline Branches: 66.7 % 48 32 16 32
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 93.3 % 90 84 6 84
Function coverage date bins:
(7,30] days: 100.0 % 8 8 8
Branch coverage date bins:
(7,30] days: 66.7 % 48 32 16 32

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgrepack.c
                                  4                 :                :  *      Logical Replication output plugin for REPACK command
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2026, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *        src/backend/replication/pgrepack/pgrepack.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : #include "postgres.h"
                                 14                 :                : 
                                 15                 :                : #include "access/detoast.h"
                                 16                 :                : #include "commands/repack_internal.h"
                                 17                 :                : #include "replication/snapbuild.h"
                                 18                 :                : #include "utils/memutils.h"
                                 19                 :                : 
   29 alvherre@kurilemu.de       20                 :GNC           3 : PG_MODULE_MAGIC;
                                 21                 :                : 
                                 22                 :                : static void repack_startup(LogicalDecodingContext *ctx,
                                 23                 :                :                            OutputPluginOptions *opt, bool is_init);
                                 24                 :                : static void repack_shutdown(LogicalDecodingContext *ctx);
                                 25                 :                : static void repack_begin_txn(LogicalDecodingContext *ctx,
                                 26                 :                :                              ReorderBufferTXN *txn);
                                 27                 :                : static void repack_commit_txn(LogicalDecodingContext *ctx,
                                 28                 :                :                               ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
                                 29                 :                : static void repack_process_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
                                 30                 :                :                                   Relation relation, ReorderBufferChange *change);
                                 31                 :                : static void repack_store_change(LogicalDecodingContext *ctx, Relation relation,
                                 32                 :                :                                 ConcurrentChangeKind kind, HeapTuple tuple);
                                 33                 :                : 
                                 34                 :                : void
                                 35                 :              3 : _PG_output_plugin_init(OutputPluginCallbacks *cb)
                                 36                 :                : {
                                 37                 :              3 :     cb->startup_cb = repack_startup;
                                 38                 :              3 :     cb->begin_cb = repack_begin_txn;
                                 39                 :              3 :     cb->change_cb = repack_process_change;
                                 40                 :              3 :     cb->commit_cb = repack_commit_txn;
                                 41                 :              3 :     cb->shutdown_cb = repack_shutdown;
                                 42                 :              3 : }
                                 43                 :                : 
                                 44                 :                : 
                                 45                 :                : /* initialize this plugin */
                                 46                 :                : static void
                                 47                 :              3 : repack_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
                                 48                 :                :                bool is_init)
                                 49                 :                : {
                                 50                 :              3 :     ctx->output_plugin_private = NULL;
                                 51                 :                : 
                                 52                 :                :     /* Probably unnecessary, as we don't use the SQL interface ... */
                                 53                 :              3 :     opt->output_type = OUTPUT_PLUGIN_BINARY_OUTPUT;
                                 54                 :                : 
                                 55                 :                :     /*
                                 56                 :                :      * REPACK doesn't need access to shared catalogs, so we can speed up the
                                 57                 :                :      * historic snapshot creation by setting this flag.  We'll only have to
                                 58                 :                :      * wait for transactions in our database.
                                 59                 :                :      */
   28                            60                 :              3 :     opt->need_shared_catalogs = false;
                                 61                 :                : 
   29                            62         [ -  + ]:              3 :     if (ctx->output_plugin_options != NIL)
                                 63                 :                :     {
   29 alvherre@kurilemu.de       64         [ #  # ]:UNC           0 :         ereport(ERROR,
                                 65                 :                :                 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 66                 :                :                 errmsg("this plugin does not expect any options"));
                                 67                 :                :     }
   29 alvherre@kurilemu.de       68                 :GNC           3 : }
                                 69                 :                : 
                                 70                 :                : static void
                                 71                 :              3 : repack_shutdown(LogicalDecodingContext *ctx)
                                 72                 :                : {
                                 73                 :              3 : }
                                 74                 :                : 
                                 75                 :                : /*
                                 76                 :                :  * As we don't release the slot during processing of particular table, there's
                                 77                 :                :  * no room for SQL interface, even for debugging purposes. Therefore we need
                                 78                 :                :  * neither OutputPluginPrepareWrite() nor OutputPluginWrite() in the plugin
                                 79                 :                :  * callbacks. (Although we might want to write custom callbacks, this API
                                 80                 :                :  * seems to be unnecessarily generic for our purposes.)
                                 81                 :                :  */
                                 82                 :                : 
                                 83                 :                : /* BEGIN callback */
                                 84                 :                : static void
                                 85                 :              7 : repack_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
                                 86                 :                : {
                                 87                 :              7 : }
                                 88                 :                : 
                                 89                 :                : /* COMMIT callback */
                                 90                 :                : static void
                                 91                 :              7 : repack_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
                                 92                 :                :                   XLogRecPtr commit_lsn)
                                 93                 :                : {
                                 94                 :              7 : }
                                 95                 :                : 
                                 96                 :                : /*
                                 97                 :                :  * Callback for individual changed tuples
                                 98                 :                :  */
                                 99                 :                : static void
                                100                 :             30 : repack_process_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
                                101                 :                :                       Relation relation, ReorderBufferChange *change)
                                102                 :                : {
                                103                 :             30 :     RepackDecodingState *private PG_USED_FOR_ASSERTS_ONLY =
                                104                 :                :         (RepackDecodingState *) ctx->output_writer_private;
                                105                 :                : 
                                106                 :                :     /* Changes of other relation should not have been decoded. */
                                107         [ -  + ]:             30 :     Assert(RelationGetRelid(relation) == private->relid);
                                108                 :                : 
                                109                 :                :     /* Decode entry depending on its type */
                                110   [ +  +  +  - ]:             30 :     switch (change->action)
                                111                 :                :     {
                                112                 :              7 :         case REORDER_BUFFER_CHANGE_INSERT:
                                113                 :                :             {
                                114                 :                :                 HeapTuple   newtuple;
                                115                 :                : 
                                116                 :              7 :                 newtuple = change->data.tp.newtuple;
                                117                 :                : 
                                118                 :                :                 /*
                                119                 :                :                  * Identity checks in the main function should have made this
                                120                 :                :                  * impossible.
                                121                 :                :                  */
                                122         [ -  + ]:              7 :                 if (newtuple == NULL)
   29 alvherre@kurilemu.de      123         [ #  # ]:UNC           0 :                     elog(ERROR, "incomplete insert info");
                                124                 :                : 
   29 alvherre@kurilemu.de      125                 :GNC           7 :                 repack_store_change(ctx, relation, CHANGE_INSERT, newtuple);
                                126                 :                :             }
                                127                 :              7 :             break;
                                128                 :             20 :         case REORDER_BUFFER_CHANGE_UPDATE:
                                129                 :                :             {
                                130                 :                :                 HeapTuple   oldtuple,
                                131                 :                :                             newtuple;
                                132                 :                : 
                                133                 :             20 :                 oldtuple = change->data.tp.oldtuple;
                                134                 :             20 :                 newtuple = change->data.tp.newtuple;
                                135                 :                : 
                                136         [ -  + ]:             20 :                 if (newtuple == NULL)
   29 alvherre@kurilemu.de      137         [ #  # ]:UNC           0 :                     elog(ERROR, "incomplete update info");
                                138                 :                : 
   29 alvherre@kurilemu.de      139         [ +  + ]:GNC          20 :                 if (oldtuple != NULL)
                                140                 :              8 :                     repack_store_change(ctx, relation, CHANGE_UPDATE_OLD, oldtuple);
                                141                 :                : 
                                142                 :             20 :                 repack_store_change(ctx, relation, CHANGE_UPDATE_NEW, newtuple);
                                143                 :                :             }
                                144                 :             20 :             break;
                                145                 :              3 :         case REORDER_BUFFER_CHANGE_DELETE:
                                146                 :                :             {
                                147                 :                :                 HeapTuple   oldtuple;
                                148                 :                : 
                                149                 :              3 :                 oldtuple = change->data.tp.oldtuple;
                                150                 :                : 
                                151         [ -  + ]:              3 :                 if (oldtuple == NULL)
   29 alvherre@kurilemu.de      152         [ #  # ]:UNC           0 :                     elog(ERROR, "incomplete delete info");
                                153                 :                : 
   29 alvherre@kurilemu.de      154                 :GNC           3 :                 repack_store_change(ctx, relation, CHANGE_DELETE, oldtuple);
                                155                 :                :             }
                                156                 :              3 :             break;
   29 alvherre@kurilemu.de      157                 :UNC           0 :         default:
                                158                 :                : 
                                159                 :                :             /*
                                160                 :                :              * Should not come here. This includes TRUNCATE of the table being
                                161                 :                :              * processed. heap_decode() cannot check the file locator easily,
                                162                 :                :              * but we assume that TRUNCATE uses AccessExclusiveLock on the
                                163                 :                :              * table so it should not occur during REPACK (CONCURRENTLY).
                                164                 :                :              */
                                165                 :              0 :             Assert(false);
                                166                 :                :             break;
                                167                 :                :     }
   29 alvherre@kurilemu.de      168                 :GNC          30 : }
                                169                 :                : 
                                170                 :                : /*
                                171                 :                :  * Write the given tuple, with the given change kind, to the repack spill
                                172                 :                :  * file.  Later, the repack decoding worker can read these and replay
                                173                 :                :  * the operations on the new copy of the table.
                                174                 :                :  *
                                175                 :                :  * For each change affecting the table being repacked, we store enough
                                176                 :                :  * information about each tuple in it, so that it can be replayed in the
                                177                 :                :  * new copy of the table.
                                178                 :                :  */
                                179                 :                : static void
                                180                 :             38 : repack_store_change(LogicalDecodingContext *ctx, Relation relation,
                                181                 :                :                     ConcurrentChangeKind kind, HeapTuple tuple)
                                182                 :                : {
                                183                 :                :     RepackDecodingState *dstate;
                                184                 :                :     MemoryContext oldcxt;
                                185                 :                :     BufFile    *file;
                                186                 :             38 :     List       *attrs_ext = NIL;
                                187                 :                :     int         natt_ext;
                                188                 :                : 
                                189                 :             38 :     dstate = (RepackDecodingState *) ctx->output_writer_private;
                                190                 :             38 :     file = dstate->file;
                                191                 :                : 
                                192                 :                :     /* Store the change kind. */
                                193                 :             38 :     BufFileWrite(file, &kind, 1);
                                194                 :                : 
                                195                 :                :     /* Use a frequently-reset context to avoid dealing with leaks manually */
                                196                 :             38 :     oldcxt = MemoryContextSwitchTo(dstate->change_cxt);
                                197                 :                : 
                                198                 :                :     /*
                                199                 :                :      * If the tuple contains "external indirect" attributes, we need to write
                                200                 :                :      * the contents to the file because we have no control over that memory.
                                201                 :                :      */
                                202         [ +  + ]:             38 :     if (HeapTupleHasExternal(tuple))
                                203                 :                :     {
                                204                 :             13 :         TupleDesc   desc = RelationGetDescr(relation);
                                205                 :                :         TupleTableSlot *slot;
                                206                 :                : 
                                207                 :                :         /* Initialize the slot, if not done already */
                                208         [ +  + ]:             13 :         if (dstate->slot == NULL)
                                209                 :                :         {
                                210                 :                :             ResourceOwner saveResourceOwner;
                                211                 :                : 
                                212                 :              1 :             MemoryContextSwitchTo(dstate->worker_cxt);
                                213                 :              1 :             saveResourceOwner = CurrentResourceOwner;
                                214                 :              1 :             CurrentResourceOwner = dstate->worker_resowner;
                                215                 :              1 :             dstate->slot = MakeSingleTupleTableSlot(desc, &TTSOpsHeapTuple);
                                216                 :              1 :             MemoryContextSwitchTo(dstate->change_cxt);
                                217                 :              1 :             CurrentResourceOwner = saveResourceOwner;
                                218                 :                :         }
                                219                 :                : 
                                220                 :             13 :         slot = dstate->slot;
                                221                 :             13 :         ExecStoreHeapTuple(tuple, slot, false);
                                222                 :                : 
                                223                 :                :         /*
                                224                 :                :          * Loop over all attributes, and find out which ones we need to spill
                                225                 :                :          * separately, to wit: each one that's a non-null varlena and stored
                                226                 :                :          * out of line.
                                227                 :                :          */
                                228         [ +  + ]:             78 :         for (int i = 0; i < desc->natts; i++)
                                229                 :                :         {
                                230                 :             65 :             CompactAttribute *attr = TupleDescCompactAttr(desc, i);
                                231                 :                :             varlena    *varlen;
                                232                 :                : 
                                233   [ +  +  +  +  :             91 :             if (attr->attisdropped || attr->attlen != -1 ||
                                              -  + ]
                                234                 :             26 :                 slot_attisnull(slot, i + 1))
                                235                 :             39 :                 continue;
                                236                 :                : 
                                237                 :             26 :             slot_getsomeattrs(slot, i + 1);
                                238                 :                : 
                                239                 :                :             /*
                                240                 :                :              * This is a non-null varlena datum, but we only care if it's
                                241                 :                :              * out-of-line
                                242                 :                :              */
                                243                 :             26 :             varlen = (varlena *) DatumGetPointer(slot->tts_values[i]);
                                244         [ +  + ]:             26 :             if (!VARATT_IS_EXTERNAL(varlen))
                                245                 :              9 :                 continue;
                                246                 :                : 
                                247                 :                :             /*
                                248                 :                :              * We spill any indirect-external attributes separately from the
                                249                 :                :              * heap tuple.  Anything else is written as is.
                                250                 :                :              */
                                251         [ +  + ]:             17 :             if (VARATT_IS_EXTERNAL_INDIRECT(varlen))
                                252                 :             15 :                 attrs_ext = lappend(attrs_ext, varlen);
                                253                 :                :             else
                                254                 :                :             {
                                255                 :                :                 /*
                                256                 :                :                  * Logical decoding should not produce "external expanded"
                                257                 :                :                  * attributes (those actually should never appear on disk), so
                                258                 :                :                  * only TOASTed attribute can be seen here.
                                259                 :                :                  *
                                260                 :                :                  * We get here if the table has external values but only
                                261                 :                :                  * in-line values are being updated now.
                                262                 :                :                  */
                                263         [ -  + ]:              2 :                 Assert(VARATT_IS_EXTERNAL_ONDISK(varlen));
                                264                 :                :             }
                                265                 :                :         }
                                266                 :                : 
                                267                 :             13 :         ExecClearTuple(slot);
                                268                 :                :     }
                                269                 :                : 
                                270                 :                :     /*
                                271                 :                :      * First, write the original heap tuple, prefixed by its length.  Note
                                272                 :                :      * that the external-toast tag for each toasted attribute will be present
                                273                 :                :      * in what we write, so that we know where to restore each one later.
                                274                 :                :      */
                                275                 :             38 :     BufFileWrite(file, &tuple->t_len, sizeof(tuple->t_len));
                                276                 :             38 :     BufFileWrite(file, tuple->t_data, tuple->t_len);
                                277                 :                : 
                                278                 :                :     /* Then, write the number of external attributes we found. */
                                279                 :             38 :     natt_ext = list_length(attrs_ext);
                                280                 :             38 :     BufFileWrite(file, &natt_ext, sizeof(natt_ext));
                                281                 :                : 
                                282                 :                :     /* Finally, the attributes themselves, if any */
                                283   [ +  +  +  +  :             91 :     foreach_ptr(varlena, attr_val, attrs_ext)
                                              +  + ]
                                284                 :                :     {
                                285                 :             15 :         attr_val = detoast_external_attr(attr_val);
                                286                 :             15 :         BufFileWrite(file, attr_val, VARSIZE_ANY(attr_val));
                                287                 :                :         /* These attributes could be large, so free them right away */
                                288                 :             15 :         pfree(attr_val);
                                289                 :                :     }
                                290                 :                : 
                                291                 :                :     /* Cleanup. */
                                292                 :             38 :     MemoryContextSwitchTo(oldcxt);
                                293                 :             38 :     MemoryContextReset(dstate->change_cxt);
                                294                 :             38 : }
        

Generated by: LCOV version 2.5.0-beta