Skip to content

Instantly share code, notes, and snippets.

@Jeetah
Created March 4, 2024 11:00
Show Gist options
  • Save Jeetah/c1e03e1c1c80e4cb47d713bddd095907 to your computer and use it in GitHub Desktop.
Save Jeetah/c1e03e1c1c80e4cb47d713bddd095907 to your computer and use it in GitHub Desktop.

Revisions

  1. Jeetah created this gist Mar 4, 2024.
    8 changes: 8 additions & 0 deletions pg_array_distinct.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    CREATE FUNCTION array_distinct(
    anyarray, -- input array
    boolean DEFAULT false -- flag to ignore nulls
    ) RETURNS anyarray AS $f$
    SELECT array_agg(DISTINCT x)
    FROM unnest($1) t(x)
    WHERE CASE WHEN $2 THEN x IS NOT NULL ELSE true END;
    $f$ LANGUAGE SQL IMMUTABLE;