Sample Header Ad - 728x90

PostgreSQL extending a variable-width bit string type?

0 votes
1 answer
308 views
PostgreSQL supports [Bit Strings](https://www.postgresql.org/docs/current/static/functions-bitstring.html) , SELECT B'01010101', B'01010101', '01010101'::bit(8), X'CC'; ?column? | ?column? | bit | ?column? ----------+----------+----------+---------- 01010101 | 01010101 | 01010101 | 11001100 SELECT pg_typeof(B'01010101'), pg_typeof(B'01010101'), pg_typeof('01010101'::bit(8)), pg_typeof(X'CC'); pg_typeof | pg_typeof | pg_typeof | pg_typeof -----------+-----------+-----------+----------- bit | bit | bit | bit The type bit is typed such that I can create a function that accepts a bit of any length regardless (variable-length), and return a Bit String of variable-length: CREATE FUNCTION type_test(_x bit) RETURNS bit AS $$ SELECT _x $$ LANGUAGE sql; SELECT type_test(X'CC'), type_test(X'CCCC'); Let's say I want to do an operation though that requires a bit-shifting algorithm, or a scratch pad bigger than the input type, how would I go about declaring that for internal use to the function. Like if I want to create a function that given X'CC' (8 bits) could use a 10 bit scratch pad. This would almost do what I want, CREATE FUNCTION type_test(_x bit) RETURNS bit AS $$ SELECT ('0' || _x || '0')::bit $$ LANGUAGE sql; SELECT type_test(X'55'); -- returns 0 But, it doesn't work because bit is bit(1) so the cast to it truncates the input to a single bit. Given bit(n), how do I create a bit(n+2) to work with.
Asked by Evan Carroll (65502 rep)
Apr 24, 2018, 07:38 PM
Last activity: Apr 24, 2018, 07:55 PM