ActiveSqlTransaction: CREATE INDEX CONCURRENTLY cannot run inside a transaction block #371

Closed
opened 2026-02-20 17:39:11 -05:00 by yindo · 3 comments
Owner

Originally created by @santoshgsk on GitHub (Dec 27, 2024).

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

from psycopg import Connection
from langgraph.store.postgres import PostgresStore

conn_str = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWD}@{IP_ADDRESS}:{PORT}/{MEMSTORE_DB}"

conn = Connection.connect(conn_str)

pg_store = PostgresStore(conn=conn)

pg_store.setup()

# Store and retrieve data
pg_store.put(("users", "123"), "prefs", {"theme": "dark"})
item = pg_store.get(("users", "123"), "prefs")

Error Message and Stack Trace (if applicable)

ActiveSqlTransaction                      Traceback (most recent call last)
Cell In[1], line 17
     13 from langgraph.store.postgres import PostgresStore
     15 pg_store = PostgresStore(conn=conn)
---> 17 pg_store.setup()
     19 # Store and retrieve data
     20 pg_store.put(("users", "123"), "prefs", {"theme": "dark"})

File ~/Downloads/Code/venv/lib/python3.11/site-packages/langgraph/store/postgres/base.py:867, in PostgresStore.setup(self)
    865 version = _get_version(cur, table="store_migrations")
    866 for v, sql in enumerate(self.MIGRATIONS[version + 1 :], start=version + 1):
--> 867     cur.execute(sql)
    868     cur.execute("INSERT INTO store_migrations (v) VALUES (%s)", (v,))
    870 if self.index_config:

File ~/Downloads/Code/venv/lib/python3.11/site-packages/psycopg/cursor.py:97, in Cursor.execute(self, query, params, prepare, binary)
     93         self._conn.wait(
     94             self._execute_gen(query, params, prepare=prepare, binary=binary)
     95         )
     96 except e._NO_TRACEBACK as ex:
---> 97     raise ex.with_traceback(None)
     98 return self

ActiveSqlTransaction: CREATE INDEX CONCURRENTLY cannot run inside a transaction block

Description

I am trying to connect to postgresql as a MemoryStore using langgraph store postgres

The connection is established using psycopg - PostgresStore is not taking in the connection string

However, when I am trying to run the .setup() it is showing the error

ActiveSqlTransaction Traceback (most recent call last)
Cell In[1], line 17
13 from langgraph.store.postgres import PostgresStore
15 pg_store = PostgresStore(conn=conn)
---> 17 pg_store.setup()
19 # Store and retrieve data
20 pg_store.put(("users", "123"), "prefs", {"theme": "dark"})

File ~/Downloads/Code/venv/lib/python3.11/site-packages/langgraph/store/postgres/base.py:867, in PostgresStore.setup(self)
865 version = _get_version(cur, table="store_migrations")
866 for v, sql in enumerate(self.MIGRATIONS[version + 1 :], start=version + 1):
--> 867 cur.execute(sql)
868 cur.execute("INSERT INTO store_migrations (v) VALUES (%s)", (v,))
870 if self.index_config:

File ~/Downloads/Code/venv/lib/python3.11/site-packages/psycopg/cursor.py:97, in Cursor.execute(self, query, params, prepare, binary)
93 self._conn.wait(
94 self._execute_gen(query, params, prepare=prepare, binary=binary)
95 )
96 except e._NO_TRACEBACK as ex:
---> 97 raise ex.with_traceback(None)
98 return self

ActiveSqlTransaction: CREATE INDEX CONCURRENTLY cannot run inside a transaction block

Python 3.11

langgraph==0.2.60
langgraph-checkpoint==2.0.9
langgraph-checkpoint-postgres==2.0.9
langgraph-sdk==0.1.48

psycopg==3.2.3
psycopg-c==3.2.3

langchain==0.3.13
langchain-chroma==0.1.4
langchain-community==0.3.7
langchain-core==0.3.28
langchain-experimental==0.3.3
langchain-google-genai==2.0.5
langchain-google-vertexai==2.0.7
langchain-openai==0.2.9
langchain-text-splitters==0.3.4
langchainhub==0.1.15

System Info

Output of "python -m langchain_core.sys_info"

System Information

OS: Darwin
OS Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
Python Version: 3.11.5 (v3.11.5:cce6ba91b3, Aug 24 2023, 10:50:31) [Clang 13.0.0 (clang-1300.0.29.30)]

Package Information

langchain_core: 0.3.28
langchain: 0.3.13
langchain_community: 0.3.7
langsmith: 0.1.144
langchain_chroma: 0.1.4
langchain_experimental: 0.3.3
langchain_google_genai: 2.0.5
langchain_google_vertexai: 2.0.7
langchain_openai: 0.2.9
langchain_text_splitters: 0.3.4
langchainhub: 0.1.15
langgraph_sdk: 0.1.48

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.10.5
anthropic[vertexai]: Installed. No version info available.
async-timeout: 4.0.3
chromadb: 0.5.20
dataclasses-json: 0.5.14
fastapi: 0.115.5
google-cloud-aiplatform: 1.71.1
google-cloud-storage: 2.18.2
google-generativeai: 0.8.3
httpx: 0.27.0
httpx-sse: 0.4.0
jsonpatch: 1.33
langchain-mistralai: Installed. No version info available.
numpy: 1.25.2
openai: 1.55.0
orjson: 3.10.3
packaging: 24.1
pydantic: 2.9.0
pydantic-settings: 2.6.1
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 1.4.53
tenacity: 8.5.0
tiktoken: 0.7.0
types-requests: 2.31.0.20240406
typing-extensions: 4.12.2

Originally created by @santoshgsk on GitHub (Dec 27, 2024). ### Checked other resources - [X] This is a bug, not a usage question. For questions, please use GitHub Discussions. - [X] I added a clear and detailed title that summarizes the issue. - [X] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [X] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python from psycopg import Connection from langgraph.store.postgres import PostgresStore conn_str = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWD}@{IP_ADDRESS}:{PORT}/{MEMSTORE_DB}" conn = Connection.connect(conn_str) pg_store = PostgresStore(conn=conn) pg_store.setup() # Store and retrieve data pg_store.put(("users", "123"), "prefs", {"theme": "dark"}) item = pg_store.get(("users", "123"), "prefs") ``` ### Error Message and Stack Trace (if applicable) ```shell ActiveSqlTransaction Traceback (most recent call last) Cell In[1], line 17 13 from langgraph.store.postgres import PostgresStore 15 pg_store = PostgresStore(conn=conn) ---> 17 pg_store.setup() 19 # Store and retrieve data 20 pg_store.put(("users", "123"), "prefs", {"theme": "dark"}) File ~/Downloads/Code/venv/lib/python3.11/site-packages/langgraph/store/postgres/base.py:867, in PostgresStore.setup(self) 865 version = _get_version(cur, table="store_migrations") 866 for v, sql in enumerate(self.MIGRATIONS[version + 1 :], start=version + 1): --> 867 cur.execute(sql) 868 cur.execute("INSERT INTO store_migrations (v) VALUES (%s)", (v,)) 870 if self.index_config: File ~/Downloads/Code/venv/lib/python3.11/site-packages/psycopg/cursor.py:97, in Cursor.execute(self, query, params, prepare, binary) 93 self._conn.wait( 94 self._execute_gen(query, params, prepare=prepare, binary=binary) 95 ) 96 except e._NO_TRACEBACK as ex: ---> 97 raise ex.with_traceback(None) 98 return self ActiveSqlTransaction: CREATE INDEX CONCURRENTLY cannot run inside a transaction block ``` ### Description I am trying to connect to postgresql as a MemoryStore using langgraph store postgres The connection is established using psycopg - PostgresStore is not taking in the connection string However, when I am trying to run the .setup() it is showing the error ActiveSqlTransaction Traceback (most recent call last) Cell In[1], line 17 13 from langgraph.store.postgres import PostgresStore 15 pg_store = PostgresStore(conn=conn) ---> 17 pg_store.setup() 19 # Store and retrieve data 20 pg_store.put(("users", "123"), "prefs", {"theme": "dark"}) File ~/Downloads/Code/venv/lib/python3.11/site-packages/langgraph/store/postgres/base.py:867, in PostgresStore.setup(self) 865 version = _get_version(cur, table="store_migrations") 866 for v, sql in enumerate(self.MIGRATIONS[version + 1 :], start=version + 1): --> 867 cur.execute(sql) 868 cur.execute("INSERT INTO store_migrations (v) VALUES (%s)", (v,)) 870 if self.index_config: File ~/Downloads/Code/venv/lib/python3.11/site-packages/psycopg/cursor.py:97, in Cursor.execute(self, query, params, prepare, binary) 93 self._conn.wait( 94 self._execute_gen(query, params, prepare=prepare, binary=binary) 95 ) 96 except e._NO_TRACEBACK as ex: ---> 97 raise ex.with_traceback(None) 98 return self ActiveSqlTransaction: CREATE INDEX CONCURRENTLY cannot run inside a transaction block Python 3.11 langgraph==0.2.60 langgraph-checkpoint==2.0.9 langgraph-checkpoint-postgres==2.0.9 langgraph-sdk==0.1.48 psycopg==3.2.3 psycopg-c==3.2.3 langchain==0.3.13 langchain-chroma==0.1.4 langchain-community==0.3.7 langchain-core==0.3.28 langchain-experimental==0.3.3 langchain-google-genai==2.0.5 langchain-google-vertexai==2.0.7 langchain-openai==0.2.9 langchain-text-splitters==0.3.4 langchainhub==0.1.15 ### System Info **Output of "python -m langchain_core.sys_info"** System Information ------------------ > OS: Darwin > OS Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 > Python Version: 3.11.5 (v3.11.5:cce6ba91b3, Aug 24 2023, 10:50:31) [Clang 13.0.0 (clang-1300.0.29.30)] Package Information ------------------- > langchain_core: 0.3.28 > langchain: 0.3.13 > langchain_community: 0.3.7 > langsmith: 0.1.144 > langchain_chroma: 0.1.4 > langchain_experimental: 0.3.3 > langchain_google_genai: 2.0.5 > langchain_google_vertexai: 2.0.7 > langchain_openai: 0.2.9 > langchain_text_splitters: 0.3.4 > langchainhub: 0.1.15 > langgraph_sdk: 0.1.48 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > aiohttp: 3.10.5 > anthropic[vertexai]: Installed. No version info available. > async-timeout: 4.0.3 > chromadb: 0.5.20 > dataclasses-json: 0.5.14 > fastapi: 0.115.5 > google-cloud-aiplatform: 1.71.1 > google-cloud-storage: 2.18.2 > google-generativeai: 0.8.3 > httpx: 0.27.0 > httpx-sse: 0.4.0 > jsonpatch: 1.33 > langchain-mistralai: Installed. No version info available. > numpy: 1.25.2 > openai: 1.55.0 > orjson: 3.10.3 > packaging: 24.1 > pydantic: 2.9.0 > pydantic-settings: 2.6.1 > PyYAML: 6.0.2 > requests: 2.32.3 > requests-toolbelt: 1.0.0 > SQLAlchemy: 1.4.53 > tenacity: 8.5.0 > tiktoken: 0.7.0 > types-requests: 2.31.0.20240406 > typing-extensions: 4.12.2
yindo closed this issue 2026-02-20 17:39:12 -05:00
Author
Owner

@hinthornw commented on GitHub (Dec 28, 2024):

Hello - I'll look into fixing this - I believe it works if you open it in a context manager and/or use a connection pool though

@hinthornw commented on GitHub (Dec 28, 2024): Hello - I'll look into fixing this - I believe it works if you open it in a context manager and/or use a connection pool though
Author
Owner

@sumanmichael commented on GitHub (Jan 5, 2025):

I encountered the same CREATE INDEX CONCURRENTLY error. Setting autocommit=True on the PostgreSQL connection resolved it:

from langgraph.store.postgres import PostgresStore
from psycopg import Connection

conn_str = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWD}@{IP_ADDRESS}:{PORT}/{MEMSTORE_DB}"

conn = Connection.connect(conn_str, autocommit=True)
postgres_store = PostgresStore(conn)
postgres_store.setup()

PostgreSQL doesn't allow CREATE INDEX CONCURRENTLY within a transaction (see docs). psycopg uses implicit transactions by default, which conflicts with this. autocommit=True makes each statement its own transaction, avoiding the issue.

This might be a useful workaround until the library handles this internally

@sumanmichael commented on GitHub (Jan 5, 2025): I encountered the same `CREATE INDEX CONCURRENTLY` error. Setting `autocommit=True` on the PostgreSQL connection resolved it: ```python from langgraph.store.postgres import PostgresStore from psycopg import Connection conn_str = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWD}@{IP_ADDRESS}:{PORT}/{MEMSTORE_DB}" conn = Connection.connect(conn_str, autocommit=True) postgres_store = PostgresStore(conn) postgres_store.setup() ``` PostgreSQL doesn't allow `CREATE INDEX CONCURRENTLY` within a transaction (see [docs](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY:~:text=a%20regular%20CREATE%20INDEX%20command%20can%20be%20performed%20within%20a%20transaction%20block%2C%20but%20CREATE%20INDEX%20CONCURRENTLY%20cannot.)). `psycopg` uses implicit transactions by default, which conflicts with this. `autocommit=True` **makes each statement its own transaction**, avoiding the issue. This might be a useful workaround until the library handles this internally
Author
Owner

@vbarda commented on GitHub (Jan 10, 2025):

Glad you resolved it. We already handle this when using PostgresStore.from_conn_string https://github.com/langchain-ai/langgraph/blob/main/libs/checkpoint-postgres/langgraph/store/postgres/base.py#L651, and we provide a callout in the https://github.com/langchain-ai/langgraph/tree/main/libs/checkpoint-postgres

Will close this issue, but let us know if you're still having problems

@vbarda commented on GitHub (Jan 10, 2025): Glad you resolved it. We already handle this when using `PostgresStore.from_conn_string` https://github.com/langchain-ai/langgraph/blob/main/libs/checkpoint-postgres/langgraph/store/postgres/base.py#L651, and we provide a callout in the https://github.com/langchain-ai/langgraph/tree/main/libs/checkpoint-postgres Will close this issue, but let us know if you're still having problems
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#371