@@ -23,6 +23,11 @@ static void check_for_reg_data_type_usage(ClusterInfo *cluster);
23
23
static void check_for_support_lib(ClusterInfo *cluster);
24
24
static void get_bin_version(ClusterInfo *cluster);
25
25
26
+ #ifndef WIN32
27
+ #define ECHO_QUOTE "'"
28
+ #else
29
+ #define ECHO_QUOTE ""
30
+ #endif
26
31
27
32
void
28
33
output_check_banner(bool *live_check)
@@ -193,21 +198,20 @@ issue_warnings(char *sequence_script_file_name)
193
198
194
199
195
200
void
196
- output_completion_banner(char *deletion_script_file_name)
201
+ output_completion_banner(char *analyze_script_file_name,
202
+ char *deletion_script_file_name)
197
203
{
198
204
/* Did we copy the free space files? */
199
205
if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
200
206
pg_log(PG_REPORT,
201
- "Optimizer statistics are not transferred by pg_upgrade so\n"
202
- "consider running:\n"
203
- " vacuumdb --all --analyze-only\n"
204
- "on the newly-upgraded cluster.\n\n");
207
+ "Optimizer statistics are not transferred by pg_upgrade so,\n"
208
+ "once you start the new server, consider running:\n"
209
+ " %s\n\n", analyze_script_file_name);
205
210
else
206
211
pg_log(PG_REPORT,
207
212
"Optimizer statistics and free space information are not transferred\n"
208
- "by pg_upgrade so consider running:\n"
209
- " vacuumdb --all --analyze\n"
210
- "on the newly-upgraded cluster.\n\n");
213
+ "by pg_upgrade so, once you start the new server, consider running:\n"
214
+ " %s\n\n", analyze_script_file_name);
211
215
212
216
pg_log(PG_REPORT,
213
217
"Running this script will delete the old cluster's data files:\n"
@@ -379,6 +383,130 @@ check_new_cluster_is_empty(void)
379
383
}
380
384
381
385
386
+ /*
387
+ * create_script_for_cluster_analyze()
388
+ *
389
+ * This incrementally generates better optimizer statistics
390
+ */
391
+ void
392
+ create_script_for_cluster_analyze(char **analyze_script_file_name)
393
+ {
394
+ FILE *script = NULL;
395
+
396
+ *analyze_script_file_name = pg_malloc(MAXPGPATH);
397
+
398
+ prep_status("Creating script to analyze new cluster");
399
+
400
+ snprintf(*analyze_script_file_name, MAXPGPATH, "analyze_new_cluster.%s",
401
+ SCRIPT_EXT);
402
+
403
+ if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
404
+ pg_log(PG_FATAL, "Could not open file \"%s\": %s\n",
405
+ *analyze_script_file_name, getErrorText(errno));
406
+
407
+ #ifndef WIN32
408
+ /* add shebang header */
409
+ fprintf(script, "#!/bin/sh\n\n");
410
+ #endif
411
+
412
+ fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n",
413
+ ECHO_QUOTE, ECHO_QUOTE);
414
+ fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n",
415
+ ECHO_QUOTE, ECHO_QUOTE);
416
+ fprintf(script, "echo %swith increasing accuracy. When it is done, your system will%s\n",
417
+ ECHO_QUOTE, ECHO_QUOTE);
418
+ fprintf(script, "echo %shave the default level of optimizer statistics.%s\n",
419
+ ECHO_QUOTE, ECHO_QUOTE);
420
+ fprintf(script, "echo\n\n");
421
+
422
+ fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n",
423
+ ECHO_QUOTE, ECHO_QUOTE);
424
+ fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n",
425
+ ECHO_QUOTE, ECHO_QUOTE);
426
+ fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n",
427
+ ECHO_QUOTE, ECHO_QUOTE);
428
+ fprintf(script, "echo\n\n");
429
+
430
+ fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n",
431
+ ECHO_QUOTE, ECHO_QUOTE);
432
+ fprintf(script, "echo %sthis script and run:%s\n",
433
+ ECHO_QUOTE, ECHO_QUOTE);
434
+ fprintf(script, "echo %s vacuumdb --all %s%s\n", ECHO_QUOTE,
435
+ /* Did we copy the free space files? */
436
+ (GET_MAJOR_VERSION(old_cluster.major_version) >= 804) ?
437
+ "--analyze-only" : "--analyze", ECHO_QUOTE);
438
+ fprintf(script, "echo\n\n");
439
+
440
+ #ifndef WIN32
441
+ fprintf(script, "sleep 2\n");
442
+ fprintf(script, "PGOPTIONS='-c default_statistics_target=1 -c vacuum_cost_delay=0'\n");
443
+ /* only need to export once */
444
+ fprintf(script, "export PGOPTIONS\n");
445
+ #else
446
+ fprintf(script, "REM simulate sleep 2\n");
447
+ fprintf(script, "PING 1.1.1.1 -n 1 -w 2000 > nul\n");
448
+ fprintf(script, "SET PGOPTIONS=-c default_statistics_target=1 -c vacuum_cost_delay=0\n");
449
+ #endif
450
+
451
+ fprintf(script, "echo %sGenerating minimal optimizer statistics (1 target)%s\n",
452
+ ECHO_QUOTE, ECHO_QUOTE);
453
+ fprintf(script, "echo %s--------------------------------------------------%s\n",
454
+ ECHO_QUOTE, ECHO_QUOTE);
455
+ fprintf(script, "vacuumdb --all --analyze-only\n");
456
+ fprintf(script, "echo\n");
457
+ fprintf(script, "echo %sThe server is now available with minimal optimizer statistics.%s\n",
458
+ ECHO_QUOTE, ECHO_QUOTE);
459
+ fprintf(script, "echo %sQuery performance will be optimal once this script completes.%s\n",
460
+ ECHO_QUOTE, ECHO_QUOTE);
461
+ fprintf(script, "echo\n\n");
462
+
463
+ #ifndef WIN32
464
+ fprintf(script, "sleep 2\n");
465
+ fprintf(script, "PGOPTIONS='-c default_statistics_target=10'\n");
466
+ #else
467
+ fprintf(script, "REM simulate sleep\n");
468
+ fprintf(script, "PING 1.1.1.1 -n 1 -w 2000 > nul\n");
469
+ fprintf(script, "SET PGOPTIONS=-c default_statistics_target=10\n");
470
+ #endif
471
+
472
+ fprintf(script, "echo %sGenerating medium optimizer statistics (10 targets)%s\n",
473
+ ECHO_QUOTE, ECHO_QUOTE);
474
+ fprintf(script, "echo %s---------------------------------------------------%s\n",
475
+ ECHO_QUOTE, ECHO_QUOTE);
476
+ fprintf(script, "vacuumdb --all --analyze-only\n");
477
+ fprintf(script, "echo\n\n");
478
+
479
+ #ifndef WIN32
480
+ fprintf(script, "unset PGOPTIONS\n");
481
+ #else
482
+ fprintf(script, "SET PGOPTIONS\n");
483
+ #endif
484
+
485
+ fprintf(script, "echo %sGenerating default (full) optimizer statistics (100 targets?)%s\n",
486
+ ECHO_QUOTE, ECHO_QUOTE);
487
+ fprintf(script, "echo %s-------------------------------------------------------------%s\n",
488
+ ECHO_QUOTE, ECHO_QUOTE);
489
+ fprintf(script, "vacuumdb --all %s\n",
490
+ /* Did we copy the free space files? */
491
+ (GET_MAJOR_VERSION(old_cluster.major_version) >= 804) ?
492
+ "--analyze-only" : "--analyze");
493
+
494
+ fprintf(script, "echo\n\n");
495
+ fprintf(script, "echo %sDone%s\n",
496
+ ECHO_QUOTE, ECHO_QUOTE);
497
+
498
+ fclose(script);
499
+
500
+ #ifndef WIN32
501
+ if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
502
+ pg_log(PG_FATAL, "Could not add execute permission to file \"%s\": %s\n",
503
+ *analyze_script_file_name, getErrorText(errno));
504
+ #endif
505
+
506
+ check_ok();
507
+ }
508
+
509
+
382
510
/*
383
511
* create_script_for_old_cluster_deletion()
384
512
*
0 commit comments