Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve makeArrayTypeName's algorithm for choosing array type names. #152

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 34 additions & 56 deletions src/backend/catalog/pg_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "commands/typecmds.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
Expand All @@ -47,9 +48,6 @@
#include "catalog/pg_type_encoding.h"
#include "cdb/cdbvars.h"

static char *makeUniqueTypeName(const char *typeName, Oid typeNamespace,
bool tryOriginal);

/* GPDB_14_MERGE_FIXME: Do we need to keep binary_upgrade_next_pg_type_oid
* for binary upgrade?
*/
Expand Down Expand Up @@ -964,16 +962,41 @@ RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
char *
makeArrayTypeName(const char *typeName, Oid typeNamespace)
{
char *arr;
char *arr_name;
int pass = 0;
char suffix[NAMEDATALEN];

arr = makeUniqueTypeName(typeName, typeNamespace, false);
if (arr == NULL)
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("could not form array type name for type \"%s\"",
typeName)));
/*
* Per ancient Postgres tradition, array type names are made by prepending
* an underscore to the base type name. Much client code knows that
* convention, so don't muck with it. However, the tradition is less
* clear about what to do in the corner cases where the resulting name is
* too long or conflicts with an existing name. Our current rules are (1)
* truncate the base name on the right as needed, and (2) if there is a
* conflict, append another underscore and some digits chosen to make it
* unique. This is similar to what ChooseRelationName() does.
*
* The actual name generation can be farmed out to makeObjectName() by
* giving it an empty first name component.
*/

/* First, try with no numeric suffix */
arr_name = makeObjectName("", typeName, NULL);

for (;;)
{
if (!SearchSysCacheExists2(TYPENAMENSP,
CStringGetDatum(arr_name),
ObjectIdGetDatum(typeNamespace)))
break;

/* That attempt conflicted. Prepare a new name with some digits. */
pfree(arr_name);
snprintf(suffix, sizeof(suffix), "%d", ++pass);
arr_name = makeObjectName("", typeName, suffix);
}

return arr;
return arr_name;
}


Expand Down Expand Up @@ -1080,48 +1103,3 @@ makeMultirangeTypeName(const char *rangeTypeName, Oid typeNamespace)

return pstrdup(buf);
}

/*
* makeUniqueTypeName
* Generate a unique name for a prospective new type
*
* Given a typeName, return a new palloc'ed name by prepending underscores
* until a non-conflicting name results.
*
* If tryOriginal, first try with zero underscores.
*/
static char *
makeUniqueTypeName(const char *typeName, Oid typeNamespace, bool tryOriginal)
{
int i;
int namelen;
char dest[NAMEDATALEN];

Assert(strlen(typeName) <= NAMEDATALEN - 1);

if (tryOriginal &&
!SearchSysCacheExists2(TYPENAMENSP,
CStringGetDatum(typeName),
ObjectIdGetDatum(typeNamespace)))
return pstrdup(typeName);

/*
* The idea is to prepend underscores as needed until we make a name that
* doesn't collide with anything ...
*/
namelen = strlen(typeName);
for (i = 1; i < NAMEDATALEN - 1; i++)
{
dest[i - 1] = '_';
strlcpy(dest + i, typeName, NAMEDATALEN - i);
if (namelen + i >= NAMEDATALEN)
truncate_identifier(dest, NAMEDATALEN, false);

if (!SearchSysCacheExists2(TYPENAMENSP,
CStringGetDatum(dest),
ObjectIdGetDatum(typeNamespace)))
return pstrdup(dest);
}

return NULL;
}
6 changes: 3 additions & 3 deletions src/test/regress/expected/alter_table.out
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ SELECT typname FROM pg_type WHERE oid = 'attmp_array[]'::regtype;
(1 row)

SELECT typname FROM pg_type WHERE oid = '_attmp_array[]'::regtype;
typname
----------------
___attmp_array
typname
-----------------
__attmp_array_1
(1 row)

DROP TABLE _attmp_array;
Expand Down