Question: I have created a blogging system with php+postgresql.
Now I want to add a web chat ( in REAL TIME for Million of users simultaneously ) where every message is saved in database.

I am thinking to use Erlang+Mnesia on a different webserver for this issue.

Message's table will be like this:

message\_id, user\_id, message, date

user\_id should be related with users table in Postgresql database in another webserver.

How can I do that without lose performance ?

If you have any other creative solutions tell me please ;).

Answer: 
I'm not sure why you want to save every single message in a database, but mnesia doesn't sound like a particularly good choice for doing that. Mnesia is more of a distributed key-value store, that you can use to keep the state of your application, when you need to store "tabular data" and query it, in a simple to medium-complex fashion.

For large amounts of text, I've heard lucene is supposed to be good, it has fulltext search features etc. which are said to be efficient, you might want to look into it:

[Apache Lucene Project page](http://lucene.apache.org/java/docs/index.html)

Other than that, using erlang as chatserver, using mnesia to hold all the other state sounds like a good idea, You could write a javascript client that uses something like JSONP (to overcome the cross-domaine-issue) and mochiweb on the erlang site to do the webserver part.

Writing the rest of the core chat system should be fairly simple, the fun part, so to say :)
This project is using postgresql with great success. 

<http://zotonic.com/>

You may want to use the same code for db access to postgresql.