Different types of Index in PostgreSQL

Bookmark and Share

This is part two of our PostgreSQL optimization series. You can read the first article where we discuss when to index here.

PostgreSQL uses a different set of algorithm while indexing tables, each type of algorithm is good for a certain set of data. Here we will be discussing the various algorithms available and when we should be using them. (Note these are the algorithms found in PostgreSQL 9.5)


B-Tree (Balance Tree), is the default algorithm used when we build indexes in Rails. It keeps a sorted copy of our column, which would be our index. So if we want to find the row of the word starting with a then as soon as the words starting with a are over. It will stop searching and return null, as the index has kept everything sorted. It is good in most cases, hence it is the default algorithm used.

Hash is one of the most popular indexing algorithms. But only the equate operator works on it, thus the query planner will only use an index with a hash algorithm if we do an equal operation searching for it. Another point to note is that Hash index …

Read More

How to write maintainable routes in rails

Bookmark and Share

config/routes.rb is the gateway to your ruby on rails application. All request send by your users are directed to the appropriate code by the routes.


When someone visits your-website.com/profiles then the request is taken to the Index action of the UsersController. Under that action you will get the index.html.erb. So using routes we have configured the UsersController to respond to the users request it is its responsibility to do it now.

We can declare routes in various ways:

Since there are multiple ways to declare routes (as all forms are right), its best to stick with a single method for the code to be more readable. routes.rb is going to be one of the most heavily edited file in your project as when ever you add a new page or create a new form, you need to add a route to access the page or an end-point to accept the request. So it is most likely that your routes.rb file will start to grow ugly:

So here we will share some tips to write proper, maintainable routes:

First important point to note is that, its best to write routes as resources


declaring resources will create the following 7 routes.

Read More

Mina: Faster deployment and remote server automation

Bookmark and Share

Even though we use CodeShip/Circle CI/Jenkins for continuous integration, we still need to write scripts to automate our deployment. We also need to write small commands to clear cache, restart queues, etc. It’s always a good practice to not have to enter the server directly but to have it done through scripts. There are many tools available for this purpose (in multiple languages) like Capistrano, Vlad, etc.

Mina is a similar tool, but faster. The reason why it runs faster is because it generates a bash script, uploads it to the server and run there, rather than creating a new ssh session and run every command one by one. Mina is one of our default tools at Red Panthers.

To use mina in your project, add mina to your Gemfile.

and to get started do




Having a one step deployment is an important requirement for any project so have one ready using mina or capistrano.



Assets pre-compile


Run another rake task

Run shell commands

The syntax queue is referring to the fact that, the commands are all made into a single shell script and then later executed together.


Read More

application_record.rb available since rails 5

Bookmark and Share

Those who have been starting with Rails 5, must have noticed the new application_record.rb file present in your model folder. And all new models seems to be inheriting the application_record.rb instead of the ActiveRecord::Base. This is done so that we don’t pollute the ActiveRecord::Base namespace with our own extensions.  Before when we require something, say an extension to the ActiveRecord itself we used to have it included using the following code.

Now the problem with this approach is that when we use rails engines this NewFeature gets added in there as well, and it could end up doing things that we didn’t expect.

With the new application_record.rb, which would be inherited by all the models, we need to include the new module at the ApplicationRecord and it would be available as the new feature of ActiveRecord. Every new engine generated using rails plugin new would also be having their own application_reocord.rb

One more point to note is that we can place application wide hooks in this file. So if you were to do

it would be triggered when a new record is created in any of the models of the rails application.

Read More