Skip to content

Commit

Permalink
BABEL: Fix a syntax error to set attoptions bbf_original_name having …
Browse files Browse the repository at this point in the history
…a special character (yugabyte#13)

* Fix a syntax error to set attoptions bbf_original_name having a special character

Babelfish keeps a column original name in attoptions field in pg_atrribute.
If special chracter is involved, as pg_dump generates an ALTER TABLE
ALTER COLUMN SET without any escaping, it can cause an error.

To prevent this issue, when generating ALTER statement, enclose column
original name with single quotes so that original name can be treated as
a string literal. This single quotes will be stripped out by parser so
attoptions stores the original name without quotes. So this will not
break backward-compatibility.

Task: BABEL-3121
Signed-off-by: Sangil Song [email protected]
(cherry picked from commit 773b995c45d56fbcdfb74b7e983f7ba2c7ae1717)
  • Loading branch information
sangilsong authored and abhinab-yb committed Nov 14, 2024
1 parent bf480b7 commit 385a006
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/postgres/src/bin/pg_dump/dump_babel_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "catalog/pg_class_d.h"
#include "catalog/pg_proc_d.h"
#include "catalog/pg_type_d.h"
#include "common/logging.h"
#include "dump_babel_utils.h"
#include "pg_backup_db.h"
#include "pg_dump.h"
Expand Down Expand Up @@ -281,6 +282,40 @@ getTsqlTvfType(Archive *fout, const FuncInfo *finfo, char prokind, bool proretse
return PLTSQL_TVFTYPE_NONE;
}

void fixAttoptionsBbfOriginalName(Archive *fout, char **attoptions)
{
PGresult *res;
PQExpBuffer q;

if (!isBabelfishDatabase(fout))
return;

q = createPQExpBuffer();

/*
* As attoptions can be a list of options,
* we will split options first, make them as an array, find an option starting with 'bbf_original_name',
* enclose its value with single quotes, and aggregate all array elements into a single string.
*/
appendPQExpBuffer(q,
"SELECT substring(options, 2, length(options)-2) as new_options FROM ( "
"SELECT array_agg( "
"CASE "
"WHEN option LIKE 'bbf_original_name=%%' "
"THEN 'bbf_original_name=' || '\'\'' || substring(option, length('bbf_original_name=')+1) || '\'\'' "
"ELSE option "
"END)::text as options "
"FROM unnest(string_to_array('%s',',')) AS option "
") T;",
*attoptions);

res = ExecuteSqlQueryForSingleRow(fout, q->data);
*attoptions = pg_strdup(PQgetvalue(res, 0, 0));

destroyPQExpBuffer(q);
PQclear(res);
}

/*
* setOrResetPltsqlFuncRestoreGUCs:
* sets/resets GUCs required to properly restore
Expand Down Expand Up @@ -308,4 +343,4 @@ setOrResetPltsqlFuncRestoreGUCs(Archive *fout, PQExpBuffer q, const FuncInfo *fi
default:
break;
}
}
}
1 change: 1 addition & 0 deletions src/postgres/src/bin/pg_dump/dump_babel_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern bool isBabelfishDatabase(Archive *fout);
extern void fixTsqlTableTypeDependency(Archive *fout, DumpableObject *func, DumpableObject *tabletype, char deptype);
extern bool isTsqlTableType(Archive *fout, const TableInfo *tbinfo);
extern int getTsqlTvfType(Archive *fout, const FuncInfo *finfo, char prokind, bool proretset);
extern void fixAttoptionsBbfOriginalName(Archive *fout, char **attoptions);
extern void setOrResetPltsqlFuncRestoreGUCs(Archive *fout, PQExpBuffer q, const FuncInfo *finfo, char prokind, bool proretset, bool is_set);

#endif
4 changes: 4 additions & 0 deletions src/postgres/src/bin/pg_dump/pg_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -16333,10 +16333,14 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
* Dump per-column attributes.
*/
if (tbinfo->attoptions[j][0] != '\0')
{
fixAttoptionsBbfOriginalName(fout, &tbinfo->attoptions[j]);

appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
foreign, qualrelname,
fmtId(tbinfo->attnames[j]),
tbinfo->attoptions[j]);
}

/*
* Dump per-column fdw options.
Expand Down

0 comments on commit 385a006

Please sign in to comment.