Skip to content

Instantly share code, notes, and snippets.

@langpavel
Last active July 12, 2021 10:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langpavel/9db4b617d7c8f067b61a to your computer and use it in GitHub Desktop.
Save langpavel/9db4b617d7c8f067b61a to your computer and use it in GitHub Desktop.
PostgreSQL URL slug usable for indices
/**
* original unaccent function isn't usable for indices
*/
CREATE OR REPLACE FUNCTION uaccent(text) RETURNS text AS
$uaccent$
SELECT unaccent('unaccent',$1::text);
$uaccent$ LANGUAGE sql IMMUTABLE;
CREATE OR REPLACE FUNCTION url_slug(text) RETURNS text AS
$url_slug$
SELECT trim(both '-' from
regexp_replace(
unaccent('unaccent', lower($1::text)),
'[^a-z0-9]+', '-', 'g'
)
);
$url_slug$ LANGUAGE sql IMMUTABLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment