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

Postgres: add a native reverse(bytea) function #45

Open
bitjson opened this issue Nov 24, 2021 · 0 comments
Open

Postgres: add a native reverse(bytea) function #45

bitjson opened this issue Nov 24, 2021 · 0 comments
Labels
enhancement New feature or request external An issue we're tracking in another project

Comments

@bitjson
Copy link
Member

bitjson commented Nov 24, 2021

It would be nice if Postgres supported a native reverse(bytea) function. Currently we're manually reversing byteas with this plpgsql function (which is presumably somewhat slower than a native option?):

CREATE OR REPLACE FUNCTION reverse_bytea(source_bytes bytea) RETURNS bytea
LANGUAGE plpgsql IMMUTABLE
AS $$
DECLARE
last_source_index integer := octet_length(source_bytes) - 1;
reversed bytea := source_bytes; -- set result to proper length
BEGIN
FOR i IN 0..last_source_index LOOP
reversed := set_byte(reversed, i, get_byte(source_bytes, last_source_index - i));
END LOOP;
RETURN reversed;
END;
$$;
COMMENT ON FUNCTION reverse_bytea(bytea) IS 'Reverse the byte order of a bytea.';

There doesn't seem to be a good reason why Postgres wouldn't support reversing bytea types natively. We might want to submit a patch. Looks like we'd need to add it to src/backend/utils/adt/varlena.c (see text_reverse).

@bitjson bitjson added enhancement New feature or request external An issue we're tracking in another project labels Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request external An issue we're tracking in another project
Projects
None yet
Development

No branches or pull requests

1 participant