From dd4fce9f0a9b33f744e643affb7ebd2a5e44d56e Mon Sep 17 00:00:00 2001 From: Walt Boettge Date: Tue, 7 Feb 2023 21:03:50 -0800 Subject: [PATCH] Fix scope_identity rewrite for other types (#102) * Rewrite scope_identity on some index lookups Currently, a problem arises when using scope_identity to lookup on an index of identities. Babelfish implemented identity columns using INT sequences, but scope_identity returns a numeric type. Numeric has higher precedence than int, so the index column is implicitly cast to a numeric. However, this means queries that should use an index lookup instead use a sequential scan, significantly degrading performance. With this change, if a call to scope_identity or babelfish_get_last_identity_numeric (used with @@identity) is found in a where clause compared to an integer, the function is replaced with babelfish_get_last_identity, which returns an int so the index is used. Task: BABEL-3384 Signed-off-by: Walt Boettge * Fix scope_identity rewrite for other types A previous commit rewrites scope_identity to return an integer so that an index may be used for certain queries. This only applied to columns of type integer, but identity columns can be of type smallint and bigint as well. Now, if scope_identity is compared against columns of these types the call will be rewritten and the index will be used. Signed-off-by: Walt Boettge --- src/postgres/src/backend/parser/parse_expr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/postgres/src/backend/parser/parse_expr.c b/src/postgres/src/backend/parser/parse_expr.c index 7d4ec452a594..7deb6ba00365 100644 --- a/src/postgres/src/backend/parser/parse_expr.c +++ b/src/postgres/src/backend/parser/parse_expr.c @@ -948,7 +948,7 @@ rewrite_scope_identity_call(ParseState *pstate, Node **lexpr, Node **rexpr) if (strcmp(func_name, "babelfish_get_last_identity_numeric") != 0 && strcmp(func_name, "scope_identity") != 0) return; - if (col_expr->vartype != INT4OID) + if (col_expr->vartype != INT2OID && col_expr->vartype != INT4OID && col_expr->vartype != INT8OID) return; new_call = makeFuncCall(list_make1(makeString("babelfish_get_last_identity")), NULL, COERCE_EXPLICIT_CALL, -1);