Skip to main content

Posts

Showing posts with the label Database

Golang Context Pitfalls: Handling Cancellation in SQL Transactions

  A sudden spike in   MaxOpenConnections   errors or a PostgreSQL database choking on "Idle in transaction" states is rarely a database tuning issue. In Go services, it is almost exclusively a resource leak caused by mishandling   context.Context   cancellation during SQL transactions. When a request is cancelled—via client disconnect or timeout—the Go runtime stops processing the handler. However, if the database transaction ( sql.Tx ) is not explicitly rolled back, the underlying TCP connection remains reserved and the database session stays active, holding locks on rows and tables until the database's own timeout (often hours) kicks in. This post analyzes why standard  if err != nil  handling fails in concurrent environments and provides a closure-based transaction wrapper to guarantee atomicity and connection cleanup. The Root Cause: Connection Pooling vs. Context Lifecycle To understand the leak, we must look at how  database/sql  manage...

PostgreSQL vs. Dedicated Vector DBs in 2025: The RAG Architecture Debate

  The Architecture Trap: "Just Add Another Database" It is 2025. You are designing a Retrieval-Augmented Generation (RAG) pipeline for a high-traffic SaaS application. Your application data—users, permissions, document metadata—already lives in PostgreSQL. When the requirement for "Semantic Search" lands on your desk, the immediate impulse is often to provision a dedicated vector database like Pinecone, Weaviate, or Qdrant. This is often a mistake. While dedicated vector databases offer impressive niche features, introducing them creates a  Distributed State Problem . You now have two sources of truth. When a user updates a document in PostgreSQL, you must synchronously update the vector store. If the PostgreSQL transaction commits but the API call to the vector DB fails (or lags), your user sees inconsistent search results. You are essentially fighting the CAP theorem unnecessarily. For 95% of use cases—specifically those under 100 million vectors—the architectural...