@@ -287,3 +287,115 @@ old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster)
287287	else 
288288		check_ok ();
289289}
290+ 
291+ /* 
292+  * old_9_6_invalidate_hash_indexes() 
293+  *	9.6 -> 10 
294+  *	Hash index binary format has changed from 9.6->10.0 
295+  */ 
296+ void 
297+ old_9_6_invalidate_hash_indexes (ClusterInfo  * cluster , bool  check_mode )
298+ {
299+ 	int 			dbnum ;
300+ 	FILE 	   * script  =  NULL ;
301+ 	bool 		found  =  false;
302+ 	char 	   * output_path  =  "reindex_hash.sql" ;
303+ 
304+ 	prep_status ("Checking for hash indexes" );
305+ 
306+ 	for  (dbnum  =  0 ; dbnum  <  cluster -> dbarr .ndbs ; dbnum ++ )
307+ 	{
308+ 		PGresult    * res ;
309+ 		bool 		db_used  =  false;
310+ 		int 			ntups ;
311+ 		int 			rowno ;
312+ 		int 			i_nspname ,
313+ 					i_relname ;
314+ 		DbInfo 	   * active_db  =  & cluster -> dbarr .dbs [dbnum ];
315+ 		PGconn 	   * conn  =  connectToServer (cluster , active_db -> db_name );
316+ 
317+ 		/* find hash indexes */ 
318+ 		res  =  executeQueryOrDie (conn ,
319+ 								"SELECT n.nspname, c.relname " 
320+ 								"FROM	pg_catalog.pg_class c, " 
321+ 								"		pg_catalog.pg_index i, " 
322+ 								"		pg_catalog.pg_am a, " 
323+ 								"		pg_catalog.pg_namespace n " 
324+ 								"WHERE	i.indexrelid = c.oid AND " 
325+ 								"		c.relam = a.oid AND " 
326+ 								"		c.relnamespace = n.oid AND " 
327+ 								"		a.amname = 'hash'" 
328+ 			);
329+ 
330+ 		ntups  =  PQntuples (res );
331+ 		i_nspname  =  PQfnumber (res , "nspname" );
332+ 		i_relname  =  PQfnumber (res , "relname" );
333+ 		for  (rowno  =  0 ; rowno  <  ntups ; rowno ++ )
334+ 		{
335+ 			found  =  true;
336+ 			if  (!check_mode )
337+ 			{
338+ 				if  (script  ==  NULL  &&  (script  =  fopen_priv (output_path , "w" )) ==  NULL )
339+ 					pg_fatal ("could not open file \"%s\": %s\n" , output_path ,
340+ 							 strerror (errno ));
341+ 				if  (!db_used )
342+ 				{
343+ 					PQExpBufferData  connectbuf ;
344+ 
345+ 					initPQExpBuffer (& connectbuf );
346+ 					appendPsqlMetaConnect (& connectbuf , active_db -> db_name );
347+ 					fputs (connectbuf .data , script );
348+ 					termPQExpBuffer (& connectbuf );
349+ 					db_used  =  true;
350+ 				}
351+ 				fprintf (script , "REINDEX INDEX %s.%s;\n" ,
352+ 						quote_identifier (PQgetvalue (res , rowno , i_nspname )),
353+ 						quote_identifier (PQgetvalue (res , rowno , i_relname )));
354+ 			}
355+ 		}
356+ 
357+ 		PQclear (res );
358+ 
359+ 		if  (!check_mode  &&  db_used )
360+ 		{
361+ 			/* mark hash indexes as invalid */ 
362+ 			PQclear (executeQueryOrDie (conn ,
363+ 									  "UPDATE pg_catalog.pg_index i " 
364+ 									  "SET	indisvalid = false " 
365+ 									  "FROM	pg_catalog.pg_class c, " 
366+ 									  "		pg_catalog.pg_am a, " 
367+ 									  "		pg_catalog.pg_namespace n " 
368+ 									  "WHERE	i.indexrelid = c.oid AND " 
369+ 									  "		c.relam = a.oid AND " 
370+ 									  "		c.relnamespace = n.oid AND " 
371+ 									  "		a.amname = 'hash'" ));
372+ 		}
373+ 
374+ 		PQfinish (conn );
375+ 	}
376+ 
377+ 	if  (script )
378+ 		fclose (script );
379+ 
380+ 	if  (found )
381+ 	{
382+ 		report_status (PG_WARNING , "warning" );
383+ 		if  (check_mode )
384+ 			pg_log (PG_WARNING , "\n" 
385+ 				   "Your installation contains hash indexes.  These indexes have different\n" 
386+ 				   "internal formats between your old and new clusters, so they must be\n" 
387+ 				   "reindexed with the REINDEX command.  After upgrading, you will be given\n" 
388+ 				   "REINDEX instructions.\n\n" );
389+ 		else 
390+ 			pg_log (PG_WARNING , "\n" 
391+ 				   "Your installation contains hash indexes.  These indexes have different\n" 
392+ 				   "internal formats between your old and new clusters, so they must be\n" 
393+ 				   "reindexed with the REINDEX command.  The file:\n" 
394+ 				   "    %s\n" 
395+ 				   "when executed by psql by the database superuser will recreate all invalid\n" 
396+ 			  "indexes; until then, none of these indexes will be used.\n\n" ,
397+ 				   output_path );
398+ 	}
399+ 	else 
400+ 		check_ok ();
401+ }
0 commit comments