2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* function.c
|
|
|
|
*
|
|
|
|
* server-side function support
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/function.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include "access/transam.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-01-09 02:44:44 +08:00
|
|
|
* install_support_functions_in_new_db()
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
|
|
|
* pg_upgrade requires some support functions that enable it to modify
|
|
|
|
* backend behavior.
|
|
|
|
*/
|
|
|
|
void
|
2011-01-09 02:44:44 +08:00
|
|
|
install_support_functions_in_new_db(const char *db_name)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
PGconn *conn = connectToServer(&new_cluster, db_name);
|
|
|
|
|
2011-01-08 11:01:44 +08:00
|
|
|
/* suppress NOTICE of dropped objects */
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"SET client_min_messages = warning;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
2011-04-10 23:42:00 +08:00
|
|
|
"DROP SCHEMA IF EXISTS binary_upgrade CASCADE;"));
|
2011-01-08 11:01:44 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"RESET client_min_messages;"));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-08 11:01:44 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE SCHEMA binary_upgrade;"));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-08 11:01:44 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-02-10 08:17:33 +08:00
|
|
|
"binary_upgrade.set_next_pg_type_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-04-10 23:42:00 +08:00
|
|
|
"binary_upgrade.set_next_array_pg_type_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-04-10 23:42:00 +08:00
|
|
|
"binary_upgrade.set_next_toast_pg_type_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-04-10 23:42:00 +08:00
|
|
|
"binary_upgrade.set_next_heap_pg_class_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-04-10 23:42:00 +08:00
|
|
|
"binary_upgrade.set_next_index_pg_class_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-04-10 23:42:00 +08:00
|
|
|
"binary_upgrade.set_next_toast_pg_class_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-02-10 08:17:33 +08:00
|
|
|
"binary_upgrade.set_next_pg_enum_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
2011-02-10 08:17:33 +08:00
|
|
|
"binary_upgrade.set_next_pg_authid_oid(OID) "
|
2011-01-08 11:01:44 +08:00
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C STRICT;"));
|
2011-02-10 08:17:33 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"CREATE OR REPLACE FUNCTION "
|
|
|
|
"binary_upgrade.create_empty_extension(text, text, bool, text, oid[], text[], text[]) "
|
|
|
|
"RETURNS VOID "
|
|
|
|
"AS '$libdir/pg_upgrade_support' "
|
|
|
|
"LANGUAGE C;"));
|
2011-01-08 11:01:44 +08:00
|
|
|
PQfinish(conn);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-01-09 02:44:44 +08:00
|
|
|
uninstall_support_functions_from_new_cluster(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Removing support functions from new cluster");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-06 09:14:59 +08:00
|
|
|
DbInfo *new_db = &new_cluster.dbarr.dbs[dbnum];
|
|
|
|
PGconn *conn = connectToServer(&new_cluster, new_db->db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* suppress NOTICE of dropped objects */
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"SET client_min_messages = warning;"));
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"DROP SCHEMA binary_upgrade CASCADE;"));
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"RESET client_min_messages;"));
|
|
|
|
PQfinish(conn);
|
|
|
|
}
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_loadable_libraries()
|
|
|
|
*
|
|
|
|
* Fetch the names of all old libraries containing C-language functions.
|
|
|
|
* We will later check that they all exist in the new installation.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
get_loadable_libraries(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
PGresult **ress;
|
|
|
|
int totaltups;
|
|
|
|
int dbnum;
|
|
|
|
|
2011-11-02 01:49:03 +08:00
|
|
|
ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
|
2010-05-12 10:19:11 +08:00
|
|
|
totaltups = 0;
|
|
|
|
|
|
|
|
/* Fetch all library names, removing duplicates within each DB */
|
2011-01-02 01:06:36 +08:00
|
|
|
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
DbInfo *active_db = &old_cluster.dbarr.dbs[dbnum];
|
|
|
|
PGconn *conn = connectToServer(&old_cluster, active_db->db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* Fetch all libraries referenced in this DB */
|
2010-10-20 05:38:16 +08:00
|
|
|
ress[dbnum] = executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"SELECT DISTINCT probin "
|
|
|
|
"FROM pg_catalog.pg_proc "
|
|
|
|
"WHERE prolang = 13 /* C */ AND "
|
2011-02-10 08:17:33 +08:00
|
|
|
"probin IS NOT NULL AND "
|
|
|
|
"oid >= %u;",
|
2010-05-12 10:19:11 +08:00
|
|
|
FirstNormalObjectId);
|
|
|
|
totaltups += PQntuples(ress[dbnum]);
|
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate what's certainly enough space */
|
|
|
|
if (totaltups > 0)
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.libraries = (char **) pg_malloc(totaltups * sizeof(char *));
|
2010-05-12 10:19:11 +08:00
|
|
|
else
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.libraries = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now remove duplicates across DBs. This is pretty inefficient code, but
|
|
|
|
* there probably aren't enough entries to matter.
|
|
|
|
*/
|
|
|
|
totaltups = 0;
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
PGresult *res = ress[dbnum];
|
|
|
|
int ntups;
|
|
|
|
int rowno;
|
|
|
|
|
|
|
|
ntups = PQntuples(res);
|
|
|
|
for (rowno = 0; rowno < ntups; rowno++)
|
|
|
|
{
|
|
|
|
char *lib = PQgetvalue(res, rowno, 0);
|
|
|
|
bool dup = false;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < totaltups; n++)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
if (strcmp(lib, os_info.libraries[n]) == 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
dup = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!dup)
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.libraries[totaltups++] = pg_strdup(lib);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.num_libraries = totaltups;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
pg_free(ress);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_loadable_libraries()
|
|
|
|
*
|
|
|
|
* Check that the new cluster contains all required libraries.
|
|
|
|
* We do this by actually trying to LOAD each one, thereby testing
|
|
|
|
* compatibility as well as presence.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
check_loadable_libraries(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
PGconn *conn = connectToServer(&new_cluster, "template1");
|
2010-05-12 10:19:11 +08:00
|
|
|
int libnum;
|
|
|
|
FILE *script = NULL;
|
|
|
|
bool found = false;
|
|
|
|
char output_path[MAXPGPATH];
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Checking for presence of required libraries");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
snprintf(output_path, sizeof(output_path), "%s/loadable_libraries.txt",
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.cwd);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
for (libnum = 0; libnum < os_info.num_libraries; libnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
char *lib = os_info.libraries[libnum];
|
2010-05-12 10:19:11 +08:00
|
|
|
int llen = strlen(lib);
|
2010-10-20 05:38:16 +08:00
|
|
|
char *cmd = (char *) pg_malloc(8 + 2 * llen + 1);
|
2010-05-12 10:19:11 +08:00
|
|
|
PGresult *res;
|
|
|
|
|
|
|
|
strcpy(cmd, "LOAD '");
|
|
|
|
PQescapeStringConn(conn, cmd + 6, lib, llen, NULL);
|
|
|
|
strcat(cmd, "'");
|
|
|
|
|
|
|
|
res = PQexec(conn, cmd);
|
|
|
|
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
if (script == NULL && (script = fopen(output_path, "w")) == NULL)
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "Could not open file \"%s\": %s\n",
|
|
|
|
output_path, getErrorText(errno));
|
|
|
|
fprintf(script, "Could not load library \"%s\"\n%s\n",
|
2010-05-12 10:19:11 +08:00
|
|
|
lib,
|
|
|
|
PQerrorMessage(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
pg_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
fclose(script);
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "fatal\n");
|
|
|
|
pg_log(PG_FATAL,
|
2011-07-12 12:13:51 +08:00
|
|
|
"Your installation references loadable libraries that are missing from the\n"
|
|
|
|
"new installation. You can add these libraries to the new installation,\n"
|
|
|
|
"or remove the functions using them from the old installation. A list of\n"
|
|
|
|
"problem libraries is in the file:\n"
|
|
|
|
" %s\n\n", output_path);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
else
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|