You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using SQL (rather than PL/pgSQL) means that it can be inlined
Using IMMUTABLE means that it can be optimized away when used on the same data
Using PARALLEL SAFE indicates it can be used in many threads at once
SQL to reverse bytea
CREATE OR REPLACEFUNCTIONbytea_reverse_series (bytea) RETURNS bytea
LANGUAGE SQL
IMMUTABLE
PARALLEL SAFE
RETURN (
SELECT string_agg(substring($1FROM byte_pos FOR 1), '')
FROM generate_series(octet_length($1), 1, -1) AS byte_pos
);
PL/pgSQL to reverse bytea
CREATE OR REPLACEfunctionbytea_reverse_loop (input_bytea bytea)
RETURNS byteaAS $$
DECLARE
reversed_bytea bytea := repeat(E'\\000', length(input_bytea));
byte_length int;
BEGIN
byte_length := length(input_bytea);
FOR i IN1..byte_length LOOP
reversed_bytea := set_byte(reversed_bytea, i -1, get_byte(input_bytea, byte_length - i));
END LOOP;
RETURN reversed_bytea;
END;
$$ language plpgsql
License
The code snippets and documentation in this issue are licensed under the CC0-1.0 (Public Domain), as follows:
PostgreSQL Reverse BYTEA - reverse a byte array
Authored in 2024 by AJ ONeal [email protected]
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
PARALLEL SAFE
indicates it can be used in many threads at onceSQL to reverse bytea
PL/pgSQL to reverse bytea
License
The code snippets and documentation in this issue are licensed under the CC0-1.0 (Public Domain), as follows:
The text was updated successfully, but these errors were encountered: