How to terminate a long running postgres query (in plv8 extension)? - postgresql

I have executed a plv8 function on pgadmin 4, that uses nested for loops and some array functions.
This query was stopped using pgadmin cancel query (stop) button in GUI.
24 hours later, This query still shows up as active and running.
I have Tried, pg_cancel_backend and pg_terminate_backend, but seems to have no impact.
Tried pg_ctl kill (TERM/ABRT/INT)command, but pid is not found.
However, AWS performance insights dashboard shows up, the query using 25-30% of CPU.
If you guys could suggest alternate ways, to terminate this query, That'd be really helpful.
Thanks.

Related

Postgres query idle after running for a few hours

I'm doing a huge upgrade on a Database. I have quite a complex query that needs a few hours to run - I tested it on some sample data and the query is fine.
After analyzing my queries, I saw that my query changed from state 'ACTIVE' to 'IDLE' after running for 3:30h.
What does that exactly mean? The PostgreSQL manual indicates that this means the transaction is open (inside BEGIN) and idle. I
Will my query end? Should I kill it and find a smarter way to upgrade?

How to cancel PostgreSQL query?

I am not very familiar with databases or SQL and wanted to make sure that I don't mess anything up. I did:
SELECT pid, state, usename, query FROM pg_stat_activity;
To check if I had any queries and there were several that had the state active. Do I just cancel them by doing:
select pg_cancel_backend(PID);
And this won't affect anything except the my queries, correct? I also wanted to figure out why those queries were still in the state active. I have a python file where I read in my sql file, but I stopped running the python file in the middle of reading my sql file. Is that possibly why it happened and why the states are still active?
Yes, this is what pg_cancel_backend(pid) is for. Why exactly the query is still running depends on a few things - could be waiting to grab a lock, or the query could just take a long time - but given the python processes that started the queries have exited, the connection is technically already closed, the PG backend process just hasn't noticed yet. It won't notice until the query completes and it tries to return the query status to the client, at which point it'll rollback the transaction when it sees the connection is no longer present.
The only effect pg_cancel_backend on the PIDs of those backends should have is to cause PG to notice the connection is closed immediately, rather than whenever the query completes.

Concurrent indexing Postgres statement exit

When creating an index concurrently in Postgres, how do you make the statement run in the background? Once I run the query in psql, the statement does not return and I'm not able to quit the process and disconnect SSH to server.
Edit: I understand we could use something like tmux to keep the shell alive in the background. But I'm trying to understand if Postgres' CONCURRENT index operation does not return immediately.
Yes, although the index is created concurrently, the DDL itself will not return immediately.
Ref: https://gist.github.com/bryanrite/36714b13e0aece2f6c43#safe
Add an index concurrently (Example), Note: it will still take a long time to run the migration, but it won't write-lock the table.
Use screen in linux,
screen -S session_name
then execute the command in psql.
You can detach from the screen session at any time by typing:
Ctrl+a d
To resume your session use
screen -r
Read this for more info
This way you can let your process run in the background even though you disconnect your session.

Is there any way to trace and debug a query in Postgresql?

Is there any way/tool to trace and debug a query in PostgreSQL 9.3.18?
I'm a SQL programmer and sometimes I need to trace and debug my queries and see the values of different fields at execution time. I've Googled this but didn't get any relevant result.
Any idea would be appreciated
PG Admin (database interaction GUI) that is sometimes bundled with PostgreSQL includes a step through debugger for query/calls to Postgres database functions (as opposed to every query that goes to the server).
https://www.pgadmin.org/docs/pgadmin4/4.29/debugger.html
Before using it you have to enable it as a plugin/library in PG Admin.
The debugger will step to statements so sometimes a complex single statement will execute without letting you step through it's details. Still, if you need to see a basic step through of a longer multi statement function or variable values at some points it can be useful. Note, this debug applies to database functions and not general queries.

Postgres table queries not responding

I have been trying to truncate a table using SQlWorkbench. Suddenly, SqlWorkbench got freezed, while the truncate was in progress. I had to kill workbench from taskmanager. But now none of the queries are working on the table on which the truncate was aborted abruptly. For other tables queries are working fine. Need help, as I have to upload fresh data on the same table. Currently I am not even able to drop the table. What can be done to resolve this issue?
This looks like the TRUNCATE got stuck behind a lock, and then you killed the front end, while TRUNCATE kept running.
Connect to the database as superuser and examine the pg_stat_activity view; you should see some long running transactions.
Use the function pg_terminate_backend to kill these sessions by their pid.