0

When trying to run a simple create statement in a new schema within postgres (healthcare), the query never finishes. My question is why?

CREATE TABLE healthcare.admission_source(
    admission_source_id INTEGER, 
    admission_source INTEGER,
    description varchar(255)
);

Verified the schema exists with ownership & rights:

enter image description here

I have ran this same query before, and it runs endlessly. I canceled it at several hours last time, but wanted to show it takes a long time to run a CREATE TABLE statement:

enter image description here


For comparison, the query runs for 42 msec on the public schema of this database. The table is created correctly and the query finishes quickly, so leaves me thinking that the healthcare schema is the issue. Would love all thoughts and assistance enter image description here

6
  • which version of postgres? Commented Mar 4, 2024 at 21:33
  • @nbk PostgreSQL 13.8, compiled by Visual C++ build 1914, 64-bit Commented Mar 4, 2024 at 21:44
  • Which client application are you using? Does it have autocommit=off? Could be the CREATE has never been committed. Commented Mar 4, 2024 at 21:45
  • ran \echo :AUTOCOMMIT; and it returned on; Commented Mar 4, 2024 at 21:47
  • 1
    1) Make your life easier and use the Postgres command line client psql to do this. 2) Do a select on pg_stat_activity. I'm guessing there is a query on healthcare.admission_source that is holding a lock and preventing the CREATE TABLE. Commented Mar 4, 2024 at 22:34

1 Answer 1

1

It is probably blocking on a lock. If you have log_lock_waits turned on, you can look in the log file to see what is going on.

This could happen for example of another session tried to create a table of the same name inside a transaction and didn't commit the transaction.

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

2 Comments

This was exactly the issue - had a lock state on the queries and some that were idle. I was trying to run a create table from jupytr notebook while also in PG Admin. SOLUTION: I ran select * from pg_stat_activity where cardinality(pg_blocking_pids(pid)) > 0; which showed me the blocked queries. Then ran select pg_cancel_backend(<PID>); & select pg_terminate_backend(<PID>); to terminate the locks. Have then successfully created the tables in the new schema. Thanks @jjanes and @Adrian Klaver
Glad it was solved. What it shows is the importance of holding connections open for only the period necessary to do the work and then closing them.

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.