What’s the purpose of the following postgresql stored procedure, it’s used when execute a query, thank you!
CREATE OR REPLACE FUNCTION extract_fp_query(int[]) RETURNS int[]
AS $$
SELECT uniq(sort(subarray($1 - 627964279,
greatest(0, least(icount($1 - 627964279) - 120, 80)), 120)));
$$ LANGUAGE 'SQL' IMMUTABLE STRICT;
This code relies heavily on functionality in the
intarrayextension. Once you know that you can work it out by breaking it down into steps.It looks to me like some kind of poor-man’s crypto or obfuscation routine, but you’ve failed to provide any information about its inputs so it’s hard to say more. Essentially it returns a sorted, de-duplicated subset of the passed array, deciding which subset to return based on the number of elements in the array. If I read it correctly it removes the value 627964279 from the array if it appears there, then returns 120 elements (before de-duplication) from an offset of 0 and 80 elements into the array depending on the number of elements in the array.
Create a test database. I’ll call mine
regress. Create it and install theintarraycontrib module into it as a superuser:Now running as
myusername, whatever your unpriveleged user account is, break the code down into steps and try each step. Just like you would in C. It might help to format it into expressions:then evaluate each sub-expression by hand with a known input, substituting results as you go and simplifying the expression while jotting down what it does. I can’t do it for you because you haven’t supplied a sample input, but, replacing
ARRAY[42,5,9,24,1,627964279]with your input array you’d do something like:Now, by substituting
5for the expressiongreatest(...)into thesubarrayexpression we get:which is after the evaluation of the array item removal:
In this case the
sortanduniqhave no further effect.What’s it for? Who knows, as you haven’t provided an input array that might offer clues.
See the intarray documentation.