Pattern similarity in time-series
Ok so this is what I would recommend:


Use a custom aggregate for a moving average.  the aggregate would use essentially an array to store values and return the average of the values in the array.   New values would be appended to the end, and old values dropped off.  One could use array_agg with unnest(), limit, and offset to do the work.
Use a windowing function to tie the moving average to the row.
Do a calculation between values in the above to generate your pattern....
Create another aggregate which gives you a moving pattern returning an array
Write an operator matching the array and use a windowing function call to tack this on to the returned record


I do think that the above can all be done in SQL with SQL-language functions.  It is, however, way too complicated to even get into sample code here.  However I can't help but wonder whether PL/R might not be helpful here as well.