| 1 |
/* |
| 2 |
* info.c |
| 3 |
* |
| 4 |
* information support functions |
| 5 |
* |
| 6 |
* Copyright (c) 2010-2025, PostgreSQL Global Development Group |
| 7 |
* src/bin/pg_upgrade/info.c |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "postgres_fe.h" |
| 11 |
|
| 12 |
#include "access/transam.h" |
| 13 |
#include "catalog/pg_class_d.h" |
| 14 |
#include "pg_upgrade.h" |
| 15 |
#include "pqexpbuffer.h" |
| 16 |
|
| 17 |
static void create_rel_filename_map(const char *old_data, const char *new_data, |
| 18 |
const DbInfo *old_db, const DbInfo *new_db, |
| 19 |
const RelInfo *old_rel, const RelInfo *new_rel, |
| 20 |
FileNameMap *map); |
| 21 |
static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db, |
| 22 |
bool is_new_db); |
| 23 |
static void free_db_and_rel_infos(DbInfoArr *db_arr); |
| 24 |
static void get_template0_info(ClusterInfo *cluster); |
| 25 |
static void get_db_infos(ClusterInfo *cluster); |
| 26 |
static char *get_rel_infos_query(void); |
| 27 |
static void process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg); |
| 28 |
static void free_rel_infos(RelInfoArr *rel_arr); |
| 29 |
static void print_db_infos(DbInfoArr *db_arr); |
| 30 |
static void print_rel_infos(RelInfoArr *rel_arr); |
| 31 |
static void print_slot_infos(LogicalSlotInfoArr *slot_arr); |
| 32 |
static char *get_old_cluster_logical_slot_infos_query(void); |
| 33 |
static void process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg); |
| 34 |
|
| 35 |
|
| 36 |
/* |
| 37 |
* gen_db_file_maps() |
| 38 |
* |
| 39 |
* generates a database mapping from "old_db" to "new_db". |
| 40 |
* |
| 41 |
* Returns a malloc'ed array of mappings. The length of the array |
| 42 |
* is returned into *nmaps. |
| 43 |
*/ |
| 44 |
FileNameMap * |
| 45 |
gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, |
| 46 |
int *nmaps, |
| 47 |
const char *old_pgdata, const char *new_pgdata) |
| 48 |
{ |
| 49 |
FileNameMap *maps; |
| 50 |
int old_relnum, |
| 51 |
new_relnum; |
| 52 |
int num_maps = 0; |
| 53 |
bool all_matched = true; |
| 54 |
|
| 55 |
/* There will certainly not be more mappings than there are old rels */ |
| 56 |
maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) * |
| 57 |
old_db->rel_arr.nrels); |
| 58 |
|
| 59 |
/* |
| 60 |
* Each of the RelInfo arrays should be sorted by OID. Scan through them |
| 61 |
* and match them up. If we fail to match everything, we'll abort, but |
| 62 |
* first print as much info as we can about mismatches. |
| 63 |
*/ |
| 64 |
old_relnum = new_relnum = 0; |
| 65 |
while (old_relnum < old_db->rel_arr.nrels || |
| 66 |
new_relnum < new_db->rel_arr.nrels) |
| 67 |
{ |
| 68 |
RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ? |
| 69 |
&old_db->rel_arr.rels[old_relnum] : NULL; |
| 70 |
RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ? |
| 71 |
&new_db->rel_arr.rels[new_relnum] : NULL; |
| 72 |
|
| 73 |
/* handle running off one array before the other */ |
| 74 |
if (!new_rel) |
| 75 |
{ |
| 76 |
/* |
| 77 |
* old_rel is unmatched. This should never happen, because we |
| 78 |
* force new rels to have TOAST tables if the old one did. |
| 79 |
*/ |
| 80 |
report_unmatched_relation(old_rel, old_db, false); |
| 81 |
all_matched = false; |
| 82 |
old_relnum++; |
| 83 |
continue; |
| 84 |
} |
| 85 |
if (!old_rel) |
| 86 |
{ |
| 87 |
/* |
| 88 |
* new_rel is unmatched. This shouldn't really happen either, but |
| 89 |
* if it's a TOAST table, we can ignore it and continue |
| 90 |
* processing, assuming that the new server made a TOAST table |
| 91 |
* that wasn't needed. |
| 92 |
*/ |
| 93 |
if (strcmp(new_rel->nspname, "pg_toast") != 0) |
| 94 |
{ |
| 95 |
report_unmatched_relation(new_rel, new_db, true); |
| 96 |
all_matched = false; |
| 97 |
} |
| 98 |
new_relnum++; |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
/* check for mismatched OID */ |
| 103 |
if (old_rel->reloid < new_rel->reloid) |
| 104 |
{ |
| 105 |
/* old_rel is unmatched, see comment above */ |
| 106 |
report_unmatched_relation(old_rel, old_db, false); |
| 107 |
all_matched = false; |
| 108 |
old_relnum++; |
| 109 |
continue; |
| 110 |
} |
| 111 |
else if (old_rel->reloid > new_rel->reloid) |
| 112 |
{ |
| 113 |
/* new_rel is unmatched, see comment above */ |
| 114 |
if (strcmp(new_rel->nspname, "pg_toast") != 0) |
| 115 |
{ |
| 116 |
report_unmatched_relation(new_rel, new_db, true); |
| 117 |
all_matched = false; |
| 118 |
} |
| 119 |
new_relnum++; |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* Verify that rels of same OID have same name. The namespace name |
| 125 |
* should always match, but the relname might not match for TOAST |
| 126 |
* tables (and, therefore, their indexes). |
| 127 |
*/ |
| 128 |
if (strcmp(old_rel->nspname, new_rel->nspname) != 0 || |
| 129 |
strcmp(old_rel->relname, new_rel->relname) != 0) |
| 130 |
{ |
| 131 |
pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: " |
| 132 |
"old name \"%s.%s\", new name \"%s.%s\"", |
| 133 |
old_rel->reloid, old_db->db_name, |
| 134 |
old_rel->nspname, old_rel->relname, |
| 135 |
new_rel->nspname, new_rel->relname); |
| 136 |
all_matched = false; |
| 137 |
old_relnum++; |
| 138 |
new_relnum++; |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
/* OK, create a mapping entry */ |
| 143 |
create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db, |
| 144 |
old_rel, new_rel, maps + num_maps); |
| 145 |
num_maps++; |
| 146 |
old_relnum++; |
| 147 |
new_relnum++; |
| 148 |
} |
| 149 |
|
| 150 |
if (!all_matched) |
| 151 |
pg_fatal("Failed to match up old and new tables in database \"%s\"", |
| 152 |
old_db->db_name); |
| 153 |
|
| 154 |
*nmaps = num_maps; |
| 155 |
return maps; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/* |
| 160 |
* create_rel_filename_map() |
| 161 |
* |
| 162 |
* fills a file node map structure and returns it in "map". |
| 163 |
*/ |
| 164 |
static void |
| 165 |
create_rel_filename_map(const char *old_data, const char *new_data, |
| 166 |
const DbInfo *old_db, const DbInfo *new_db, |
| 167 |
const RelInfo *old_rel, const RelInfo *new_rel, |
| 168 |
FileNameMap *map) |
| 169 |
{ |
| 170 |
/* In case old/new tablespaces don't match, do them separately. */ |
| 171 |
if (strlen(old_rel->tablespace) == 0) |
| 172 |
{ |
| 173 |
/* |
| 174 |
* relation belongs to the default tablespace, hence relfiles should |
| 175 |
* exist in the data directories. |
| 176 |
*/ |
| 177 |
map->old_tablespace = old_data; |
| 178 |
map->old_tablespace_suffix = "/base"; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
/* relation belongs to a tablespace, so use the tablespace location */ |
| 183 |
map->old_tablespace = old_rel->tablespace; |
| 184 |
map->old_tablespace_suffix = old_cluster.tablespace_suffix; |
| 185 |
} |
| 186 |
|
| 187 |
/* Do the same for new tablespaces */ |
| 188 |
if (strlen(new_rel->tablespace) == 0) |
| 189 |
{ |
| 190 |
map->new_tablespace = new_data; |
| 191 |
map->new_tablespace_suffix = "/base"; |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
map->new_tablespace = new_rel->tablespace; |
| 196 |
map->new_tablespace_suffix = new_cluster.tablespace_suffix; |
| 197 |
} |
| 198 |
|
| 199 |
/* DB oid and relfilenumbers are preserved between old and new cluster */ |
| 200 |
map->db_oid = old_db->db_oid; |
| 201 |
map->relfilenumber = old_rel->relfilenumber; |
| 202 |
|
| 203 |
/* used only for logging and error reporting, old/new are identical */ |
| 204 |
map->nspname = old_rel->nspname; |
| 205 |
map->relname = old_rel->relname; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/* |
| 210 |
* Complain about a relation we couldn't match to the other database, |
| 211 |
* identifying it as best we can. |
| 212 |
*/ |
| 213 |
static void |
| 214 |
report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db) |
| 215 |
{ |
| 216 |
Oid reloid = rel->reloid; /* we might change rel below */ |
| 217 |
char reldesc[1000]; |
| 218 |
int i; |
| 219 |
|
| 220 |
snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"", |
| 221 |
rel->nspname, rel->relname); |
| 222 |
if (rel->indtable) |
| 223 |
{ |
| 224 |
for (i = 0; i < db->rel_arr.nrels; i++) |
| 225 |
{ |
| 226 |
const RelInfo *hrel = &db->rel_arr.rels[i]; |
| 227 |
|
| 228 |
if (hrel->reloid == rel->indtable) |
| 229 |
{ |
| 230 |
snprintf(reldesc + strlen(reldesc), |
| 231 |
sizeof(reldesc) - strlen(reldesc), |
| 232 |
_(" which is an index on \"%s.%s\""), |
| 233 |
hrel->nspname, hrel->relname); |
| 234 |
/* Shift attention to index's table for toast check */ |
| 235 |
rel = hrel; |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
if (i >= db->rel_arr.nrels) |
| 240 |
snprintf(reldesc + strlen(reldesc), |
| 241 |
sizeof(reldesc) - strlen(reldesc), |
| 242 |
_(" which is an index on OID %u"), rel->indtable); |
| 243 |
} |
| 244 |
if (rel->toastheap) |
| 245 |
{ |
| 246 |
for (i = 0; i < db->rel_arr.nrels; i++) |
| 247 |
{ |
| 248 |
const RelInfo *brel = &db->rel_arr.rels[i]; |
| 249 |
|
| 250 |
if (brel->reloid == rel->toastheap) |
| 251 |
{ |
| 252 |
snprintf(reldesc + strlen(reldesc), |
| 253 |
sizeof(reldesc) - strlen(reldesc), |
| 254 |
_(" which is the TOAST table for \"%s.%s\""), |
| 255 |
brel->nspname, brel->relname); |
| 256 |
break; |
| 257 |
} |
| 258 |
} |
| 259 |
if (i >= db->rel_arr.nrels) |
| 260 |
snprintf(reldesc + strlen(reldesc), |
| 261 |
sizeof(reldesc) - strlen(reldesc), |
| 262 |
_(" which is the TOAST table for OID %u"), rel->toastheap); |
| 263 |
} |
| 264 |
|
| 265 |
if (is_new_db) |
| 266 |
pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s", |
| 267 |
reloid, db->db_name, reldesc); |
| 268 |
else |
| 269 |
pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s", |
| 270 |
reloid, db->db_name, reldesc); |
| 271 |
} |
| 272 |
|
| 273 |
/* |
| 274 |
* get_db_rel_and_slot_infos() |
| 275 |
* |
| 276 |
* higher level routine to generate dbinfos for the database running |
| 277 |
* on the given "port". Assumes that server is already running. |
| 278 |
*/ |
| 279 |
void |
| 280 |
get_db_rel_and_slot_infos(ClusterInfo *cluster) |
| 281 |
{ |
| 282 |
UpgradeTask *task = upgrade_task_create(); |
| 283 |
char *rel_infos_query = NULL; |
| 284 |
char *logical_slot_infos_query = NULL; |
| 285 |
|
| 286 |
if (cluster->dbarr.dbs != NULL) |
| 287 |
free_db_and_rel_infos(&cluster->dbarr); |
| 288 |
|
| 289 |
get_template0_info(cluster); |
| 290 |
get_db_infos(cluster); |
| 291 |
|
| 292 |
rel_infos_query = get_rel_infos_query(); |
| 293 |
upgrade_task_add_step(task, |
| 294 |
rel_infos_query, |
| 295 |
process_rel_infos, |
| 296 |
true, NULL); |
| 297 |
|
| 298 |
/* |
| 299 |
* Logical slots are only carried over to the new cluster when the old |
| 300 |
* cluster is on PG17 or newer. This is because before that the logical |
| 301 |
* slots are not saved at shutdown, so there is no guarantee that the |
| 302 |
* latest confirmed_flush_lsn is saved to disk which can lead to data |
| 303 |
* loss. It is still not guaranteed for manually created slots in PG17, so |
| 304 |
* subsequent checks done in check_old_cluster_for_valid_slots() would |
| 305 |
* raise a FATAL error if such slots are included. |
| 306 |
*/ |
| 307 |
if (cluster == &old_cluster && |
| 308 |
GET_MAJOR_VERSION(cluster->major_version) > 1600) |
| 309 |
{ |
| 310 |
logical_slot_infos_query = get_old_cluster_logical_slot_infos_query(); |
| 311 |
upgrade_task_add_step(task, |
| 312 |
logical_slot_infos_query, |
| 313 |
process_old_cluster_logical_slot_infos, |
| 314 |
true, NULL); |
| 315 |
} |
| 316 |
|
| 317 |
upgrade_task_run(task, cluster); |
| 318 |
upgrade_task_free(task); |
| 319 |
|
| 320 |
pg_free(rel_infos_query); |
| 321 |
if (logical_slot_infos_query) |
| 322 |
pg_free(logical_slot_infos_query); |
| 323 |
|
| 324 |
if (cluster == &old_cluster) |
| 325 |
pg_log(PG_VERBOSE, "\nsource databases:"); |
| 326 |
else |
| 327 |
pg_log(PG_VERBOSE, "\ntarget databases:"); |
| 328 |
|
| 329 |
if (log_opts.verbose) |
| 330 |
print_db_infos(&cluster->dbarr); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
/* |
| 335 |
* Get information about template0, which will be copied from the old cluster |
| 336 |
* to the new cluster. |
| 337 |
*/ |
| 338 |
static void |
| 339 |
get_template0_info(ClusterInfo *cluster) |
| 340 |
{ |
| 341 |
PGconn *conn = connectToServer(cluster, "template1"); |
| 342 |
DbLocaleInfo *locale; |
| 343 |
PGresult *dbres; |
| 344 |
int i_datencoding; |
| 345 |
int i_datlocprovider; |
| 346 |
int i_datcollate; |
| 347 |
int i_datctype; |
| 348 |
int i_datlocale; |
| 349 |
|
| 350 |
if (GET_MAJOR_VERSION(cluster->major_version) >= 1700) |
| 351 |
dbres = executeQueryOrDie(conn, |
| 352 |
"SELECT encoding, datlocprovider, " |
| 353 |
" datcollate, datctype, datlocale " |
| 354 |
"FROM pg_catalog.pg_database " |
| 355 |
"WHERE datname='template0'"); |
| 356 |
else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500) |
| 357 |
dbres = executeQueryOrDie(conn, |
| 358 |
"SELECT encoding, datlocprovider, " |
| 359 |
" datcollate, datctype, daticulocale AS datlocale " |
| 360 |
"FROM pg_catalog.pg_database " |
| 361 |
"WHERE datname='template0'"); |
| 362 |
else |
| 363 |
dbres = executeQueryOrDie(conn, |
| 364 |
"SELECT encoding, 'c' AS datlocprovider, " |
| 365 |
" datcollate, datctype, NULL AS datlocale " |
| 366 |
"FROM pg_catalog.pg_database " |
| 367 |
"WHERE datname='template0'"); |
| 368 |
|
| 369 |
|
| 370 |
if (PQntuples(dbres) != 1) |
| 371 |
pg_fatal("template0 not found"); |
| 372 |
|
| 373 |
locale = pg_malloc(sizeof(DbLocaleInfo)); |
| 374 |
|
| 375 |
i_datencoding = PQfnumber(dbres, "encoding"); |
| 376 |
i_datlocprovider = PQfnumber(dbres, "datlocprovider"); |
| 377 |
i_datcollate = PQfnumber(dbres, "datcollate"); |
| 378 |
i_datctype = PQfnumber(dbres, "datctype"); |
| 379 |
i_datlocale = PQfnumber(dbres, "datlocale"); |
| 380 |
|
| 381 |
locale->db_encoding = atoi(PQgetvalue(dbres, 0, i_datencoding)); |
| 382 |
locale->db_collprovider = PQgetvalue(dbres, 0, i_datlocprovider)[0]; |
| 383 |
locale->db_collate = pg_strdup(PQgetvalue(dbres, 0, i_datcollate)); |
| 384 |
locale->db_ctype = pg_strdup(PQgetvalue(dbres, 0, i_datctype)); |
| 385 |
if (PQgetisnull(dbres, 0, i_datlocale)) |
| 386 |
locale->db_locale = NULL; |
| 387 |
else |
| 388 |
locale->db_locale = pg_strdup(PQgetvalue(dbres, 0, i_datlocale)); |
| 389 |
|
| 390 |
cluster->template0 = locale; |
| 391 |
|
| 392 |
PQclear(dbres); |
| 393 |
PQfinish(conn); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
/* |
| 398 |
* get_db_infos() |
| 399 |
* |
| 400 |
* Scans pg_database system catalog and populates all user |
| 401 |
* databases. |
| 402 |
*/ |
| 403 |
static void |
| 404 |
get_db_infos(ClusterInfo *cluster) |
| 405 |
{ |
| 406 |
PGconn *conn = connectToServer(cluster, "template1"); |
| 407 |
PGresult *res; |
| 408 |
int ntups; |
| 409 |
int tupnum; |
| 410 |
DbInfo *dbinfos; |
| 411 |
int i_datname, |
| 412 |
i_oid, |
| 413 |
i_spclocation; |
| 414 |
char query[QUERY_ALLOC]; |
| 415 |
|
| 416 |
snprintf(query, sizeof(query), |
| 417 |
"SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "); |
| 418 |
if (GET_MAJOR_VERSION(cluster->major_version) >= 1700) |
| 419 |
snprintf(query + strlen(query), sizeof(query) - strlen(query), |
| 420 |
"datlocprovider, datlocale, "); |
| 421 |
else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500) |
| 422 |
snprintf(query + strlen(query), sizeof(query) - strlen(query), |
| 423 |
"datlocprovider, daticulocale AS datlocale, "); |
| 424 |
else |
| 425 |
snprintf(query + strlen(query), sizeof(query) - strlen(query), |
| 426 |
"'c' AS datlocprovider, NULL AS datlocale, "); |
| 427 |
snprintf(query + strlen(query), sizeof(query) - strlen(query), |
| 428 |
"pg_catalog.pg_tablespace_location(t.oid) AS spclocation " |
| 429 |
"FROM pg_catalog.pg_database d " |
| 430 |
" LEFT OUTER JOIN pg_catalog.pg_tablespace t " |
| 431 |
" ON d.dattablespace = t.oid " |
| 432 |
"WHERE d.datallowconn = true " |
| 433 |
"ORDER BY 1"); |
| 434 |
|
| 435 |
res = executeQueryOrDie(conn, "%s", query); |
| 436 |
|
| 437 |
i_oid = PQfnumber(res, "oid"); |
| 438 |
i_datname = PQfnumber(res, "datname"); |
| 439 |
i_spclocation = PQfnumber(res, "spclocation"); |
| 440 |
|
| 441 |
ntups = PQntuples(res); |
| 442 |
dbinfos = (DbInfo *) pg_malloc0(sizeof(DbInfo) * ntups); |
| 443 |
|
| 444 |
for (tupnum = 0; tupnum < ntups; tupnum++) |
| 445 |
{ |
| 446 |
dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid)); |
| 447 |
dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname)); |
| 448 |
snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s", |
| 449 |
PQgetvalue(res, tupnum, i_spclocation)); |
| 450 |
} |
| 451 |
PQclear(res); |
| 452 |
|
| 453 |
PQfinish(conn); |
| 454 |
|
| 455 |
cluster->dbarr.dbs = dbinfos; |
| 456 |
cluster->dbarr.ndbs = ntups; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
/* |
| 461 |
* get_rel_infos_query() |
| 462 |
* |
| 463 |
* Returns the query for retrieving the relation information for all the user |
| 464 |
* tables and indexes in the database, for use by get_db_rel_and_slot_infos()'s |
| 465 |
* UpgradeTask. |
| 466 |
* |
| 467 |
* Note: the result is assumed to be sorted by OID. This allows later |
| 468 |
* processing to match up old and new databases efficiently. |
| 469 |
*/ |
| 470 |
static char * |
| 471 |
get_rel_infos_query(void) |
| 472 |
{ |
| 473 |
PQExpBufferData query; |
| 474 |
|
| 475 |
initPQExpBuffer(&query); |
| 476 |
|
| 477 |
/* |
| 478 |
* Create a CTE that collects OIDs of regular user tables and matviews, |
| 479 |
* but excluding toast tables and indexes. We assume that relations with |
| 480 |
* OIDs >= FirstNormalObjectId belong to the user. (That's probably |
| 481 |
* redundant with the namespace-name exclusions, but let's be safe.) |
| 482 |
* |
| 483 |
* pg_largeobject contains user data that does not appear in pg_dump |
| 484 |
* output, so we have to copy that system table. It's easiest to do that |
| 485 |
* by treating it as a user table. |
| 486 |
*/ |
| 487 |
appendPQExpBuffer(&query, |
| 488 |
"WITH regular_heap (reloid, indtable, toastheap) AS ( " |
| 489 |
" SELECT c.oid, 0::oid, 0::oid " |
| 490 |
" FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n " |
| 491 |
" ON c.relnamespace = n.oid " |
| 492 |
" WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", " |
| 493 |
CppAsString2(RELKIND_MATVIEW) "%s) AND " |
| 494 |
/* exclude possible orphaned temp tables */ |
| 495 |
" ((n.nspname !~ '^pg_temp_' AND " |
| 496 |
" n.nspname !~ '^pg_toast_temp_' AND " |
| 497 |
" n.nspname NOT IN ('pg_catalog', 'information_schema', " |
| 498 |
" 'binary_upgrade', 'pg_toast') AND " |
| 499 |
" c.oid >= %u::pg_catalog.oid) OR " |
| 500 |
" (n.nspname = 'pg_catalog' AND " |
| 501 |
" relname IN ('pg_largeobject') ))), ", |
| 502 |
(user_opts.transfer_mode == TRANSFER_MODE_SWAP) ? |
| 503 |
", " CppAsString2(RELKIND_SEQUENCE) : "", |
| 504 |
FirstNormalObjectId); |
| 505 |
|
| 506 |
/* |
| 507 |
* Add a CTE that collects OIDs of toast tables belonging to the tables |
| 508 |
* selected by the regular_heap CTE. (We have to do this separately |
| 509 |
* because the namespace-name rules above don't work for toast tables.) |
| 510 |
*/ |
| 511 |
appendPQExpBufferStr(&query, |
| 512 |
" toast_heap (reloid, indtable, toastheap) AS ( " |
| 513 |
" SELECT c.reltoastrelid, 0::oid, c.oid " |
| 514 |
" FROM regular_heap JOIN pg_catalog.pg_class c " |
| 515 |
" ON regular_heap.reloid = c.oid " |
| 516 |
" WHERE c.reltoastrelid != 0), "); |
| 517 |
|
| 518 |
/* |
| 519 |
* Add a CTE that collects OIDs of all valid indexes on the previously |
| 520 |
* selected tables. We can ignore invalid indexes since pg_dump does. |
| 521 |
* Testing indisready is necessary in 9.2, and harmless in earlier/later |
| 522 |
* versions. |
| 523 |
*/ |
| 524 |
appendPQExpBufferStr(&query, |
| 525 |
" all_index (reloid, indtable, toastheap) AS ( " |
| 526 |
" SELECT indexrelid, indrelid, 0::oid " |
| 527 |
" FROM pg_catalog.pg_index " |
| 528 |
" WHERE indisvalid AND indisready " |
| 529 |
" AND indrelid IN " |
| 530 |
" (SELECT reloid FROM regular_heap " |
| 531 |
" UNION ALL " |
| 532 |
" SELECT reloid FROM toast_heap)) "); |
| 533 |
|
| 534 |
/* |
| 535 |
* And now we can write the query that retrieves the data we want for each |
| 536 |
* heap and index relation. Make sure result is sorted by OID. |
| 537 |
*/ |
| 538 |
appendPQExpBufferStr(&query, |
| 539 |
"SELECT all_rels.*, n.nspname, c.relname, " |
| 540 |
" c.relfilenode, c.reltablespace, " |
| 541 |
" pg_catalog.pg_tablespace_location(t.oid) AS spclocation " |
| 542 |
"FROM (SELECT * FROM regular_heap " |
| 543 |
" UNION ALL " |
| 544 |
" SELECT * FROM toast_heap " |
| 545 |
" UNION ALL " |
| 546 |
" SELECT * FROM all_index) all_rels " |
| 547 |
" JOIN pg_catalog.pg_class c " |
| 548 |
" ON all_rels.reloid = c.oid " |
| 549 |
" JOIN pg_catalog.pg_namespace n " |
| 550 |
" ON c.relnamespace = n.oid " |
| 551 |
" LEFT OUTER JOIN pg_catalog.pg_tablespace t " |
| 552 |
" ON c.reltablespace = t.oid " |
| 553 |
"ORDER BY 1"); |
| 554 |
|
| 555 |
return query.data; |
| 556 |
} |
| 557 |
|
| 558 |
/* |
| 559 |
* Callback function for processing results of the query returned by |
| 560 |
* get_rel_infos_query(), which is used for get_db_rel_and_slot_infos()'s |
| 561 |
* UpgradeTask. This function stores the relation information for later use. |
| 562 |
*/ |
| 563 |
static void |
| 564 |
process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg) |
| 565 |
{ |
| 566 |
int ntups = PQntuples(res); |
| 567 |
RelInfo *relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups); |
| 568 |
int i_reloid = PQfnumber(res, "reloid"); |
| 569 |
int i_indtable = PQfnumber(res, "indtable"); |
| 570 |
int i_toastheap = PQfnumber(res, "toastheap"); |
| 571 |
int i_nspname = PQfnumber(res, "nspname"); |
| 572 |
int i_relname = PQfnumber(res, "relname"); |
| 573 |
int i_relfilenumber = PQfnumber(res, "relfilenode"); |
| 574 |
int i_reltablespace = PQfnumber(res, "reltablespace"); |
| 575 |
int i_spclocation = PQfnumber(res, "spclocation"); |
| 576 |
int num_rels = 0; |
| 577 |
char *nspname = NULL; |
| 578 |
char *relname = NULL; |
| 579 |
char *tablespace = NULL; |
| 580 |
char *last_namespace = NULL; |
| 581 |
char *last_tablespace = NULL; |
| 582 |
|
| 583 |
AssertVariableIsOfType(&process_rel_infos, UpgradeTaskProcessCB); |
| 584 |
|
| 585 |
for (int relnum = 0; relnum < ntups; relnum++) |
| 586 |
{ |
| 587 |
RelInfo *curr = &relinfos[num_rels++]; |
| 588 |
|
| 589 |
curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid)); |
| 590 |
curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable)); |
| 591 |
curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap)); |
| 592 |
|
| 593 |
nspname = PQgetvalue(res, relnum, i_nspname); |
| 594 |
curr->nsp_alloc = false; |
| 595 |
|
| 596 |
/* |
| 597 |
* Many of the namespace and tablespace strings are identical, so we |
| 598 |
* try to reuse the allocated string pointers where possible to reduce |
| 599 |
* memory consumption. |
| 600 |
*/ |
| 601 |
/* Can we reuse the previous string allocation? */ |
| 602 |
if (last_namespace && strcmp(nspname, last_namespace) == 0) |
| 603 |
curr->nspname = last_namespace; |
| 604 |
else |
| 605 |
{ |
| 606 |
last_namespace = curr->nspname = pg_strdup(nspname); |
| 607 |
curr->nsp_alloc = true; |
| 608 |
} |
| 609 |
|
| 610 |
relname = PQgetvalue(res, relnum, i_relname); |
| 611 |
curr->relname = pg_strdup(relname); |
| 612 |
|
| 613 |
curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber)); |
| 614 |
curr->tblsp_alloc = false; |
| 615 |
|
| 616 |
/* Is the tablespace oid non-default? */ |
| 617 |
if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0) |
| 618 |
{ |
| 619 |
/* |
| 620 |
* The tablespace location might be "", meaning the cluster |
| 621 |
* default location, i.e. pg_default or pg_global. |
| 622 |
*/ |
| 623 |
tablespace = PQgetvalue(res, relnum, i_spclocation); |
| 624 |
|
| 625 |
/* Can we reuse the previous string allocation? */ |
| 626 |
if (last_tablespace && strcmp(tablespace, last_tablespace) == 0) |
| 627 |
curr->tablespace = last_tablespace; |
| 628 |
else |
| 629 |
{ |
| 630 |
last_tablespace = curr->tablespace = pg_strdup(tablespace); |
| 631 |
curr->tblsp_alloc = true; |
| 632 |
} |
| 633 |
} |
| 634 |
else |
| 635 |
/* A zero reltablespace oid indicates the database tablespace. */ |
| 636 |
curr->tablespace = dbinfo->db_tablespace; |
| 637 |
} |
| 638 |
|
| 639 |
dbinfo->rel_arr.rels = relinfos; |
| 640 |
dbinfo->rel_arr.nrels = num_rels; |
| 641 |
} |
| 642 |
|
| 643 |
/* |
| 644 |
* get_old_cluster_logical_slot_infos_query() |
| 645 |
* |
| 646 |
* Returns the query for retrieving the logical slot information for all the |
| 647 |
* logical replication slots in the database, for use by |
| 648 |
* get_db_rel_and_slot_infos()'s UpgradeTask. The status of each logical slot |
| 649 |
* is checked in check_old_cluster_for_valid_slots(). |
| 650 |
*/ |
| 651 |
static char * |
| 652 |
get_old_cluster_logical_slot_infos_query(void) |
| 653 |
{ |
| 654 |
/* |
| 655 |
* Fetch the logical replication slot information. The check whether the |
| 656 |
* slot is considered caught up is done by an upgrade function. This |
| 657 |
* regards the slot as caught up if we don't find any decodable changes. |
| 658 |
* See binary_upgrade_logical_slot_has_caught_up(). |
| 659 |
* |
| 660 |
* Note that we can't ensure whether the slot is caught up during |
| 661 |
* live_check as the new WAL records could be generated. |
| 662 |
* |
| 663 |
* We intentionally skip checking the WALs for invalidated slots as the |
| 664 |
* corresponding WALs could have been removed for such slots. |
| 665 |
* |
| 666 |
* The temporary slots are explicitly ignored while checking because such |
| 667 |
* slots cannot exist after the upgrade. During the upgrade, clusters are |
| 668 |
* started and stopped several times causing any temporary slots to be |
| 669 |
* removed. |
| 670 |
*/ |
| 671 |
return psprintf("SELECT slot_name, plugin, two_phase, failover, " |
| 672 |
"%s as caught_up, invalidation_reason IS NOT NULL as invalid " |
| 673 |
"FROM pg_catalog.pg_replication_slots " |
| 674 |
"WHERE slot_type = 'logical' AND " |
| 675 |
"database = current_database() AND " |
| 676 |
"temporary IS FALSE;", |
| 677 |
user_opts.live_check ? "FALSE" : |
| 678 |
"(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE " |
| 679 |
"ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) " |
| 680 |
"END)"); |
| 681 |
} |
| 682 |
|
| 683 |
/* |
| 684 |
* Callback function for processing results of the query returned by |
| 685 |
* get_old_cluster_logical_slot_infos_query(), which is used for |
| 686 |
* get_db_rel_and_slot_infos()'s UpgradeTask. This function stores the logical |
| 687 |
* slot information for later use. |
| 688 |
*/ |
| 689 |
static void |
| 690 |
process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg) |
| 691 |
{ |
| 692 |
LogicalSlotInfo *slotinfos = NULL; |
| 693 |
int num_slots = PQntuples(res); |
| 694 |
|
| 695 |
AssertVariableIsOfType(&process_old_cluster_logical_slot_infos, |
| 696 |
UpgradeTaskProcessCB); |
| 697 |
|
| 698 |
if (num_slots) |
| 699 |
{ |
| 700 |
int i_slotname; |
| 701 |
int i_plugin; |
| 702 |
int i_twophase; |
| 703 |
int i_failover; |
| 704 |
int i_caught_up; |
| 705 |
int i_invalid; |
| 706 |
|
| 707 |
slotinfos = (LogicalSlotInfo *) pg_malloc(sizeof(LogicalSlotInfo) * num_slots); |
| 708 |
|
| 709 |
i_slotname = PQfnumber(res, "slot_name"); |
| 710 |
i_plugin = PQfnumber(res, "plugin"); |
| 711 |
i_twophase = PQfnumber(res, "two_phase"); |
| 712 |
i_failover = PQfnumber(res, "failover"); |
| 713 |
i_caught_up = PQfnumber(res, "caught_up"); |
| 714 |
i_invalid = PQfnumber(res, "invalid"); |
| 715 |
|
| 716 |
for (int slotnum = 0; slotnum < num_slots; slotnum++) |
| 717 |
{ |
| 718 |
LogicalSlotInfo *curr = &slotinfos[slotnum]; |
| 719 |
|
| 720 |
curr->slotname = pg_strdup(PQgetvalue(res, slotnum, i_slotname)); |
| 721 |
curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin)); |
| 722 |
curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0); |
| 723 |
curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0); |
| 724 |
curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0); |
| 725 |
curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
dbinfo->slot_arr.slots = slotinfos; |
| 730 |
dbinfo->slot_arr.nslots = num_slots; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
/* |
| 735 |
* count_old_cluster_logical_slots() |
| 736 |
* |
| 737 |
* Returns the number of logical replication slots for all databases. |
| 738 |
* |
| 739 |
* Note: this function always returns 0 if the old_cluster is PG16 and prior |
| 740 |
* because we gather slot information only for cluster versions greater than or |
| 741 |
* equal to PG17. See get_old_cluster_logical_slot_infos(). |
| 742 |
*/ |
| 743 |
int |
| 744 |
count_old_cluster_logical_slots(void) |
| 745 |
{ |
| 746 |
int slot_count = 0; |
| 747 |
|
| 748 |
for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++) |
| 749 |
slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots; |
| 750 |
|
| 751 |
return slot_count; |
| 752 |
} |
| 753 |
|
| 754 |
/* |
| 755 |
* get_subscription_count() |
| 756 |
* |
| 757 |
* Gets the number of subscriptions in the cluster. |
| 758 |
*/ |
| 759 |
void |
| 760 |
get_subscription_count(ClusterInfo *cluster) |
| 761 |
{ |
| 762 |
PGconn *conn; |
| 763 |
PGresult *res; |
| 764 |
|
| 765 |
conn = connectToServer(cluster, "template1"); |
| 766 |
res = executeQueryOrDie(conn, "SELECT count(*) " |
| 767 |
"FROM pg_catalog.pg_subscription"); |
| 768 |
cluster->nsubs = atoi(PQgetvalue(res, 0, 0)); |
| 769 |
|
| 770 |
PQclear(res); |
| 771 |
PQfinish(conn); |
| 772 |
} |
| 773 |
|
| 774 |
static void |
| 775 |
free_db_and_rel_infos(DbInfoArr *db_arr) |
| 776 |
{ |
| 777 |
int dbnum; |
| 778 |
|
| 779 |
for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++) |
| 780 |
{ |
| 781 |
free_rel_infos(&db_arr->dbs[dbnum].rel_arr); |
| 782 |
pg_free(db_arr->dbs[dbnum].db_name); |
| 783 |
} |
| 784 |
pg_free(db_arr->dbs); |
| 785 |
db_arr->dbs = NULL; |
| 786 |
db_arr->ndbs = 0; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
static void |
| 791 |
free_rel_infos(RelInfoArr *rel_arr) |
| 792 |
{ |
| 793 |
int relnum; |
| 794 |
|
| 795 |
for (relnum = 0; relnum < rel_arr->nrels; relnum++) |
| 796 |
{ |
| 797 |
if (rel_arr->rels[relnum].nsp_alloc) |
| 798 |
pg_free(rel_arr->rels[relnum].nspname); |
| 799 |
pg_free(rel_arr->rels[relnum].relname); |
| 800 |
if (rel_arr->rels[relnum].tblsp_alloc) |
| 801 |
pg_free(rel_arr->rels[relnum].tablespace); |
| 802 |
} |
| 803 |
pg_free(rel_arr->rels); |
| 804 |
rel_arr->nrels = 0; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
static void |
| 809 |
print_db_infos(DbInfoArr *db_arr) |
| 810 |
{ |
| 811 |
int dbnum; |
| 812 |
|
| 813 |
for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++) |
| 814 |
{ |
| 815 |
DbInfo *pDbInfo = &db_arr->dbs[dbnum]; |
| 816 |
|
| 817 |
pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name); |
| 818 |
print_rel_infos(&pDbInfo->rel_arr); |
| 819 |
print_slot_infos(&pDbInfo->slot_arr); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
static void |
| 825 |
print_rel_infos(RelInfoArr *rel_arr) |
| 826 |
{ |
| 827 |
int relnum; |
| 828 |
|
| 829 |
for (relnum = 0; relnum < rel_arr->nrels; relnum++) |
| 830 |
pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"", |
| 831 |
rel_arr->rels[relnum].nspname, |
| 832 |
rel_arr->rels[relnum].relname, |
| 833 |
rel_arr->rels[relnum].reloid, |
| 834 |
rel_arr->rels[relnum].tablespace); |
| 835 |
} |
| 836 |
|
| 837 |
static void |
| 838 |
print_slot_infos(LogicalSlotInfoArr *slot_arr) |
| 839 |
{ |
| 840 |
/* Quick return if there are no logical slots. */ |
| 841 |
if (slot_arr->nslots == 0) |
| 842 |
return; |
| 843 |
|
| 844 |
pg_log(PG_VERBOSE, "Logical replication slots in the database:"); |
| 845 |
|
| 846 |
for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++) |
| 847 |
{ |
| 848 |
LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum]; |
| 849 |
|
| 850 |
pg_log(PG_VERBOSE, "slot name: \"%s\", output plugin: \"%s\", two_phase: %s", |
| 851 |
slot_info->slotname, |
| 852 |
slot_info->plugin, |
| 853 |
slot_info->two_phase ? "true" : "false"); |
| 854 |
} |
| 855 |
} |