LCOV - differential code coverage report
Current view: top level - src/backend/catalog - pg_subscription.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 95.0 % 241 229 7 5 50 179 2 12
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 13 13 7 6 3
Baseline: lcov-20260315-024220-baseline Branches: 70.2 % 114 80 17 17 35 45 5 7
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 90.9 % 11 10 1 10
(30,360] days: 88.7 % 53 47 6 40 7
(360..) days: 97.2 % 177 172 5 172
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 3 3 3
(360..) days: 100.0 % 9 9 3 6
Branch coverage date bins:
(7,30] days: 62.5 % 8 5 3 5
(30,360] days: 68.0 % 50 34 14 2 30 4
(360..) days: 73.2 % 56 41 15 41

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_subscription.c
                                  4                 :                :  *      replication subscriptions
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  * IDENTIFICATION
                                 10                 :                :  *      src/backend/catalog/pg_subscription.c
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : 
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/genam.h"
                                 18                 :                : #include "access/heapam.h"
                                 19                 :                : #include "access/htup_details.h"
                                 20                 :                : #include "access/tableam.h"
                                 21                 :                : #include "catalog/indexing.h"
                                 22                 :                : #include "catalog/pg_foreign_server.h"
                                 23                 :                : #include "catalog/pg_subscription.h"
                                 24                 :                : #include "catalog/pg_subscription_rel.h"
                                 25                 :                : #include "catalog/pg_type.h"
                                 26                 :                : #include "foreign/foreign.h"
                                 27                 :                : #include "miscadmin.h"
                                 28                 :                : #include "storage/lmgr.h"
                                 29                 :                : #include "utils/acl.h"
                                 30                 :                : #include "utils/array.h"
                                 31                 :                : #include "utils/builtins.h"
                                 32                 :                : #include "utils/fmgroids.h"
                                 33                 :                : #include "utils/lsyscache.h"
                                 34                 :                : #include "utils/pg_lsn.h"
                                 35                 :                : #include "utils/rel.h"
                                 36                 :                : #include "utils/syscache.h"
                                 37                 :                : 
                                 38                 :                : static List *textarray_to_stringlist(ArrayType *textarray);
                                 39                 :                : 
                                 40                 :                : /*
                                 41                 :                :  * Add a comma-separated list of publication names to the 'dest' string.
                                 42                 :                :  */
                                 43                 :                : void
  506 michael@paquier.xyz        44                 :CBC         536 : GetPublicationsStr(List *publications, StringInfo dest, bool quote_literal)
                                 45                 :                : {
                                 46                 :                :     ListCell   *lc;
                                 47                 :            536 :     bool        first = true;
                                 48                 :                : 
                                 49         [ -  + ]:            536 :     Assert(publications != NIL);
                                 50                 :                : 
                                 51   [ +  -  +  +  :           1380 :     foreach(lc, publications)
                                              +  + ]
                                 52                 :                :     {
                                 53                 :            844 :         char       *pubname = strVal(lfirst(lc));
                                 54                 :                : 
                                 55         [ +  + ]:            844 :         if (first)
                                 56                 :            536 :             first = false;
                                 57                 :                :         else
                                 58                 :            308 :             appendStringInfoString(dest, ", ");
                                 59                 :                : 
                                 60         [ +  + ]:            844 :         if (quote_literal)
                                 61                 :            834 :             appendStringInfoString(dest, quote_literal_cstr(pubname));
                                 62                 :                :         else
                                 63                 :                :         {
                                 64                 :             10 :             appendStringInfoChar(dest, '"');
                                 65                 :             10 :             appendStringInfoString(dest, pubname);
                                 66                 :             10 :             appendStringInfoChar(dest, '"');
                                 67                 :                :         }
                                 68                 :                :     }
                                 69                 :            536 : }
                                 70                 :                : 
                                 71                 :                : /*
                                 72                 :                :  * Fetch the subscription from the syscache.
                                 73                 :                :  */
                                 74                 :                : Subscription *
    9 jdavis@postgresql.or       75                 :GNC         932 : GetSubscription(Oid subid, bool missing_ok, bool aclcheck)
                                 76                 :                : {
                                 77                 :                :     HeapTuple   tup;
                                 78                 :                :     Subscription *sub;
                                 79                 :                :     Form_pg_subscription subform;
                                 80                 :                :     Datum       datum;
                                 81                 :                :     bool        isnull;
                                 82                 :                : 
 3342 peter_e@gmx.net            83                 :CBC         932 :     tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
                                 84                 :                : 
                                 85         [ +  + ]:            932 :     if (!HeapTupleIsValid(tup))
                                 86                 :                :     {
                                 87         [ +  - ]:             64 :         if (missing_ok)
                                 88                 :             64 :             return NULL;
                                 89                 :                : 
 3342 peter_e@gmx.net            90         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for subscription %u", subid);
                                 91                 :                :     }
                                 92                 :                : 
 3342 peter_e@gmx.net            93                 :CBC         868 :     subform = (Form_pg_subscription) GETSTRUCT(tup);
                                 94                 :                : 
   95 michael@paquier.xyz        95                 :GNC         868 :     sub = palloc_object(Subscription);
 3342 peter_e@gmx.net            96                 :CBC         868 :     sub->oid = subid;
                                 97                 :            868 :     sub->dbid = subform->subdbid;
 1438 akapila@postgresql.o       98                 :            868 :     sub->skiplsn = subform->subskiplsn;
 3342 peter_e@gmx.net            99                 :            868 :     sub->name = pstrdup(NameStr(subform->subname));
                                100                 :            868 :     sub->owner = subform->subowner;
                                101                 :            868 :     sub->enabled = subform->subenabled;
 2066 tgl@sss.pgh.pa.us         102                 :            868 :     sub->binary = subform->subbinary;
 2019 akapila@postgresql.o      103                 :            868 :     sub->stream = subform->substream;
 1705                           104                 :            868 :     sub->twophasestate = subform->subtwophasestate;
 1462                           105                 :            868 :     sub->disableonerr = subform->subdisableonerr;
 1081 rhaas@postgresql.org      106                 :            868 :     sub->passwordrequired = subform->subpasswordrequired;
 1076                           107                 :            868 :     sub->runasowner = subform->subrunasowner;
  775 akapila@postgresql.o      108                 :            868 :     sub->failover = subform->subfailover;
  235 akapila@postgresql.o      109                 :GNC         868 :     sub->retaindeadtuples = subform->subretaindeadtuples;
  194                           110                 :            868 :     sub->maxretention = subform->submaxretention;
                                111                 :            868 :     sub->retentionactive = subform->subretentionactive;
                                112                 :                : 
                                113                 :                :     /* Get conninfo */
    9 jdavis@postgresql.or      114         [ +  + ]:            868 :     if (OidIsValid(subform->subserver))
                                115                 :                :     {
                                116                 :                :         AclResult   aclresult;
                                117                 :                : 
                                118                 :                :         /* recheck ACL if requested */
                                119         [ +  + ]:              6 :         if (aclcheck)
                                120                 :                :         {
                                121                 :              3 :             aclresult = object_aclcheck(ForeignServerRelationId,
                                122                 :                :                                         subform->subserver,
                                123                 :                :                                         subform->subowner, ACL_USAGE);
                                124                 :                : 
                                125         [ -  + ]:              3 :             if (aclresult != ACLCHECK_OK)
    9 jdavis@postgresql.or      126         [ #  # ]:UNC           0 :                 ereport(ERROR,
                                127                 :                :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                                128                 :                :                          errmsg("subscription owner \"%s\" does not have permission on foreign server \"%s\"",
                                129                 :                :                                 GetUserNameFromId(subform->subowner, false),
                                130                 :                :                                 ForeignServerName(subform->subserver))));
                                131                 :                :         }
                                132                 :                : 
    9 jdavis@postgresql.or      133                 :GNC           6 :         sub->conninfo = ForeignServerConnectionString(subform->subowner,
                                134                 :                :                                                       subform->subserver);
                                135                 :                :     }
                                136                 :                :     else
                                137                 :                :     {
                                138                 :            862 :         datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
                                139                 :                :                                        tup,
                                140                 :                :                                        Anum_pg_subscription_subconninfo);
                                141                 :            862 :         sub->conninfo = TextDatumGetCString(datum);
                                142                 :                :     }
                                143                 :                : 
                                144                 :                :     /* Get slotname */
 3342 peter_e@gmx.net           145                 :CBC         868 :     datum = SysCacheGetAttr(SUBSCRIPTIONOID,
                                146                 :                :                             tup,
                                147                 :                :                             Anum_pg_subscription_subslotname,
                                148                 :                :                             &isnull);
 3232                           149         [ +  + ]:            868 :     if (!isnull)
                                150                 :            835 :         sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
                                151                 :                :     else
                                152                 :             33 :         sub->slotname = NULL;
                                153                 :                : 
                                154                 :                :     /* Get synccommit */
 1086 dgustafsson@postgres      155                 :            868 :     datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
                                156                 :                :                                    tup,
                                157                 :                :                                    Anum_pg_subscription_subsynccommit);
 3257 peter_e@gmx.net           158                 :            868 :     sub->synccommit = TextDatumGetCString(datum);
                                159                 :                : 
                                160                 :                :     /* Get walrcvtimeout */
   23 fujii@postgresql.org      161                 :GNC         868 :     datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
                                162                 :                :                                    tup,
                                163                 :                :                                    Anum_pg_subscription_subwalrcvtimeout);
                                164                 :            868 :     sub->walrcvtimeout = TextDatumGetCString(datum);
                                165                 :                : 
                                166                 :                :     /* Get publications */
 1086 dgustafsson@postgres      167                 :CBC         868 :     datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
                                168                 :                :                                    tup,
                                169                 :                :                                    Anum_pg_subscription_subpublications);
 3342 peter_e@gmx.net           170                 :            868 :     sub->publications = textarray_to_stringlist(DatumGetArrayTypeP(datum));
                                171                 :                : 
                                172                 :                :     /* Get origin */
 1086 dgustafsson@postgres      173                 :            868 :     datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
                                174                 :                :                                    tup,
                                175                 :                :                                    Anum_pg_subscription_suborigin);
 1333 akapila@postgresql.o      176                 :            868 :     sub->origin = TextDatumGetCString(datum);
                                177                 :                : 
                                178                 :                :     /* Is the subscription owner a superuser? */
  880                           179                 :            868 :     sub->ownersuperuser = superuser_arg(sub->owner);
                                180                 :                : 
 3342 peter_e@gmx.net           181                 :            868 :     ReleaseSysCache(tup);
                                182                 :                : 
                                183                 :            868 :     return sub;
                                184                 :                : }
                                185                 :                : 
                                186                 :                : /*
                                187                 :                :  * Return number of subscriptions defined in given database.
                                188                 :                :  * Used by dropdb() to check if database can indeed be dropped.
                                189                 :                :  */
                                190                 :                : int
                                191                 :             46 : CountDBSubscriptions(Oid dbid)
                                192                 :                : {
 3224 bruce@momjian.us          193                 :             46 :     int         nsubs = 0;
                                194                 :                :     Relation    rel;
                                195                 :                :     ScanKeyData scankey;
                                196                 :                :     SysScanDesc scan;
                                197                 :                :     HeapTuple   tup;
                                198                 :                : 
 2610 andres@anarazel.de        199                 :             46 :     rel = table_open(SubscriptionRelationId, RowExclusiveLock);
                                200                 :                : 
 3342 peter_e@gmx.net           201                 :             46 :     ScanKeyInit(&scankey,
                                202                 :                :                 Anum_pg_subscription_subdbid,
                                203                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                204                 :                :                 ObjectIdGetDatum(dbid));
                                205                 :                : 
                                206                 :             46 :     scan = systable_beginscan(rel, InvalidOid, false,
                                207                 :                :                               NULL, 1, &scankey);
                                208                 :                : 
                                209         [ -  + ]:             46 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
 3342 peter_e@gmx.net           210                 :UBC           0 :         nsubs++;
                                211                 :                : 
 3342 peter_e@gmx.net           212                 :CBC          46 :     systable_endscan(scan);
                                213                 :                : 
 2610 andres@anarazel.de        214                 :             46 :     table_close(rel, NoLock);
                                215                 :                : 
 3342 peter_e@gmx.net           216                 :             46 :     return nsubs;
                                217                 :                : }
                                218                 :                : 
                                219                 :                : /*
                                220                 :                :  * Free memory allocated by subscription struct.
                                221                 :                :  */
                                222                 :                : void
                                223                 :             47 : FreeSubscription(Subscription *sub)
                                224                 :                : {
                                225                 :             47 :     pfree(sub->name);
                                226                 :             47 :     pfree(sub->conninfo);
 3232                           227         [ +  - ]:             47 :     if (sub->slotname)
                                228                 :             47 :         pfree(sub->slotname);
 3342                           229                 :             47 :     list_free_deep(sub->publications);
                                230                 :             47 :     pfree(sub);
                                231                 :             47 : }
                                232                 :                : 
                                233                 :                : /*
                                234                 :                :  * Disable the given subscription.
                                235                 :                :  */
                                236                 :                : void
 1462 akapila@postgresql.o      237                 :              4 : DisableSubscription(Oid subid)
                                238                 :                : {
                                239                 :                :     Relation    rel;
                                240                 :                :     bool        nulls[Natts_pg_subscription];
                                241                 :                :     bool        replaces[Natts_pg_subscription];
                                242                 :                :     Datum       values[Natts_pg_subscription];
                                243                 :                :     HeapTuple   tup;
                                244                 :                : 
                                245                 :                :     /* Look up the subscription in the catalog */
                                246                 :              4 :     rel = table_open(SubscriptionRelationId, RowExclusiveLock);
                                247                 :              4 :     tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
                                248                 :                : 
                                249         [ -  + ]:              4 :     if (!HeapTupleIsValid(tup))
 1462 akapila@postgresql.o      250         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for subscription %u", subid);
                                251                 :                : 
 1462 akapila@postgresql.o      252                 :CBC           4 :     LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
                                253                 :                : 
                                254                 :                :     /* Form a new tuple. */
                                255                 :              4 :     memset(values, 0, sizeof(values));
                                256                 :              4 :     memset(nulls, false, sizeof(nulls));
                                257                 :              4 :     memset(replaces, false, sizeof(replaces));
                                258                 :                : 
                                259                 :                :     /* Set the subscription to disabled. */
                                260                 :              4 :     values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(false);
                                261                 :              4 :     replaces[Anum_pg_subscription_subenabled - 1] = true;
                                262                 :                : 
                                263                 :                :     /* Update the catalog */
                                264                 :              4 :     tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
                                265                 :                :                             replaces);
                                266                 :              4 :     CatalogTupleUpdate(rel, &tup->t_self, tup);
                                267                 :              4 :     heap_freetuple(tup);
                                268                 :                : 
                                269                 :              4 :     table_close(rel, NoLock);
                                270                 :              4 : }
                                271                 :                : 
                                272                 :                : /*
                                273                 :                :  * Convert text array to list of strings.
                                274                 :                :  *
                                275                 :                :  * Note: the resulting list of strings is pallocated here.
                                276                 :                :  */
                                277                 :                : static List *
 3342 peter_e@gmx.net           278                 :            868 : textarray_to_stringlist(ArrayType *textarray)
                                279                 :                : {
                                280                 :                :     Datum      *elems;
                                281                 :                :     int         nelems,
                                282                 :                :                 i;
 3224 bruce@momjian.us          283                 :            868 :     List       *res = NIL;
                                284                 :                : 
 1353 peter@eisentraut.org      285                 :            868 :     deconstruct_array_builtin(textarray, TEXTOID, &elems, NULL, &nelems);
                                286                 :                : 
 3342 peter_e@gmx.net           287         [ -  + ]:            868 :     if (nelems == 0)
 3342 peter_e@gmx.net           288                 :UBC           0 :         return NIL;
                                289                 :                : 
 3342 peter_e@gmx.net           290         [ +  + ]:CBC        2130 :     for (i = 0; i < nelems; i++)
 3257                           291                 :           1262 :         res = lappend(res, makeString(TextDatumGetCString(elems[i])));
                                292                 :                : 
 3342                           293                 :            868 :     return res;
                                294                 :                : }
                                295                 :                : 
                                296                 :                : /*
                                297                 :                :  * Add new state record for a subscription table.
                                298                 :                :  *
                                299                 :                :  * If retain_lock is true, then don't release the locks taken in this function.
                                300                 :                :  * We normally release the locks at the end of transaction but in binary-upgrade
                                301                 :                :  * mode, we expect to release those immediately.
                                302                 :                :  */
                                303                 :                : void
 2900                           304                 :            223 : AddSubscriptionRelState(Oid subid, Oid relid, char state,
                                305                 :                :                         XLogRecPtr sublsn, bool retain_lock)
                                306                 :                : {
                                307                 :                :     Relation    rel;
                                308                 :                :     HeapTuple   tup;
                                309                 :                :     bool        nulls[Natts_pg_subscription_rel];
                                310                 :                :     Datum       values[Natts_pg_subscription_rel];
                                311                 :                : 
 3177                           312                 :            223 :     LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
                                313                 :                : 
 2610 andres@anarazel.de        314                 :            223 :     rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
                                315                 :                : 
                                316                 :                :     /* Try finding existing mapping. */
 3279 peter_e@gmx.net           317                 :            223 :     tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
                                318                 :                :                               ObjectIdGetDatum(relid),
                                319                 :                :                               ObjectIdGetDatum(subid));
 2900                           320         [ -  + ]:            223 :     if (HeapTupleIsValid(tup))
  143 akapila@postgresql.o      321         [ #  # ]:UNC           0 :         elog(ERROR, "subscription relation %u in subscription %u already exists",
                                322                 :                :              relid, subid);
                                323                 :                : 
                                324                 :                :     /* Form the tuple. */
 2900 peter_e@gmx.net           325                 :CBC         223 :     memset(values, 0, sizeof(values));
                                326                 :            223 :     memset(nulls, false, sizeof(nulls));
                                327                 :            223 :     values[Anum_pg_subscription_rel_srsubid - 1] = ObjectIdGetDatum(subid);
                                328                 :            223 :     values[Anum_pg_subscription_rel_srrelid - 1] = ObjectIdGetDatum(relid);
                                329                 :            223 :     values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
  129 alvherre@kurilemu.de      330         [ +  + ]:GNC         223 :     if (XLogRecPtrIsValid(sublsn))
 2900 peter_e@gmx.net           331                 :CBC           2 :         values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
                                332                 :                :     else
                                333                 :            221 :         nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
                                334                 :                : 
                                335                 :            223 :     tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
                                336                 :                : 
                                337                 :                :     /* Insert tuple into catalog. */
 2672 andres@anarazel.de        338                 :            223 :     CatalogTupleInsert(rel, tup);
                                339                 :                : 
 2900 peter_e@gmx.net           340                 :            223 :     heap_freetuple(tup);
                                341                 :                : 
                                342                 :                :     /* Cleanup. */
  803 akapila@postgresql.o      343         [ +  + ]:            223 :     if (retain_lock)
                                344                 :                :     {
                                345                 :            220 :         table_close(rel, NoLock);
                                346                 :                :     }
                                347                 :                :     else
                                348                 :                :     {
                                349                 :              3 :         table_close(rel, RowExclusiveLock);
                                350                 :              3 :         UnlockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
                                351                 :                :     }
 2900 peter_e@gmx.net           352                 :            223 : }
                                353                 :                : 
                                354                 :                : /*
                                355                 :                :  * Update the state of a subscription table.
                                356                 :                :  */
                                357                 :                : void
                                358                 :            793 : UpdateSubscriptionRelState(Oid subid, Oid relid, char state,
                                359                 :                :                            XLogRecPtr sublsn, bool already_locked)
                                360                 :                : {
                                361                 :                :     Relation    rel;
                                362                 :                :     HeapTuple   tup;
                                363                 :                :     bool        nulls[Natts_pg_subscription_rel];
                                364                 :                :     Datum       values[Natts_pg_subscription_rel];
                                365                 :                :     bool        replaces[Natts_pg_subscription_rel];
                                366                 :                : 
  226 akapila@postgresql.o      367         [ +  + ]:            793 :     if (already_locked)
                                368                 :                :     {
                                369                 :                : #ifdef USE_ASSERT_CHECKING
                                370                 :                :         LOCKTAG     tag;
                                371                 :                : 
                                372         [ -  + ]:            188 :         Assert(CheckRelationOidLockedByMe(SubscriptionRelRelationId,
                                373                 :                :                                           RowExclusiveLock, true));
                                374                 :            188 :         SET_LOCKTAG_OBJECT(tag, InvalidOid, SubscriptionRelationId, subid, 0);
                                375         [ -  + ]:            188 :         Assert(LockHeldByMe(&tag, AccessShareLock, true));
                                376                 :                : #endif
                                377                 :                : 
                                378                 :            188 :         rel = table_open(SubscriptionRelRelationId, NoLock);
                                379                 :                :     }
                                380                 :                :     else
                                381                 :                :     {
                                382                 :            605 :         LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
                                383                 :            605 :         rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
                                384                 :                :     }
                                385                 :                : 
                                386                 :                :     /* Try finding existing mapping. */
 2900 peter_e@gmx.net           387                 :            793 :     tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
                                388                 :                :                               ObjectIdGetDatum(relid),
                                389                 :                :                               ObjectIdGetDatum(subid));
                                390         [ -  + ]:            793 :     if (!HeapTupleIsValid(tup))
  130 akapila@postgresql.o      391         [ #  # ]:UNC           0 :         elog(ERROR, "subscription relation %u in subscription %u does not exist",
                                392                 :                :              relid, subid);
                                393                 :                : 
                                394                 :                :     /* Update the tuple. */
 2900 peter_e@gmx.net           395                 :CBC         793 :     memset(values, 0, sizeof(values));
                                396                 :            793 :     memset(nulls, false, sizeof(nulls));
                                397                 :            793 :     memset(replaces, false, sizeof(replaces));
                                398                 :                : 
                                399                 :            793 :     replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
                                400                 :            793 :     values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
                                401                 :                : 
                                402                 :            793 :     replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
  129 alvherre@kurilemu.de      403         [ +  + ]:GNC         793 :     if (XLogRecPtrIsValid(sublsn))
 2900 peter_e@gmx.net           404                 :CBC         390 :         values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
                                405                 :                :     else
                                406                 :            403 :         nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
                                407                 :                : 
                                408                 :            793 :     tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
                                409                 :                :                             replaces);
                                410                 :                : 
                                411                 :                :     /* Update the catalog. */
                                412                 :            793 :     CatalogTupleUpdate(rel, &tup->t_self, tup);
                                413                 :                : 
                                414                 :                :     /* Cleanup. */
 2610 andres@anarazel.de        415                 :            793 :     table_close(rel, NoLock);
 3279 peter_e@gmx.net           416                 :            793 : }
                                417                 :                : 
                                418                 :                : /*
                                419                 :                :  * Get state of subscription table.
                                420                 :                :  *
                                421                 :                :  * Returns SUBREL_STATE_UNKNOWN when the table is not in the subscription.
                                422                 :                :  */
                                423                 :                : char
 1977 alvherre@alvh.no-ip.      424                 :           1263 : GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
                                425                 :                : {
                                426                 :                :     HeapTuple   tup;
                                427                 :                :     char        substate;
                                428                 :                :     bool        isnull;
                                429                 :                :     Datum       d;
                                430                 :                :     Relation    rel;
                                431                 :                : 
                                432                 :                :     /*
                                433                 :                :      * This is to avoid the race condition with AlterSubscription which tries
                                434                 :                :      * to remove this relstate.
                                435                 :                :      */
 1857 akapila@postgresql.o      436                 :           1263 :     rel = table_open(SubscriptionRelRelationId, AccessShareLock);
                                437                 :                : 
                                438                 :                :     /* Try finding the mapping. */
 3279 peter_e@gmx.net           439                 :           1263 :     tup = SearchSysCache2(SUBSCRIPTIONRELMAP,
                                440                 :                :                           ObjectIdGetDatum(relid),
                                441                 :                :                           ObjectIdGetDatum(subid));
                                442                 :                : 
                                443         [ +  + ]:           1263 :     if (!HeapTupleIsValid(tup))
                                444                 :                :     {
 1844 akapila@postgresql.o      445                 :             26 :         table_close(rel, AccessShareLock);
 1977 alvherre@alvh.no-ip.      446                 :             26 :         *sublsn = InvalidXLogRecPtr;
                                447                 :             26 :         return SUBREL_STATE_UNKNOWN;
                                448                 :                :     }
                                449                 :                : 
                                450                 :                :     /* Get the state. */
                                451                 :           1237 :     substate = ((Form_pg_subscription_rel) GETSTRUCT(tup))->srsubstate;
                                452                 :                : 
                                453                 :                :     /* Get the LSN */
 3279 peter_e@gmx.net           454                 :           1237 :     d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
                                455                 :                :                         Anum_pg_subscription_rel_srsublsn, &isnull);
                                456         [ +  + ]:           1237 :     if (isnull)
                                457                 :            663 :         *sublsn = InvalidXLogRecPtr;
                                458                 :                :     else
                                459                 :            574 :         *sublsn = DatumGetLSN(d);
                                460                 :                : 
                                461                 :                :     /* Cleanup */
                                462                 :           1237 :     ReleaseSysCache(tup);
                                463                 :                : 
 1857 akapila@postgresql.o      464                 :           1237 :     table_close(rel, AccessShareLock);
                                465                 :                : 
 3279 peter_e@gmx.net           466                 :           1237 :     return substate;
                                467                 :                : }
                                468                 :                : 
                                469                 :                : /*
                                470                 :                :  * Drop subscription relation mapping. These can be for a particular
                                471                 :                :  * subscription, or for a particular relation, or both.
                                472                 :                :  */
                                473                 :                : void
                                474                 :          26212 : RemoveSubscriptionRel(Oid subid, Oid relid)
                                475                 :                : {
                                476                 :                :     Relation    rel;
                                477                 :                :     TableScanDesc scan;
                                478                 :                :     ScanKeyData skey[2];
                                479                 :                :     HeapTuple   tup;
                                480                 :          26212 :     int         nkeys = 0;
                                481                 :                : 
 2610 andres@anarazel.de        482                 :          26212 :     rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
                                483                 :                : 
 3279 peter_e@gmx.net           484         [ +  + ]:          26212 :     if (OidIsValid(subid))
                                485                 :                :     {
                                486                 :            148 :         ScanKeyInit(&skey[nkeys++],
                                487                 :                :                     Anum_pg_subscription_rel_srsubid,
                                488                 :                :                     BTEqualStrategyNumber,
                                489                 :                :                     F_OIDEQ,
                                490                 :                :                     ObjectIdGetDatum(subid));
                                491                 :                :     }
                                492                 :                : 
                                493         [ +  + ]:          26212 :     if (OidIsValid(relid))
                                494                 :                :     {
                                495                 :          26085 :         ScanKeyInit(&skey[nkeys++],
                                496                 :                :                     Anum_pg_subscription_rel_srrelid,
                                497                 :                :                     BTEqualStrategyNumber,
                                498                 :                :                     F_OIDEQ,
                                499                 :                :                     ObjectIdGetDatum(relid));
                                500                 :                :     }
                                501                 :                : 
                                502                 :                :     /* Do the search and delete what we found. */
 2561 andres@anarazel.de        503                 :          26212 :     scan = table_beginscan_catalog(rel, nkeys, skey);
 3279 peter_e@gmx.net           504         [ +  + ]:          26337 :     while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
                                505                 :                :     {
                                506                 :                :         Form_pg_subscription_rel subrel;
                                507                 :                : 
 1857 akapila@postgresql.o      508                 :            125 :         subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
                                509                 :                : 
                                510                 :                :         /*
                                511                 :                :          * We don't allow to drop the relation mapping when the table
                                512                 :                :          * synchronization is in progress unless the caller updates the
                                513                 :                :          * corresponding subscription as well. This is to ensure that we don't
                                514                 :                :          * leave tablesync slots or origins in the system when the
                                515                 :                :          * corresponding table is dropped. For sequences, however, it's ok to
                                516                 :                :          * drop them since no separate slots or origins are created during
                                517                 :                :          * synchronization.
                                518                 :                :          */
  143 akapila@postgresql.o      519         [ +  + ]:GNC         125 :         if (!OidIsValid(subid) &&
                                520   [ -  +  -  - ]:             16 :             subrel->srsubstate != SUBREL_STATE_READY &&
  143 akapila@postgresql.o      521                 :UNC           0 :             get_rel_relkind(subrel->srrelid) != RELKIND_SEQUENCE)
                                522                 :                :         {
 1857 akapila@postgresql.o      523         [ #  # ]:UBC           0 :             ereport(ERROR,
                                524                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                525                 :                :                      errmsg("could not drop relation mapping for subscription \"%s\"",
                                526                 :                :                             get_subscription_name(subrel->srsubid, false)),
                                527                 :                :                      errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".",
                                528                 :                :                                get_rel_name(relid), subrel->srsubstate),
                                529                 :                : 
                                530                 :                :             /*
                                531                 :                :              * translator: first %s is a SQL ALTER command and second %s is a
                                532                 :                :              * SQL DROP command
                                533                 :                :              */
                                534                 :                :                      errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.",
                                535                 :                :                              "ALTER SUBSCRIPTION ... ENABLE",
                                536                 :                :                              "DROP SUBSCRIPTION ...")));
                                537                 :                :         }
                                538                 :                : 
 3196 tgl@sss.pgh.pa.us         539                 :CBC         125 :         CatalogTupleDelete(rel, &tup->t_self);
                                540                 :                :     }
 2561 andres@anarazel.de        541                 :          26212 :     table_endscan(scan);
                                542                 :                : 
 2610                           543                 :          26212 :     table_close(rel, RowExclusiveLock);
 3279 peter_e@gmx.net           544                 :          26212 : }
                                545                 :                : 
                                546                 :                : /*
                                547                 :                :  * Does the subscription have any tables?
                                548                 :                :  *
                                549                 :                :  * Use this function only to know true/false, and when you have no need for the
                                550                 :                :  * List returned by GetSubscriptionRelations.
                                551                 :                :  */
                                552                 :                : bool
  150 akapila@postgresql.o      553                 :GNC         275 : HasSubscriptionTables(Oid subid)
                                554                 :                : {
                                555                 :                :     Relation    rel;
                                556                 :                :     ScanKeyData skey[1];
                                557                 :                :     SysScanDesc scan;
                                558                 :                :     HeapTuple   tup;
  143                           559                 :            275 :     bool        has_subtables = false;
                                560                 :                : 
 1705 akapila@postgresql.o      561                 :CBC         275 :     rel = table_open(SubscriptionRelRelationId, AccessShareLock);
                                562                 :                : 
                                563                 :            275 :     ScanKeyInit(&skey[0],
                                564                 :                :                 Anum_pg_subscription_rel_srsubid,
                                565                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                566                 :                :                 ObjectIdGetDatum(subid));
                                567                 :                : 
                                568                 :            275 :     scan = systable_beginscan(rel, InvalidOid, false,
                                569                 :                :                               NULL, 1, skey);
                                570                 :                : 
  143 akapila@postgresql.o      571         [ +  + ]:GNC         313 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                                572                 :                :     {
                                573                 :                :         Form_pg_subscription_rel subrel;
                                574                 :                :         char        relkind;
                                575                 :                : 
                                576                 :            298 :         subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
                                577                 :            298 :         relkind = get_rel_relkind(subrel->srrelid);
                                578                 :                : 
                                579   [ +  +  +  + ]:            298 :         if (relkind == RELKIND_RELATION ||
                                580                 :                :             relkind == RELKIND_PARTITIONED_TABLE)
                                581                 :                :         {
                                582                 :            260 :             has_subtables = true;
                                583                 :            260 :             break;
                                584                 :                :         }
                                585                 :                :     }
                                586                 :                : 
                                587                 :                :     /* Cleanup */
 1705 akapila@postgresql.o      588                 :CBC         275 :     systable_endscan(scan);
                                589                 :            275 :     table_close(rel, AccessShareLock);
                                590                 :                : 
  143 akapila@postgresql.o      591                 :GNC         275 :     return has_subtables;
                                592                 :                : }
                                593                 :                : 
                                594                 :                : /*
                                595                 :                :  * Get the relations for the subscription.
                                596                 :                :  *
                                597                 :                :  * If not_ready is true, return only the relations that are not in a ready
                                598                 :                :  * state, otherwise return all the relations of the subscription.  The
                                599                 :                :  * returned list is palloc'ed in the current memory context.
                                600                 :                :  */
                                601                 :                : List *
                                602                 :           1177 : GetSubscriptionRelations(Oid subid, bool tables, bool sequences,
                                603                 :                :                          bool not_ready)
                                604                 :                : {
 3279 peter_e@gmx.net           605                 :CBC        1177 :     List       *res = NIL;
                                606                 :                :     Relation    rel;
                                607                 :                :     HeapTuple   tup;
                                608                 :           1177 :     int         nkeys = 0;
                                609                 :                :     ScanKeyData skey[2];
                                610                 :                :     SysScanDesc scan;
                                611                 :                : 
                                612                 :                :     /* One or both of 'tables' and 'sequences' must be true. */
  143 akapila@postgresql.o      613   [ +  +  -  + ]:GNC        1177 :     Assert(tables || sequences);
                                614                 :                : 
 2610 andres@anarazel.de        615                 :CBC        1177 :     rel = table_open(SubscriptionRelRelationId, AccessShareLock);
                                616                 :                : 
 3279 peter_e@gmx.net           617                 :           1177 :     ScanKeyInit(&skey[nkeys++],
                                618                 :                :                 Anum_pg_subscription_rel_srsubid,
                                619                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                620                 :                :                 ObjectIdGetDatum(subid));
                                621                 :                : 
 1327 michael@paquier.xyz       622         [ +  + ]:           1177 :     if (not_ready)
                                623                 :           1139 :         ScanKeyInit(&skey[nkeys++],
                                624                 :                :                     Anum_pg_subscription_rel_srsubstate,
                                625                 :                :                     BTEqualStrategyNumber, F_CHARNE,
                                626                 :                :                     CharGetDatum(SUBREL_STATE_READY));
                                627                 :                : 
 3279 peter_e@gmx.net           628                 :           1177 :     scan = systable_beginscan(rel, InvalidOid, false,
                                629                 :                :                               NULL, nkeys, skey);
                                630                 :                : 
                                631         [ +  + ]:           3133 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                                632                 :                :     {
                                633                 :                :         Form_pg_subscription_rel subrel;
                                634                 :                :         SubscriptionRelState *relstate;
                                635                 :                :         Datum       d;
                                636                 :                :         bool        isnull;
                                637                 :                :         char        relkind;
                                638                 :                : 
                                639                 :           1956 :         subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
                                640                 :                : 
                                641                 :                :         /* Relation is either a sequence or a table */
  143 akapila@postgresql.o      642                 :GNC        1956 :         relkind = get_rel_relkind(subrel->srrelid);
                                643   [ +  +  +  +  :           1956 :         Assert(relkind == RELKIND_SEQUENCE || relkind == RELKIND_RELATION ||
                                              -  + ]
                                644                 :                :                relkind == RELKIND_PARTITIONED_TABLE);
                                645                 :                : 
                                646                 :                :         /* Skip sequences if they were not requested */
                                647   [ +  +  -  + ]:           1956 :         if ((relkind == RELKIND_SEQUENCE) && !sequences)
  143 akapila@postgresql.o      648                 :UNC           0 :             continue;
                                649                 :                : 
                                650                 :                :         /* Skip tables if they were not requested */
  143 akapila@postgresql.o      651   [ +  +  +  + ]:GNC        1956 :         if ((relkind == RELKIND_RELATION ||
                                652         [ -  + ]:           1928 :              relkind == RELKIND_PARTITIONED_TABLE) && !tables)
  143 akapila@postgresql.o      653                 :UNC           0 :             continue;
                                654                 :                : 
   95 michael@paquier.xyz       655                 :GNC        1956 :         relstate = palloc_object(SubscriptionRelState);
 3279 peter_e@gmx.net           656                 :CBC        1956 :         relstate->relid = subrel->srrelid;
                                657                 :           1956 :         relstate->state = subrel->srsubstate;
 2064 tgl@sss.pgh.pa.us         658                 :           1956 :         d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
                                659                 :                :                             Anum_pg_subscription_rel_srsublsn, &isnull);
                                660         [ +  + ]:           1956 :         if (isnull)
                                661                 :           1614 :             relstate->lsn = InvalidXLogRecPtr;
                                662                 :                :         else
                                663                 :            342 :             relstate->lsn = DatumGetLSN(d);
                                664                 :                : 
 3279 peter_e@gmx.net           665                 :           1956 :         res = lappend(res, relstate);
                                666                 :                :     }
                                667                 :                : 
                                668                 :                :     /* Cleanup */
                                669                 :           1177 :     systable_endscan(scan);
 2610 andres@anarazel.de        670                 :           1177 :     table_close(rel, AccessShareLock);
                                671                 :                : 
 3279 peter_e@gmx.net           672                 :           1177 :     return res;
                                673                 :                : }
                                674                 :                : 
                                675                 :                : /*
                                676                 :                :  * Update the dead tuple retention status for the given subscription.
                                677                 :                :  */
                                678                 :                : void
  194 akapila@postgresql.o      679                 :GNC           2 : UpdateDeadTupleRetentionStatus(Oid subid, bool active)
                                680                 :                : {
                                681                 :                :     Relation    rel;
                                682                 :                :     bool        nulls[Natts_pg_subscription];
                                683                 :                :     bool        replaces[Natts_pg_subscription];
                                684                 :                :     Datum       values[Natts_pg_subscription];
                                685                 :                :     HeapTuple   tup;
                                686                 :                : 
                                687                 :                :     /* Look up the subscription in the catalog */
                                688                 :              2 :     rel = table_open(SubscriptionRelationId, RowExclusiveLock);
                                689                 :              2 :     tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
                                690                 :                : 
                                691         [ -  + ]:              2 :     if (!HeapTupleIsValid(tup))
  194 akapila@postgresql.o      692         [ #  # ]:UNC           0 :         elog(ERROR, "cache lookup failed for subscription %u", subid);
                                693                 :                : 
  194 akapila@postgresql.o      694                 :GNC           2 :     LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
                                695                 :                : 
                                696                 :                :     /* Form a new tuple. */
                                697                 :              2 :     memset(values, 0, sizeof(values));
                                698                 :              2 :     memset(nulls, false, sizeof(nulls));
                                699                 :              2 :     memset(replaces, false, sizeof(replaces));
                                700                 :                : 
                                701                 :                :     /* Set the subscription to disabled. */
                                702                 :              2 :     values[Anum_pg_subscription_subretentionactive - 1] = active;
                                703                 :              2 :     replaces[Anum_pg_subscription_subretentionactive - 1] = true;
                                704                 :                : 
                                705                 :                :     /* Update the catalog */
                                706                 :              2 :     tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
                                707                 :                :                             replaces);
                                708                 :              2 :     CatalogTupleUpdate(rel, &tup->t_self, tup);
                                709                 :              2 :     heap_freetuple(tup);
                                710                 :                : 
                                711                 :              2 :     table_close(rel, NoLock);
                                712                 :              2 : }
        

Generated by: LCOV version 2.4-beta