LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - function.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 61 85 71.8 %
Date: 2019-11-21 14:06:36 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-2019, 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           4 : library_name_compare(const void *p1, const void *p2)
      29             : {
      30           4 :     const char *str1 = ((const LibraryInfo *) p1)->name;
      31           4 :     const char *str2 = ((const LibraryInfo *) p2)->name;
      32           4 :     int         slen1 = strlen(str1);
      33           4 :     int         slen2 = strlen(str2);
      34           4 :     int         cmp = strcmp(str1, str2);
      35             : 
      36           4 :     if (slen1 != slen2)
      37           2 :         return slen1 - slen2;
      38           2 :     if (cmp != 0)
      39           2 :         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           2 : get_loadable_libraries(void)
      54             : {
      55             :     PGresult  **ress;
      56             :     int         totaltups;
      57             :     int         dbnum;
      58           2 :     bool        found_public_plpython_handler = false;
      59             : 
      60           2 :     ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
      61           2 :     totaltups = 0;
      62             : 
      63             :     /* Fetch all library names, removing duplicates within each DB */
      64          14 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
      65             :     {
      66          12 :         DbInfo     *active_db = &old_cluster.dbarr.dbs[dbnum];
      67          12 :         PGconn     *conn = connectToServer(&old_cluster, active_db->db_name);
      68             : 
      69             :         /*
      70             :          * Fetch all libraries containing non-built-in C functions in this DB.
      71             :          */
      72          12 :         ress[dbnum] = executeQueryOrDie(conn,
      73             :                                         "SELECT DISTINCT probin "
      74             :                                         "FROM pg_catalog.pg_proc "
      75             :                                         "WHERE prolang = %u AND "
      76             :                                         "probin IS NOT NULL AND "
      77             :                                         "oid >= %u;",
      78             :                                         ClanguageId,
      79             :                                         FirstNormalObjectId);
      80          12 :         totaltups += PQntuples(ress[dbnum]);
      81             : 
      82             :         /*
      83             :          * Systems that install plpython before 8.1 have
      84             :          * plpython_call_handler() defined in the "public" schema, causing
      85             :          * pg_dump to dump it.  However that function still references
      86             :          * "plpython" (no "2"), so it throws an error on restore.  This code
      87             :          * checks for the problem function, reports affected databases to the
      88             :          * user and explains how to remove them. 8.1 git commit:
      89             :          * e0dedd0559f005d60c69c9772163e69c204bac69
      90             :          * http://archives.postgresql.org/pgsql-hackers/2012-03/msg01101.php
      91             :          * http://archives.postgresql.org/pgsql-bugs/2012-05/msg00206.php
      92             :          */
      93          12 :         if (GET_MAJOR_VERSION(old_cluster.major_version) < 901)
      94             :         {
      95             :             PGresult   *res;
      96             : 
      97           0 :             res = executeQueryOrDie(conn,
      98             :                                     "SELECT 1 "
      99             :                                     "FROM pg_catalog.pg_proc p "
     100             :                                     "    JOIN pg_catalog.pg_namespace n "
     101             :                                     "    ON pronamespace = n.oid "
     102             :                                     "WHERE proname = 'plpython_call_handler' AND "
     103             :                                     "nspname = 'public' AND "
     104             :                                     "prolang = %u AND "
     105             :                                     "probin = '$libdir/plpython' AND "
     106             :                                     "p.oid >= %u;",
     107             :                                     ClanguageId,
     108             :                                     FirstNormalObjectId);
     109           0 :             if (PQntuples(res) > 0)
     110             :             {
     111           0 :                 if (!found_public_plpython_handler)
     112             :                 {
     113           0 :                     pg_log(PG_WARNING,
     114             :                            "\nThe old cluster has a \"plpython_call_handler\" function defined\n"
     115             :                            "in the \"public\" schema which is a duplicate of the one defined\n"
     116             :                            "in the \"pg_catalog\" schema.  You can confirm this by executing\n"
     117             :                            "in psql:\n"
     118             :                            "\n"
     119             :                            "    \\df *.plpython_call_handler\n"
     120             :                            "\n"
     121             :                            "The \"public\" schema version of this function was created by a\n"
     122             :                            "pre-8.1 install of plpython, and must be removed for pg_upgrade\n"
     123             :                            "to complete because it references a now-obsolete \"plpython\"\n"
     124             :                            "shared object file.  You can remove the \"public\" schema version\n"
     125             :                            "of this function by running the following command:\n"
     126             :                            "\n"
     127             :                            "    DROP FUNCTION public.plpython_call_handler()\n"
     128             :                            "\n"
     129             :                            "in each affected database:\n"
     130             :                            "\n");
     131             :                 }
     132           0 :                 pg_log(PG_WARNING, "    %s\n", active_db->db_name);
     133           0 :                 found_public_plpython_handler = true;
     134             :             }
     135           0 :             PQclear(res);
     136             :         }
     137             : 
     138          12 :         PQfinish(conn);
     139             :     }
     140             : 
     141           2 :     if (found_public_plpython_handler)
     142           0 :         pg_fatal("Remove the problem functions from the old cluster to continue.\n");
     143             : 
     144           2 :     os_info.libraries = (LibraryInfo *) pg_malloc(totaltups * sizeof(LibraryInfo));
     145           2 :     totaltups = 0;
     146             : 
     147          14 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
     148             :     {
     149          12 :         PGresult   *res = ress[dbnum];
     150             :         int         ntups;
     151             :         int         rowno;
     152             : 
     153          12 :         ntups = PQntuples(res);
     154          18 :         for (rowno = 0; rowno < ntups; rowno++)
     155             :         {
     156           6 :             char       *lib = PQgetvalue(res, rowno, 0);
     157             : 
     158           6 :             os_info.libraries[totaltups].name = pg_strdup(lib);
     159           6 :             os_info.libraries[totaltups].dbnum = dbnum;
     160             : 
     161           6 :             totaltups++;
     162             :         }
     163          12 :         PQclear(res);
     164             :     }
     165             : 
     166           2 :     pg_free(ress);
     167             : 
     168           2 :     os_info.num_libraries = totaltups;
     169           2 : }
     170             : 
     171             : 
     172             : /*
     173             :  * check_loadable_libraries()
     174             :  *
     175             :  *  Check that the new cluster contains all required libraries.
     176             :  *  We do this by actually trying to LOAD each one, thereby testing
     177             :  *  compatibility as well as presence.
     178             :  */
     179             : void
     180           2 : check_loadable_libraries(void)
     181             : {
     182           2 :     PGconn     *conn = connectToServer(&new_cluster, "template1");
     183             :     int         libnum;
     184           2 :     int         was_load_failure = false;
     185           2 :     FILE       *script = NULL;
     186           2 :     bool        found = false;
     187             :     char        output_path[MAXPGPATH];
     188             : 
     189           2 :     prep_status("Checking for presence of required libraries");
     190             : 
     191           2 :     snprintf(output_path, sizeof(output_path), "loadable_libraries.txt");
     192             : 
     193             :     /*
     194             :      * Now we want to sort the library names into order.  This avoids multiple
     195             :      * probes of the same library, and ensures that libraries are probed in a
     196             :      * consistent order, which is important for reproducible behavior if one
     197             :      * library depends on another.
     198             :      */
     199           2 :     qsort((void *) os_info.libraries, os_info.num_libraries,
     200             :           sizeof(LibraryInfo), library_name_compare);
     201             : 
     202           8 :     for (libnum = 0; libnum < os_info.num_libraries; libnum++)
     203             :     {
     204           6 :         char       *lib = os_info.libraries[libnum].name;
     205           6 :         int         llen = strlen(lib);
     206             :         char        cmd[7 + 2 * MAXPGPATH + 1];
     207             :         PGresult   *res;
     208             : 
     209             :         /* Did the library name change?  Probe it. */
     210           6 :         if (libnum == 0 || strcmp(lib, os_info.libraries[libnum - 1].name) != 0)
     211             :         {
     212             :             /*
     213             :              * In Postgres 9.0, Python 3 support was added, and to do that, a
     214             :              * plpython2u language was created with library name plpython2.so
     215             :              * as a symbolic link to plpython.so.  In Postgres 9.1, only the
     216             :              * plpython2.so library was created, and both plpythonu and
     217             :              * plpython2u pointing to it.  For this reason, any reference to
     218             :              * library name "plpython" in an old PG <= 9.1 cluster must look
     219             :              * for "plpython2" in the new cluster.
     220             :              *
     221             :              * For this case, we could check pg_pltemplate, but that only
     222             :              * works for languages, and does not help with function shared
     223             :              * objects, so we just do a general fix.
     224             :              */
     225           6 :             if (GET_MAJOR_VERSION(old_cluster.major_version) < 901 &&
     226           0 :                 strcmp(lib, "$libdir/plpython") == 0)
     227             :             {
     228           0 :                 lib = "$libdir/plpython2";
     229           0 :                 llen = strlen(lib);
     230             :             }
     231             : 
     232           6 :             strcpy(cmd, "LOAD '");
     233           6 :             PQescapeStringConn(conn, cmd + strlen(cmd), lib, llen, NULL);
     234           6 :             strcat(cmd, "'");
     235             : 
     236           6 :             res = PQexec(conn, cmd);
     237             : 
     238           6 :             if (PQresultStatus(res) != PGRES_COMMAND_OK)
     239             :             {
     240           0 :                 found = true;
     241           0 :                 was_load_failure = true;
     242             : 
     243           0 :                 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
     244           0 :                     pg_fatal("could not open file \"%s\": %s\n",
     245           0 :                              output_path, strerror(errno));
     246           0 :                 fprintf(script, _("could not load library \"%s\": %s"),
     247             :                         lib,
     248             :                         PQerrorMessage(conn));
     249             :             }
     250             :             else
     251           6 :                 was_load_failure = false;
     252             : 
     253           6 :             PQclear(res);
     254             :         }
     255             : 
     256           6 :         if (was_load_failure)
     257           0 :             fprintf(script, _("In database: %s\n"),
     258           0 :                     old_cluster.dbarr.dbs[os_info.libraries[libnum].dbnum].db_name);
     259             :     }
     260             : 
     261           2 :     PQfinish(conn);
     262             : 
     263           2 :     if (found)
     264             :     {
     265           0 :         fclose(script);
     266           0 :         pg_log(PG_REPORT, "fatal\n");
     267           0 :         pg_fatal("Your installation references loadable libraries that are missing from the\n"
     268             :                  "new installation.  You can add these libraries to the new installation,\n"
     269             :                  "or remove the functions using them from the old installation.  A list of\n"
     270             :                  "problem libraries is in the file:\n"
     271             :                  "    %s\n\n", output_path);
     272             :     }
     273             :     else
     274           2 :         check_ok();
     275           2 : }

Generated by: LCOV version 1.13