Skip to content

Instantly share code, notes, and snippets.

@zmij
Created June 16, 2015 08:10
Show Gist options
  • Save zmij/1c90a2af3445f363ca83 to your computer and use it in GitHub Desktop.
Save zmij/1c90a2af3445f363ca83 to your computer and use it in GitHub Desktop.
Cast inet to bigint and back
create or replace function bigint_to_inet (intaddr bigint) returns inet
as
$BODY$
begin
return '0.0.0.0'::inet + intaddr;
end
$BODY$
language plpgsql immutable strict;
create cast (bigint as inet) with function bigint_to_inet(bigint);
create or replace function inet_to_bigint (addr inet) returns bigint
as
$BODY$
begin
return addr - '0.0.0.0';
end
$BODY$
language plpgsql immutable strict;
create cast (inet as bigint) with function inet_to_bigint(inet);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment