Commit a7ae1dcf authored by Bruce Momjian's avatar Bruce Momjian

pg_upgrade: prevent automatic oid assignment

Prevent automatic oid assignment when in binary upgrade mode.  Also
throw an error when contrib/pg_upgrade_support functions are called when
not in binary upgrade mode.

This prevent automatically-assigned oids from conflicting with later
pre-assigned oids coming from the old cluster.  It also makes sure oids
are preserved in call important cases.
parent 73fe8750
...@@ -38,12 +38,20 @@ PG_FUNCTION_INFO_V1(set_next_pg_authid_oid); ...@@ -38,12 +38,20 @@ PG_FUNCTION_INFO_V1(set_next_pg_authid_oid);
PG_FUNCTION_INFO_V1(create_empty_extension); PG_FUNCTION_INFO_V1(create_empty_extension);
#define CHECK_IS_BINARY_UPGRADE \
do { \
if (!IsBinaryUpgrade) \
ereport(ERROR, \
(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM), \
(errmsg("function can only be called when server is in binary upgrade mode")))); \
} while (0)
Datum Datum
set_next_pg_type_oid(PG_FUNCTION_ARGS) set_next_pg_type_oid(PG_FUNCTION_ARGS)
{ {
Oid typoid = PG_GETARG_OID(0); Oid typoid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_pg_type_oid = typoid; binary_upgrade_next_pg_type_oid = typoid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -54,6 +62,7 @@ set_next_array_pg_type_oid(PG_FUNCTION_ARGS) ...@@ -54,6 +62,7 @@ set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
{ {
Oid typoid = PG_GETARG_OID(0); Oid typoid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_array_pg_type_oid = typoid; binary_upgrade_next_array_pg_type_oid = typoid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -64,6 +73,7 @@ set_next_toast_pg_type_oid(PG_FUNCTION_ARGS) ...@@ -64,6 +73,7 @@ set_next_toast_pg_type_oid(PG_FUNCTION_ARGS)
{ {
Oid typoid = PG_GETARG_OID(0); Oid typoid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_toast_pg_type_oid = typoid; binary_upgrade_next_toast_pg_type_oid = typoid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -74,6 +84,7 @@ set_next_heap_pg_class_oid(PG_FUNCTION_ARGS) ...@@ -74,6 +84,7 @@ set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
{ {
Oid reloid = PG_GETARG_OID(0); Oid reloid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_heap_pg_class_oid = reloid; binary_upgrade_next_heap_pg_class_oid = reloid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -84,6 +95,7 @@ set_next_index_pg_class_oid(PG_FUNCTION_ARGS) ...@@ -84,6 +95,7 @@ set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{ {
Oid reloid = PG_GETARG_OID(0); Oid reloid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_index_pg_class_oid = reloid; binary_upgrade_next_index_pg_class_oid = reloid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -94,6 +106,7 @@ set_next_toast_pg_class_oid(PG_FUNCTION_ARGS) ...@@ -94,6 +106,7 @@ set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{ {
Oid reloid = PG_GETARG_OID(0); Oid reloid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_toast_pg_class_oid = reloid; binary_upgrade_next_toast_pg_class_oid = reloid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -104,6 +117,7 @@ set_next_pg_enum_oid(PG_FUNCTION_ARGS) ...@@ -104,6 +117,7 @@ set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{ {
Oid enumoid = PG_GETARG_OID(0); Oid enumoid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_pg_enum_oid = enumoid; binary_upgrade_next_pg_enum_oid = enumoid;
PG_RETURN_VOID(); PG_RETURN_VOID();
...@@ -114,6 +128,7 @@ set_next_pg_authid_oid(PG_FUNCTION_ARGS) ...@@ -114,6 +128,7 @@ set_next_pg_authid_oid(PG_FUNCTION_ARGS)
{ {
Oid authoid = PG_GETARG_OID(0); Oid authoid = PG_GETARG_OID(0);
CHECK_IS_BINARY_UPGRADE;
binary_upgrade_next_pg_authid_oid = authoid; binary_upgrade_next_pg_authid_oid = authoid;
PG_RETURN_VOID(); PG_RETURN_VOID();
} }
...@@ -129,6 +144,8 @@ create_empty_extension(PG_FUNCTION_ARGS) ...@@ -129,6 +144,8 @@ create_empty_extension(PG_FUNCTION_ARGS)
Datum extCondition; Datum extCondition;
List *requiredExtensions; List *requiredExtensions;
CHECK_IS_BINARY_UPGRADE;
if (PG_ARGISNULL(4)) if (PG_ARGISNULL(4))
extConfig = PointerGetDatum(NULL); extConfig = PointerGetDatum(NULL);
else else
......
...@@ -1088,19 +1088,21 @@ heap_create_with_catalog(const char *relname, ...@@ -1088,19 +1088,21 @@ heap_create_with_catalog(const char *relname,
*/ */
if (!OidIsValid(relid)) if (!OidIsValid(relid))
{ {
/* /* Use binary-upgrade override for pg_class.oid/relfilenode? */
* Use binary-upgrade override for pg_class.oid/relfilenode, if
* supplied.
*/
if (IsBinaryUpgrade && if (IsBinaryUpgrade &&
OidIsValid(binary_upgrade_next_heap_pg_class_oid) &&
(relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE || (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW || relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW ||
relkind == RELKIND_COMPOSITE_TYPE || relkind == RELKIND_FOREIGN_TABLE)) relkind == RELKIND_COMPOSITE_TYPE || relkind == RELKIND_FOREIGN_TABLE))
{ {
if (!OidIsValid(binary_upgrade_next_heap_pg_class_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_class heap OID value not set when in binary upgrade mode")));
relid = binary_upgrade_next_heap_pg_class_oid; relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid; binary_upgrade_next_heap_pg_class_oid = InvalidOid;
} }
/* There might be no TOAST table, so we have to test for it. */
else if (IsBinaryUpgrade && else if (IsBinaryUpgrade &&
OidIsValid(binary_upgrade_next_toast_pg_class_oid) && OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
relkind == RELKIND_TOASTVALUE) relkind == RELKIND_TOASTVALUE)
......
...@@ -796,13 +796,14 @@ index_create(Relation heapRelation, ...@@ -796,13 +796,14 @@ index_create(Relation heapRelation,
*/ */
if (!OidIsValid(indexRelationId)) if (!OidIsValid(indexRelationId))
{ {
/* /* Use binary-upgrade override for pg_class.oid/relfilenode? */
* Use binary-upgrade override for pg_class.oid/relfilenode, if if (IsBinaryUpgrade)
* supplied.
*/
if (IsBinaryUpgrade &&
OidIsValid(binary_upgrade_next_index_pg_class_oid))
{ {
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_class index OID value not set when in binary upgrade mode")));
indexRelationId = binary_upgrade_next_index_pg_class_oid; indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid; binary_upgrade_next_index_pg_class_oid = InvalidOid;
} }
......
...@@ -341,8 +341,13 @@ restart: ...@@ -341,8 +341,13 @@ restart:
} }
/* Get a new OID for the new label */ /* Get a new OID for the new label */
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_pg_enum_oid)) if (IsBinaryUpgrade)
{ {
if (!OidIsValid(binary_upgrade_next_pg_enum_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_enum OID value not set when in binary upgrade mode")));
/* /*
* Use binary-upgrade override for pg_enum.oid, if supplied. During * Use binary-upgrade override for pg_enum.oid, if supplied. During
* binary upgrade, all pg_enum.oid's are set this way so they are * binary upgrade, all pg_enum.oid's are set this way so they are
......
...@@ -126,9 +126,14 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId) ...@@ -126,9 +126,14 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
*/ */
tup = heap_form_tuple(tupDesc, values, nulls); tup = heap_form_tuple(tupDesc, values, nulls);
/* Use binary-upgrade override for pg_type.oid, if supplied. */ /* Use binary-upgrade override for pg_type.oid? */
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_pg_type_oid)) if (IsBinaryUpgrade)
{ {
if (!OidIsValid(binary_upgrade_next_pg_type_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_type OID value not set when in binary upgrade mode")));
HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid); HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid);
binary_upgrade_next_pg_type_oid = InvalidOid; binary_upgrade_next_pg_type_oid = InvalidOid;
} }
...@@ -437,8 +442,13 @@ TypeCreate(Oid newTypeOid, ...@@ -437,8 +442,13 @@ TypeCreate(Oid newTypeOid,
if (OidIsValid(newTypeOid)) if (OidIsValid(newTypeOid))
HeapTupleSetOid(tup, newTypeOid); HeapTupleSetOid(tup, newTypeOid);
/* Use binary-upgrade override for pg_type.oid, if supplied. */ /* Use binary-upgrade override for pg_type.oid, if supplied. */
else if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_pg_type_oid)) else if (IsBinaryUpgrade)
{ {
if (!OidIsValid(binary_upgrade_next_pg_type_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_type OID value not set when in binary upgrade mode")));
HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid); HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid);
binary_upgrade_next_pg_type_oid = InvalidOid; binary_upgrade_next_pg_type_oid = InvalidOid;
} }
......
...@@ -259,7 +259,11 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, ...@@ -259,7 +259,11 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
else else
namespaceid = PG_TOAST_NAMESPACE; namespaceid = PG_TOAST_NAMESPACE;
/* Use binary-upgrade override for pg_type.oid, if supplied. */ /*
* Use binary-upgrade override for pg_type.oid, if supplied. We might
* be in the post-schema-restore phase where we are doing ALTER TABLE
* to create TOAST tables that didn't exist in the old cluster.
*/
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_toast_pg_type_oid)) if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_toast_pg_type_oid))
{ {
toast_typid = binary_upgrade_next_toast_pg_type_oid; toast_typid = binary_upgrade_next_toast_pg_type_oid;
......
...@@ -1986,9 +1986,14 @@ AssignTypeArrayOid(void) ...@@ -1986,9 +1986,14 @@ AssignTypeArrayOid(void)
{ {
Oid type_array_oid; Oid type_array_oid;
/* Use binary-upgrade override for pg_type.typarray, if supplied. */ /* Use binary-upgrade override for pg_type.typarray? */
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_array_pg_type_oid)) if (IsBinaryUpgrade)
{ {
if (!OidIsValid(binary_upgrade_next_array_pg_type_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_type array OID value not set when in binary upgrade mode")));
type_array_oid = binary_upgrade_next_array_pg_type_oid; type_array_oid = binary_upgrade_next_array_pg_type_oid;
binary_upgrade_next_array_pg_type_oid = InvalidOid; binary_upgrade_next_array_pg_type_oid = InvalidOid;
} }
......
...@@ -379,10 +379,15 @@ CreateRole(CreateRoleStmt *stmt) ...@@ -379,10 +379,15 @@ CreateRole(CreateRoleStmt *stmt)
/* /*
* pg_largeobject_metadata contains pg_authid.oid's, so we use the * pg_largeobject_metadata contains pg_authid.oid's, so we use the
* binary-upgrade override, if specified. * binary-upgrade override.
*/ */
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_pg_authid_oid)) if (IsBinaryUpgrade)
{ {
if (!OidIsValid(binary_upgrade_next_pg_authid_oid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_authid OID value not set when in binary upgrade mode")));
HeapTupleSetOid(tuple, binary_upgrade_next_pg_authid_oid); HeapTupleSetOid(tuple, binary_upgrade_next_pg_authid_oid);
binary_upgrade_next_pg_authid_oid = InvalidOid; binary_upgrade_next_pg_authid_oid = InvalidOid;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment