Skip to content

Commit 4d9e6cd

Browse files
committed
Fix: fix pg_upgrade and pg_dump for upgrade
1. Add database type and version to pg_upgrade and pg_dump to distinguish different catalogs. 2. Fixed serveral issues encountered when using pg_dump and pg_upgrade.
1 parent bc1aae0 commit 4d9e6cd

File tree

16 files changed

+192
-16
lines changed

16 files changed

+192
-16
lines changed

src/backend/utils/adt/pg_upgrade_support.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "catalog/pg_class.h"
2121
#include "catalog/pg_enum.h"
2222
#include "catalog/pg_namespace.h"
23+
#include "catalog/pg_tablespace.h"
2324
#include "catalog/pg_type.h"
2425
#include "cdb/cdbvars.h"
2526
#include "commands/extension.h"
@@ -38,6 +39,19 @@ do { \
3839
errmsg("function can only be called when server is in binary upgrade mode"))); \
3940
} while (0)
4041

42+
Datum
43+
binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
44+
{
45+
Oid tablespaceoid = PG_GETARG_OID(0);
46+
char *tablespacename = GET_STR(PG_GETARG_TEXT_P(1));
47+
48+
CHECK_IS_BINARY_UPGRADE;
49+
AddPreassignedOidFromBinaryUpgrade(tablespaceoid, TableSpaceRelationId, tablespacename,
50+
InvalidOid, InvalidOid, InvalidOid);
51+
52+
PG_RETURN_VOID();
53+
}
54+
4155
Datum
4256
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
4357
{

src/bin/pg_dump/pg_backup.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ typedef struct _dumpOptions
184184
int do_nothing;
185185
} DumpOptions;
186186

187+
188+
typedef enum
189+
{
190+
Greenplum,
191+
Cloudberry
192+
} DatabaseType;
193+
194+
typedef struct
195+
{
196+
DatabaseType type;
197+
int version;
198+
} DatabaseVersion;
187199
/*
188200
* We may want to have some more user-readable data, but in the mean
189201
* time this gives us some abstraction and type checking.
@@ -216,6 +228,8 @@ typedef struct Archive
216228
bool exit_on_error; /* whether to exit on SQL errors... */
217229
int n_errors; /* number of errors (if no die) */
218230

231+
DatabaseVersion version;
232+
219233
/* The rest is private */
220234
} Archive;
221235

src/bin/pg_dump/pg_backup_db.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ _check_database_version(ArchiveHandle *AH)
3535
const char *remoteversion_str;
3636
int remoteversion;
3737
PGresult *res;
38+
char *version;
39+
char dbstring[1024];
40+
char dbstring2[1024];
41+
int v1;
3842

3943
remoteversion_str = PQparameterStatus(AH->connection, "server_version");
4044
remoteversion = PQserverVersion(AH->connection);
@@ -68,6 +72,29 @@ _check_database_version(ArchiveHandle *AH)
6872
}
6973
else
7074
AH->public.isStandby = false;
75+
76+
res = ExecuteSqlQueryForSingleRow((Archive *) AH, "SELECT version()");
77+
78+
version = PQgetvalue(res, 0, 0);
79+
80+
if(sscanf(version, "%*[^(](%s %s %d.%*d.%*d-%*[^)])", dbstring, dbstring2, &v1) != 3)
81+
pg_log_error("could not get version output from select version();\n");
82+
83+
if (strcasecmp("Greenplum", dbstring) == 0 && strcasecmp("Database", dbstring2) == 0)
84+
{
85+
AH->public.version.type = Greenplum;
86+
AH->public.version.version = v1;
87+
88+
} else if((strcasecmp("Cloudberry", dbstring) == 0 && strcasecmp("Database", dbstring2) == 0)
89+
|| (strcasecmp("Apache", dbstring) == 0 && strcasecmp("Cloudberry", dbstring2) == 0))
90+
{
91+
AH->public.version.type = Cloudberry;
92+
AH->public.version.version = v1;
93+
}
94+
else
95+
pg_log_error("could not upgrade from non Greenplum/Cloudberry version: %s %s\n", dbstring, dbstring2);
96+
97+
PQclear(res);
7198
}
7299

73100
/*

src/bin/pg_dump/pg_dump.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7384,7 +7384,7 @@ getTables(Archive *fout, int *numTables)
73847384
"%s AS ispartition, "
73857385
"%s AS partbound, "
73867386
"c.relisivm AS isivm, "
7387-
"c.relisdynamic AS isdynamic "
7387+
"%s"
73887388
"FROM pg_class c "
73897389
"LEFT JOIN pg_depend d ON "
73907390
"(c.relkind = '%c' AND "
@@ -7414,6 +7414,7 @@ getTables(Archive *fout, int *numTables)
74147414
partkeydef,
74157415
ispartition,
74167416
partbound,
7417+
(fout->version.type == Cloudberry && fout->version.version >= 2) ? "c.relisdynamic AS isdynamic " : "false AS isdynamic ",
74177418
RELKIND_SEQUENCE,
74187419
RELKIND_PARTITIONED_TABLE,
74197420
RELKIND_RELATION, RELKIND_SEQUENCE,
@@ -15596,18 +15597,18 @@ dumpAgg(Archive *fout, const AggInfo *agginfo)
1559615597
if (fout->remoteVersion >= 110000)
1559715598
appendPQExpBufferStr(query,
1559815599
"aggfinalmodify,\n"
15599-
"aggmfinalmodify\n");
15600+
"aggmfinalmodify,\n");
1560015601
else
1560115602
appendPQExpBufferStr(query,
1560215603
"'0' AS aggfinalmodify,\n"
15603-
"'0' AS aggmfinalmodify\n");
15604+
"'0' AS aggmfinalmodify,\n");
1560415605

15605-
if (fout->remoteVersion >= 140000)
15606+
if (fout->remoteVersion >= 140000 && fout->version.type == Cloudberry && fout->version.version >= 2)
1560615607
appendPQExpBufferStr(query,
15607-
"aggrepsafeexec,\n");
15608+
"aggrepsafeexec\n");
1560815609
else
1560915610
appendPQExpBufferStr(query,
15610-
"false AS aggrepsafeexec,\n");
15611+
"false AS aggrepsafeexec\n");
1561115612

1561215613
appendPQExpBuffer(query,
1561315614
"FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "

src/bin/pg_dump/pg_dumpall.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,12 @@ dumpTablespaces(PGconn *conn)
17591759
/* needed for buildACLCommands() */
17601760
fspcname = pg_strdup(fmtId(spcname));
17611761

1762+
if (binary_upgrade)
1763+
{
1764+
appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
1765+
appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid, '%s'::text);\n", spcoid, spcname);
1766+
}
1767+
17621768
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", spcname);
17631769
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
17641770

src/bin/pg_upgrade/check.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ check_for_new_tablespace_dir(ClusterInfo *new_cluster)
576576
os_info.old_tablespaces[tblnum],
577577
new_cluster->tablespace_suffix);
578578

579-
if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
579+
if (is_greenplum_dispatcher_mode() && (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT))
580580
gp_fatal_log("new cluster tablespace directory already exists: \"%s\"\n",
581581
new_tablespace_dir);
582582
}
@@ -1598,6 +1598,8 @@ check_for_cluster_key_failure(ClusterInfo *cluster)
15981598
{
15991599
struct stat buffer;
16001600

1601+
prep_status("Checking for presence of pg_cryptokeys");
1602+
16011603
if (stat (KMGR_DIR_PID, &buffer) == 0)
16021604
{
16031605
if (cluster == &old_cluster)

src/bin/pg_upgrade/controldata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
555555
cluster->controldata.data_checksum_version = str2uint(p);
556556
got_data_checksum_version = true;
557557
}
558-
else if ((p = strstr(bufin, "Cluster file encryption method:")) != NULL)
558+
else if ((p = strstr(bufin, "File encryption method:")) != NULL)
559559
{
560560
p = strchr(p, ':');
561561

src/bin/pg_upgrade/exec.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ get_bin_version(ClusterInfo *cluster)
5252

5353
pclose(output);
5454

55-
if (sscanf(cmd_output, "%*s (%s %s) %d.%d", dbstring, dbstring2, &v1, &v2) < 1)
55+
if (sscanf(cmd_output, "%*s (%s %[^)]) %d.%d", dbstring, dbstring2, &v1, &v2) != 4)
5656
pg_fatal("could not get pg_ctl version output from %s\n", cmd);
5757

58-
if ((strcmp("Greenplum", dbstring) == 0 && strcmp("Database", dbstring2) == 0) \
59-
|| (strcmp("Apache", dbstring2) == 0 && strcmp("Cloudberry", dbstring2) == 0))
58+
if (!((strcmp("Greenplum", dbstring) == 0 && strcmp("Database", dbstring2) == 0) \
59+
|| (strcmp("Cloudberry", dbstring) == 0 && strcmp("Database", dbstring2) == 0)
60+
|| (strcmp("Apache", dbstring) == 0 && strcmp("Cloudberry", dbstring2) == 0)))
6061
pg_fatal("could not upgrade from non Greenplum/Cloudberry version: %s %s\n", dbstring, dbstring2);
6162

6263
if (v1 < 10)

src/bin/pg_upgrade/greenplum/check_gp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,14 @@ check_for_plpython2_dependent_functions(ClusterInfo *cluster)
665665
char output_path[MAXPGPATH];
666666
bool found = false;
667667

668+
669+
if (cluster->version.type == Cloudberry)
670+
{
671+
prep_status("Skip checking for plpython2 functions, CBDB use plpython3");
672+
check_ok();
673+
return ;
674+
}
675+
668676
prep_status("Checking for functions dependent on plpython2");
669677

670678
snprintf(output_path, sizeof(output_path), "%s/%s",

src/bin/pg_upgrade/info.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,15 @@ gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
119119
* Verify that rels of same OID have same name. The namespace name
120120
* should always match, but the relname might not match for TOAST
121121
* tables (and, therefore, their indexes).
122+
*
123+
* XXX GPDB: for TOAST tables, don't insist on a match at all
124+
* yet; there are other ways for us to get mismatched names. Ideally
125+
* this will go away eventually.
122126
*/
123127
if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
124-
strcmp(old_rel->relname, new_rel->relname) != 0)
128+
(strcmp(old_rel->relname, new_rel->relname) != 0 &&
129+
(/* GET_MAJOR_VERSION(old_cluster.major_version) >= 900 || */
130+
strcmp(old_rel->nspname, "pg_toast") != 0)))
125131
{
126132
pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
127133
"old name \"%s.%s\", new name \"%s.%s\"\n",

0 commit comments

Comments
 (0)