LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - pg_upgrade_support.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 91.6 % 178 163 3 12 5 158 3
Current Date: 2026-11-28 16:48:44 +0900 Functions: 95.5 % 22 21 1 2 19
Baseline: lcov-20261128-baseline Branches: 32.6 % 138 45 3 90 1 44
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: 87.5 % 16 14 2 14
(30,360] days: 78.6 % 14 11 3 5 6
(360..) days: 93.2 % 148 138 10 138
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 2 2 1 1
(360..) days: 94.7 % 19 18 1 1 17
Branch coverage date bins:
(7,30] days: 41.7 % 12 5 7 5
(30,360] days: 50.0 % 6 3 3 1 2
(360..) days: 30.8 % 120 37 83 37

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  *  pg_upgrade_support.c
                                  3                 :                :  *
                                  4                 :                :  *  server-side functions to set backend global variables
                                  5                 :                :  *  to control oid and relfilenumber assignment, and do other special
                                  6                 :                :  *  hacks needed for pg_upgrade.
                                  7                 :                :  *
                                  8                 :                :  *  Copyright (c) 2010-2026, PostgreSQL Global Development Group
                                  9                 :                :  *  src/backend/utils/adt/pg_upgrade_support.c
                                 10                 :                :  */
                                 11                 :                : 
                                 12                 :                : #include "postgres.h"
                                 13                 :                : 
                                 14                 :                : #include "access/relation.h"
                                 15                 :                : #include "access/table.h"
                                 16                 :                : #include "catalog/binary_upgrade.h"
                                 17                 :                : #include "catalog/heap.h"
                                 18                 :                : #include "catalog/namespace.h"
                                 19                 :                : #include "catalog/pg_subscription_rel.h"
                                 20                 :                : #include "catalog/pg_type.h"
                                 21                 :                : #include "commands/extension.h"
                                 22                 :                : #include "miscadmin.h"
                                 23                 :                : #include "replication/logical.h"
                                 24                 :                : #include "replication/logicallauncher.h"
                                 25                 :                : #include "replication/origin.h"
                                 26                 :                : #include "replication/worker_internal.h"
                                 27                 :                : #include "storage/lmgr.h"
                                 28                 :                : #include "utils/array.h"
                                 29                 :                : #include "utils/builtins.h"
                                 30                 :                : #include "utils/lsyscache.h"
                                 31                 :                : #include "utils/memutils.h"
                                 32                 :                : #include "utils/pg_lsn.h"
                                 33                 :                : 
                                 34                 :                : 
                                 35                 :                : #define CHECK_IS_BINARY_UPGRADE                                 \
                                 36                 :                : do {                                                            \
                                 37                 :                :     if (!IsBinaryUpgrade)                                       \
                                 38                 :                :         ereport(ERROR,                                          \
                                 39                 :                :                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),    \
                                 40                 :                :                  errmsg("function can only be called when server is in binary upgrade mode"))); \
                                 41                 :                : } while (0)
                                 42                 :                : 
                                 43                 :                : Datum
 1776 rhaas@postgresql.org       44                 :CBC           4 : binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
                                 45                 :                : {
                                 46                 :              4 :     Oid         tbspoid = PG_GETARG_OID(0);
                                 47                 :                : 
                                 48   [ -  +  -  - ]:              4 :     CHECK_IS_BINARY_UPGRADE;
                                 49                 :              4 :     binary_upgrade_next_pg_tablespace_oid = tbspoid;
                                 50                 :                : 
                                 51                 :              4 :     PG_RETURN_VOID();
                                 52                 :                : }
                                 53                 :                : 
                                 54                 :                : Datum
 4281 peter_e@gmx.net            55                 :            933 : binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
                                 56                 :                : {
 6044 bruce@momjian.us           57                 :            933 :     Oid         typoid = PG_GETARG_OID(0);
                                 58                 :                : 
 4478                            59   [ -  +  -  - ]:            933 :     CHECK_IS_BINARY_UPGRADE;
 6044                            60                 :            933 :     binary_upgrade_next_pg_type_oid = typoid;
                                 61                 :                : 
                                 62                 :            933 :     PG_RETURN_VOID();
                                 63                 :                : }
                                 64                 :                : 
                                 65                 :                : Datum
 4281 peter_e@gmx.net            66                 :            932 : binary_upgrade_set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
                                 67                 :                : {
 6044 bruce@momjian.us           68                 :            932 :     Oid         typoid = PG_GETARG_OID(0);
                                 69                 :                : 
 4478                            70   [ -  +  -  - ]:            932 :     CHECK_IS_BINARY_UPGRADE;
 5804                            71                 :            932 :     binary_upgrade_next_array_pg_type_oid = typoid;
                                 72                 :                : 
 6044                            73                 :            932 :     PG_RETURN_VOID();
                                 74                 :                : }
                                 75                 :                : 
                                 76                 :                : Datum
 2169 akorotkov@postgresql       77                 :              7 : binary_upgrade_set_next_multirange_pg_type_oid(PG_FUNCTION_ARGS)
                                 78                 :                : {
                                 79                 :              7 :     Oid         typoid = PG_GETARG_OID(0);
                                 80                 :                : 
                                 81   [ -  +  -  - ]:              7 :     CHECK_IS_BINARY_UPGRADE;
                                 82                 :              7 :     binary_upgrade_next_mrng_pg_type_oid = typoid;
                                 83                 :                : 
                                 84                 :              7 :     PG_RETURN_VOID();
                                 85                 :                : }
                                 86                 :                : 
                                 87                 :                : Datum
                                 88                 :              7 : binary_upgrade_set_next_multirange_array_pg_type_oid(PG_FUNCTION_ARGS)
                                 89                 :                : {
                                 90                 :              7 :     Oid         typoid = PG_GETARG_OID(0);
                                 91                 :                : 
                                 92   [ -  +  -  - ]:              7 :     CHECK_IS_BINARY_UPGRADE;
                                 93                 :              7 :     binary_upgrade_next_mrng_array_pg_type_oid = typoid;
                                 94                 :                : 
                                 95                 :              7 :     PG_RETURN_VOID();
                                 96                 :                : }
                                 97                 :                : 
                                 98                 :                : Datum
 4281 peter_e@gmx.net            99                 :            938 : binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
                                100                 :                : {
 5804 bruce@momjian.us          101                 :            938 :     Oid         reloid = PG_GETARG_OID(0);
                                102                 :                : 
 4478                           103   [ -  +  -  - ]:            938 :     CHECK_IS_BINARY_UPGRADE;
 5804                           104                 :            938 :     binary_upgrade_next_heap_pg_class_oid = reloid;
                                105                 :                : 
 6044                           106                 :            938 :     PG_RETURN_VOID();
                                107                 :                : }
                                108                 :                : 
                                109                 :                : Datum
 1776 rhaas@postgresql.org      110                 :            834 : binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
                                111                 :                : {
 1522                           112                 :            834 :     RelFileNumber relfilenumber = PG_GETARG_OID(0);
                                113                 :                : 
 1776                           114   [ -  +  -  - ]:            834 :     CHECK_IS_BINARY_UPGRADE;
 1606                           115                 :            834 :     binary_upgrade_next_heap_pg_class_relfilenumber = relfilenumber;
                                116                 :                : 
 1776                           117                 :            834 :     PG_RETURN_VOID();
                                118                 :                : }
                                119                 :                : 
                                120                 :                : Datum
 4281 peter_e@gmx.net           121                 :            594 : binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
                                122                 :                : {
 5804 bruce@momjian.us          123                 :            594 :     Oid         reloid = PG_GETARG_OID(0);
                                124                 :                : 
 4478                           125   [ -  +  -  - ]:            594 :     CHECK_IS_BINARY_UPGRADE;
 5804                           126                 :            594 :     binary_upgrade_next_index_pg_class_oid = reloid;
                                127                 :                : 
 6044                           128                 :            594 :     PG_RETURN_VOID();
                                129                 :                : }
                                130                 :                : 
                                131                 :                : Datum
 1776 rhaas@postgresql.org      132                 :            662 : binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
                                133                 :                : {
 1522                           134                 :            662 :     RelFileNumber relfilenumber = PG_GETARG_OID(0);
                                135                 :                : 
 1776                           136   [ -  +  -  - ]:            662 :     CHECK_IS_BINARY_UPGRADE;
 1606                           137                 :            662 :     binary_upgrade_next_index_pg_class_relfilenumber = relfilenumber;
                                138                 :                : 
 1776                           139                 :            662 :     PG_RETURN_VOID();
                                140                 :                : }
                                141                 :                : 
                                142                 :                : Datum
 4281 peter_e@gmx.net           143                 :            290 : binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
                                144                 :                : {
 5804 bruce@momjian.us          145                 :            290 :     Oid         reloid = PG_GETARG_OID(0);
                                146                 :                : 
 4478                           147   [ -  +  -  - ]:            290 :     CHECK_IS_BINARY_UPGRADE;
 5804                           148                 :            290 :     binary_upgrade_next_toast_pg_class_oid = reloid;
                                149                 :                : 
 6044                           150                 :            290 :     PG_RETURN_VOID();
                                151                 :                : }
                                152                 :                : 
                                153                 :                : Datum
   78 michael@paquier.xyz       154                 :GNC         290 : binary_upgrade_set_next_toast_chunk_id_typoid(PG_FUNCTION_ARGS)
                                155                 :                : {
                                156                 :            290 :     Oid         typoid = PG_GETARG_OID(0);
                                157                 :                : 
                                158   [ -  +  -  - ]:            290 :     CHECK_IS_BINARY_UPGRADE;
                                159                 :            290 :     binary_upgrade_next_toast_chunk_id_typoid = typoid;
                                160                 :                : 
                                161                 :            290 :     PG_RETURN_VOID();
                                162                 :                : }
                                163                 :                : 
                                164                 :                : Datum
 1776 rhaas@postgresql.org      165                 :CBC         290 : binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
                                166                 :                : {
 1522                           167                 :            290 :     RelFileNumber relfilenumber = PG_GETARG_OID(0);
                                168                 :                : 
 1776                           169   [ -  +  -  - ]:            290 :     CHECK_IS_BINARY_UPGRADE;
 1606                           170                 :            290 :     binary_upgrade_next_toast_pg_class_relfilenumber = relfilenumber;
                                171                 :                : 
 1776                           172                 :            290 :     PG_RETURN_VOID();
                                173                 :                : }
                                174                 :                : 
                                175                 :                : /*
                                176                 :                :  * Supply the attribute versions, for the next relation to be created, from
                                177                 :                :  * the old cluster. See SetBinaryUpgradeAttributeVersions().
                                178                 :                :  */
                                179                 :                : Datum
   19 andres@anarazel.de        180                 :            881 : binary_upgrade_set_next_attribute_versions(PG_FUNCTION_ARGS)
                                181                 :                : {
                                182                 :            881 :     ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
                                183                 :                :     int16       elmlen;
                                184                 :                :     bool        elmbyval;
                                185                 :                :     char        elmalign;
                                186                 :                :     Datum      *elems;
                                187                 :                :     int         nelems;
                                188                 :                :     BinaryUpgradeAttributeVersions *versions;
                                189                 :                : 
                                190   [ -  +  -  - ]:            881 :     CHECK_IS_BINARY_UPGRADE;
                                191                 :                : 
                                192         [ -  + ]:            881 :     if (array_contains_nulls(array))
   19 andres@anarazel.de        193         [ #  # ]:UBC           0 :         elog(ERROR, "null attribute version value is not allowed");
                                194                 :                : 
   19 andres@anarazel.de        195                 :CBC         881 :     get_typlenbyvalalign(LSNOID, &elmlen, &elmbyval, &elmalign);
                                196                 :            881 :     deconstruct_array(array, LSNOID, elmlen, elmbyval, elmalign,
                                197                 :                :                       &elems, NULL, &nelems);
                                198                 :                : 
                                199                 :                :     /*
                                200                 :                :      * The array has to outlive this statement, as it is consumed by the next
                                201                 :                :      * one.  Should that statement fail, the versions stay pending and would
                                202                 :                :      * be applied to the next relation created instead.  That cannot happen in
                                203                 :                :      * practice: pg_upgrade restores with pg_restore --exit-on-error, so a
                                204                 :                :      * failed CREATE ends the restore.
                                205                 :                :      */
                                206                 :            881 :     versions = MemoryContextAlloc(TopMemoryContext,
                                207                 :            881 :                                   offsetof(BinaryUpgradeAttributeVersions, versions) +
                                208                 :                :                                   nelems * sizeof(XLogRecPtr));
                                209                 :            881 :     versions->natts = nelems;
                                210         [ +  + ]:           4258 :     for (int i = 0; i < nelems; i++)
                                211                 :           3377 :         versions->versions[i] = DatumGetLSN(elems[i]);
                                212                 :                : 
                                213         [ -  + ]:            881 :     if (binary_upgrade_next_attribute_versions != NULL)
   19 andres@anarazel.de        214                 :UBC           0 :         pfree(binary_upgrade_next_attribute_versions);
   19 andres@anarazel.de        215                 :CBC         881 :     binary_upgrade_next_attribute_versions = versions;
                                216                 :                : 
                                217                 :            881 :     PG_RETURN_VOID();
                                218                 :                : }
                                219                 :                : 
                                220                 :                : Datum
 4281 peter_e@gmx.net           221                 :             50 : binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
                                222                 :                : {
 6044 bruce@momjian.us          223                 :             50 :     Oid         enumoid = PG_GETARG_OID(0);
                                224                 :                : 
 4478                           225   [ -  +  -  - ]:             50 :     CHECK_IS_BINARY_UPGRADE;
 5879 tgl@sss.pgh.pa.us         226                 :             50 :     binary_upgrade_next_pg_enum_oid = enumoid;
                                227                 :                : 
 6044 bruce@momjian.us          228                 :             50 :     PG_RETURN_VOID();
                                229                 :                : }
                                230                 :                : 
                                231                 :                : Datum
 4281 peter_e@gmx.net           232                 :             19 : binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS)
                                233                 :                : {
 5804 bruce@momjian.us          234                 :             19 :     Oid         authoid = PG_GETARG_OID(0);
                                235                 :                : 
 4478                           236   [ -  +  -  - ]:             19 :     CHECK_IS_BINARY_UPGRADE;
 5804                           237                 :             19 :     binary_upgrade_next_pg_authid_oid = authoid;
                                238                 :             19 :     PG_RETURN_VOID();
                                239                 :                : }
                                240                 :                : 
                                241                 :                : Datum
 4281 peter_e@gmx.net           242                 :              5 : binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)
                                243                 :                : {
                                244                 :                :     text       *extName;
                                245                 :                :     text       *schemaName;
                                246                 :                :     bool        relocatable;
                                247                 :                :     text       *extVersion;
                                248                 :                :     Datum       extConfig;
                                249                 :                :     Datum       extCondition;
                                250                 :                :     List       *requiredExtensions;
                                251                 :                : 
 4478 bruce@momjian.us          252   [ -  +  -  - ]:              5 :     CHECK_IS_BINARY_UPGRADE;
                                253                 :                : 
                                254                 :                :     /* We must check these things before dereferencing the arguments */
 3982 tgl@sss.pgh.pa.us         255         [ +  - ]:              5 :     if (PG_ARGISNULL(0) ||
                                256         [ +  - ]:              5 :         PG_ARGISNULL(1) ||
                                257         [ +  - ]:              5 :         PG_ARGISNULL(2) ||
                                258         [ -  + ]:              5 :         PG_ARGISNULL(3))
 3982 tgl@sss.pgh.pa.us         259         [ #  # ]:UBC           0 :         elog(ERROR, "null argument to binary_upgrade_create_empty_extension is not allowed");
                                260                 :                : 
 3982 tgl@sss.pgh.pa.us         261                 :CBC           5 :     extName = PG_GETARG_TEXT_PP(0);
                                262                 :              5 :     schemaName = PG_GETARG_TEXT_PP(1);
                                263                 :              5 :     relocatable = PG_GETARG_BOOL(2);
                                264                 :              5 :     extVersion = PG_GETARG_TEXT_PP(3);
                                265                 :                : 
 5771                           266         [ +  - ]:              5 :     if (PG_ARGISNULL(4))
                                267                 :              5 :         extConfig = PointerGetDatum(NULL);
                                268                 :                :     else
 5771 tgl@sss.pgh.pa.us         269                 :UBC           0 :         extConfig = PG_GETARG_DATUM(4);
                                270                 :                : 
 5771 tgl@sss.pgh.pa.us         271         [ +  - ]:CBC           5 :     if (PG_ARGISNULL(5))
                                272                 :              5 :         extCondition = PointerGetDatum(NULL);
                                273                 :                :     else
 5771 tgl@sss.pgh.pa.us         274                 :UBC           0 :         extCondition = PG_GETARG_DATUM(5);
                                275                 :                : 
 5771 tgl@sss.pgh.pa.us         276                 :CBC           5 :     requiredExtensions = NIL;
                                277         [ +  - ]:              5 :     if (!PG_ARGISNULL(6))
                                278                 :                :     {
                                279                 :              5 :         ArrayType  *textArray = PG_GETARG_ARRAYTYPE_P(6);
                                280                 :                :         Datum      *textDatums;
                                281                 :                :         int         ndatums;
                                282                 :                :         int         i;
                                283                 :                : 
 1611 peter@eisentraut.org      284                 :              5 :         deconstruct_array_builtin(textArray, TEXTOID, &textDatums, NULL, &ndatums);
 5771 tgl@sss.pgh.pa.us         285         [ -  + ]:              5 :         for (i = 0; i < ndatums; i++)
                                286                 :                :         {
   81 peter@eisentraut.org      287                 :UNC           0 :             char       *reqExtName = TextDatumGetCString(textDatums[i]);
                                288                 :              0 :             Oid         reqExtOid = get_extension_oid(reqExtName, false);
                                289                 :                : 
                                290                 :              0 :             requiredExtensions = lappend_oid(requiredExtensions, reqExtOid);
                                291                 :                :         }
                                292                 :                :     }
                                293                 :                : 
 5771 tgl@sss.pgh.pa.us         294                 :CBC           5 :     InsertExtensionTuple(text_to_cstring(extName),
                                295                 :                :                          GetUserId(),
 3447                           296                 :              5 :                          get_namespace_oid(text_to_cstring(schemaName), false),
                                297                 :                :                          relocatable,
 5769                           298                 :              5 :                          text_to_cstring(extVersion),
                                299                 :                :                          extConfig,
                                300                 :                :                          extCondition,
                                301                 :                :                          requiredExtensions);
                                302                 :                : 
 5771                           303                 :              5 :     PG_RETURN_VOID();
                                304                 :                : }
                                305                 :                : 
                                306                 :                : Datum
 3888 sfrost@snowman.net        307                 :UBC           0 : binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS)
                                308                 :                : {
                                309                 :              0 :     bool        record_init_privs = PG_GETARG_BOOL(0);
                                310                 :                : 
                                311   [ #  #  #  # ]:              0 :     CHECK_IS_BINARY_UPGRADE;
                                312                 :              0 :     binary_upgrade_record_init_privs = record_init_privs;
                                313                 :                : 
                                314                 :              0 :     PG_RETURN_VOID();
                                315                 :                : }
                                316                 :                : 
                                317                 :                : Datum
 3081 andrew@dunslane.net       318                 :CBC           3 : binary_upgrade_set_missing_value(PG_FUNCTION_ARGS)
                                319                 :                : {
                                320                 :              3 :     Oid         table_id = PG_GETARG_OID(0);
                                321                 :              3 :     text       *attname = PG_GETARG_TEXT_P(1);
                                322                 :              3 :     text       *value = PG_GETARG_TEXT_P(2);
                                323                 :              3 :     char       *cattname = text_to_cstring(attname);
                                324                 :              3 :     char       *cvalue = text_to_cstring(value);
                                325                 :                : 
                                326   [ -  +  -  - ]:              3 :     CHECK_IS_BINARY_UPGRADE;
                                327                 :              3 :     SetAttrMissing(table_id, cattname, cvalue);
                                328                 :                : 
                                329                 :              3 :     PG_RETURN_VOID();
                                330                 :                : }
                                331                 :                : 
                                332                 :                : /*
                                333                 :                :  * Verify the given slot has already consumed all the WAL changes.
                                334                 :                :  *
                                335                 :                :  * Returns true if there are no decodable WAL records after the
                                336                 :                :  * confirmed_flush_lsn. Otherwise false.
                                337                 :                :  *
                                338                 :                :  * This is a special purpose function to ensure that the given slot can be
                                339                 :                :  * upgraded without data loss.
                                340                 :                :  */
                                341                 :                : Datum
  297 msawada@postgresql.o      342                 :              4 : binary_upgrade_check_logical_slot_pending_wal(PG_FUNCTION_ARGS)
                                343                 :                : {
                                344                 :                :     Name        slot_name;
                                345                 :                :     XLogRecPtr  end_of_wal;
                                346                 :                :     XLogRecPtr  scan_cutoff_lsn;
                                347                 :                :     XLogRecPtr  last_pending_wal;
                                348                 :                : 
 1129 akapila@postgresql.o      349   [ -  +  -  - ]:              4 :     CHECK_IS_BINARY_UPGRADE;
                                350                 :                : 
                                351                 :                :     /*
                                352                 :                :      * Binary upgrades only allowed super-user connections so we must have
                                353                 :                :      * permission to use replication slots.
                                354                 :                :      */
 1087                           355         [ -  + ]:              4 :     Assert(has_rolreplication(GetUserId()));
                                356                 :                : 
 1129                           357                 :              4 :     slot_name = PG_GETARG_NAME(0);
  297 msawada@postgresql.o      358                 :              4 :     scan_cutoff_lsn = PG_GETARG_LSN(1);
                                359                 :                : 
                                360                 :                :     /* Acquire the given slot */
  666 akapila@postgresql.o      361                 :              4 :     ReplicationSlotAcquire(NameStr(*slot_name), true, true);
                                362                 :                : 
 1129                           363         [ -  + ]:              4 :     Assert(SlotIsLogical(MyReplicationSlot));
                                364                 :                : 
                                365                 :                :     /* Slots must be valid as otherwise we won't be able to scan the WAL */
                                366         [ -  + ]:              4 :     Assert(MyReplicationSlot->data.invalidated == RS_INVAL_NONE);
                                367                 :                : 
                                368                 :              4 :     end_of_wal = GetFlushRecPtr(NULL);
  297 msawada@postgresql.o      369                 :              4 :     last_pending_wal = LogicalReplicationSlotCheckPendingWal(end_of_wal,
                                370                 :                :                                                              scan_cutoff_lsn);
                                371                 :                : 
                                372                 :                :     /* Clean up */
 1129 akapila@postgresql.o      373                 :              4 :     ReplicationSlotRelease();
                                374                 :                : 
  297 msawada@postgresql.o      375         [ +  + ]:              4 :     if (XLogRecPtrIsValid(last_pending_wal))
                                376                 :              1 :         PG_RETURN_LSN(last_pending_wal);
                                377                 :                :     else
                                378                 :              3 :         PG_RETURN_NULL();
                                379                 :                : }
                                380                 :                : 
                                381                 :                : /*
                                382                 :                :  * binary_upgrade_add_sub_rel_state
                                383                 :                :  *
                                384                 :                :  * Add the relation with the specified relation state to pg_subscription_rel
                                385                 :                :  * catalog.
                                386                 :                :  */
                                387                 :                : Datum
 1061 akapila@postgresql.o      388                 :              3 : binary_upgrade_add_sub_rel_state(PG_FUNCTION_ARGS)
                                389                 :                : {
                                390                 :                :     Relation    subrel;
                                391                 :                :     Relation    rel;
                                392                 :                :     Oid         subid;
                                393                 :                :     char       *subname;
                                394                 :                :     Oid         relid;
                                395                 :                :     char        relstate;
                                396                 :                :     XLogRecPtr  sublsn;
                                397                 :                : 
                                398   [ -  +  -  - ]:              3 :     CHECK_IS_BINARY_UPGRADE;
                                399                 :                : 
                                400                 :                :     /* We must check these things before dereferencing the arguments */
                                401   [ +  -  +  -  :              3 :     if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
                                              -  + ]
 1061 akapila@postgresql.o      402         [ #  # ]:UBC           0 :         elog(ERROR, "null argument to binary_upgrade_add_sub_rel_state is not allowed");
                                403                 :                : 
 1061 akapila@postgresql.o      404                 :CBC           3 :     subname = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                405                 :              3 :     relid = PG_GETARG_OID(1);
                                406                 :              3 :     relstate = PG_GETARG_CHAR(2);
                                407         [ +  + ]:              3 :     sublsn = PG_ARGISNULL(3) ? InvalidXLogRecPtr : PG_GETARG_LSN(3);
                                408                 :                : 
                                409                 :              3 :     subrel = table_open(SubscriptionRelationId, RowExclusiveLock);
                                410                 :              3 :     subid = get_subscription_oid(subname, false);
                                411                 :              3 :     rel = relation_open(relid, AccessShareLock);
                                412                 :                : 
                                413                 :                :     /*
                                414                 :                :      * Since there are no concurrent ALTER/DROP SUBSCRIPTION commands during
                                415                 :                :      * the upgrade process, and the apply worker (which builds cache based on
                                416                 :                :      * the subscription catalog) is not running, the locks can be released
                                417                 :                :      * immediately.
                                418                 :                :      */
                                419                 :              3 :     AddSubscriptionRelState(subid, relid, relstate, sublsn, false);
                                420                 :              3 :     relation_close(rel, AccessShareLock);
                                421                 :              3 :     table_close(subrel, RowExclusiveLock);
                                422                 :                : 
                                423                 :              3 :     PG_RETURN_VOID();
                                424                 :                : }
                                425                 :                : 
                                426                 :                : /*
                                427                 :                :  * binary_upgrade_replorigin_advance
                                428                 :                :  *
                                429                 :                :  * Update the remote_lsn for the subscriber's replication origin.
                                430                 :                :  */
                                431                 :                : Datum
                                432                 :              2 : binary_upgrade_replorigin_advance(PG_FUNCTION_ARGS)
                                433                 :                : {
                                434                 :                :     Relation    rel;
                                435                 :                :     Oid         subid;
                                436                 :                :     char       *subname;
                                437                 :                :     char        originname[NAMEDATALEN];
                                438                 :                :     ReplOriginId node;
                                439                 :                :     XLogRecPtr  remote_commit;
                                440                 :                : 
                                441   [ -  +  -  - ]:              2 :     CHECK_IS_BINARY_UPGRADE;
                                442                 :                : 
                                443                 :                :     /*
                                444                 :                :      * We must ensure a non-NULL subscription name before dereferencing the
                                445                 :                :      * arguments.
                                446                 :                :      */
                                447         [ -  + ]:              2 :     if (PG_ARGISNULL(0))
 1061 akapila@postgresql.o      448         [ #  # ]:UBC           0 :         elog(ERROR, "null argument to binary_upgrade_replorigin_advance is not allowed");
                                449                 :                : 
 1061 akapila@postgresql.o      450                 :CBC           2 :     subname = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                451         [ +  - ]:              2 :     remote_commit = PG_ARGISNULL(1) ? InvalidXLogRecPtr : PG_GETARG_LSN(1);
                                452                 :                : 
                                453                 :              2 :     rel = table_open(SubscriptionRelationId, RowExclusiveLock);
                                454                 :              2 :     subid = get_subscription_oid(subname, false);
                                455                 :                : 
                                456                 :              2 :     ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, sizeof(originname));
                                457                 :                : 
                                458                 :                :     /* Lock to prevent the replication origin from vanishing */
                                459                 :              2 :     LockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
                                460                 :              2 :     node = replorigin_by_name(originname, false);
                                461                 :                : 
                                462                 :                :     /*
                                463                 :                :      * The server will be stopped after setting up the objects in the new
                                464                 :                :      * cluster and the origins will be flushed during the shutdown checkpoint.
                                465                 :                :      * This will ensure that the latest LSN values for origin will be
                                466                 :                :      * available after the upgrade.
                                467                 :                :      */
                                468                 :              2 :     replorigin_advance(node, remote_commit, InvalidXLogRecPtr,
                                469                 :                :                        false /* backward */ ,
                                470                 :                :                        false /* WAL log */ );
                                471                 :                : 
                                472                 :              2 :     UnlockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
                                473                 :              2 :     table_close(rel, RowExclusiveLock);
                                474                 :                : 
                                475                 :              2 :     PG_RETURN_VOID();
                                476                 :                : }
                                477                 :                : 
                                478                 :                : /*
                                479                 :                :  * binary_upgrade_create_conflict_detection_slot
                                480                 :                :  *
                                481                 :                :  * Create a replication slot to retain information necessary for conflict
                                482                 :                :  * detection such as dead tuples, commit timestamps, and origins.
                                483                 :                :  */
                                484                 :                : Datum
  493                           485                 :              1 : binary_upgrade_create_conflict_detection_slot(PG_FUNCTION_ARGS)
                                486                 :                : {
                                487   [ -  +  -  - ]:              1 :     CHECK_IS_BINARY_UPGRADE;
                                488                 :                : 
                                489                 :              1 :     CreateConflictDetectionSlot();
                                490                 :                : 
                                491                 :              1 :     ReplicationSlotRelease();
                                492                 :                : 
                                493                 :              1 :     PG_RETURN_VOID();
                                494                 :                : }
        

Generated by: LCOV version 2.0-1