@@ -6905,6 +6905,28 @@ getPackages(Archive *fout, int *numPkgs)
69056905 int i_rolname;
69066906 int i_pkgacl;
69076907 int i_acldefault;
6908+
6909+
6910+ /*
6911+ * Check if pg_package table exists. This is needed for pg_upgrade from
6912+ * PostgreSQL to IvorySQL, where the source database is PostgreSQL which
6913+ * doesn't have pg_package table.
6914+ */
6915+ res = ExecuteSqlQuery(fout,
6916+ "SELECT 1 FROM pg_catalog.pg_class c "
6917+ "JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid "
6918+ "WHERE n.nspname = 'pg_catalog' AND c.relname = 'pg_package'",
6919+ PGRES_TUPLES_OK);
6920+ if (PQntuples(res) == 0)
6921+ {
6922+ /* pg_package table doesn't exist, return empty result */
6923+ PQclear(res);
6924+ destroyPQExpBuffer(query);
6925+ *numPkgs = 0;
6926+ return (PkgInfo *) pg_malloc0(sizeof(PkgInfo));
6927+ }
6928+ PQclear(res);
6929+
69086930 appendPQExpBuffer(query,
69096931 "SELECT p.tableoid, p.pkgname, p.oid, p.pkgnamespace,"
69106932 "p.pkgowner,"
@@ -7142,12 +7164,31 @@ getTables(Archive *fout, int *numTables)
71427164 "d.refobjsubid AS owning_col, "
71437165 "tsp.spcname AS reltablespace, ");
71447166
7145- if (fout->remoteVersion < 170000)
7146- appendPQExpBufferStr(query,
7147- "false AS relhasrowid, ");
7148- else
7149- appendPQExpBufferStr(query,
7150- "c.relhasrowid, ");
7167+ /*
7168+ * Check if relhasrowid column exists. This column is IvorySQL-specific
7169+ * for Oracle ROWID compatibility, and doesn't exist in native PostgreSQL.
7170+ * We need to check at runtime rather than relying on remoteVersion because
7171+ * when upgrading from PostgreSQL to IvorySQL, the source database is
7172+ * PostgreSQL which may have the same version number but lacks this column.
7173+ */
7174+ {
7175+ PGresult *res_rowid;
7176+ bool relhasrowid_exists = false;
7177+
7178+ res_rowid = ExecuteSqlQuery(fout,
7179+ "SELECT 1 FROM pg_catalog.pg_attribute "
7180+ "WHERE attrelid = 'pg_class'::regclass "
7181+ "AND attname = 'relhasrowid' "
7182+ "AND attnum > 0 LIMIT 1",
7183+ PGRES_TUPLES_OK);
7184+ relhasrowid_exists = (PQntuples(res_rowid) > 0);
7185+ PQclear(res_rowid);
7186+
7187+ if (relhasrowid_exists)
7188+ appendPQExpBufferStr(query, "c.relhasrowid, ");
7189+ else
7190+ appendPQExpBufferStr(query, "false AS relhasrowid, ");
7191+ }
71517192
71527193 if (fout->remoteVersion >= 120000)
71537194 appendPQExpBufferStr(query,
@@ -9232,13 +9273,31 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
92329273 appendPQExpBufferStr(q,
92339274 "'' AS attcompression,\n");
92349275
9235- if (fout->remoteVersion >= 170000)
9236- appendPQExpBuffer(q,
9237- "a.attisinvisible,\n");
9238- else
9239- appendPQExpBuffer(q,
9240- "'f' AS attisinvisible,\n");
9276+ /*
9277+ * Check if attisinvisible column exists. This column is IvorySQL-specific
9278+ * for Oracle compatibility, and doesn't exist in native PostgreSQL.
9279+ * We need to check at runtime rather than relying on remoteVersion because
9280+ * when upgrading from PostgreSQL to IvorySQL, the source database is
9281+ * PostgreSQL which may have the same version number but lacks this column.
9282+ */
9283+ {
9284+ PGresult *res_invisible;
9285+ bool attisinvisible_exists = false;
92419286
9287+ res_invisible = ExecuteSqlQuery(fout,
9288+ "SELECT 1 FROM pg_catalog.pg_attribute "
9289+ "WHERE attrelid = 'pg_attribute'::regclass "
9290+ "AND attname = 'attisinvisible' "
9291+ "AND attnum > 0 LIMIT 1",
9292+ PGRES_TUPLES_OK);
9293+ attisinvisible_exists = (PQntuples(res_invisible) > 0);
9294+ PQclear(res_invisible);
9295+
9296+ if (attisinvisible_exists)
9297+ appendPQExpBufferStr(q, "a.attisinvisible,\n");
9298+ else
9299+ appendPQExpBufferStr(q, "'f' AS attisinvisible,\n");
9300+ }
92429301
92439302 if (fout->remoteVersion >= 100000)
92449303 appendPQExpBufferStr(q,
0 commit comments