October 15th, 2008 by depesz | Tags: , , , , | 5 comments »
Did it help? If yes - maybe you can help me?

SoftNum asked on irc:

< SoftNum> does postgresql have a config option to automatically trim (both ' ' from blah) on string compares?

So, can you?

Of course there is no such option, but maybe there is a way to tell PostgreSQL to do this trim for given field? Sure there is 🙂

To do it, we will need another datatype. Don't worry – it is not complicated, and requires only some copy/paste abilities.

So, first let's create simple domain:

CREATE DOMAIN trimmed_text as TEXT;

Doesn't look scary, does it?

Now. We will need to add some operators. Basically the only ones that are important for me now are “=" and “<>“.

Since usually people use text datatype (or something else that can be casted to text), I will need to create 6 separate operators:

  1. text = trimmed_text
  2. text <> trimmed_text
  3. trimmed_text = text
  4. trimmed_text <> text
  5. trimmed_text = trimmed_text
  6. trimmed_text <> trimmed_text

Luckily, it is very simple and requires only SQL language. First operator:

CREATE FUNCTION trimmed_text_req(TEXT, trimmed_text) RETURNS bool as $$
    SELECT btrim($1) = btrim($2);
$$ language sql immutable;
 
CREATE OPERATOR = (
    leftarg = text,
    rightarg = trimmed_text,
    negator = <>,
    procedure = trimmed_text_req
);

I hope the code is self-explanatory. If not, please feel free to ask questions in comments. Or just bug me on freenode #postgresql.

Using this as a template, I quickly wrote the rest of operators:

CREATE FUNCTION trimmed_text_rne(TEXT, trimmed_text) RETURNS bool as $$
    SELECT btrim($1) <> btrim($2);
$$ language sql immutable;
 
CREATE OPERATOR <> (
    leftarg = text,
    rightarg = trimmed_text,
    negator = =,
    procedure = trimmed_text_rne
);
CREATE FUNCTION trimmed_text_leq(trimmed_text, TEXT) RETURNS bool as $$
    SELECT btrim($1) = btrim($2);
$$ language sql immutable;
 
CREATE OPERATOR = (
    leftarg = trimmed_text,
    rightarg = text,
    negator = <>,
    procedure = trimmed_text_leq
);
CREATE FUNCTION trimmed_text_lne(trimmed_text, TEXT) RETURNS bool as $$
    SELECT btrim($1) <> btrim($2);
$$ language sql immutable;
 
CREATE OPERATOR <> (
    leftarg = trimmed_text,
    rightarg = text,
    negator = =,
    procedure = trimmed_text_lne
);
CREATE FUNCTION trimmed_text_beq(trimmed_text, trimmed_text) RETURNS bool as $$
    SELECT btrim($1) = btrim($2);
$$ language sql immutable;
 
CREATE OPERATOR = (
    leftarg = trimmed_text,
    rightarg = trimmed_text,
    negator = <>,
    procedure = trimmed_text_beq
);
CREATE FUNCTION trimmed_text_bne(trimmed_text, trimmed_text) RETURNS bool as $$
    SELECT btrim($1) <> btrim($2);
$$ language sql immutable;
 
CREATE OPERATOR <> (
    leftarg = trimmed_text,
    rightarg = trimmed_text,
    negator = =,
    procedure = trimmed_text_bne
);

All done.

Now, let's test if it really works. To do it, I will need test table:

create table test (
    id serial PRIMARY KEY,
    val trimmed_text
);

With some test data:

INSERT INTO test (val) VALUES
    ('depesz'), (' depesz'), ('depesz '), (' depesz '), ('NOT depesz');

This is how it looks:

# select id, '[' || val || ']' from test;
 id |   ?column?
----+--------------
  1 | [depesz]
  2 | [ depesz]
  3 | [depesz ]
  4 | [ depesz ]
  5 | [NOT depesz]
(5 rows)

I added [ and ] to show the spaces.

So, let's check if simple select will work:

# select id, '[' || val || ']' from test where val = 'depesz';
 id |  ?column?
----+------------
  1 | [depesz]
  2 | [ depesz]
  3 | [depesz ]
  4 | [ depesz ]
(4 rows)

YES! Works.

But … what about indexing. Will index on the field work? Let's test.

First, I'll need more data. 10,000 new records should be enough:

INSERT INTO test (val) SELECT i::TEXT FROM generate_series(1,100000) i;

Now, let's create index, and analyze the table:

# create index q on test (val);
CREATE INDEX
 
# vacuum analyze test;
VACUUM

OK. So, let's check if the index will be used:

# explain analyze select * from test where val = 'depesz';
                                             QUERY PLAN
-----------------------------------------------------------------------------------------------------
 Seq Scan on test  (cost=0.00..2240.09 rows=500 width=9) (actual time=0.032..189.363 rows=4 loops=1)
   Filter: (btrim((val)::text) = btrim(('depesz'::text)::text))
 Total runtime: 189.437 ms
(3 rows)

Unfortunatelly it doesn't use the index. But, as you can see, PostgreSQL is smart enough to see what we do to modify the field (i.e. call to btrim()).

Knowing this, perhaps another index can help us …

# drop index q;
DROP INDEX
 
# CREATE INDEX q on test (btrim(val));
CREATE INDEX
 
# vacuum analyze test;
VACUUM

And, how about index usage now?

# explain analyze select * from test where val = 'depesz';
                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Index Scan using q on test  (cost=0.00..8.28 rows=1 width=9) (actual time=0.114..0.124 rows=4 loops=1)
   Index Cond: (btrim((val)::text) = btrim(('depesz'::text)::text))
 Total runtime: 0.223 ms
(3 rows)

Great. To sum it all up:

  • query doesn't have to be modified
  • datatype conversion is mostly painless
  • indexing works

Did I mention that I love PostgreSQL?

Of course the same method can be applied to create case insensitive text fields, or even field which do some more advanced things – like format normalization.

  1. 5 comments

  2. # siooh
    Oct 17, 2008

    Great post. I’ve never thought about OPERATORs in this way that they can solve such ‘problem’. PG rules ;). I love it.

  3. Oct 17, 2008

    @siooh:
    have you seen this? https://www.depesz.com/2007/11/05/encrypted-passwords-in-database/

  4. # Vincenzo Romano
    Oct 18, 2008

    It’s another great post, Depesz!
    As usual!
    How does your last remark compare to the new CITEXT feature?

  5. Oct 18, 2008

    @Vincenzo Romano:
    citext will be faster, but creating your own datatype with domains and sql functions don’t require compilation of external modules.
    also – you can create your own datatype in 8.3 and previous versions, while citext will be available as contrib from 8.4

  6. # Eugene
    Oct 21, 2008

    I am seaching for some idea to write in my blog… somehow come to your blog. best of luck. Eugene

Sorry, comments for this post are disabled.