forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheck.c
1469 lines (1239 loc) · 40.3 KB
/
check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* check.c
*
* server checks and output routines
*
* Copyright (c) 2010-2020, PostgreSQL Global Development Group
* src/bin/pg_upgrade/check.c
*/
#include "postgres_fe.h"
#include "catalog/pg_authid_d.h"
#include "fe_utils/string_utils.h"
#include "mb/pg_wchar.h"
#include "pg_upgrade.h"
static void check_new_cluster_is_empty(void);
static void check_databases_are_compatible(void);
static void check_for_changed_signatures(void);
static void check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb);
static bool equivalent_locale(int category, const char *loca, const char *locb);
static void check_is_install_user(ClusterInfo *cluster);
static void check_proper_datallowconn(ClusterInfo *cluster);
static void check_for_prepared_transactions(ClusterInfo *cluster);
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
static void check_for_tables_with_oids(ClusterInfo *cluster);
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
static void check_for_pg_role_prefix(ClusterInfo *cluster);
static char *get_canonical_locale_name(int category, const char *locale);
/*
* fix_path_separator
* For non-Windows, just return the argument.
* For Windows convert any forward slash to a backslash
* such as is suitable for arguments to builtin commands
* like RMDIR and DEL.
*/
static char *
fix_path_separator(char *path)
{
#ifdef WIN32
char *result;
char *c;
result = pg_strdup(path);
for (c = result; *c != '\0'; c++)
if (*c == '/')
*c = '\\';
return result;
#else
return path;
#endif
}
void
output_check_banner(bool live_check)
{
if (user_opts.check && live_check)
{
pg_log(PG_REPORT,
"Performing Consistency Checks on Old Live Server\n"
"------------------------------------------------\n");
}
else
{
pg_log(PG_REPORT,
"Performing Consistency Checks\n"
"-----------------------------\n");
}
}
void
check_and_dump_old_cluster(bool live_check)
{
/* -- OLD -- */
if (!live_check)
start_postmaster(&old_cluster, true);
/* Extract a list of databases and tables from the old cluster */
get_db_and_rel_infos(&old_cluster);
init_tablespaces();
get_loadable_libraries();
/*
* Check for various failure cases
*/
check_is_install_user(&old_cluster);
check_proper_datallowconn(&old_cluster);
check_for_prepared_transactions(&old_cluster);
check_for_reg_data_type_usage(&old_cluster);
check_for_isn_and_int8_passing_mismatch(&old_cluster);
/*
* Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
* supported anymore. Verify there are none, iff applicable.
*/
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
check_for_tables_with_oids(&old_cluster);
/*
* PG 12 changed the 'sql_identifier' type storage to be based on name,
* not varchar, which breaks on-disk format for existing data. So we need
* to prevent upgrade when used in user objects (tables, indexes, ...).
*/
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
old_11_check_for_sql_identifier_data_type_usage(&old_cluster);
/*
* Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
* hash indexes
*/
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
{
old_9_6_check_for_unknown_data_type_usage(&old_cluster);
if (user_opts.check)
old_9_6_invalidate_hash_indexes(&old_cluster, true);
}
/* 9.5 and below should not have roles starting with pg_ */
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
check_for_pg_role_prefix(&old_cluster);
if (GET_MAJOR_VERSION(old_cluster.major_version) == 904 &&
old_cluster.controldata.cat_ver < JSONB_FORMAT_CHANGE_CAT_VER)
check_for_jsonb_9_4_usage(&old_cluster);
/* Pre-PG 9.4 had a different 'line' data type internal format */
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 903)
old_9_3_check_for_line_data_type_usage(&old_cluster);
/* Pre-PG 9.0 had no large object permissions */
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
new_9_0_populate_pg_largeobject_metadata(&old_cluster, true);
get_non_default_acl_infos(&old_cluster);
/*
* While not a check option, we do this now because this is the only time
* the old server is running.
*/
if (!user_opts.check)
generate_old_dump();
if (!live_check)
stop_postmaster(false);
}
void
check_new_cluster(void)
{
get_db_and_rel_infos(&new_cluster);
check_new_cluster_is_empty();
check_databases_are_compatible();
check_for_changed_signatures();
check_loadable_libraries();
switch (user_opts.transfer_mode)
{
case TRANSFER_MODE_CLONE:
check_file_clone();
break;
case TRANSFER_MODE_COPY:
break;
case TRANSFER_MODE_LINK:
check_hard_link();
break;
}
check_is_install_user(&new_cluster);
check_for_prepared_transactions(&new_cluster);
}
void
report_clusters_compatible(void)
{
if (user_opts.check)
{
pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
/* stops new cluster */
stop_postmaster(false);
exit(0);
}
pg_log(PG_REPORT, "\n"
"If pg_upgrade fails after this point, you must re-initdb the\n"
"new cluster before continuing.\n");
}
void
issue_warnings_and_set_wal_level(void)
{
/*
* We unconditionally start/stop the new server because pg_resetwal -o set
* wal_level to 'minimum'. If the user is upgrading standby servers using
* the rsync instructions, they will need pg_upgrade to write its final
* WAL record showing wal_level as 'replica'.
*/
start_postmaster(&new_cluster, true);
/* Create dummy large object permissions for old < PG 9.0? */
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
new_9_0_populate_pg_largeobject_metadata(&new_cluster, false);
/* Reindex hash indexes for old < 10.0 */
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
old_9_6_invalidate_hash_indexes(&new_cluster, false);
stop_postmaster(false);
}
void
output_completion_banner(char *analyze_script_file_name,
char *deletion_script_file_name)
{
/* Did we copy the free space files? */
if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
pg_log(PG_REPORT,
"Optimizer statistics are not transferred by pg_upgrade so,\n"
"once you start the new server, consider running:\n"
" %s\n\n", analyze_script_file_name);
else
pg_log(PG_REPORT,
"Optimizer statistics and free space information are not transferred\n"
"by pg_upgrade so, once you start the new server, consider running:\n"
" %s\n\n", analyze_script_file_name);
if (deletion_script_file_name)
pg_log(PG_REPORT,
"Running this script will delete the old cluster's data files:\n"
" %s\n",
deletion_script_file_name);
else
pg_log(PG_REPORT,
"Could not create a script to delete the old cluster's data files\n"
"because user-defined tablespaces or the new cluster's data directory\n"
"exist in the old cluster directory. The old cluster's contents must\n"
"be deleted manually.\n");
}
void
check_cluster_versions(void)
{
prep_status("Checking cluster versions");
/* cluster versions should already have been obtained */
Assert(old_cluster.major_version != 0);
Assert(new_cluster.major_version != 0);
/*
* We allow upgrades from/to the same major version for alpha/beta
* upgrades
*/
if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
pg_fatal("This utility can only upgrade from PostgreSQL version 8.4 and later.\n");
/* Only current PG version is supported as a target */
if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
pg_fatal("This utility can only upgrade to PostgreSQL version %s.\n",
PG_MAJORVERSION);
/*
* We can't allow downgrading because we use the target pg_dump, and
* pg_dump cannot operate on newer database versions, only current and
* older versions.
*/
if (old_cluster.major_version > new_cluster.major_version)
pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
/* Ensure binaries match the designated data directories */
if (GET_MAJOR_VERSION(old_cluster.major_version) !=
GET_MAJOR_VERSION(old_cluster.bin_version))
pg_fatal("Old cluster data and binary directories are from different major versions.\n");
if (GET_MAJOR_VERSION(new_cluster.major_version) !=
GET_MAJOR_VERSION(new_cluster.bin_version))
pg_fatal("New cluster data and binary directories are from different major versions.\n");
check_ok();
}
void
check_cluster_compatibility(bool live_check)
{
/* get/check pg_control data of servers */
get_control_data(&old_cluster, live_check);
get_control_data(&new_cluster, false);
check_control_data(&old_cluster.controldata, &new_cluster.controldata);
/* We read the real port number for PG >= 9.1 */
if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) < 901 &&
old_cluster.port == DEF_PGUPORT)
pg_fatal("When checking a pre-PG 9.1 live old server, "
"you must specify the old server's port number.\n");
if (live_check && old_cluster.port == new_cluster.port)
pg_fatal("When checking a live server, "
"the old and new port numbers must be different.\n");
}
/*
* check_locale_and_encoding()
*
* Check that locale and encoding of a database in the old and new clusters
* are compatible.
*/
static void
check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb)
{
if (olddb->db_encoding != newdb->db_encoding)
pg_fatal("encodings for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
olddb->db_name,
pg_encoding_to_char(olddb->db_encoding),
pg_encoding_to_char(newdb->db_encoding));
if (!equivalent_locale(LC_COLLATE, olddb->db_collate, newdb->db_collate))
pg_fatal("lc_collate values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
olddb->db_name, olddb->db_collate, newdb->db_collate);
if (!equivalent_locale(LC_CTYPE, olddb->db_ctype, newdb->db_ctype))
pg_fatal("lc_ctype values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
olddb->db_name, olddb->db_ctype, newdb->db_ctype);
}
/*
* equivalent_locale()
*
* Best effort locale-name comparison. Return false if we are not 100% sure
* the locales are equivalent.
*
* Note: The encoding parts of the names are ignored. This function is
* currently used to compare locale names stored in pg_database, and
* pg_database contains a separate encoding field. That's compared directly
* in check_locale_and_encoding().
*/
static bool
equivalent_locale(int category, const char *loca, const char *locb)
{
const char *chara;
const char *charb;
char *canona;
char *canonb;
int lena;
int lenb;
/*
* If the names are equal, the locales are equivalent. Checking this first
* avoids calling setlocale() in the common case that the names are equal.
* That's a good thing, if setlocale() is buggy, for example.
*/
if (pg_strcasecmp(loca, locb) == 0)
return true;
/*
* Not identical. Canonicalize both names, remove the encoding parts, and
* try again.
*/
canona = get_canonical_locale_name(category, loca);
chara = strrchr(canona, '.');
lena = chara ? (chara - canona) : strlen(canona);
canonb = get_canonical_locale_name(category, locb);
charb = strrchr(canonb, '.');
lenb = charb ? (charb - canonb) : strlen(canonb);
if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0)
{
pg_free(canona);
pg_free(canonb);
return true;
}
pg_free(canona);
pg_free(canonb);
return false;
}
static void
check_new_cluster_is_empty(void)
{
int dbnum;
for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
{
int relnum;
RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
for (relnum = 0; relnum < rel_arr->nrels;
relnum++)
{
/* pg_largeobject and its index should be skipped */
if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"\n",
new_cluster.dbarr.dbs[dbnum].db_name,
rel_arr->rels[relnum].nspname,
rel_arr->rels[relnum].relname);
}
}
}
/*
* Check that every database that already exists in the new cluster is
* compatible with the corresponding database in the old one.
*/
static void
check_databases_are_compatible(void)
{
int newdbnum;
int olddbnum;
DbInfo *newdbinfo;
DbInfo *olddbinfo;
for (newdbnum = 0; newdbnum < new_cluster.dbarr.ndbs; newdbnum++)
{
newdbinfo = &new_cluster.dbarr.dbs[newdbnum];
/* Find the corresponding database in the old cluster */
for (olddbnum = 0; olddbnum < old_cluster.dbarr.ndbs; olddbnum++)
{
olddbinfo = &old_cluster.dbarr.dbs[olddbnum];
if (strcmp(newdbinfo->db_name, olddbinfo->db_name) == 0)
{
check_locale_and_encoding(olddbinfo, newdbinfo);
break;
}
}
}
}
/*
* Find the location of the last dot, return NULL if not found.
*/
static char *
last_dot_location(const char *identity)
{
const char *p,
*ret = NULL;
for (p = identity; *p; p++)
if (*p == '.')
ret = p;
return unconstify(char *, ret);
}
/*
* check_for_changed_signatures()
*
* Check that the old cluster doesn't have non-default ACL's for system objects
* (relations, attributes, functions and procedures) which have different
* signatures in the new cluster. Otherwise generate revoke_objects.sql.
*/
static void
check_for_changed_signatures(void)
{
PGconn *conn;
char subquery[QUERY_ALLOC];
PGresult *res;
int ntups;
int i_obj_ident;
int dbnum;
bool need_check = false;
FILE *script = NULL;
bool found_changed = false;
char output_path[MAXPGPATH];
prep_status("Checking for system objects with non-default ACL");
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
if (old_cluster.dbarr.dbs[dbnum].non_def_acl_arr.nacls > 0)
{
need_check = true;
break;
}
/*
* The old cluster doesn't have system objects with non-default ACL so
* quickly exit.
*/
if (!need_check)
{
check_ok();
return;
}
snprintf(output_path, sizeof(output_path), "revoke_objects.sql");
snprintf(subquery, sizeof(subquery),
/* Get system relations which created in pg_catalog */
"SELECT 'pg_class'::regclass classid, oid objid, 0 objsubid "
"FROM pg_catalog.pg_class "
"WHERE relnamespace = 'pg_catalog'::regnamespace "
"UNION ALL "
/* Get system relations attributes which created in pg_catalog */
"SELECT 'pg_class'::regclass, att.attrelid, att.attnum "
"FROM pg_catalog.pg_class rel "
"INNER JOIN pg_catalog.pg_attribute att ON rel.oid = att.attrelid "
"WHERE rel.relnamespace = 'pg_catalog'::regnamespace "
"UNION ALL "
/* Get system functions and procedure which created in pg_catalog */
"SELECT 'pg_proc'::regclass, oid, 0 "
"FROM pg_catalog.pg_proc "
"WHERE pronamespace = 'pg_catalog'::regnamespace ");
conn = connectToServer(&new_cluster, "template1");
res = executeQueryOrDie(conn,
"SELECT ident.type, ident.identity "
"FROM (%s) obj, "
"LATERAL pg_catalog.pg_identify_object("
" obj.classid, obj.objid, obj.objsubid) ident "
/*
* Don't rely on database collation, since we use strcmp
* comparison to find non-default ACLs.
*/
"ORDER BY ident.identity COLLATE \"C\";", subquery);
ntups = PQntuples(res);
i_obj_ident = PQfnumber(res, "identity");
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
{
DbInfo *dbinfo = &old_cluster.dbarr.dbs[dbnum];
bool db_used = false;
int aclnum = 0,
objnum = 0;
/*
* For every database check system objects with non-default ACL.
*
* AclInfo array is sorted by obj_ident. This allows us to compare
* AclInfo entries with the query result above efficiently.
*/
for (aclnum = 0; aclnum < dbinfo->non_def_acl_arr.nacls; aclnum++)
{
AclInfo *aclinfo = &dbinfo->non_def_acl_arr.aclinfos[aclnum];
bool report = false;
while (objnum < ntups)
{
int ret;
ret = strcmp(aclinfo->obj_ident,
PQgetvalue(res, objnum, i_obj_ident));
/*
* The new cluster doesn't have an object with same identity,
* exit the loop, report below and check next object.
*/
if (ret < 0)
{
report = true;
break;
}
/*
* The new cluster has an object with same identity, just exit
* the loop.
*/
else if (ret == 0)
{
objnum++;
break;
}
else
objnum++;
}
if (report)
{
found_changed = true;
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
pg_fatal("could not open file \"%s\": %s\n",
output_path, strerror(errno));
if (!db_used)
{
PQExpBufferData conn_buf;
initPQExpBuffer(&conn_buf);
appendPsqlMetaConnect(&conn_buf, dbinfo->db_name);
fputs(conn_buf.data, script);
termPQExpBuffer(&conn_buf);
db_used = true;
}
/* Handle columns separately */
if (strstr(aclinfo->obj_type, "column") != NULL)
{
char *pdot = last_dot_location(aclinfo->obj_ident);
PQExpBufferData ident_buf;
if (pdot == NULL || *(pdot + 1) == '\0')
pg_fatal("invalid column identity \"%s\"",
aclinfo->obj_ident);
initPQExpBuffer(&ident_buf);
appendBinaryPQExpBuffer(&ident_buf, aclinfo->obj_ident,
pdot - aclinfo->obj_ident);
fprintf(script, "REVOKE ALL (%s) ON %s FROM %s;\n",
/* pg_identify_object() quotes identity if necessary */
pdot + 1, ident_buf.data,
/* role_names is already quoted */
aclinfo->role_names);
termPQExpBuffer(&ident_buf);
}
/*
* For relations except sequences we don't need to specify
* the object type.
*/
else if (aclinfo->is_relation &&
strcmp(aclinfo->obj_type, "sequence") != 0)
fprintf(script, "REVOKE ALL ON %s FROM %s;\n",
/* pg_identify_object() quotes identity if necessary */
aclinfo->obj_ident,
/* role_names is already quoted */
aclinfo->role_names);
/* Other object types */
else
fprintf(script, "REVOKE ALL ON %s %s FROM %s;\n",
aclinfo->obj_type,
/* pg_identify_object() quotes identity if necessary */
aclinfo->obj_ident,
/* role_names is already quoted */
aclinfo->role_names);
}
}
}
PQclear(res);
PQfinish(conn);
if (script)
fclose(script);
if (found_changed)
{
pg_log(PG_REPORT, "fatal\n");
pg_fatal("Your installation contains non-default privileges for system objects\n"
"for which the API has changed. To perform the upgrade, reset these\n"
"privileges to default. The file\n"
" %s\n"
"when executed by psql will revoke non-default privileges for those objects.\n\n",
output_path);
}
else
check_ok();
}
/*
* create_script_for_cluster_analyze()
*
* This incrementally generates better optimizer statistics
*/
void
create_script_for_cluster_analyze(char **analyze_script_file_name)
{
FILE *script = NULL;
PQExpBufferData user_specification;
prep_status("Creating script to analyze new cluster");
initPQExpBuffer(&user_specification);
if (os_info.user_specified)
{
appendPQExpBufferStr(&user_specification, "-U ");
appendShellString(&user_specification, os_info.user);
appendPQExpBufferChar(&user_specification, ' ');
}
*analyze_script_file_name = psprintf("%sanalyze_new_cluster.%s",
SCRIPT_PREFIX, SCRIPT_EXT);
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
pg_fatal("could not open file \"%s\": %s\n",
*analyze_script_file_name, strerror(errno));
#ifndef WIN32
/* add shebang header */
fprintf(script, "#!/bin/sh\n\n");
#else
/* suppress command echoing */
fprintf(script, "@echo off\n");
#endif
fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %swith increasing accuracy. When it is done, your system will%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %shave the default level of optimizer statistics.%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo%s\n\n", ECHO_BLANK);
fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo%s\n\n", ECHO_BLANK);
fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %sthis script and run:%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fprintf(script, "echo %s \"%s/vacuumdb\" %s--all %s%s\n", ECHO_QUOTE,
new_cluster.bindir, user_specification.data,
/* Did we copy the free space files? */
(GET_MAJOR_VERSION(old_cluster.major_version) >= 804) ?
"--analyze-only" : "--analyze", ECHO_QUOTE);
fprintf(script, "echo%s\n\n", ECHO_BLANK);
fprintf(script, "\"%s/vacuumdb\" %s--all --analyze-in-stages\n",
new_cluster.bindir, user_specification.data);
/* Did we copy the free space files? */
if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
fprintf(script, "\"%s/vacuumdb\" %s--all\n", new_cluster.bindir,
user_specification.data);
fprintf(script, "echo%s\n\n", ECHO_BLANK);
fprintf(script, "echo %sDone%s\n",
ECHO_QUOTE, ECHO_QUOTE);
fclose(script);
#ifndef WIN32
if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
pg_fatal("could not add execute permission to file \"%s\": %s\n",
*analyze_script_file_name, strerror(errno));
#endif
termPQExpBuffer(&user_specification);
check_ok();
}
/*
* create_script_for_old_cluster_deletion()
*
* This is particularly useful for tablespace deletion.
*/
void
create_script_for_old_cluster_deletion(char **deletion_script_file_name)
{
FILE *script = NULL;
int tblnum;
char old_cluster_pgdata[MAXPGPATH],
new_cluster_pgdata[MAXPGPATH];
*deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
SCRIPT_PREFIX, SCRIPT_EXT);
strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
canonicalize_path(old_cluster_pgdata);
strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
canonicalize_path(new_cluster_pgdata);
/* Some people put the new data directory inside the old one. */
if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
{
pg_log(PG_WARNING,
"\nWARNING: new data directory should not be inside the old data directory, e.g. %s\n", old_cluster_pgdata);
/* Unlink file in case it is left over from a previous run. */
unlink(*deletion_script_file_name);
pg_free(*deletion_script_file_name);
*deletion_script_file_name = NULL;
return;
}
/*
* Some users (oddly) create tablespaces inside the cluster data
* directory. We can't create a proper old cluster delete script in that
* case.
*/
for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
{
char old_tablespace_dir[MAXPGPATH];
strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
canonicalize_path(old_tablespace_dir);
if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
{
/* reproduce warning from CREATE TABLESPACE that is in the log */
pg_log(PG_WARNING,
"\nWARNING: user-defined tablespace locations should not be inside the data directory, e.g. %s\n", old_tablespace_dir);
/* Unlink file in case it is left over from a previous run. */
unlink(*deletion_script_file_name);
pg_free(*deletion_script_file_name);
*deletion_script_file_name = NULL;
return;
}
}
prep_status("Creating script to delete old cluster");
if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
pg_fatal("could not open file \"%s\": %s\n",
*deletion_script_file_name, strerror(errno));
#ifndef WIN32
/* add shebang header */
fprintf(script, "#!/bin/sh\n\n");
#endif
/* delete old cluster's default tablespace */
fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
/* delete old cluster's alternate tablespaces */
for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
{
/*
* Do the old cluster's per-database directories share a directory
* with a new version-specific tablespace?
*/
if (strlen(old_cluster.tablespace_suffix) == 0)
{
/* delete per-database directories */
int dbnum;
fprintf(script, "\n");
/* remove PG_VERSION? */
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
fprintf(script, RM_CMD " %s%cPG_VERSION\n",
fix_path_separator(os_info.old_tablespaces[tblnum]),
PATH_SEPARATOR);
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
fprintf(script, RMDIR_CMD " %c%s%c%d%c\n", PATH_QUOTE,
fix_path_separator(os_info.old_tablespaces[tblnum]),
PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid,
PATH_QUOTE);
}
else
{
char *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
/*
* Simply delete the tablespace directory, which might be ".old"
* or a version-specific subdirectory.
*/
fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
fix_path_separator(os_info.old_tablespaces[tblnum]),
fix_path_separator(suffix_path), PATH_QUOTE);
pfree(suffix_path);
}
}
fclose(script);
#ifndef WIN32
if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
pg_fatal("could not add execute permission to file \"%s\": %s\n",
*deletion_script_file_name, strerror(errno));
#endif
check_ok();
}
/*
* check_is_install_user()
*
* Check we are the install user, and that the new cluster
* has no other users.
*/
static void
check_is_install_user(ClusterInfo *cluster)
{
PGresult *res;
PGconn *conn = connectToServer(cluster, "template1");
prep_status("Checking database user is the install user");
/* Can't use pg_authid because only superusers can view it. */
res = executeQueryOrDie(conn,
"SELECT rolsuper, oid "
"FROM pg_catalog.pg_roles "
"WHERE rolname = current_user "
"AND rolname !~ '^pg_'");
/*
* We only allow the install user in the new cluster (see comment below)
* and we preserve pg_authid.oid, so this must be the install user in the
* old cluster too.
*/
if (PQntuples(res) != 1 ||
atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
pg_fatal("database user \"%s\" is not the install user\n",
os_info.user);
PQclear(res);
res = executeQueryOrDie(conn,
"SELECT COUNT(*) "
"FROM pg_catalog.pg_roles "
"WHERE rolname !~ '^pg_'");
if (PQntuples(res) != 1)
pg_fatal("could not determine the number of users\n");
/*
* We only allow the install user in the new cluster because other defined
* users might match users defined in the old cluster and generate an
* error during pg_dump restore.
*/
if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1)
pg_fatal("Only the install user can be defined in the new cluster.\n");
PQclear(res);
PQfinish(conn);
check_ok();
}
static void
check_proper_datallowconn(ClusterInfo *cluster)
{
int dbnum;
PGconn *conn_template1;
PGresult *dbres;
int ntups;
int i_datname;
int i_datallowconn;
prep_status("Checking database connection settings");
conn_template1 = connectToServer(cluster, "template1");
/* get database names */
dbres = executeQueryOrDie(conn_template1,
"SELECT datname, datallowconn "
"FROM pg_catalog.pg_database");
i_datname = PQfnumber(dbres, "datname");
i_datallowconn = PQfnumber(dbres, "datallowconn");
ntups = PQntuples(dbres);
for (dbnum = 0; dbnum < ntups; dbnum++)
{
char *datname = PQgetvalue(dbres, dbnum, i_datname);
char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
if (strcmp(datname, "template0") == 0)
{
/* avoid restore failure when pg_dumpall tries to create template0 */
if (strcmp(datallowconn, "t") == 0)
pg_fatal("template0 must not allow connections, "
"i.e. its pg_database.datallowconn must be false\n");
}
else
{
/*
* avoid datallowconn == false databases from being skipped on
* restore
*/
if (strcmp(datallowconn, "f") == 0)
pg_fatal("All non-template0 databases must allow connections, "
"i.e. their pg_database.datallowconn must be true\n");
}
}
PQclear(dbres);
PQfinish(conn_template1);
check_ok();
}
/*
* check_for_prepared_transactions()
*
* Make sure there are no prepared transactions because the storage format