1 | /* |
2 | * check.c |
3 | * |
4 | * server checks and output routines |
5 | * |
6 | * Copyright (c) 2010-2019, PostgreSQL Global Development Group |
7 | * src/bin/pg_upgrade/check.c |
8 | */ |
9 | |
10 | #include "postgres_fe.h" |
11 | |
12 | #include "catalog/pg_authid_d.h" |
13 | #include "fe_utils/string_utils.h" |
14 | #include "mb/pg_wchar.h" |
15 | #include "pg_upgrade.h" |
16 | |
17 | |
18 | static void check_new_cluster_is_empty(void); |
19 | static void check_databases_are_compatible(void); |
20 | static void check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb); |
21 | static bool equivalent_locale(int category, const char *loca, const char *locb); |
22 | static void check_is_install_user(ClusterInfo *cluster); |
23 | static void check_proper_datallowconn(ClusterInfo *cluster); |
24 | static void check_for_prepared_transactions(ClusterInfo *cluster); |
25 | static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster); |
26 | static void check_for_tables_with_oids(ClusterInfo *cluster); |
27 | static void check_for_reg_data_type_usage(ClusterInfo *cluster); |
28 | static void check_for_jsonb_9_4_usage(ClusterInfo *cluster); |
29 | static void check_for_pg_role_prefix(ClusterInfo *cluster); |
30 | static char *get_canonical_locale_name(int category, const char *locale); |
31 | |
32 | |
33 | /* |
34 | * fix_path_separator |
35 | * For non-Windows, just return the argument. |
36 | * For Windows convert any forward slash to a backslash |
37 | * such as is suitable for arguments to builtin commands |
38 | * like RMDIR and DEL. |
39 | */ |
40 | static char * |
41 | fix_path_separator(char *path) |
42 | { |
43 | #ifdef WIN32 |
44 | |
45 | char *result; |
46 | char *c; |
47 | |
48 | result = pg_strdup(path); |
49 | |
50 | for (c = result; *c != '\0'; c++) |
51 | if (*c == '/') |
52 | *c = '\\'; |
53 | |
54 | return result; |
55 | #else |
56 | |
57 | return path; |
58 | #endif |
59 | } |
60 | |
61 | void |
62 | output_check_banner(bool live_check) |
63 | { |
64 | if (user_opts.check && live_check) |
65 | { |
66 | pg_log(PG_REPORT, |
67 | "Performing Consistency Checks on Old Live Server\n" |
68 | "------------------------------------------------\n" ); |
69 | } |
70 | else |
71 | { |
72 | pg_log(PG_REPORT, |
73 | "Performing Consistency Checks\n" |
74 | "-----------------------------\n" ); |
75 | } |
76 | } |
77 | |
78 | |
79 | void |
80 | check_and_dump_old_cluster(bool live_check) |
81 | { |
82 | /* -- OLD -- */ |
83 | |
84 | if (!live_check) |
85 | start_postmaster(&old_cluster, true); |
86 | |
87 | /* Extract a list of databases and tables from the old cluster */ |
88 | get_db_and_rel_infos(&old_cluster); |
89 | |
90 | init_tablespaces(); |
91 | |
92 | get_loadable_libraries(); |
93 | |
94 | |
95 | /* |
96 | * Check for various failure cases |
97 | */ |
98 | check_is_install_user(&old_cluster); |
99 | check_proper_datallowconn(&old_cluster); |
100 | check_for_prepared_transactions(&old_cluster); |
101 | check_for_reg_data_type_usage(&old_cluster); |
102 | check_for_isn_and_int8_passing_mismatch(&old_cluster); |
103 | |
104 | /* |
105 | * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not |
106 | * supported anymore. Verify there are none, iff applicable. |
107 | */ |
108 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100) |
109 | check_for_tables_with_oids(&old_cluster); |
110 | |
111 | /* |
112 | * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged |
113 | * hash indexes |
114 | */ |
115 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906) |
116 | { |
117 | old_9_6_check_for_unknown_data_type_usage(&old_cluster); |
118 | if (user_opts.check) |
119 | old_9_6_invalidate_hash_indexes(&old_cluster, true); |
120 | } |
121 | |
122 | /* 9.5 and below should not have roles starting with pg_ */ |
123 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905) |
124 | check_for_pg_role_prefix(&old_cluster); |
125 | |
126 | if (GET_MAJOR_VERSION(old_cluster.major_version) == 904 && |
127 | old_cluster.controldata.cat_ver < JSONB_FORMAT_CHANGE_CAT_VER) |
128 | check_for_jsonb_9_4_usage(&old_cluster); |
129 | |
130 | /* Pre-PG 9.4 had a different 'line' data type internal format */ |
131 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 903) |
132 | old_9_3_check_for_line_data_type_usage(&old_cluster); |
133 | |
134 | /* Pre-PG 9.0 had no large object permissions */ |
135 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) |
136 | new_9_0_populate_pg_largeobject_metadata(&old_cluster, true); |
137 | |
138 | /* |
139 | * While not a check option, we do this now because this is the only time |
140 | * the old server is running. |
141 | */ |
142 | if (!user_opts.check) |
143 | generate_old_dump(); |
144 | |
145 | if (!live_check) |
146 | stop_postmaster(false); |
147 | } |
148 | |
149 | |
150 | void |
151 | check_new_cluster(void) |
152 | { |
153 | get_db_and_rel_infos(&new_cluster); |
154 | |
155 | check_new_cluster_is_empty(); |
156 | check_databases_are_compatible(); |
157 | |
158 | check_loadable_libraries(); |
159 | |
160 | switch (user_opts.transfer_mode) |
161 | { |
162 | case TRANSFER_MODE_CLONE: |
163 | check_file_clone(); |
164 | break; |
165 | case TRANSFER_MODE_COPY: |
166 | break; |
167 | case TRANSFER_MODE_LINK: |
168 | check_hard_link(); |
169 | break; |
170 | } |
171 | |
172 | check_is_install_user(&new_cluster); |
173 | |
174 | check_for_prepared_transactions(&new_cluster); |
175 | } |
176 | |
177 | |
178 | void |
179 | report_clusters_compatible(void) |
180 | { |
181 | if (user_opts.check) |
182 | { |
183 | pg_log(PG_REPORT, "\n*Clusters are compatible*\n" ); |
184 | /* stops new cluster */ |
185 | stop_postmaster(false); |
186 | exit(0); |
187 | } |
188 | |
189 | pg_log(PG_REPORT, "\n" |
190 | "If pg_upgrade fails after this point, you must re-initdb the\n" |
191 | "new cluster before continuing.\n" ); |
192 | } |
193 | |
194 | |
195 | void |
196 | issue_warnings_and_set_wal_level(void) |
197 | { |
198 | /* |
199 | * We unconditionally start/stop the new server because pg_resetwal -o set |
200 | * wal_level to 'minimum'. If the user is upgrading standby servers using |
201 | * the rsync instructions, they will need pg_upgrade to write its final |
202 | * WAL record showing wal_level as 'replica'. |
203 | */ |
204 | start_postmaster(&new_cluster, true); |
205 | |
206 | /* Create dummy large object permissions for old < PG 9.0? */ |
207 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) |
208 | new_9_0_populate_pg_largeobject_metadata(&new_cluster, false); |
209 | |
210 | /* Reindex hash indexes for old < 10.0 */ |
211 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906) |
212 | old_9_6_invalidate_hash_indexes(&new_cluster, false); |
213 | |
214 | stop_postmaster(false); |
215 | } |
216 | |
217 | |
218 | void |
219 | output_completion_banner(char *analyze_script_file_name, |
220 | char *deletion_script_file_name) |
221 | { |
222 | /* Did we copy the free space files? */ |
223 | if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804) |
224 | pg_log(PG_REPORT, |
225 | "Optimizer statistics are not transferred by pg_upgrade so,\n" |
226 | "once you start the new server, consider running:\n" |
227 | " %s\n\n" , analyze_script_file_name); |
228 | else |
229 | pg_log(PG_REPORT, |
230 | "Optimizer statistics and free space information are not transferred\n" |
231 | "by pg_upgrade so, once you start the new server, consider running:\n" |
232 | " %s\n\n" , analyze_script_file_name); |
233 | |
234 | |
235 | if (deletion_script_file_name) |
236 | pg_log(PG_REPORT, |
237 | "Running this script will delete the old cluster's data files:\n" |
238 | " %s\n" , |
239 | deletion_script_file_name); |
240 | else |
241 | pg_log(PG_REPORT, |
242 | "Could not create a script to delete the old cluster's data files\n" |
243 | "because user-defined tablespaces or the new cluster's data directory\n" |
244 | "exist in the old cluster directory. The old cluster's contents must\n" |
245 | "be deleted manually.\n" ); |
246 | } |
247 | |
248 | |
249 | void |
250 | check_cluster_versions(void) |
251 | { |
252 | prep_status("Checking cluster versions" ); |
253 | |
254 | /* cluster versions should already have been obtained */ |
255 | Assert(old_cluster.major_version != 0); |
256 | Assert(new_cluster.major_version != 0); |
257 | |
258 | /* |
259 | * We allow upgrades from/to the same major version for alpha/beta |
260 | * upgrades |
261 | */ |
262 | |
263 | if (GET_MAJOR_VERSION(old_cluster.major_version) < 804) |
264 | pg_fatal("This utility can only upgrade from PostgreSQL version 8.4 and later.\n" ); |
265 | |
266 | /* Only current PG version is supported as a target */ |
267 | if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM)) |
268 | pg_fatal("This utility can only upgrade to PostgreSQL version %s.\n" , |
269 | PG_MAJORVERSION); |
270 | |
271 | /* |
272 | * We can't allow downgrading because we use the target pg_dump, and |
273 | * pg_dump cannot operate on newer database versions, only current and |
274 | * older versions. |
275 | */ |
276 | if (old_cluster.major_version > new_cluster.major_version) |
277 | pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n" ); |
278 | |
279 | /* Ensure binaries match the designated data directories */ |
280 | if (GET_MAJOR_VERSION(old_cluster.major_version) != |
281 | GET_MAJOR_VERSION(old_cluster.bin_version)) |
282 | pg_fatal("Old cluster data and binary directories are from different major versions.\n" ); |
283 | if (GET_MAJOR_VERSION(new_cluster.major_version) != |
284 | GET_MAJOR_VERSION(new_cluster.bin_version)) |
285 | pg_fatal("New cluster data and binary directories are from different major versions.\n" ); |
286 | |
287 | check_ok(); |
288 | } |
289 | |
290 | |
291 | void |
292 | check_cluster_compatibility(bool live_check) |
293 | { |
294 | /* get/check pg_control data of servers */ |
295 | get_control_data(&old_cluster, live_check); |
296 | get_control_data(&new_cluster, false); |
297 | check_control_data(&old_cluster.controldata, &new_cluster.controldata); |
298 | |
299 | /* We read the real port number for PG >= 9.1 */ |
300 | if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) < 901 && |
301 | old_cluster.port == DEF_PGUPORT) |
302 | pg_fatal("When checking a pre-PG 9.1 live old server, " |
303 | "you must specify the old server's port number.\n" ); |
304 | |
305 | if (live_check && old_cluster.port == new_cluster.port) |
306 | pg_fatal("When checking a live server, " |
307 | "the old and new port numbers must be different.\n" ); |
308 | } |
309 | |
310 | |
311 | /* |
312 | * check_locale_and_encoding() |
313 | * |
314 | * Check that locale and encoding of a database in the old and new clusters |
315 | * are compatible. |
316 | */ |
317 | static void |
318 | check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb) |
319 | { |
320 | if (olddb->db_encoding != newdb->db_encoding) |
321 | pg_fatal("encodings for database \"%s\" do not match: old \"%s\", new \"%s\"\n" , |
322 | olddb->db_name, |
323 | pg_encoding_to_char(olddb->db_encoding), |
324 | pg_encoding_to_char(newdb->db_encoding)); |
325 | if (!equivalent_locale(LC_COLLATE, olddb->db_collate, newdb->db_collate)) |
326 | pg_fatal("lc_collate values for database \"%s\" do not match: old \"%s\", new \"%s\"\n" , |
327 | olddb->db_name, olddb->db_collate, newdb->db_collate); |
328 | if (!equivalent_locale(LC_CTYPE, olddb->db_ctype, newdb->db_ctype)) |
329 | pg_fatal("lc_ctype values for database \"%s\" do not match: old \"%s\", new \"%s\"\n" , |
330 | olddb->db_name, olddb->db_ctype, newdb->db_ctype); |
331 | } |
332 | |
333 | /* |
334 | * equivalent_locale() |
335 | * |
336 | * Best effort locale-name comparison. Return false if we are not 100% sure |
337 | * the locales are equivalent. |
338 | * |
339 | * Note: The encoding parts of the names are ignored. This function is |
340 | * currently used to compare locale names stored in pg_database, and |
341 | * pg_database contains a separate encoding field. That's compared directly |
342 | * in check_locale_and_encoding(). |
343 | */ |
344 | static bool |
345 | equivalent_locale(int category, const char *loca, const char *locb) |
346 | { |
347 | const char *chara; |
348 | const char *charb; |
349 | char *canona; |
350 | char *canonb; |
351 | int lena; |
352 | int lenb; |
353 | |
354 | /* |
355 | * If the names are equal, the locales are equivalent. Checking this first |
356 | * avoids calling setlocale() in the common case that the names are equal. |
357 | * That's a good thing, if setlocale() is buggy, for example. |
358 | */ |
359 | if (pg_strcasecmp(loca, locb) == 0) |
360 | return true; |
361 | |
362 | /* |
363 | * Not identical. Canonicalize both names, remove the encoding parts, and |
364 | * try again. |
365 | */ |
366 | canona = get_canonical_locale_name(category, loca); |
367 | chara = strrchr(canona, '.'); |
368 | lena = chara ? (chara - canona) : strlen(canona); |
369 | |
370 | canonb = get_canonical_locale_name(category, locb); |
371 | charb = strrchr(canonb, '.'); |
372 | lenb = charb ? (charb - canonb) : strlen(canonb); |
373 | |
374 | if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0) |
375 | { |
376 | pg_free(canona); |
377 | pg_free(canonb); |
378 | return true; |
379 | } |
380 | |
381 | pg_free(canona); |
382 | pg_free(canonb); |
383 | return false; |
384 | } |
385 | |
386 | |
387 | static void |
388 | check_new_cluster_is_empty(void) |
389 | { |
390 | int dbnum; |
391 | |
392 | for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++) |
393 | { |
394 | int relnum; |
395 | RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr; |
396 | |
397 | for (relnum = 0; relnum < rel_arr->nrels; |
398 | relnum++) |
399 | { |
400 | /* pg_largeobject and its index should be skipped */ |
401 | if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog" ) != 0) |
402 | pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"\n" , |
403 | new_cluster.dbarr.dbs[dbnum].db_name, |
404 | rel_arr->rels[relnum].nspname, |
405 | rel_arr->rels[relnum].relname); |
406 | } |
407 | } |
408 | } |
409 | |
410 | /* |
411 | * Check that every database that already exists in the new cluster is |
412 | * compatible with the corresponding database in the old one. |
413 | */ |
414 | static void |
415 | check_databases_are_compatible(void) |
416 | { |
417 | int newdbnum; |
418 | int olddbnum; |
419 | DbInfo *newdbinfo; |
420 | DbInfo *olddbinfo; |
421 | |
422 | for (newdbnum = 0; newdbnum < new_cluster.dbarr.ndbs; newdbnum++) |
423 | { |
424 | newdbinfo = &new_cluster.dbarr.dbs[newdbnum]; |
425 | |
426 | /* Find the corresponding database in the old cluster */ |
427 | for (olddbnum = 0; olddbnum < old_cluster.dbarr.ndbs; olddbnum++) |
428 | { |
429 | olddbinfo = &old_cluster.dbarr.dbs[olddbnum]; |
430 | if (strcmp(newdbinfo->db_name, olddbinfo->db_name) == 0) |
431 | { |
432 | check_locale_and_encoding(olddbinfo, newdbinfo); |
433 | break; |
434 | } |
435 | } |
436 | } |
437 | } |
438 | |
439 | |
440 | /* |
441 | * create_script_for_cluster_analyze() |
442 | * |
443 | * This incrementally generates better optimizer statistics |
444 | */ |
445 | void |
446 | create_script_for_cluster_analyze(char **analyze_script_file_name) |
447 | { |
448 | FILE *script = NULL; |
449 | PQExpBufferData user_specification; |
450 | |
451 | prep_status("Creating script to analyze new cluster" ); |
452 | |
453 | initPQExpBuffer(&user_specification); |
454 | if (os_info.user_specified) |
455 | { |
456 | appendPQExpBufferStr(&user_specification, "-U " ); |
457 | appendShellString(&user_specification, os_info.user); |
458 | appendPQExpBufferChar(&user_specification, ' '); |
459 | } |
460 | |
461 | *analyze_script_file_name = psprintf("%sanalyze_new_cluster.%s" , |
462 | SCRIPT_PREFIX, SCRIPT_EXT); |
463 | |
464 | if ((script = fopen_priv(*analyze_script_file_name, "w" )) == NULL) |
465 | pg_fatal("could not open file \"%s\": %s\n" , |
466 | *analyze_script_file_name, strerror(errno)); |
467 | |
468 | #ifndef WIN32 |
469 | /* add shebang header */ |
470 | fprintf(script, "#!/bin/sh\n\n" ); |
471 | #else |
472 | /* suppress command echoing */ |
473 | fprintf(script, "@echo off\n" ); |
474 | #endif |
475 | |
476 | fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n" , |
477 | ECHO_QUOTE, ECHO_QUOTE); |
478 | fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n" , |
479 | ECHO_QUOTE, ECHO_QUOTE); |
480 | fprintf(script, "echo %swith increasing accuracy. When it is done, your system will%s\n" , |
481 | ECHO_QUOTE, ECHO_QUOTE); |
482 | fprintf(script, "echo %shave the default level of optimizer statistics.%s\n" , |
483 | ECHO_QUOTE, ECHO_QUOTE); |
484 | fprintf(script, "echo%s\n\n" , ECHO_BLANK); |
485 | |
486 | fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n" , |
487 | ECHO_QUOTE, ECHO_QUOTE); |
488 | fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n" , |
489 | ECHO_QUOTE, ECHO_QUOTE); |
490 | fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n" , |
491 | ECHO_QUOTE, ECHO_QUOTE); |
492 | fprintf(script, "echo%s\n\n" , ECHO_BLANK); |
493 | |
494 | fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n" , |
495 | ECHO_QUOTE, ECHO_QUOTE); |
496 | fprintf(script, "echo %sthis script and run:%s\n" , |
497 | ECHO_QUOTE, ECHO_QUOTE); |
498 | fprintf(script, "echo %s \"%s/vacuumdb\" %s--all %s%s\n" , ECHO_QUOTE, |
499 | new_cluster.bindir, user_specification.data, |
500 | /* Did we copy the free space files? */ |
501 | (GET_MAJOR_VERSION(old_cluster.major_version) >= 804) ? |
502 | "--analyze-only" : "--analyze" , ECHO_QUOTE); |
503 | fprintf(script, "echo%s\n\n" , ECHO_BLANK); |
504 | |
505 | fprintf(script, "\"%s/vacuumdb\" %s--all --analyze-in-stages\n" , |
506 | new_cluster.bindir, user_specification.data); |
507 | /* Did we copy the free space files? */ |
508 | if (GET_MAJOR_VERSION(old_cluster.major_version) < 804) |
509 | fprintf(script, "\"%s/vacuumdb\" %s--all\n" , new_cluster.bindir, |
510 | user_specification.data); |
511 | |
512 | fprintf(script, "echo%s\n\n" , ECHO_BLANK); |
513 | fprintf(script, "echo %sDone%s\n" , |
514 | ECHO_QUOTE, ECHO_QUOTE); |
515 | |
516 | fclose(script); |
517 | |
518 | #ifndef WIN32 |
519 | if (chmod(*analyze_script_file_name, S_IRWXU) != 0) |
520 | pg_fatal("could not add execute permission to file \"%s\": %s\n" , |
521 | *analyze_script_file_name, strerror(errno)); |
522 | #endif |
523 | |
524 | termPQExpBuffer(&user_specification); |
525 | |
526 | check_ok(); |
527 | } |
528 | |
529 | |
530 | /* |
531 | * create_script_for_old_cluster_deletion() |
532 | * |
533 | * This is particularly useful for tablespace deletion. |
534 | */ |
535 | void |
536 | create_script_for_old_cluster_deletion(char **deletion_script_file_name) |
537 | { |
538 | FILE *script = NULL; |
539 | int tblnum; |
540 | char old_cluster_pgdata[MAXPGPATH], |
541 | new_cluster_pgdata[MAXPGPATH]; |
542 | |
543 | *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s" , |
544 | SCRIPT_PREFIX, SCRIPT_EXT); |
545 | |
546 | strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH); |
547 | canonicalize_path(old_cluster_pgdata); |
548 | |
549 | strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH); |
550 | canonicalize_path(new_cluster_pgdata); |
551 | |
552 | /* Some people put the new data directory inside the old one. */ |
553 | if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata)) |
554 | { |
555 | pg_log(PG_WARNING, |
556 | "\nWARNING: new data directory should not be inside the old data directory, e.g. %s\n" , old_cluster_pgdata); |
557 | |
558 | /* Unlink file in case it is left over from a previous run. */ |
559 | unlink(*deletion_script_file_name); |
560 | pg_free(*deletion_script_file_name); |
561 | *deletion_script_file_name = NULL; |
562 | return; |
563 | } |
564 | |
565 | /* |
566 | * Some users (oddly) create tablespaces inside the cluster data |
567 | * directory. We can't create a proper old cluster delete script in that |
568 | * case. |
569 | */ |
570 | for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++) |
571 | { |
572 | char old_tablespace_dir[MAXPGPATH]; |
573 | |
574 | strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH); |
575 | canonicalize_path(old_tablespace_dir); |
576 | if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir)) |
577 | { |
578 | /* reproduce warning from CREATE TABLESPACE that is in the log */ |
579 | pg_log(PG_WARNING, |
580 | "\nWARNING: user-defined tablespace locations should not be inside the data directory, e.g. %s\n" , old_tablespace_dir); |
581 | |
582 | /* Unlink file in case it is left over from a previous run. */ |
583 | unlink(*deletion_script_file_name); |
584 | pg_free(*deletion_script_file_name); |
585 | *deletion_script_file_name = NULL; |
586 | return; |
587 | } |
588 | } |
589 | |
590 | prep_status("Creating script to delete old cluster" ); |
591 | |
592 | if ((script = fopen_priv(*deletion_script_file_name, "w" )) == NULL) |
593 | pg_fatal("could not open file \"%s\": %s\n" , |
594 | *deletion_script_file_name, strerror(errno)); |
595 | |
596 | #ifndef WIN32 |
597 | /* add shebang header */ |
598 | fprintf(script, "#!/bin/sh\n\n" ); |
599 | #endif |
600 | |
601 | /* delete old cluster's default tablespace */ |
602 | fprintf(script, RMDIR_CMD " %c%s%c\n" , PATH_QUOTE, |
603 | fix_path_separator(old_cluster.pgdata), PATH_QUOTE); |
604 | |
605 | /* delete old cluster's alternate tablespaces */ |
606 | for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++) |
607 | { |
608 | /* |
609 | * Do the old cluster's per-database directories share a directory |
610 | * with a new version-specific tablespace? |
611 | */ |
612 | if (strlen(old_cluster.tablespace_suffix) == 0) |
613 | { |
614 | /* delete per-database directories */ |
615 | int dbnum; |
616 | |
617 | fprintf(script, "\n" ); |
618 | /* remove PG_VERSION? */ |
619 | if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) |
620 | fprintf(script, RM_CMD " %s%cPG_VERSION\n" , |
621 | fix_path_separator(os_info.old_tablespaces[tblnum]), |
622 | PATH_SEPARATOR); |
623 | |
624 | for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++) |
625 | fprintf(script, RMDIR_CMD " %c%s%c%d%c\n" , PATH_QUOTE, |
626 | fix_path_separator(os_info.old_tablespaces[tblnum]), |
627 | PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid, |
628 | PATH_QUOTE); |
629 | } |
630 | else |
631 | { |
632 | char *suffix_path = pg_strdup(old_cluster.tablespace_suffix); |
633 | |
634 | /* |
635 | * Simply delete the tablespace directory, which might be ".old" |
636 | * or a version-specific subdirectory. |
637 | */ |
638 | fprintf(script, RMDIR_CMD " %c%s%s%c\n" , PATH_QUOTE, |
639 | fix_path_separator(os_info.old_tablespaces[tblnum]), |
640 | fix_path_separator(suffix_path), PATH_QUOTE); |
641 | pfree(suffix_path); |
642 | } |
643 | } |
644 | |
645 | fclose(script); |
646 | |
647 | #ifndef WIN32 |
648 | if (chmod(*deletion_script_file_name, S_IRWXU) != 0) |
649 | pg_fatal("could not add execute permission to file \"%s\": %s\n" , |
650 | *deletion_script_file_name, strerror(errno)); |
651 | #endif |
652 | |
653 | check_ok(); |
654 | } |
655 | |
656 | |
657 | /* |
658 | * check_is_install_user() |
659 | * |
660 | * Check we are the install user, and that the new cluster |
661 | * has no other users. |
662 | */ |
663 | static void |
664 | check_is_install_user(ClusterInfo *cluster) |
665 | { |
666 | PGresult *res; |
667 | PGconn *conn = connectToServer(cluster, "template1" ); |
668 | |
669 | prep_status("Checking database user is the install user" ); |
670 | |
671 | /* Can't use pg_authid because only superusers can view it. */ |
672 | res = executeQueryOrDie(conn, |
673 | "SELECT rolsuper, oid " |
674 | "FROM pg_catalog.pg_roles " |
675 | "WHERE rolname = current_user " |
676 | "AND rolname !~ '^pg_'" ); |
677 | |
678 | /* |
679 | * We only allow the install user in the new cluster (see comment below) |
680 | * and we preserve pg_authid.oid, so this must be the install user in the |
681 | * old cluster too. |
682 | */ |
683 | if (PQntuples(res) != 1 || |
684 | atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID) |
685 | pg_fatal("database user \"%s\" is not the install user\n" , |
686 | os_info.user); |
687 | |
688 | PQclear(res); |
689 | |
690 | res = executeQueryOrDie(conn, |
691 | "SELECT COUNT(*) " |
692 | "FROM pg_catalog.pg_roles " |
693 | "WHERE rolname !~ '^pg_'" ); |
694 | |
695 | if (PQntuples(res) != 1) |
696 | pg_fatal("could not determine the number of users\n" ); |
697 | |
698 | /* |
699 | * We only allow the install user in the new cluster because other defined |
700 | * users might match users defined in the old cluster and generate an |
701 | * error during pg_dump restore. |
702 | */ |
703 | if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1) |
704 | pg_fatal("Only the install user can be defined in the new cluster.\n" ); |
705 | |
706 | PQclear(res); |
707 | |
708 | PQfinish(conn); |
709 | |
710 | check_ok(); |
711 | } |
712 | |
713 | |
714 | static void |
715 | check_proper_datallowconn(ClusterInfo *cluster) |
716 | { |
717 | int dbnum; |
718 | PGconn *conn_template1; |
719 | PGresult *dbres; |
720 | int ntups; |
721 | int i_datname; |
722 | int i_datallowconn; |
723 | |
724 | prep_status("Checking database connection settings" ); |
725 | |
726 | conn_template1 = connectToServer(cluster, "template1" ); |
727 | |
728 | /* get database names */ |
729 | dbres = executeQueryOrDie(conn_template1, |
730 | "SELECT datname, datallowconn " |
731 | "FROM pg_catalog.pg_database" ); |
732 | |
733 | i_datname = PQfnumber(dbres, "datname" ); |
734 | i_datallowconn = PQfnumber(dbres, "datallowconn" ); |
735 | |
736 | ntups = PQntuples(dbres); |
737 | for (dbnum = 0; dbnum < ntups; dbnum++) |
738 | { |
739 | char *datname = PQgetvalue(dbres, dbnum, i_datname); |
740 | char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn); |
741 | |
742 | if (strcmp(datname, "template0" ) == 0) |
743 | { |
744 | /* avoid restore failure when pg_dumpall tries to create template0 */ |
745 | if (strcmp(datallowconn, "t" ) == 0) |
746 | pg_fatal("template0 must not allow connections, " |
747 | "i.e. its pg_database.datallowconn must be false\n" ); |
748 | } |
749 | else |
750 | { |
751 | /* |
752 | * avoid datallowconn == false databases from being skipped on |
753 | * restore |
754 | */ |
755 | if (strcmp(datallowconn, "f" ) == 0) |
756 | pg_fatal("All non-template0 databases must allow connections, " |
757 | "i.e. their pg_database.datallowconn must be true\n" ); |
758 | } |
759 | } |
760 | |
761 | PQclear(dbres); |
762 | |
763 | PQfinish(conn_template1); |
764 | |
765 | check_ok(); |
766 | } |
767 | |
768 | |
769 | /* |
770 | * check_for_prepared_transactions() |
771 | * |
772 | * Make sure there are no prepared transactions because the storage format |
773 | * might have changed. |
774 | */ |
775 | static void |
776 | check_for_prepared_transactions(ClusterInfo *cluster) |
777 | { |
778 | PGresult *res; |
779 | PGconn *conn = connectToServer(cluster, "template1" ); |
780 | |
781 | prep_status("Checking for prepared transactions" ); |
782 | |
783 | res = executeQueryOrDie(conn, |
784 | "SELECT * " |
785 | "FROM pg_catalog.pg_prepared_xacts" ); |
786 | |
787 | if (PQntuples(res) != 0) |
788 | { |
789 | if (cluster == &old_cluster) |
790 | pg_fatal("The source cluster contains prepared transactions\n" ); |
791 | else |
792 | pg_fatal("The target cluster contains prepared transactions\n" ); |
793 | } |
794 | |
795 | PQclear(res); |
796 | |
797 | PQfinish(conn); |
798 | |
799 | check_ok(); |
800 | } |
801 | |
802 | |
803 | /* |
804 | * check_for_isn_and_int8_passing_mismatch() |
805 | * |
806 | * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed |
807 | * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so |
808 | * it must match for the old and new servers. |
809 | */ |
810 | static void |
811 | check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster) |
812 | { |
813 | int dbnum; |
814 | FILE *script = NULL; |
815 | bool found = false; |
816 | char output_path[MAXPGPATH]; |
817 | |
818 | prep_status("Checking for contrib/isn with bigint-passing mismatch" ); |
819 | |
820 | if (old_cluster.controldata.float8_pass_by_value == |
821 | new_cluster.controldata.float8_pass_by_value) |
822 | { |
823 | /* no mismatch */ |
824 | check_ok(); |
825 | return; |
826 | } |
827 | |
828 | snprintf(output_path, sizeof(output_path), |
829 | "contrib_isn_and_int8_pass_by_value.txt" ); |
830 | |
831 | for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) |
832 | { |
833 | PGresult *res; |
834 | bool db_used = false; |
835 | int ntups; |
836 | int rowno; |
837 | int i_nspname, |
838 | i_proname; |
839 | DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; |
840 | PGconn *conn = connectToServer(cluster, active_db->db_name); |
841 | |
842 | /* Find any functions coming from contrib/isn */ |
843 | res = executeQueryOrDie(conn, |
844 | "SELECT n.nspname, p.proname " |
845 | "FROM pg_catalog.pg_proc p, " |
846 | " pg_catalog.pg_namespace n " |
847 | "WHERE p.pronamespace = n.oid AND " |
848 | " p.probin = '$libdir/isn'" ); |
849 | |
850 | ntups = PQntuples(res); |
851 | i_nspname = PQfnumber(res, "nspname" ); |
852 | i_proname = PQfnumber(res, "proname" ); |
853 | for (rowno = 0; rowno < ntups; rowno++) |
854 | { |
855 | found = true; |
856 | if (script == NULL && (script = fopen_priv(output_path, "w" )) == NULL) |
857 | pg_fatal("could not open file \"%s\": %s\n" , |
858 | output_path, strerror(errno)); |
859 | if (!db_used) |
860 | { |
861 | fprintf(script, "Database: %s\n" , active_db->db_name); |
862 | db_used = true; |
863 | } |
864 | fprintf(script, " %s.%s\n" , |
865 | PQgetvalue(res, rowno, i_nspname), |
866 | PQgetvalue(res, rowno, i_proname)); |
867 | } |
868 | |
869 | PQclear(res); |
870 | |
871 | PQfinish(conn); |
872 | } |
873 | |
874 | if (script) |
875 | fclose(script); |
876 | |
877 | if (found) |
878 | { |
879 | pg_log(PG_REPORT, "fatal\n" ); |
880 | pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n" |
881 | "bigint data type. Your old and new clusters pass bigint values\n" |
882 | "differently so this cluster cannot currently be upgraded. You can\n" |
883 | "manually upgrade databases that use \"contrib/isn\" facilities and remove\n" |
884 | "\"contrib/isn\" from the old cluster and restart the upgrade. A list of\n" |
885 | "the problem functions is in the file:\n" |
886 | " %s\n\n" , output_path); |
887 | } |
888 | else |
889 | check_ok(); |
890 | } |
891 | |
892 | |
893 | /* |
894 | * Verify that no tables are declared WITH OIDS. |
895 | */ |
896 | static void |
897 | check_for_tables_with_oids(ClusterInfo *cluster) |
898 | { |
899 | int dbnum; |
900 | FILE *script = NULL; |
901 | bool found = false; |
902 | char output_path[MAXPGPATH]; |
903 | |
904 | prep_status("Checking for tables WITH OIDS" ); |
905 | |
906 | snprintf(output_path, sizeof(output_path), |
907 | "tables_with_oids.txt" ); |
908 | |
909 | /* Find any tables declared WITH OIDS */ |
910 | for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) |
911 | { |
912 | PGresult *res; |
913 | bool db_used = false; |
914 | int ntups; |
915 | int rowno; |
916 | int i_nspname, |
917 | i_relname; |
918 | DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; |
919 | PGconn *conn = connectToServer(cluster, active_db->db_name); |
920 | |
921 | res = executeQueryOrDie(conn, |
922 | "SELECT n.nspname, c.relname " |
923 | "FROM pg_catalog.pg_class c, " |
924 | " pg_catalog.pg_namespace n " |
925 | "WHERE c.relnamespace = n.oid AND " |
926 | " c.relhasoids AND" |
927 | " n.nspname NOT IN ('pg_catalog')" ); |
928 | |
929 | ntups = PQntuples(res); |
930 | i_nspname = PQfnumber(res, "nspname" ); |
931 | i_relname = PQfnumber(res, "relname" ); |
932 | for (rowno = 0; rowno < ntups; rowno++) |
933 | { |
934 | found = true; |
935 | if (script == NULL && (script = fopen_priv(output_path, "w" )) == NULL) |
936 | pg_fatal("could not open file \"%s\": %s\n" , |
937 | output_path, strerror(errno)); |
938 | if (!db_used) |
939 | { |
940 | fprintf(script, "Database: %s\n" , active_db->db_name); |
941 | db_used = true; |
942 | } |
943 | fprintf(script, " %s.%s\n" , |
944 | PQgetvalue(res, rowno, i_nspname), |
945 | PQgetvalue(res, rowno, i_relname)); |
946 | } |
947 | |
948 | PQclear(res); |
949 | |
950 | PQfinish(conn); |
951 | } |
952 | |
953 | if (script) |
954 | fclose(script); |
955 | |
956 | if (found) |
957 | { |
958 | pg_log(PG_REPORT, "fatal\n" ); |
959 | pg_fatal("Your installation contains tables declared WITH OIDS, which is not supported\n" |
960 | "anymore. Consider removing the oid column using\n" |
961 | " ALTER TABLE ... SET WITHOUT OIDS;\n" |
962 | "A list of tables with the problem is in the file:\n" |
963 | " %s\n\n" , output_path); |
964 | } |
965 | else |
966 | check_ok(); |
967 | } |
968 | |
969 | |
970 | /* |
971 | * check_for_reg_data_type_usage() |
972 | * pg_upgrade only preserves these system values: |
973 | * pg_class.oid |
974 | * pg_type.oid |
975 | * pg_enum.oid |
976 | * |
977 | * Many of the reg* data types reference system catalog info that is |
978 | * not preserved, and hence these data types cannot be used in user |
979 | * tables upgraded by pg_upgrade. |
980 | */ |
981 | static void |
982 | check_for_reg_data_type_usage(ClusterInfo *cluster) |
983 | { |
984 | int dbnum; |
985 | FILE *script = NULL; |
986 | bool found = false; |
987 | char output_path[MAXPGPATH]; |
988 | |
989 | prep_status("Checking for reg* data types in user tables" ); |
990 | |
991 | snprintf(output_path, sizeof(output_path), "tables_using_reg.txt" ); |
992 | |
993 | for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) |
994 | { |
995 | PGresult *res; |
996 | bool db_used = false; |
997 | int ntups; |
998 | int rowno; |
999 | int i_nspname, |
1000 | i_relname, |
1001 | i_attname; |
1002 | DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; |
1003 | PGconn *conn = connectToServer(cluster, active_db->db_name); |
1004 | |
1005 | /* |
1006 | * While several relkinds don't store any data, e.g. views, they can |
1007 | * be used to define data types of other columns, so we check all |
1008 | * relkinds. |
1009 | */ |
1010 | res = executeQueryOrDie(conn, |
1011 | "SELECT n.nspname, c.relname, a.attname " |
1012 | "FROM pg_catalog.pg_class c, " |
1013 | " pg_catalog.pg_namespace n, " |
1014 | " pg_catalog.pg_attribute a, " |
1015 | " pg_catalog.pg_type t " |
1016 | "WHERE c.oid = a.attrelid AND " |
1017 | " NOT a.attisdropped AND " |
1018 | " a.atttypid = t.oid AND " |
1019 | " t.typnamespace = " |
1020 | " (SELECT oid FROM pg_namespace " |
1021 | " WHERE nspname = 'pg_catalog') AND" |
1022 | " t.typname IN ( " |
1023 | /* regclass.oid is preserved, so 'regclass' is OK */ |
1024 | " 'regconfig', " |
1025 | " 'regdictionary', " |
1026 | " 'regnamespace', " |
1027 | " 'regoper', " |
1028 | " 'regoperator', " |
1029 | " 'regproc', " |
1030 | " 'regprocedure' " |
1031 | /* regrole.oid is preserved, so 'regrole' is OK */ |
1032 | /* regtype.oid is preserved, so 'regtype' is OK */ |
1033 | " ) AND " |
1034 | " c.relnamespace = n.oid AND " |
1035 | " n.nspname NOT IN ('pg_catalog', 'information_schema')" ); |
1036 | |
1037 | ntups = PQntuples(res); |
1038 | i_nspname = PQfnumber(res, "nspname" ); |
1039 | i_relname = PQfnumber(res, "relname" ); |
1040 | i_attname = PQfnumber(res, "attname" ); |
1041 | for (rowno = 0; rowno < ntups; rowno++) |
1042 | { |
1043 | found = true; |
1044 | if (script == NULL && (script = fopen_priv(output_path, "w" )) == NULL) |
1045 | pg_fatal("could not open file \"%s\": %s\n" , |
1046 | output_path, strerror(errno)); |
1047 | if (!db_used) |
1048 | { |
1049 | fprintf(script, "Database: %s\n" , active_db->db_name); |
1050 | db_used = true; |
1051 | } |
1052 | fprintf(script, " %s.%s.%s\n" , |
1053 | PQgetvalue(res, rowno, i_nspname), |
1054 | PQgetvalue(res, rowno, i_relname), |
1055 | PQgetvalue(res, rowno, i_attname)); |
1056 | } |
1057 | |
1058 | PQclear(res); |
1059 | |
1060 | PQfinish(conn); |
1061 | } |
1062 | |
1063 | if (script) |
1064 | fclose(script); |
1065 | |
1066 | if (found) |
1067 | { |
1068 | pg_log(PG_REPORT, "fatal\n" ); |
1069 | pg_fatal("Your installation contains one of the reg* data types in user tables.\n" |
1070 | "These data types reference system OIDs that are not preserved by\n" |
1071 | "pg_upgrade, so this cluster cannot currently be upgraded. You can\n" |
1072 | "remove the problem tables and restart the upgrade. A list of the problem\n" |
1073 | "columns is in the file:\n" |
1074 | " %s\n\n" , output_path); |
1075 | } |
1076 | else |
1077 | check_ok(); |
1078 | } |
1079 | |
1080 | |
1081 | /* |
1082 | * check_for_jsonb_9_4_usage() |
1083 | * |
1084 | * JSONB changed its storage format during 9.4 beta, so check for it. |
1085 | */ |
1086 | static void |
1087 | check_for_jsonb_9_4_usage(ClusterInfo *cluster) |
1088 | { |
1089 | int dbnum; |
1090 | FILE *script = NULL; |
1091 | bool found = false; |
1092 | char output_path[MAXPGPATH]; |
1093 | |
1094 | prep_status("Checking for incompatible \"jsonb\" data type" ); |
1095 | |
1096 | snprintf(output_path, sizeof(output_path), "tables_using_jsonb.txt" ); |
1097 | |
1098 | for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) |
1099 | { |
1100 | PGresult *res; |
1101 | bool db_used = false; |
1102 | int ntups; |
1103 | int rowno; |
1104 | int i_nspname, |
1105 | i_relname, |
1106 | i_attname; |
1107 | DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; |
1108 | PGconn *conn = connectToServer(cluster, active_db->db_name); |
1109 | |
1110 | /* |
1111 | * While several relkinds don't store any data, e.g. views, they can |
1112 | * be used to define data types of other columns, so we check all |
1113 | * relkinds. |
1114 | */ |
1115 | res = executeQueryOrDie(conn, |
1116 | "SELECT n.nspname, c.relname, a.attname " |
1117 | "FROM pg_catalog.pg_class c, " |
1118 | " pg_catalog.pg_namespace n, " |
1119 | " pg_catalog.pg_attribute a " |
1120 | "WHERE c.oid = a.attrelid AND " |
1121 | " NOT a.attisdropped AND " |
1122 | " a.atttypid = 'pg_catalog.jsonb'::pg_catalog.regtype AND " |
1123 | " c.relnamespace = n.oid AND " |
1124 | /* exclude possible orphaned temp tables */ |
1125 | " n.nspname !~ '^pg_temp_' AND " |
1126 | " n.nspname NOT IN ('pg_catalog', 'information_schema')" ); |
1127 | |
1128 | ntups = PQntuples(res); |
1129 | i_nspname = PQfnumber(res, "nspname" ); |
1130 | i_relname = PQfnumber(res, "relname" ); |
1131 | i_attname = PQfnumber(res, "attname" ); |
1132 | for (rowno = 0; rowno < ntups; rowno++) |
1133 | { |
1134 | found = true; |
1135 | if (script == NULL && (script = fopen_priv(output_path, "w" )) == NULL) |
1136 | pg_fatal("could not open file \"%s\": %s\n" , |
1137 | output_path, strerror(errno)); |
1138 | if (!db_used) |
1139 | { |
1140 | fprintf(script, "Database: %s\n" , active_db->db_name); |
1141 | db_used = true; |
1142 | } |
1143 | fprintf(script, " %s.%s.%s\n" , |
1144 | PQgetvalue(res, rowno, i_nspname), |
1145 | PQgetvalue(res, rowno, i_relname), |
1146 | PQgetvalue(res, rowno, i_attname)); |
1147 | } |
1148 | |
1149 | PQclear(res); |
1150 | |
1151 | PQfinish(conn); |
1152 | } |
1153 | |
1154 | if (script) |
1155 | fclose(script); |
1156 | |
1157 | if (found) |
1158 | { |
1159 | pg_log(PG_REPORT, "fatal\n" ); |
1160 | pg_fatal("Your installation contains the \"jsonb\" data type in user tables.\n" |
1161 | "The internal format of \"jsonb\" changed during 9.4 beta so this cluster cannot currently\n" |
1162 | "be upgraded. You can remove the problem tables and restart the upgrade. A list\n" |
1163 | "of the problem columns is in the file:\n" |
1164 | " %s\n\n" , output_path); |
1165 | } |
1166 | else |
1167 | check_ok(); |
1168 | } |
1169 | |
1170 | /* |
1171 | * check_for_pg_role_prefix() |
1172 | * |
1173 | * Versions older than 9.6 should not have any pg_* roles |
1174 | */ |
1175 | static void |
1176 | check_for_pg_role_prefix(ClusterInfo *cluster) |
1177 | { |
1178 | PGresult *res; |
1179 | PGconn *conn = connectToServer(cluster, "template1" ); |
1180 | |
1181 | prep_status("Checking for roles starting with \"pg_\"" ); |
1182 | |
1183 | res = executeQueryOrDie(conn, |
1184 | "SELECT * " |
1185 | "FROM pg_catalog.pg_roles " |
1186 | "WHERE rolname ~ '^pg_'" ); |
1187 | |
1188 | if (PQntuples(res) != 0) |
1189 | { |
1190 | if (cluster == &old_cluster) |
1191 | pg_fatal("The source cluster contains roles starting with \"pg_\"\n" ); |
1192 | else |
1193 | pg_fatal("The target cluster contains roles starting with \"pg_\"\n" ); |
1194 | } |
1195 | |
1196 | PQclear(res); |
1197 | |
1198 | PQfinish(conn); |
1199 | |
1200 | check_ok(); |
1201 | } |
1202 | |
1203 | |
1204 | /* |
1205 | * get_canonical_locale_name |
1206 | * |
1207 | * Send the locale name to the system, and hope we get back a canonical |
1208 | * version. This should match the backend's check_locale() function. |
1209 | */ |
1210 | static char * |
1211 | get_canonical_locale_name(int category, const char *locale) |
1212 | { |
1213 | char *save; |
1214 | char *res; |
1215 | |
1216 | /* get the current setting, so we can restore it. */ |
1217 | save = setlocale(category, NULL); |
1218 | if (!save) |
1219 | pg_fatal("failed to get the current locale\n" ); |
1220 | |
1221 | /* 'save' may be pointing at a modifiable scratch variable, so copy it. */ |
1222 | save = pg_strdup(save); |
1223 | |
1224 | /* set the locale with setlocale, to see if it accepts it. */ |
1225 | res = setlocale(category, locale); |
1226 | |
1227 | if (!res) |
1228 | pg_fatal("failed to get system locale name for \"%s\"\n" , locale); |
1229 | |
1230 | res = pg_strdup(res); |
1231 | |
1232 | /* restore old value. */ |
1233 | if (!setlocale(category, save)) |
1234 | pg_fatal("failed to restore old locale \"%s\"\n" , save); |
1235 | |
1236 | pg_free(save); |
1237 | |
1238 | return res; |
1239 | } |
1240 | |