Move table to different schema in postgres - postgresql

I would like to change schema of few tables in my Postgres DB. Problem is that all the time there are long running queries and as I understand schema change needs exclusive lock.
Question is how can I do it? Of course I can kill all existing queries and try to do schema rename (move table to different schema) but there is a huge chance that in the meantime new queries will appear.
Thanks for help!

run SELECT pg_backend_pid() before running the ALTER TABLE
start the ALTER TABLE statement
in a second database session, run SELECT pg_blocking_pids(12345), where 12345 is the result from the first query
cancel all the blocking transactions found with the previous query with SELECT pg_cancel_backend(23456)

Related

Redshift queries do not finish after dropping and recreating tables

I have a few tables in a redshift cluster. When I drop say table A and recreate the table with the records in it using CREATE and the COPY command, any query on table A never finishes execution (even a simple select * from A;). I need to reboot the cluster and execute the query again for it to finish, be it any sort of query. What's the solution here ?

PostgreSQL select query with where clause 'COLUMN_1 is null' hangs

Recently we restored PostgreSQL database from the backup which was created without stopping database (I know this was very wrong and now we are paying the price). The backup was simple database directory backup.
Now we noticed that when we execute
select *
from table
where COLUMN_1 is null
query in one of our tables the query just hangs (freezes) and never finishes. Other queries on the same table run fine and distinct(COLUMN_1) returns all the values. The same query runs correctly on the other column COLUMN_2 is null. It seems there is something wrong with that one column.
How can I repair such possibly damaged table?
Dump the whole database with pg_dump and restore it to a new cluster. If that works, it will get rid of all data corruption.
If that fails, you should hire a specialist.
If you attach to the hanging backend with a debugger, you can investigate what it is doing (if you are familiar with the source).
I ran vacuum full and it solved my problem. The query now executes with is null clause on that table.

Query to find a critical situation in Greenplum database(4.3.12) which has postgres version 8.2.15

Need a SELECT query that can find the users,procpids along with query statements runs by them with the waiting_state when a particular table is accessed in their respective queries which as a result leads to the concept of blocking table and waiting table.
Thanks.

Greenplum: Not able to drop/truncate tables

I have a table in Greenplum(4.3.5.1), i want to remove constraint as initially created with a primary key constraint. I tried to do but query is running for 2-3 hours, i have cancelled it as no other option left,
then i have takne a backup and tried to drop table but query is running 2-3 hours and finally i cancelled the query again
(When drop table query executed, it is showing RowExclusiveLock on table pg_depend, pg_class and pg_type)
I just tried truncate also but same problem
Can anyone help on this, what could be the reason?? and what will be the best way to resolve this??
Regards
Most likely you hit a locking issue. First thing to check is pg_locks - it would show you the current locks on the table. I bet your table is locked by some process, this is why truncate and drop table is hanging. Find the blocking query and terminate it, then you would be able to easily drop/truncate target table.
Here is the query that would help you:
select * from pg_locks where relation = 'mytablename'::regclass::oid;
You should use truncate:
TRUNCATE TABLE table_name;
http://www.postgresql.org/docs/9.1/static/sql-truncate.html

Postgres and temporary tables

I have a stored procedure that use temp table and explicitly drops it when done.
What happed when same procedure is run at same time by 2 different sessions? Sessions are run as single user (webapp).
Will one session interfere with temp table, and data inside it, of other session?
I'm using Postgres 9.0.
In postgresql temporary tables are unique for each session, so no problem.
If I am not totally wrong, the result would be one temp table per query in your procedure which generates the temp table, so the two temp tables would not disturb eachother.