LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - function.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 56 68 82.4 %
Date: 2022-12-05 11:11:09 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  function.c
       3             :  *
       4             :  *  server-side function support
       5             :  *
       6             :  *  Copyright (c) 2010-2022, 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 "pg_upgrade.h"
      15             : 
      16             : /*
      17             :  * qsort comparator for pointers to library names
      18             :  *
      19             :  * We sort first by name length, then alphabetically for names of the
      20             :  * same length, then database array index.  This is to ensure that, eg,
      21             :  * "hstore_plpython" sorts after both "hstore" and "plpython"; otherwise
      22             :  * transform modules will probably fail their LOAD tests.  (The backend
      23             :  * ought to cope with that consideration, but it doesn't yet, and even
      24             :  * when it does it'll still be a good idea to have a predictable order of
      25             :  * probing here.)
      26             :  */
      27             : static int
      28           8 : library_name_compare(const void *p1, const void *p2)
      29             : {
      30           8 :     const char *str1 = ((const LibraryInfo *) p1)->name;
      31           8 :     const char *str2 = ((const LibraryInfo *) p2)->name;
      32           8 :     int         slen1 = strlen(str1);
      33           8 :     int         slen2 = strlen(str2);
      34           8 :     int         cmp = strcmp(str1, str2);
      35             : 
      36           8 :     if (slen1 != slen2)
      37           4 :         return slen1 - slen2;
      38           4 :     if (cmp != 0)
      39           4 :         return cmp;
      40             :     else
      41           0 :         return ((const LibraryInfo *) p1)->dbnum -
      42           0 :             ((const LibraryInfo *) p2)->dbnum;
      43             : }
      44             : 
      45             : 
      46             : /*
      47             :  * get_loadable_libraries()
      48             :  *
      49             :  *  Fetch the names of all old libraries containing C-language functions.
      50             :  *  We will later check that they all exist in the new installation.
      51             :  */
      52             : void
      53           4 : get_loadable_libraries(void)
      54             : {
      55             :     PGresult  **ress;
      56             :     int         totaltups;
      57             :     int         dbnum;
      58             : 
      59           4 :     ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
      60           4 :     totaltups = 0;
      61             : 
      62             :     /* Fetch all library names, removing duplicates within each DB */
      63          28 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
      64             :     {
      65          24 :         DbInfo     *active_db = &old_cluster.dbarr.dbs[dbnum];
      66          24 :         PGconn     *conn = connectToServer(&old_cluster, active_db->db_name);
      67             : 
      68             :         /*
      69             :          * Fetch all libraries containing non-built-in C functions in this DB.
      70             :          */
      71          24 :         ress[dbnum] = executeQueryOrDie(conn,
      72             :                                         "SELECT DISTINCT probin "
      73             :                                         "FROM pg_catalog.pg_proc "
      74             :                                         "WHERE prolang = %u AND "
      75             :                                         "probin IS NOT NULL AND "
      76             :                                         "oid >= %u;",
      77             :                                         ClanguageId,
      78             :                                         FirstNormalObjectId);
      79          24 :         totaltups += PQntuples(ress[dbnum]);
      80             : 
      81          24 :         PQfinish(conn);
      82             :     }
      83             : 
      84           4 :     os_info.libraries = (LibraryInfo *) pg_malloc(totaltups * sizeof(LibraryInfo));
      85           4 :     totaltups = 0;
      86             : 
      87          28 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
      88             :     {
      89          24 :         PGresult   *res = ress[dbnum];
      90             :         int         ntups;
      91             :         int         rowno;
      92             : 
      93          24 :         ntups = PQntuples(res);
      94          36 :         for (rowno = 0; rowno < ntups; rowno++)
      95             :         {
      96          12 :             char       *lib = PQgetvalue(res, rowno, 0);
      97             : 
      98          12 :             os_info.libraries[totaltups].name = pg_strdup(lib);
      99          12 :             os_info.libraries[totaltups].dbnum = dbnum;
     100             : 
     101          12 :             totaltups++;
     102             :         }
     103          24 :         PQclear(res);
     104             :     }
     105             : 
     106           4 :     pg_free(ress);
     107             : 
     108           4 :     os_info.num_libraries = totaltups;
     109           4 : }
     110             : 
     111             : 
     112             : /*
     113             :  * check_loadable_libraries()
     114             :  *
     115             :  *  Check that the new cluster contains all required libraries.
     116             :  *  We do this by actually trying to LOAD each one, thereby testing
     117             :  *  compatibility as well as presence.
     118             :  */
     119             : void
     120           4 : check_loadable_libraries(void)
     121             : {
     122           4 :     PGconn     *conn = connectToServer(&new_cluster, "template1");
     123             :     int         libnum;
     124           4 :     int         was_load_failure = false;
     125           4 :     FILE       *script = NULL;
     126             :     char        output_path[MAXPGPATH];
     127             : 
     128           4 :     prep_status("Checking for presence of required libraries");
     129             : 
     130           4 :     snprintf(output_path, sizeof(output_path), "%s/%s",
     131             :              log_opts.basedir, "loadable_libraries.txt");
     132             : 
     133             :     /*
     134             :      * Now we want to sort the library names into order.  This avoids multiple
     135             :      * probes of the same library, and ensures that libraries are probed in a
     136             :      * consistent order, which is important for reproducible behavior if one
     137             :      * library depends on another.
     138             :      */
     139           4 :     qsort((void *) os_info.libraries, os_info.num_libraries,
     140             :           sizeof(LibraryInfo), library_name_compare);
     141             : 
     142          16 :     for (libnum = 0; libnum < os_info.num_libraries; libnum++)
     143             :     {
     144          12 :         char       *lib = os_info.libraries[libnum].name;
     145          12 :         int         llen = strlen(lib);
     146             :         char        cmd[7 + 2 * MAXPGPATH + 1];
     147             :         PGresult   *res;
     148             : 
     149             :         /* Did the library name change?  Probe it. */
     150          12 :         if (libnum == 0 || strcmp(lib, os_info.libraries[libnum - 1].name) != 0)
     151             :         {
     152          12 :             strcpy(cmd, "LOAD '");
     153          12 :             PQescapeStringConn(conn, cmd + strlen(cmd), lib, llen, NULL);
     154          12 :             strcat(cmd, "'");
     155             : 
     156          12 :             res = PQexec(conn, cmd);
     157             : 
     158          12 :             if (PQresultStatus(res) != PGRES_COMMAND_OK)
     159             :             {
     160           0 :                 was_load_failure = true;
     161             : 
     162           0 :                 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
     163           0 :                     pg_fatal("could not open file \"%s\": %s",
     164           0 :                              output_path, strerror(errno));
     165           0 :                 fprintf(script, _("could not load library \"%s\": %s"),
     166             :                         lib,
     167             :                         PQerrorMessage(conn));
     168             :             }
     169             :             else
     170          12 :                 was_load_failure = false;
     171             : 
     172          12 :             PQclear(res);
     173             :         }
     174             : 
     175          12 :         if (was_load_failure)
     176           0 :             fprintf(script, _("In database: %s\n"),
     177           0 :                     old_cluster.dbarr.dbs[os_info.libraries[libnum].dbnum].db_name);
     178             :     }
     179             : 
     180           4 :     PQfinish(conn);
     181             : 
     182           4 :     if (script)
     183             :     {
     184           0 :         fclose(script);
     185           0 :         pg_log(PG_REPORT, "fatal");
     186           0 :         pg_fatal("Your installation references loadable libraries that are missing from the\n"
     187             :                  "new installation.  You can add these libraries to the new installation,\n"
     188             :                  "or remove the functions using them from the old installation.  A list of\n"
     189             :                  "problem libraries is in the file:\n"
     190             :                  "    %s", output_path);
     191             :     }
     192             :     else
     193           4 :         check_ok();
     194           4 : }

Generated by: LCOV version 1.14