1

In my database, there is a table that provides no result whenever I issue a query through the command line.

For example, if I type:

select * from <table>

Nothing happens. The terminal stops responding until I press ^C to cancel the request.

All the other tables work fine, I am not sure what is causing this error. It just started happening out of nowhere.

5
  • What happens with select * from <table> limit 1; - with limit 1 and with semicolon? Commented Apr 23, 2020 at 2:06
  • The terminal just sits there with no response. Commented Apr 23, 2020 at 2:08
  • Then please add your version of Postgres and psql, the exact name and definition of the table and the result of SELECT pg_size_pretty(pg_relation_size('<table>')); Also, any concurrent activity? There may be locks ... Look at: SELECT * FROM pg_stat_activity; and SELECT * FROM pg_locks; Commented Apr 23, 2020 at 2:11
  • I have posted a solution that I believe solves the problem. Please let me know if I have made any mistake. Commented Apr 23, 2020 at 2:18
  • Just to be sure: you do use a trailing ; to end the statement? Commented Apr 23, 2020 at 6:19

2 Answers 2

1

Okay, I found a way to solve it.

First run:

select * from pg_locks where not granted;

Then, from that table, find the relation id. It should be in the column under relation.

Then run this command:

select * from pg_locks where relation = <relationid>;

After that, determine the pid of the relation that has granted equal to true. There should be a granted column with boolean t and f values. Determine that relation's pid.

Finally, run:

select pg_terminate_backend(<pid>);

Then, you should now be able to access the table.

Sign up to request clarification or add additional context in comments.

2 Comments

Start with select * from pg_locks where relation = '<table>'::regclass;. And also have a look at pg_stat_activity to check what the session with pid = X actually does before you terminate it ... If it's "idle in transaction", someone / something started a transaction and left it in limbo without commit / rollback. May be something to investigate.
A select can only be blocked, by an exclusive lock on the table. The connection holding that lock most probably issued a DDL statement that wasn't committed.
0

There is a simpler way

Basically 3 points to consider when planning to user PostGreSQL from cmd prompt.

  1. Semi-colon
  2. Set Search path
  3. Handle case sensitivity and naming conventions

First answer is to add a semi-colon after the statement.

Second, if you are using postgre from command line , and you have created you tables in public schema, so every time instead of writing

SELECT * FROM public."Users";

we can set the search path Set Search Path Temporarily in That Particular Session:

psql -U your_username -d your_database
SET search_path TO public;
SELECT * FROM Users;

Permanently Setting the Search Path for a Role:

psql -U your_username -d your_database
ALTER ROLE your_username SET search_path TO public;
SELECT * FROM Users;

Lastly, very important case-sensitivity. If you create tables using PgAdmin then they will be created as "Users" so if you will run statement from command prompt as

SELECT * FROM Users;
OR 
SELECT * FROM users;

that wont work, you will have to write

SELECT * FROM "Users";

So its better to create table in lowercase and alter existing tables as

ALTER TABLE "Users" RENAME TO users;

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.