views:

74

answers:

2

I am migrating to Postgresql, but I am facing a problem.

A "trick" I am using with SQL Server is to login always using a single user (tipically sa) and I write the program_name in the database connection to check the number of currently logged users in the application. Everytime I do a db connection for UserX I set the program_name in the connection as "MyApp_UserX". In this way with a query like the following I can count how many users are connected to my app. I use this for license check, and it is very reliable in sql server.

select count(sp.loginame) as CNT 
from Master.dbo.sysprocesses sp
join Master.dbo.sysdatabases sd on sp.dbid = sd.dbid
where sd.name = MYDATABASE and sp.program_name like 'MyApp%'

Now Postgresql doesn't allow me in the connection to specify a string like program_name. What can you suggest?

For Delphi users: Note I am using unidac, migrating from SDAC. in SDAC I had TMSConnection.ApplicationName, but there is no a Postgresql equivalent.

+3  A: 

You could either wait for 9.0 or make a few assumptions and count (almost all) connections to the database in question.

Milen A. Radev
Thanks for the valuable information, really 9.0 contains the exact same feature and it would be great to have it.
+1  A: 

If you login from different client computers, you could use the IP address reported in the system view pg_stat_activity. For each connection that is made to the server, the IP address of the client is shown. If different users use different computers, this might work for you (until 9.0 is out), until then it might be a workaround.

http://www.postgresql.org/docs/current/static/monitoring-stats.html

a_horse_with_no_name
thanks for the info. It is a god suggestion, but in my case I cannot apply because not IP address it is not enough for at least 2 reasons: one can be using pgadmin and "using a connection", or 2 users can be using the same pc (in terminal server).