Monday, November 26, 2007

Viewing current PostgreSQL Queries

I am a heavy MySQL user, so I constantly use a SHOW PROCESSLIST; to view how my developers or software are using the test database server. So now that we have more than one project using postgresql and since locking down the cpu is becoming more and more common I had to figure out a way to view the queries that are being executed on PostgreSQL.

I found this:

SELECT * FROM pg_stat_activity;


but since PostgreSQL turns off the storing of query strings all I could see was who where using the databse. So in the postgresql.conf file (in FreeBSD that's in /usr/local/pgsql/data/postgresql.conf) i just uncommented and edited the line with this option:

stats_command_string = true


Then just restart the postgresql and it is ready!

Tuesday, October 30, 2007

Converting numerical bases in python

Working with hexadecimal, binaries, and other bases is becoming common at work, at first we try to use custom functions to convert from and to some bases, especially hexadecimal. But other languages have built-in methods to do that conversion, and so python too.

First to convert and hexadecimal string to an integer we use:

int_number = int('ab1', 16)

The variable int_number will be 2737. That is ab1 hex to integer conversion. Now what about the other way, integer to hexadecimal, that would be:

hex(int_number)

And that would return '0xab1'. Note that it returns a string and has the 0x notation, we could use that notation also in the first conversion like this:

int_number = int('0xab1', 16)

So as you can see we pass a parameter to the int function to indicate the base, if we dont pass any python assumes that we are using a 10 base. As you have also seen there is a hex built-in function but there is no binary, or any other base built-in, so since we work a lot with binary we built a small function to do the work. There are hundreds of ways to do this, using recursion, whiles, fors, lamdas, etc. We end up using this function:

def dectobin(number):
if number < 1:
return ""
else:
return dectobin(number / 2) + str(number & 1)


Hope this works for you.

Wednesday, October 24, 2007

FreeBSD 7.0 Beta1 Available!

Since I first tried FreeBSD on the server side I have a preference for it over Linux. I think it has superior performance, not that I have done any real benchmark but I have, sort of, felt it. Anyway some friends have done some tests in the past, but tests or not I still think it is a rock solid OS. My experience comes from FreeBSD 4.11 and 5.3, thos two versions were probably the best, now that they are both out of maintenance i use a mix between 5.5 and 6.0. But for some reason those two versions have not been as solid as the 5.3 or 4.11 were. In most cases it was a hardware compatibility problem.

Now FreeBSD 7.0 is nearing its release, probably in December we will have a brand new FreeBSD. I expect to see better hardware support and much more stability in general. So in order to accomplish that I hope to test the new beta release and any other change in the future. Just hope to have some time at work.