Saturday, November 7, 2009

How to find locks in postgres ?

Table pg_locks is very usefull...


select
 pg_stat_activity.datname,
 pg_class.relname,
 pg_locks.transaction,
 pg_locks.mode,
 pg_locks.granted,
 pg_stat_activity.usename,
 substr(pg_stat_activity.current_query,1,30),
 pg_stat_activity.query_start,
 age(now(),pg_stat_activity.query_start) as "age",
 pg_stat_activity.procpid
from 
 pg_stat_activity,
 pg_locks left outer join pg_class on (pg_locks.relation = pg_class.oid)  
 where
 pg_locks.pid=pg_stat_activity.procpid order by query_start;

Friday, November 6, 2009

Update massive number of records

Simple Pl/SQL procedure which help to do massive update.


create or replace PROCEDURE move_column AS
  type num_table IS TABLE OF NUMBER INDEX BY pls_integer;
  round_trips number;
  recid_table num_table;
  avid_table num_table;
  scale NUMBER;
  starting_point NUMBER;
  ending_point NUMBER;
BEGIN
  SELECT recid,
    attrib_value_id bulk collect
  INTO recid_table,
    avid_table
  FROM very_big_table;
  scale := SQL % rowcount;

  round_trips := TRUNC((scale + 9999) / 10000);


  FOR bite IN 1 .. round_trips
  LOOP
    starting_point :=(bite -1) *10000 + 1;
    ending_point := least(scale,   bite *10000);
    forall this IN starting_point .. ending_point

      UPDATE another_very_big_table SET rec_id = recid_table(this)
       WHERE id = avid_table(this);
    COMMIT;
    --DBMS_OUTPUT.PUT_LINE('Move: ' || ending_point);
  END LOOP;

END move_column;

Wednesday, November 4, 2009

read write file from gridfs

This simple program reads file from gridfs and write its content to local file system.

//gridfs_to_file.cpp
#include <iostream>
#include <vector>
#include <fstream>

#include <boost/algorithm/string.hpp>

#include <mongo/client/dbclient.h>
#include <mongo/client/gridfs.h>

// g++ gridfs_to_file.cpp -lmongoclient -lboost_thread -lboost_filesystem -o gridfs_to_file

using namespace std;
using namespace mongo;

int main(int argc, const char **argv) {
   const char *gridFileName = "";
   std::fstream out;


   if (argc != 3) {
      cerr << "Usage " << argv[0] << " gridfs_file local_file"  << endl;
      return -12;
   }

   out.open(argv[2], ios::out);

   if ( !out.is_open()) {
      cerr << "Can't open " << argv[2] << endl;
      return -2;
   }
   gridFileName = argv[1];

   DBClientConnection c;
   c.connect("localhost"); 
   cout << "connected ok" <<endl;
   GridFS gfs = GridFS(c, "test", "testcpp");
   GridFile gf = gfs.findFile(gridFileName);

   if (true != gf.exists()) {
      cerr << "There is no file like " << argv[1] << endl;
      return -2;
   }
   gf.write(out);   
   out.close();

   return 0;
}

This program reads file from local file system and send it to gridfs.
#include <mongo/client/dbclient.h>
#include <mongo/client/gridfs.h>

// g++ local_to_gridfs.cpp -lmongoclient -lboost_thread -lboost_filesystem -o local_to_gridfs

using namespace std;
using namespace mongo;

int main(int argc, const char **argv) { 
    char *data;
    int len;

    if (argc != 3) {
        cerr << "Usage " << argv[0] << " localFile remoteName "  << endl; 
        return -12;
    }

    fstream in(argv[1], ios::in|ios::binary|ios::ate);
    
    DBClientConnection c;
    c.connect("localhost"); 
    cout << "connected ok" <<endl;

    GridFS gfs = GridFS(c, "test", "testcpp");
    if ( in.is_open() ) {
        len = in.tellg();
        in.seekg( 0, ios::beg);
        data = new char[len];
        in.read(data, len);
    }
    //storing file

    gfs.storeFile(data, len, argv[2], "text/plain");
    delete [] data;
    cout << "file stored to gridfs" << endl;

    return 0;
}

Tuesday, November 3, 2009

Great news for MongoDB users

When I saw Mongodb, it was most promising non-relational database. There's no time to lose. 



"10gen, a New York-based developer of an open-source, non-relational database called MongoDB, has raised $3.4 million in Series B funding. Flybridge Capital Partners led the round, and was joined by return backer Union Square Ventures."

http://www.pehub.com/54541/mongodb-developer-10gen-raises-34-million/