Postgresql – LIKE search in bytea datatype (PostgreSQL)

byteapostgresql

Table:

\d blob

 Column  |       Type   | Collation |                 Default                  
---------+--------------+-----------+------------------------------------------
 id      | integer      |           | nextval('modlink_blob_id_seq'::regclass)
 content | bytea        |           | 

This statement:

SELECT COUNT(*) "job" INNER JOIN "blob" ON ("job"."input_id" = "blob"."id")
  WHERE UPPER("blob"."content"::text) LIKE UPPER('%csv%');

Error message:

ERROR:  invalid memory alloc request size 1989028364

What can I do to make this statement not fail?

Best Answer

The best way i found to make 'LIKE' search in PostgreSQL in columns with a lot of content was using tsvector columns.

One advantages is more fast than make 'like' searches and allow create index.

The boring part is create a trigger to replicate the content in a new column.

The article below shows how to use tsvector.

http://rachbelaid.com/postgres-full-text-search-is-good-enough/

Column  |       Type   | Collation |                 Default                  
---------+--------------+-----------+------------------------------------------
 id      | integer      |           | nextval('modlink_blob_id_seq'::regclass)
 content | bytea        |           |
 search  | tsvector     |           | 

This is the SQL

SELECT COUNT(*) FROM "job" INNER JOIN "blob" ON ("job"."input_id" = "blob"."id") 
WHERE blob.search @@ plainto_tsquery('csv');

A observation: I use text columns instead bytea.