Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6f13100

Browse files
committed
pg_upgrade: have pg_upgrade fail for old 9.4 JSONB format
Backpatch through 9.4
1 parent f6b7d4f commit 6f13100

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

contrib/pg_upgrade/check.c

+95
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static void check_is_super_user(ClusterInfo *cluster);
2323
static void check_for_prepared_transactions(ClusterInfo *cluster);
2424
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2525
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
26+
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
2627
static void get_bin_version(ClusterInfo *cluster);
2728
static char *get_canonical_locale_name(int category, const char *locale);
2829

@@ -99,6 +100,10 @@ check_and_dump_old_cluster(bool live_check, char **sequence_script_file_name)
99100
check_for_reg_data_type_usage(&old_cluster);
100101
check_for_isn_and_int8_passing_mismatch(&old_cluster);
101102

103+
if (GET_MAJOR_VERSION(old_cluster.major_version) == 904 &&
104+
old_cluster.controldata.cat_ver < JSONB_FORMAT_CHANGE_CAT_VER)
105+
check_for_jsonb_9_4_usage(&old_cluster);
106+
102107
/* old = PG 8.3 checks? */
103108
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 803)
104109
{
@@ -964,6 +969,96 @@ check_for_reg_data_type_usage(ClusterInfo *cluster)
964969
}
965970

966971

972+
/*
973+
* check_for_jsonb_9_4_usage()
974+
*
975+
* JSONB changed its storage format during 9.4 beta, so check for it.
976+
*/
977+
static void
978+
check_for_jsonb_9_4_usage(ClusterInfo *cluster)
979+
{
980+
int dbnum;
981+
FILE *script = NULL;
982+
bool found = false;
983+
char output_path[MAXPGPATH];
984+
985+
prep_status("Checking for JSONB user data types");
986+
987+
snprintf(output_path, sizeof(output_path), "tables_using_jsonb.txt");
988+
989+
for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
990+
{
991+
PGresult *res;
992+
bool db_used = false;
993+
int ntups;
994+
int rowno;
995+
int i_nspname,
996+
i_relname,
997+
i_attname;
998+
DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
999+
PGconn *conn = connectToServer(cluster, active_db->db_name);
1000+
1001+
/*
1002+
* While several relkinds don't store any data, e.g. views, they can
1003+
* be used to define data types of other columns, so we check all
1004+
* relkinds.
1005+
*/
1006+
res = executeQueryOrDie(conn,
1007+
"SELECT n.nspname, c.relname, a.attname "
1008+
"FROM pg_catalog.pg_class c, "
1009+
" pg_catalog.pg_namespace n, "
1010+
" pg_catalog.pg_attribute a "
1011+
"WHERE c.oid = a.attrelid AND "
1012+
" NOT a.attisdropped AND "
1013+
" a.atttypid = 'pg_catalog.jsonb'::pg_catalog.regtype AND "
1014+
" c.relnamespace = n.oid AND "
1015+
/* exclude possible orphaned temp tables */
1016+
" n.nspname !~ '^pg_temp_' AND "
1017+
" n.nspname NOT IN ('pg_catalog', 'information_schema')");
1018+
1019+
ntups = PQntuples(res);
1020+
i_nspname = PQfnumber(res, "nspname");
1021+
i_relname = PQfnumber(res, "relname");
1022+
i_attname = PQfnumber(res, "attname");
1023+
for (rowno = 0; rowno < ntups; rowno++)
1024+
{
1025+
found = true;
1026+
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1027+
pg_fatal("Could not open file \"%s\": %s\n",
1028+
output_path, getErrorText(errno));
1029+
if (!db_used)
1030+
{
1031+
fprintf(script, "Database: %s\n", active_db->db_name);
1032+
db_used = true;
1033+
}
1034+
fprintf(script, " %s.%s.%s\n",
1035+
PQgetvalue(res, rowno, i_nspname),
1036+
PQgetvalue(res, rowno, i_relname),
1037+
PQgetvalue(res, rowno, i_attname));
1038+
}
1039+
1040+
PQclear(res);
1041+
1042+
PQfinish(conn);
1043+
}
1044+
1045+
if (script)
1046+
fclose(script);
1047+
1048+
if (found)
1049+
{
1050+
pg_log(PG_REPORT, "fatal\n");
1051+
pg_fatal("Your installation contains one of the JSONB data types in user tables.\n"
1052+
"The internal format of JSONB changed during 9.4 beta so this cluster cannot currently\n"
1053+
"be upgraded. You can remove the problem tables and restart the upgrade. A list\n"
1054+
"of the problem columns is in the file:\n"
1055+
" %s\n\n", output_path);
1056+
}
1057+
else
1058+
check_ok();
1059+
}
1060+
1061+
9671062
static void
9681063
get_bin_version(ClusterInfo *cluster)
9691064
{

contrib/pg_upgrade/pg_upgrade.h

+5
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ extern char *output_files[];
107107
*/
108108
#define VISIBILITY_MAP_CRASHSAFE_CAT_VER 201107031
109109

110+
/*
111+
* change in JSONB format during 9.4 beta
112+
*/
113+
#define JSONB_FORMAT_CHANGE_CAT_VER 201409291
114+
110115
/*
111116
* pg_multixact format changed in 9.3 commit 0ac5ad5134f2769ccbaefec73844f85,
112117
* ("Improve concurrency of foreign key locking") which also updated catalog

0 commit comments

Comments
 (0)