Unique

A Unique node takes a sorted set of rows as an input and outputs one with all the duplicates removed. It can appear when using DISTINCT and when UNION is eliminating duplicates in its output. The output will be sorted the same as the input.

As of PostgreSQL 8.4, DISTINCT will usually be implementing using a HashAggregate node instead of a Unique one, which on smaller tables can collect the unique values without having to explicitly sort them first:

EXPLAIN ANALYZE SELECT DISTINCT(state) FROM customers;
QUERY PLAN
----------
HashAggregate  (cost=726.00..726.51 rows=51 width=3) (actual time=91.950..92.048 rows=52 loops=1)
     ->  Seq Scan on customers  (cost=0.00..676.00 rows=20000 width=3) (actual time=0.009..49.466 rows=20000 loops=1)

Get PostgreSQL 10 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.