Skip to content

Commit e5b457c

Browse files
committed
Add backend and pg_dump code to allow preservation of pg_enum oids, for
use in binary upgrades. Bump catalog version for detection by pg_migrator of new backend API.
1 parent 1fd9883 commit e5b457c

File tree

5 files changed

+73
-29
lines changed

5 files changed

+73
-29
lines changed

src/backend/catalog/pg_enum.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/catalog/pg_enum.c,v 1.11 2009/12/24 22:17:58 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/catalog/pg_enum.c,v 1.12 2009/12/27 14:50:41 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -33,7 +33,8 @@ static int oid_cmp(const void *p1, const void *p2);
3333
* vals is a list of Value strings.
3434
*/
3535
void
36-
EnumValuesCreate(Oid enumTypeOid, List *vals)
36+
EnumValuesCreate(Oid enumTypeOid, List *vals,
37+
Oid binary_upgrade_next_pg_enum_oid)
3738
{
3839
Relation pg_enum;
3940
TupleDesc tupDesc;
@@ -58,25 +59,39 @@ EnumValuesCreate(Oid enumTypeOid, List *vals)
5859
tupDesc = pg_enum->rd_att;
5960

6061
/*
61-
* Allocate oids. While this method does not absolutely guarantee that we
62-
* generate no duplicate oids (since we haven't entered each oid into the
63-
* table before allocating the next), trouble could only occur if the oid
64-
* counter wraps all the way around before we finish. Which seems
65-
* unlikely.
62+
* Allocate oids
6663
*/
6764
oids = (Oid *) palloc(num_elems * sizeof(Oid));
68-
for (elemno = 0; elemno < num_elems; elemno++)
65+
if (OidIsValid(binary_upgrade_next_pg_enum_oid))
66+
{
67+
if (num_elems != 1)
68+
ereport(ERROR,
69+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
70+
errmsg("EnumValuesCreate() can only set a single OID")));
71+
oids[0] = binary_upgrade_next_pg_enum_oid;
72+
binary_upgrade_next_pg_enum_oid = InvalidOid;
73+
}
74+
else
6975
{
7076
/*
71-
* The pg_enum.oid is stored in user tables. This oid must be
72-
* preserved by binary upgrades.
77+
* While this method does not absolutely guarantee that we generate
78+
* no duplicate oids (since we haven't entered each oid into the
79+
* table before allocating the next), trouble could only occur if
80+
* the oid counter wraps all the way around before we finish. Which
81+
* seems unlikely.
7382
*/
74-
oids[elemno] = GetNewOid(pg_enum);
83+
for (elemno = 0; elemno < num_elems; elemno++)
84+
{
85+
/*
86+
* The pg_enum.oid is stored in user tables. This oid must be
87+
* preserved by binary upgrades.
88+
*/
89+
oids[elemno] = GetNewOid(pg_enum);
90+
}
91+
/* sort them, just in case counter wrapped from high to low */
92+
qsort(oids, num_elems, sizeof(Oid), oid_cmp);
7593
}
7694

77-
/* sort them, just in case counter wrapped from high to low */
78-
qsort(oids, num_elems, sizeof(Oid), oid_cmp);
79-
8095
/* and make the entries */
8196
memset(nulls, false, sizeof(nulls));
8297

src/backend/commands/typecmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.141 2009/12/24 22:09:23 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.142 2009/12/27 14:50:43 momjian Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1161,7 +1161,7 @@ DefineEnum(CreateEnumStmt *stmt)
11611161
false); /* Type NOT NULL */
11621162

11631163
/* Enter the enum's values into pg_enum */
1164-
EnumValuesCreate(enumTypeOid, stmt->vals);
1164+
EnumValuesCreate(enumTypeOid, stmt->vals, InvalidOid);
11651165

11661166
/*
11671167
* Create the array type that goes with it.

src/bin/pg_dump/pg_dump.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.562 2009/12/26 16:55:21 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.563 2009/12/27 14:50:44 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -6528,12 +6528,14 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
65286528
PGresult *res;
65296529
int num,
65306530
i;
6531+
Oid enum_oid;
65316532
char *label;
65326533

65336534
/* Set proper schema search path so regproc references list correctly */
65346535
selectSourceSchema(tyinfo->dobj.namespace->dobj.name);
65356536

6536-
appendPQExpBuffer(query, "SELECT enumlabel FROM pg_catalog.pg_enum "
6537+
appendPQExpBuffer(query, "SELECT oid, enumlabel "
6538+
"FROM pg_catalog.pg_enum "
65376539
"WHERE enumtypid = '%u'"
65386540
"ORDER BY oid",
65396541
tyinfo->dobj.catId.oid);
@@ -6556,18 +6558,44 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
65566558
if (binary_upgrade)
65576559
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
65586560

6559-
appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (\n",
6561+
appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
65606562
fmtId(tyinfo->dobj.name));
6561-
for (i = 0; i < num; i++)
6563+
6564+
if (!binary_upgrade)
65626565
{
6563-
label = PQgetvalue(res, i, 0);
6564-
if (i > 0)
6565-
appendPQExpBuffer(q, ",\n");
6566-
appendPQExpBuffer(q, " ");
6567-
appendStringLiteralAH(q, label, fout);
6566+
/* Labels with server-assigned oids */
6567+
for (i = 0; i < num; i++)
6568+
{
6569+
label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
6570+
if (i > 0)
6571+
appendPQExpBuffer(q, ",");
6572+
appendPQExpBuffer(q, "\n ");
6573+
appendStringLiteralAH(q, label, fout);
6574+
}
65686575
}
6576+
65696577
appendPQExpBuffer(q, "\n);\n");
65706578

6579+
if (binary_upgrade)
6580+
{
6581+
/* Labels with dump-assigned (preserved) oids */
6582+
for (i = 0; i < num; i++)
6583+
{
6584+
enum_oid = atooid(PQgetvalue(res, i, PQfnumber(res, "oid")));
6585+
label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
6586+
6587+
if (i == 0)
6588+
appendPQExpBuffer(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
6589+
appendPQExpBuffer(q,
6590+
"SELECT binary_upgrade.add_pg_enum_label('%u'::pg_catalog.oid, "
6591+
"'%u'::pg_catalog.oid, ",
6592+
enum_oid, tyinfo->dobj.catId.oid);
6593+
appendStringLiteralAH(q, label, fout);
6594+
appendPQExpBuffer(q, ");\n");
6595+
}
6596+
appendPQExpBuffer(q, "\n");
6597+
}
6598+
65716599
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
65726600
tyinfo->dobj.name,
65736601
tyinfo->dobj.namespace->dobj.name,

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.560 2009/12/19 04:08:32 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.561 2009/12/27 14:50:46 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200912181
56+
#define CATALOG_VERSION_NO 200912271
5757

5858
#endif

src/include/catalog/pg_enum.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 2006-2009, PostgreSQL Global Development Group
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_enum.h,v 1.5 2009/01/01 17:23:57 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_enum.h,v 1.6 2009/12/27 14:50:46 momjian Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -60,7 +60,8 @@ typedef FormData_pg_enum *Form_pg_enum;
6060
/*
6161
* prototypes for functions in pg_enum.c
6262
*/
63-
extern void EnumValuesCreate(Oid enumTypeOid, List *vals);
63+
extern void EnumValuesCreate(Oid enumTypeOid, List *vals,
64+
Oid binary_upgrade_next_pg_enum_oid);
6465
extern void EnumValuesDelete(Oid enumTypeOid);
6566

6667
#endif /* PG_ENUM_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy