Skip to content

Commit

Permalink
Fix scope_identity rewrite for other types (yugabyte#102)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
wboettge authored and abhinab-yb committed Nov 14, 2024
1 parent daab9a4 commit dd4fce9
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/postgres/src/backend/parser/parse_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit dd4fce9

Please sign in to comment.