#DEFINE CRTAB CHR(13)+CHR(10)+CHR(9)
IF m.issql
    DO open_database
    sql_select = "SELECT datname, usename, application_name, client_addr, client_hostname, state, state_change, query FROM pg_catalog.pg_stat_activity"
    sql_select( m.sql_select, "vejsledek", "database C(55), username C(22), app_name C(33), addr C(17), hostname C(33), state C(15), state_chng T, query C(33)")
    SELECT  vejsledek 
    REPLACE FOR ISNULL(database  ) database   WITH ""

    REPLACE FOR .not. ISNULL(username) database WITH ALLTRIM(database) + "/" + username
    REPLACE FOR .not. ISNULL(app_name) database WITH ALLTRIM(database) + "/" + app_name

    REPLACE FOR LEN(ALLTRIM(database)) < 11 database WITH ALLTRIM(database) 

    REPLACE FOR ISNULL(hostname  ) hostname   WITH ""
    REPLACE FOR ISNULL(addr      ) addr       WITH ""
    REPLACE FOR ISNULL(state     ) state      WITH ""
    REPLACE FOR ISNULL(state_chng) state_chng WITH {}
    REPLACE FOR ISNULL(query     ) query      WITH ""

    INDEX on database + addr + state TAG db_adr
    vysledek = ""
    pocet_radku = 0
    SCAN 
        radek = PADR( database, 44) + " " + ALLTRIM(addr + PADR(state, 13))
        DO CASE 
        CASE LEFT(database, 1) = "/"
            vysledek = m.vysledek + CHRTRAN(database, " " + CHR(9), "") + " * "
        CASE ATC(m.radek, m.vysledek) = 0
            vysledek = m.vysledek + CRTAB + m.radek + " *"
        OTHERWISE 
            vysledek = m.vysledek +  "*"
        ENDCASE 
        pocet_radku = m.pocet_radku + 1
    ENDSCAN 
    USE 
    vysledek = m.vysledek + CRTAB + "Celkem connections: " + nx(m.pocet_radku) + "   " + REPLICATE("*", m.pocet_radku)
    small_log( CRTAB + m.vysledek + CRTAB, "connections")
ENDIF 
