Re: select statement fails

Поиск
Список
Период
Сортировка
От Erik Jones
Тема Re: select statement fails
Дата
Msg-id D3D79D52-64EF-4E49-BE6E-E97001C23F07@myemma.com
обсуждение исходный текст
Ответ на Re: select statement fails  ("Andrus" <kobruleht2@hot.ee>)
Список pgsql-general
On Apr 10, 2008, at 3:13 AM, Andrus wrote:
> Albe,
>
>>> select * from test where tc=' '::text;
>> Because the arguments to the operator "=" are of different type,
>> implicit type conversion takes place.
>> "character(1)" will by converted to "text", during this conversion
>> trailing blanks will be ignored, as befits the "character(n)" type.
>
> Thank you.
>
> 1. SQL assumes that CHARACTER(n) column is always padded with spaces
> in
> right.

That is only for storage and display.

> So casting to text should preserve spaces.
> Why PostgreSQL cast to text violates SQL ?

It doesn't and it is right there in the manual:

"Values of type character are physically padded with spaces to the
specified width n, and are stored and displayed that way. However, the
padding spaces are treated as semantically insignificant. Trailing
spaces are disregarded when comparing two values of type character,
and they will be removed when converting a character value to one of
the other string types. Note that trailing spaces are semantically
significant in character varying and text values."

http://www.postgresql.org/docs/current/interactive/datatype-character.html

> 2.
>
> create table test ( tc char(1) );
> create index tc on test(tc);
> select * from test where tc='x'::text;
>
> I'm afraid that if test table has large number of rows, PostgreSQL
> is not
> capable to use index for this query doe to the cast to text.
> Is it so ?

You have two options:

1. Just us text for the column's data type.
2. Create an index on the column cast as text:

CREATE INDEX test_tc_txt_idx ON test (tc::text);

Erik Jones

DBA | Emma®
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com




В списке pgsql-general по дате отправления:

Предыдущее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: select statement fails
Следующее
От: Tom Lane
Дата:
Сообщение: Re: select statement fails