LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - function.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 86.7 % 75 65 2 8 13 52 4 4
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 3 3 3
Baseline: 16@8cea358b128 Branches: 73.3 % 30 22 1 7 2 3 17
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 83.3 % 6 5 1 5
(120,180] days: 88.9 % 9 8 1 8
(240..) days: 86.7 % 60 52 8 52
Function coverage date bins:
(240..) days: 100.0 % 3 3 3
Branch coverage date bins:
(120,180] days: 75.0 % 4 3 1 3
(240..) days: 73.1 % 26 19 7 2 17

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  *  function.c
                                  3                 :                :  *
                                  4                 :                :  *  server-side function support
                                  5                 :                :  *
                                  6                 :                :  *  Copyright (c) 2010-2024, PostgreSQL Global Development Group
                                  7                 :                :  *  src/bin/pg_upgrade/function.c
                                  8                 :                :  */
                                  9                 :                : 
                                 10                 :                : #include "postgres_fe.h"
                                 11                 :                : 
                                 12                 :                : #include "access/transam.h"
                                 13                 :                : #include "catalog/pg_language_d.h"
                                 14                 :                : #include "common/int.h"
                                 15                 :                : #include "pg_upgrade.h"
                                 16                 :                : 
                                 17                 :                : /*
                                 18                 :                :  * qsort comparator for pointers to library names
                                 19                 :                :  *
                                 20                 :                :  * We sort first by name length, then alphabetically for names of the
                                 21                 :                :  * same length, then database array index.  This is to ensure that, eg,
                                 22                 :                :  * "hstore_plpython" sorts after both "hstore" and "plpython"; otherwise
                                 23                 :                :  * transform modules will probably fail their LOAD tests.  (The backend
                                 24                 :                :  * ought to cope with that consideration, but it doesn't yet, and even
                                 25                 :                :  * when it does it'll still be a good idea to have a predictable order of
                                 26                 :                :  * probing here.)
                                 27                 :                :  */
                                 28                 :                : static int
 2750 tgl@sss.pgh.pa.us          29                 :CBC           5 : library_name_compare(const void *p1, const void *p2)
                                 30                 :                : {
 2087 bruce@momjian.us           31                 :              5 :     const char *str1 = ((const LibraryInfo *) p1)->name;
                                 32                 :              5 :     const char *str2 = ((const LibraryInfo *) p2)->name;
   58 nathan@postgresql.or       33                 :GNC           5 :     size_t      slen1 = strlen(str1);
                                 34                 :              5 :     size_t      slen2 = strlen(str2);
 2087 bruce@momjian.us           35                 :CBC           5 :     int         cmp = strcmp(str1, str2);
                                 36                 :                : 
 2750 tgl@sss.pgh.pa.us          37         [ +  + ]:              5 :     if (slen1 != slen2)
   58 nathan@postgresql.or       38                 :GNC           2 :         return pg_cmp_size(slen1, slen2);
 2087 bruce@momjian.us           39         [ +  + ]:CBC           3 :     if (cmp != 0)
                                 40                 :              2 :         return cmp;
   58 nathan@postgresql.or       41                 :GNC           1 :     return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
                                 42                 :              1 :                       ((const LibraryInfo *) p2)->dbnum);
                                 43                 :                : }
                                 44                 :                : 
                                 45                 :                : 
                                 46                 :                : /*
                                 47                 :                :  * get_loadable_libraries()
                                 48                 :                :  *
                                 49                 :                :  *  Fetch the names of all old libraries containing either C-language functions
                                 50                 :                :  *  or are corresponding to logical replication output plugins.
                                 51                 :                :  *
                                 52                 :                :  *  We will later check that they all exist in the new installation.
                                 53                 :                :  */
                                 54                 :                : void
 4926 bruce@momjian.us           55                 :CBC           8 : get_loadable_libraries(void)
                                 56                 :                : {
                                 57                 :                :     PGresult  **ress;
                                 58                 :                :     int         totaltups;
                                 59                 :                :     int         dbnum;
                                 60                 :                :     int         n_libinfos;
                                 61                 :                : 
 4548                            62                 :              8 :     ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
 5086                            63                 :              8 :     totaltups = 0;
                                 64                 :                : 
                                 65                 :                :     /* Fetch all library names, removing duplicates within each DB */
 4852                            66         [ +  + ]:             32 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
                                 67                 :                :     {
                                 68                 :             24 :         DbInfo     *active_db = &old_cluster.dbarr.dbs[dbnum];
                                 69                 :             24 :         PGconn     *conn = connectToServer(&old_cluster, active_db->db_name);
                                 70                 :                : 
                                 71                 :                :         /*
                                 72                 :                :          * Fetch all libraries containing non-built-in C functions in this DB.
                                 73                 :                :          */
 4926                            74                 :             24 :         ress[dbnum] = executeQueryOrDie(conn,
                                 75                 :                :                                         "SELECT DISTINCT probin "
                                 76                 :                :                                         "FROM pg_catalog.pg_proc "
                                 77                 :                :                                         "WHERE prolang = %u AND "
                                 78                 :                :                                         "probin IS NOT NULL AND "
                                 79                 :                :                                         "oid >= %u;",
                                 80                 :                :                                         ClanguageId,
                                 81                 :                :                                         FirstNormalObjectId);
 5086                            82                 :             24 :         totaltups += PQntuples(ress[dbnum]);
                                 83                 :                : 
                                 84                 :             24 :         PQfinish(conn);
                                 85                 :                :     }
                                 86                 :                : 
                                 87                 :                :     /*
                                 88                 :                :      * Allocate memory for required libraries and logical replication output
                                 89                 :                :      * plugins.
                                 90                 :                :      */
  171 akapila@postgresql.o       91                 :GNC           8 :     n_libinfos = totaltups + count_old_cluster_logical_slots();
                                 92                 :              8 :     os_info.libraries = (LibraryInfo *) pg_malloc(sizeof(LibraryInfo) * n_libinfos);
 5086 bruce@momjian.us           93                 :CBC           8 :     totaltups = 0;
                                 94                 :                : 
 4852                            95         [ +  + ]:             32 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
                                 96                 :                :     {
 5086                            97                 :             24 :         PGresult   *res = ress[dbnum];
                                 98                 :                :         int         ntups;
                                 99                 :                :         int         rowno;
  171 akapila@postgresql.o      100                 :GNC          24 :         LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
                                101                 :                : 
 5086 bruce@momjian.us          102                 :CBC          24 :         ntups = PQntuples(res);
                                103         [ +  + ]:             30 :         for (rowno = 0; rowno < ntups; rowno++)
                                104                 :                :         {
                                105                 :              6 :             char       *lib = PQgetvalue(res, rowno, 0);
                                106                 :                : 
 2087                           107                 :              6 :             os_info.libraries[totaltups].name = pg_strdup(lib);
                                108                 :              6 :             os_info.libraries[totaltups].dbnum = dbnum;
                                109                 :                : 
                                110                 :              6 :             totaltups++;
                                111                 :                :         }
 5086                           112                 :             24 :         PQclear(res);
                                113                 :                : 
                                114                 :                :         /*
                                115                 :                :          * Store the names of output plugins as well. There is a possibility
                                116                 :                :          * that duplicated plugins are set, but the consumer function
                                117                 :                :          * check_loadable_libraries() will avoid checking the same library, so
                                118                 :                :          * we do not have to consider their uniqueness here.
                                119                 :                :          */
  171 akapila@postgresql.o      120         [ +  + ]:GNC          29 :         for (int slotno = 0; slotno < slot_arr->nslots; slotno++)
                                121                 :                :         {
                                122         [ -  + ]:              5 :             if (slot_arr->slots[slotno].invalid)
  171 akapila@postgresql.o      123                 :UNC           0 :                 continue;
                                124                 :                : 
  171 akapila@postgresql.o      125                 :GNC           5 :             os_info.libraries[totaltups].name = pg_strdup(slot_arr->slots[slotno].plugin);
                                126                 :              5 :             os_info.libraries[totaltups].dbnum = dbnum;
                                127                 :                : 
                                128                 :              5 :             totaltups++;
                                129                 :                :         }
                                130                 :                :     }
                                131                 :                : 
 5086 bruce@momjian.us          132                 :CBC           8 :     pg_free(ress);
                                133                 :                : 
 2750 tgl@sss.pgh.pa.us         134                 :              8 :     os_info.num_libraries = totaltups;
 5086 bruce@momjian.us          135                 :              8 : }
                                136                 :                : 
                                137                 :                : 
                                138                 :                : /*
                                139                 :                :  * check_loadable_libraries()
                                140                 :                :  *
                                141                 :                :  *  Check that the new cluster contains all required libraries.
                                142                 :                :  *  We do this by actually trying to LOAD each one, thereby testing
                                143                 :                :  *  compatibility as well as presence.
                                144                 :                :  */
                                145                 :                : void
 4926                           146                 :              6 : check_loadable_libraries(void)
                                147                 :                : {
 4852                           148                 :              6 :     PGconn     *conn = connectToServer(&new_cluster, "template1");
                                149                 :                :     int         libnum;
 2087                           150                 :              6 :     int         was_load_failure = false;
 5086                           151                 :              6 :     FILE       *script = NULL;
                                152                 :                :     char        output_path[MAXPGPATH];
                                153                 :                : 
 4926                           154                 :              6 :     prep_status("Checking for presence of required libraries");
                                155                 :                : 
  798 michael@paquier.xyz       156                 :              6 :     snprintf(output_path, sizeof(output_path), "%s/%s",
                                157                 :                :              log_opts.basedir, "loadable_libraries.txt");
                                158                 :                : 
                                159                 :                :     /*
                                160                 :                :      * Now we want to sort the library names into order.  This avoids multiple
                                161                 :                :      * probes of the same library, and ensures that libraries are probed in a
                                162                 :                :      * consistent order, which is important for reproducible behavior if one
                                163                 :                :      * library depends on another.
                                164                 :                :      */
  432 peter@eisentraut.org      165                 :              6 :     qsort(os_info.libraries, os_info.num_libraries,
                                166                 :                :           sizeof(LibraryInfo), library_name_compare);
                                167                 :                : 
 4926 bruce@momjian.us          168         [ +  + ]:             15 :     for (libnum = 0; libnum < os_info.num_libraries; libnum++)
                                169                 :                :     {
 2087                           170                 :              9 :         char       *lib = os_info.libraries[libnum].name;
 5086                           171                 :              9 :         int         llen = strlen(lib);
                                172                 :                :         char        cmd[7 + 2 * MAXPGPATH + 1];
                                173                 :                :         PGresult   *res;
                                174                 :                : 
                                175                 :                :         /* Did the library name change?  Probe it. */
 2087                           176   [ +  +  +  + ]:              9 :         if (libnum == 0 || strcmp(lib, os_info.libraries[libnum - 1].name) != 0)
                                177                 :                :         {
                                178                 :              8 :             strcpy(cmd, "LOAD '");
                                179                 :              8 :             PQescapeStringConn(conn, cmd + strlen(cmd), lib, llen, NULL);
                                180                 :              8 :             strcat(cmd, "'");
                                181                 :                : 
                                182                 :              8 :             res = PQexec(conn, cmd);
                                183                 :                : 
                                184         [ -  + ]:              8 :             if (PQresultStatus(res) != PGRES_COMMAND_OK)
                                185                 :                :             {
 2087 bruce@momjian.us          186                 :UBC           0 :                 was_load_failure = true;
                                187                 :                : 
                                188   [ #  #  #  # ]:              0 :                 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
   33 michael@paquier.xyz       189                 :UNC           0 :                     pg_fatal("could not open file \"%s\": %m", output_path);
 2087 bruce@momjian.us          190                 :UBC           0 :                 fprintf(script, _("could not load library \"%s\": %s"),
                                191                 :                :                         lib,
                                192                 :                :                         PQerrorMessage(conn));
                                193                 :                :             }
                                194                 :                :             else
 2087 bruce@momjian.us          195                 :CBC           8 :                 was_load_failure = false;
                                196                 :                : 
                                197                 :              8 :             PQclear(res);
                                198                 :                :         }
                                199                 :                : 
                                200         [ -  + ]:              9 :         if (was_load_failure)
 1650 bruce@momjian.us          201                 :UBC           0 :             fprintf(script, _("In database: %s\n"),
 1789 tgl@sss.pgh.pa.us         202                 :              0 :                     old_cluster.dbarr.dbs[os_info.libraries[libnum].dbnum].db_name);
                                203                 :                :     }
                                204                 :                : 
 5086 bruce@momjian.us          205                 :CBC           6 :     PQfinish(conn);
                                206                 :                : 
  592 dgustafsson@postgres      207         [ -  + ]:              6 :     if (script)
                                208                 :                :     {
 5086 bruce@momjian.us          209                 :UBC           0 :         fclose(script);
  642 tgl@sss.pgh.pa.us         210                 :              0 :         pg_log(PG_REPORT, "fatal");
 3848 peter_e@gmx.net           211                 :              0 :         pg_fatal("Your installation references loadable libraries that are missing from the\n"
                                212                 :                :                  "new installation.  You can add these libraries to the new installation,\n"
                                213                 :                :                  "or remove the functions using them from the old installation.  A list of\n"
                                214                 :                :                  "problem libraries is in the file:\n"
                                215                 :                :                  "    %s", output_path);
                                216                 :                :     }
                                217                 :                :     else
 4926 bruce@momjian.us          218                 :CBC           6 :         check_ok();
 5086                           219                 :              6 : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622