Skip to content

Commit 55659cf

Browse files
bmdoilmy-ship-it
authored andcommitted
Refactor dumping of pg_type_encoding attributes.
This commit drops the GPDB specific TypeStorageOptions struct and supporting code in favor of adding pg_type_encoding.typoptions to the getTypes query. The usage of CatalogId as the hash index caused collisions with the TypeInfo indexes when TypeStorageOptions was a unique dumpable object, since the only unique aspect was the typstorage field. Now, when dumpType is called, we check the typstorage field and dump the ALTER TABLE...SET DEFAULT ENCODING TOC entry if needed. Authored-by: Brent Doil <[email protected]>
1 parent 01a7acd commit 55659cf

File tree

5 files changed

+38
-142
lines changed

5 files changed

+38
-142
lines changed

src/bin/pg_dump/common.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ getSchemaData(Archive *fout, int *numTablesPtr)
125125
int numForeignServers;
126126
int numDefaultACLs;
127127
int numEventTriggers;
128-
int numTypeStorageOptions;
129128

130129
/* GPDB specific variables */
131130
int numExtProtocols;
@@ -171,10 +170,6 @@ getSchemaData(Archive *fout, int *numTablesPtr)
171170
pg_log_info("reading user-defined types");
172171
(void) getTypes(fout, &numTypes);
173172

174-
/* this must be after getFuncs */
175-
pg_log_info("reading type storage options");
176-
getTypeStorageOptions(fout, &numTypeStorageOptions);
177-
178173
/* this must be after getFuncs, too */
179174
pg_log_info("reading procedural languages");
180175
getProcLangs(fout, &numProcLangs);

src/bin/pg_dump/pg_dump.c

Lines changed: 21 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo);
266266

267267
/* GPDB follow upstream style */
268268
static void dumpExtProtocol(Archive *fout, const ExtProtInfo *ptcinfo);
269-
static void dumpTypeStorageOptions(Archive *fout, const TypeStorageOptions *tstorageoptions);
270269

271270
static void dumpUserMappings(Archive *fout,
272271
const char *servername, const char *namespace,
@@ -368,6 +367,7 @@ static bool forcePartitionRootLoad(const TableInfo *tbinfo);
368367

369368

370369
/* START MPP ADDITION */
370+
static void dumpTypeStorageOptions(Archive *fout, const TypeInfo *tyinfo);
371371
static void setExtPartDependency(TableInfo *tblinfo, int numTables);
372372
static char *format_table_function_columns(Archive *fout, const FuncInfo *finfo, int nallargs,
373373
char **allargtypes,
@@ -5948,6 +5948,7 @@ getTypes(Archive *fout, int *numTypes)
59485948
int i_typtype;
59495949
int i_typisdefined;
59505950
int i_isarray;
5951+
int i_typstorage;
59515952

59525953
/*
59535954
* we include even the built-in types because those may be used as array
@@ -5988,8 +5989,10 @@ getTypes(Archive *fout, int *numTypes)
59885989
"ELSE (SELECT relkind FROM pg_class WHERE oid = t.typrelid) END AS typrelkind, "
59895990
"t.typtype, t.typisdefined, "
59905991
"t.typname[0] = '_' AND t.typelem != 0 AND "
5991-
"(SELECT typarray FROM pg_type te WHERE oid = t.typelem) = t.oid AS isarray "
5992+
"(SELECT typarray FROM pg_type te WHERE oid = t.typelem) = t.oid AS isarray, "
5993+
"coalesce(array_to_string(e.typoptions, ', '), '') AS typstorage "
59925994
"FROM pg_type t "
5995+
"LEFT JOIN pg_type_encoding e ON t.oid = e.typid "
59935996
"LEFT JOIN pg_init_privs pip ON "
59945997
"(t.oid = pip.objoid "
59955998
"AND pip.classoid = 'pg_type'::regclass "
@@ -6057,6 +6060,7 @@ getTypes(Archive *fout, int *numTypes)
60576060
i_typtype = PQfnumber(res, "typtype");
60586061
i_typisdefined = PQfnumber(res, "typisdefined");
60596062
i_isarray = PQfnumber(res, "isarray");
6063+
i_typstorage = PQfnumber(res, "typstorage");
60606064

60616065
for (i = 0; i < ntups; i++)
60626066
{
@@ -6089,6 +6093,8 @@ getTypes(Archive *fout, int *numTypes)
60896093
else
60906094
tyinfo[i].isArray = false;
60916095

6096+
tyinfo[i].typstorage = pg_strdup(PQgetvalue(res, i, i_typstorage));
6097+
60926098
if (tyinfo[i].typtype == 'm')
60936099
tyinfo[i].isMultirange = true;
60946100
else
@@ -6157,91 +6163,6 @@ getTypes(Archive *fout, int *numTypes)
61576163
return tyinfo;
61586164
}
61596165

6160-
6161-
6162-
/*
6163-
* getTypeStorageOptions:
6164-
* read all types with storage options in the system catalogs and return them in the
6165-
* TypeStorageOptions* structure
6166-
*
6167-
* numTypes is set to the number of types with storage options read in
6168-
*
6169-
*/
6170-
TypeStorageOptions *
6171-
getTypeStorageOptions(Archive *fout, int *numTypes)
6172-
{
6173-
PGresult *res;
6174-
int ntups;
6175-
int i;
6176-
PQExpBuffer query = createPQExpBuffer();
6177-
TypeStorageOptions *tstorageoptions;
6178-
int i_tableoid;
6179-
int i_oid;
6180-
int i_typname;
6181-
int i_typnamespace;
6182-
int i_typoptions;
6183-
int i_rolname;
6184-
6185-
/*
6186-
* The following statement used format_type to resolve an internal name to its equivalent sql name.
6187-
* The format_type seems to do two things, it translates an internal type name (e.g. bpchar) into its
6188-
* sql equivalent (e.g. character), and it puts trailing "[]" on a type if it is an array.
6189-
* For any user defined type (ie. oid > 10000) or any type that might be an array (ie. starts with '_'),
6190-
* then we will call quote_ident. If the type is a system defined type (i.e. oid <= 10000)
6191-
* and can not possibly be an array (i.e. does not start with '_'), then call format_type to get the name. The
6192-
* reason we do not call format_type for arrays is that it will return a '[]' on the end, which can not be used
6193-
* when dumping the type.
6194-
*/
6195-
appendPQExpBuffer(query, "SELECT "
6196-
"CASE WHEN t.oid > 10000 OR substring(t.typname from 1 for 1) = '_' "
6197-
"THEN quote_ident(t.typname) "
6198-
"ELSE pg_catalog.format_type(t.oid, NULL) "
6199-
"END as typname, "
6200-
"t.tableoid as tableoid, "
6201-
"t.oid AS oid, "
6202-
"t.typnamespace AS typnamespace, "
6203-
"(%s typowner) as rolname, "
6204-
"array_to_string(a.typoptions, ', ') AS typoptions "
6205-
"FROM pg_type t "
6206-
"JOIN pg_catalog.pg_type_encoding a ON a.typid = t.oid "
6207-
"WHERE t.typisdefined = 't'", username_subquery);
6208-
6209-
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6210-
6211-
ntups = PQntuples(res);
6212-
6213-
tstorageoptions = (TypeStorageOptions *) pg_malloc(ntups * sizeof(TypeStorageOptions));
6214-
6215-
i_tableoid = PQfnumber(res, "tableoid");
6216-
i_oid = PQfnumber(res, "oid");
6217-
i_typname = PQfnumber(res, "typname");
6218-
i_typnamespace = PQfnumber(res, "typnamespace");
6219-
i_typoptions = PQfnumber(res, "typoptions");
6220-
i_rolname = PQfnumber(res, "rolname");
6221-
6222-
for (i = 0; i < ntups; i++)
6223-
{
6224-
tstorageoptions[i].dobj.objType = DO_TYPE_STORAGE_OPTIONS;
6225-
tstorageoptions[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6226-
tstorageoptions[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6227-
AssignDumpId(&tstorageoptions[i].dobj);
6228-
tstorageoptions[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
6229-
tstorageoptions[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)));
6230-
tstorageoptions[i].typoptions = pg_strdup(PQgetvalue(res, i, i_typoptions));
6231-
tstorageoptions[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
6232-
}
6233-
6234-
*numTypes = ntups;
6235-
6236-
PQclear(res);
6237-
6238-
destroyPQExpBuffer(query);
6239-
6240-
return tstorageoptions;
6241-
}
6242-
6243-
6244-
62456166
/*
62466167
* getOperators:
62476168
* read all operators in the system catalogs and return them in the
@@ -11488,9 +11409,6 @@ dumpDumpableObject(Archive *fout, const DumpableObject *dobj)
1148811409
case DO_TYPE:
1148911410
dumpType(fout, (const TypeInfo *) dobj);
1149011411
break;
11491-
case DO_TYPE_STORAGE_OPTIONS:
11492-
dumpTypeStorageOptions(fout, (const TypeStorageOptions *) dobj);
11493-
break;
1149411412
case DO_SHELL_TYPE:
1149511413
dumpShellType(fout, (const ShellTypeInfo *) dobj);
1149611414
break;
@@ -11871,6 +11789,10 @@ dumpType(Archive *fout, const TypeInfo *tyinfo)
1187111789
else
1187211790
pg_log_warning("typtype of data type \"%s\" appears to be invalid",
1187311791
tyinfo->dobj.name);
11792+
11793+
if (tyinfo->typstorage && *tyinfo->typstorage != '\0')
11794+
dumpTypeStorageOptions(fout, tyinfo);
11795+
1187411796
}
1187511797

1187611798
/*
@@ -12486,36 +12408,28 @@ dumpBaseType(Archive *fout, const TypeInfo *tyinfo)
1248612408
* writes out to fout the ALTER TYPE queries to set default storage options for type
1248712409
*/
1248812410
static void
12489-
dumpTypeStorageOptions(Archive *fout, const TypeStorageOptions *tstorageoptions)
12411+
dumpTypeStorageOptions(Archive *fout, const TypeInfo *tyinfo)
1249012412
{
1249112413
PQExpBuffer q;
12492-
PQExpBuffer delq;
12493-
1249412414
q = createPQExpBuffer();
12495-
delq = createPQExpBuffer();
1249612415

12497-
/*
12498-
* Type name is already quoted by caller using quote_ident, hence used
12499-
* directly here.
12500-
*/
1250112416
appendPQExpBuffer(q, "ALTER TYPE %s.",
12502-
fmtId(tstorageoptions->dobj.namespace->dobj.name));
12417+
fmtId(tyinfo->dobj.namespace->dobj.name));
1250312418
appendPQExpBuffer(q, "%s SET DEFAULT ENCODING (%s);\n",
12504-
tstorageoptions->dobj.name,
12505-
tstorageoptions->typoptions);
12419+
fmtId(tyinfo->dobj.name),
12420+
tyinfo->typstorage);
1250612421

1250712422
ArchiveEntry(fout,
12508-
tstorageoptions->dobj.catId,
12509-
tstorageoptions->dobj.dumpId,
12510-
ARCHIVE_OPTS(.tag = tstorageoptions->dobj.name,
12511-
.namespace = tstorageoptions->dobj.namespace->dobj.name,
12512-
.owner = tstorageoptions->rolname,
12423+
tyinfo->dobj.catId,
12424+
tyinfo->dobj.dumpId,
12425+
ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12426+
.namespace = tyinfo->dobj.namespace->dobj.name,
12427+
.owner = tyinfo->rolname,
1251312428
.description = "TYPE STORAGE OPTIONS",
1251412429
.section = SECTION_PRE_DATA,
1251512430
.createStmt = q->data));
1251612431

1251712432
destroyPQExpBuffer(q);
12518-
destroyPQExpBuffer(delq);
1251912433
}
1252012434

1252112435
/*
@@ -21030,7 +20944,6 @@ addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
2103020944
case DO_TRANSFORM:
2103120945
case DO_BLOB:
2103220946
case DO_EXTPROTOCOL:
21033-
case DO_TYPE_STORAGE_OPTIONS:
2103420947
case DO_BINARY_UPGRADE:
2103520948
/* Pre-data objects: must come before the pre-data boundary */
2103620949
addObjectDependency(preDataBound, dobj->dumpId);

src/bin/pg_dump/pg_dump.h

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ typedef enum
7777
DO_BLOB,
7878
DO_BLOB_DATA,
7979
DO_EXTPROTOCOL,
80-
DO_TYPE_STORAGE_OPTIONS,
8180
DO_PRE_DATA_BOUNDARY,
8281
DO_POST_DATA_BOUNDARY,
8382
DO_EVENT_TRIGGER,
@@ -178,12 +177,12 @@ typedef struct _typeInfo
178177
* result of format_type(), which will be quoted if needed, and might be
179178
* schema-qualified too.
180179
*/
181-
char *ftypname;
182-
char *rolname; /* name of owner, or empty string */
183-
char *typacl;
184-
char *rtypacl;
185-
char *inittypacl;
186-
char *initrtypacl;
180+
char *ftypname;
181+
char *rolname; /* name of owner, or empty string */
182+
char *typacl;
183+
char *rtypacl;
184+
char *inittypacl;
185+
char *initrtypacl;
187186
Oid typelem;
188187
Oid typrelid;
189188
char typrelkind; /* 'r', 'v', 'c', etc */
@@ -196,24 +195,10 @@ typedef struct _typeInfo
196195
/* If it's a domain, we store links to its constraints here: */
197196
int nDomChecks;
198197
struct _constraintInfo *domChecks;
198+
char *typstorage; /* GPDB: store the type's encoding clause */
199199
} TypeInfo;
200200

201201

202-
typedef struct _typeStorageOptions
203-
{
204-
DumpableObject dobj;
205-
206-
/*
207-
* Note: dobj.name is the pg_type.typname entry. format_type() might
208-
* produce something different than typname
209-
*/
210-
char *typnamespace;
211-
char *typoptions; /* storage options */
212-
char *rolname; /* name of owner, or empty string */
213-
} TypeStorageOptions;
214-
215-
216-
217202
typedef struct _shellTypeInfo
218203
{
219204
DumpableObject dobj;
@@ -779,7 +764,6 @@ extern void getPublicationTables(Archive *fout, TableInfo tblinfo[],
779764
extern void getSubscriptions(Archive *fout);
780765

781766
/* START MPP ADDITION */
782-
extern TypeStorageOptions *getTypeStorageOptions(Archive *fout, int *numTypes);
783767
extern ExtProtInfo *getExtProtocols(Archive *fout, int *numExtProtocols);
784768
extern BinaryUpgradeInfo *getBinaryUpgradeObjects(void);
785769

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ static const int dbObjectTypePriority[] =
138138
21, /* DO_BLOB */
139139
25, /* DO_BLOB_DATA */
140140
8, /* DO_EXTPROTOCOL */
141-
21, /* DO_TYPE_STORAGE_OPTIONS */
142141
1, /* DO_BINARY_UPGRADE */
143142
22, /* DO_PRE_DATA_BOUNDARY */
144143
26, /* DO_POST_DATA_BOUNDARY */
@@ -1287,11 +1286,6 @@ describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
12871286
"TYPE %s (ID %d OID %u)",
12881287
obj->name, obj->dumpId, obj->catId.oid);
12891288
return;
1290-
case DO_TYPE_STORAGE_OPTIONS:
1291-
snprintf(buf, bufsize,
1292-
"TYPE STORAGE OPTIONS FOR TYPE %s.%s (ID %d OID %u) OPTIONS %s",
1293-
((TypeStorageOptions *)obj)->typnamespace, obj->name, obj->dumpId, obj->catId.oid, ((TypeStorageOptions *)obj)->typoptions);
1294-
return;
12951289
case DO_SHELL_TYPE:
12961290
snprintf(buf, bufsize,
12971291
"SHELL TYPE %s (ID %d OID %u)",

src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,16 @@
20222022
unlike => { exclude_dump_test_schema => 1, },
20232023
},
20242024
2025+
'ALTER TYPE dump_test.int42 SET DEFAULT ENCODING' => {
2026+
create_order => 43,
2027+
create_sql => 'ALTER TYPE dump_test.int42 SET DEFAULT ENCODING
2028+
(compresstype=rle_type, blocksize=8192, compresslevel=4);',
2029+
regexp => qr/^\QALTER TYPE dump_test.int42 SET DEFAULT ENCODING (compresstype=rle_type, blocksize=8192, compresslevel=4);\E/m,
2030+
like =>
2031+
{ %full_runs, %dump_test_schema_runs, section_pre_data => 1, },
2032+
unlike => { exclude_dump_test_schema => 1, },
2033+
},
2034+
20252035
'CREATE FOREIGN DATA WRAPPER dummy' => {
20262036
create_order => 35,
20272037
create_sql => 'CREATE FOREIGN DATA WRAPPER dummy;',

0 commit comments

Comments
 (0)