Stopping a blocking query in postgresql

Problem

You run a query in PostgreSQL that takes a lot of time to finish (maybe because of an error) and you want to stop/kill it as it is blocking other queries in db.

Solution

You will need to find the pid for the query, either by using a GUI program like Pgadmin or by issuing the following to an SQL prompt:

select * from pg_stat_activity

make a note of the pid of the query that you want to stop/kill and issue the following:

select pg_cancel_backend(pid)

(issues) lib/issues/github_issues.ex:14: Issues.GithubIssues.handle_response({:error, %HTTPoison.Error{id: nil, reason: :connect_timeout}})

Problem

Trying to follow the example in ‘Programming Elixir 1.2’ for fetching the issues from Github you are getting the following error:

(issues) lib/issues/github_issues.ex:14: Issues.GithubIssues.handle_response({:error, %HTTPoison.Error{id: nil, reason: :connect_timeout}})

Solution

You are quite likely behind a proxy and you get a timeout. Add the proxy details to the HTTPoison.get request as follows:

iex(4)> HTTPoison.get!( "https://api.github.com/repos/elixir-lang/elixir", [], [{:proxy, "your_proxy_ip:your_proxy_port"}])

And you could make the following change to the github_issues.ex file to be able to work with the proxy:

def fetch(user, project, proxy) do
  issues_url(user, project)
  |> HTTPoison.get(@user_agent, proxy)
  |> handle_response
end

So you can call it like the following from iex:

iex(1)> Issues.GithubIssues.fetch("elixir-lang", "elixir", [{:proxy, "your_proxy_ip:your_proxy_port"}])

function for getting the bitmask integer value in PostgreSQL

Problem

You have set up some bitmask values and you would like to return the calculated value given a string with one or more of the codes used, to be able to used it in an SQL query.

Solution

You can use the following to create a function called bitmask. This one deals with any number of individual codes (2 and 3 digit codes in this case).
It also does not calculate duplicate values since they are removed.
So for this example the string ’02a’ is the same as ’02a,02a’ and should give 16 in both cases.

CREATE OR REPLACE FUNCTION bitmask(code text) RETURNS integer AS $$
  DECLARE bm INTEGER := 0;
  DECLARE total INTEGER := 0;
  DECLARE counter INTEGER := 0;
  DECLARE code_to_translate TEXT := '';

  BEGIN
    LOOP 
      EXIT WHEN LENGTH(code) = 0;
      /* only take first 2 or 3 characters */
      code_to_translate := SPLIT_PART(code, ',', 1);
      CASE code_to_translate
        WHEN 'XX'  THEN bm =      0;
        WHEN '01a' THEN bm =      2;
        WHEN '01b' THEN bm =      4;
        WHEN '01c' THEN bm =      8;
        WHEN '02a' THEN bm =     16;
        WHEN '02b' THEN bm =     32;
        WHEN '03'  THEN bm =     64;
        WHEN '04a' THEN bm =    128;
        WHEN '04b' THEN bm =    256;
        WHEN '04c' THEN bm =    512;
        WHEN '04d' THEN bm =   1024;
        WHEN '05'  THEN bm =   2048;
        WHEN '06a' THEN bm =   4096;
        WHEN '06b' THEN bm =   8192;
        WHEN '10a' THEN bm =  16384;
        WHEN '10b' THEN bm =  32768;
        WHEN '10c' THEN bm =  65536;
        WHEN '10d' THEN bm = 131072;
        WHEN '11a' THEN bm = 262144;
        WHEN '11b' THEN bm = 524288;
        ELSE bm = 0;
      END CASE;
      total := total + bm;
      counter := counter + 1;
      /* Remove checked code so we do not have duplicates */
      code := regexp_replace(code, code_to_translate||'[,]*', '', 'g');
    END LOOP ;
   RETURN total;
  END;
$$ LANGUAGE plpgsql;

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: …

Problem

You have a Ruby on Rails application/engine with some tests in Minitest and using postgresql as the db. In the original development environment all the tests that use fixtures and foreign key validations pass, but when you try to move to a different environment with new database some or all of you tests fail with a message that refers to the ForeignKeyViolation as in:

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "tbl_name" violates foreign key constraint "tbl_name_cln_name_fk_tbl_name_fk"
DETAIL:  Key (col_name)=(VALUE) is not present in table "fk_tbl_name".

Solution

Since active record disables the foreign keys before loading the fixtures in the database, the user that you run the tests with needs to have superuser permissions in the test database. Otherwise the fixtures are not loaded. So add the superuser permissions to your user:

psql -U super_user
alter role use_for_tests superuser;

You may need to drop and create your test database.

Request failed (404) ** (Mix) Package fetch failed and no cached copy available

Problem

You are trying to use hex for installing dependencies but when you are behind a proxy you get the following message:

Request failed (404)
** (Mix) Package fetch failed and no cached copy available

Solution

You can use the following to set up hex (taken from the answer here:

mix hex.config http_proxy http://proxy.mycompany.com

Error: Brunch 2+ requires node v4.0+. Upgrade node or use older brunch for old node.js: npm i -g brunch@1

Problem

Trying to start a new phoenix application while your node installation is old you get the following error:

Error: Brunch 2+ requires node v4.0+. Upgrade node or use older brunch for old node.js: npm i -g brunch@1 -g brunch@1

Solution

You will need to upgrade your node installation by following the steps below:

$> node -v
v0.10.32
$> sudo yum install nodejs npm (if npm is not available)
$> npm cache clean -f
$> npm install -g n
$> sudo /path/n/was/installed(ie /home/kosmas/bin/n) stable
$> node -v
v5.10.1

Running a specific Cop in Rubocop

Problem

You are working on an existing rails project with many rubocop warnings and you would like to fix certain types or warnings by getting only the specified files.

Solution

So for example when you use rubocop you first get the following for all the files in the project:

rubocop
799 files inspected, 17214 offenses detected

You can get a grouping of the errors by using the following:

rubocop --format offenses
7176 Style/StringLiterals
3596 Metrics/LineLength
1031 Style/VariableName
790  Style/IndentationConsistency
548  Style/SpaceAroundOperators
423  Style/Documentation
415  Style/HashSyntax
386  Style/TrailingWhiteSpace
198  Style/SpaceAfterComma
...

If you would like to know only the files for a certain type of warning to be able to fix them you could try the following:

rubocop --only Style/TrailingWhitespace
..... list of files
799 files inspected, 386 offenses detected