0% found this document useful (0 votes)
43 views5 pages

Viernes

This log captures an error when attempting to connect to a PostgreSQL database. The error indicates that there are too many clients already connected, preventing a new connection from being established. This leads to failures when querying the database and executing transactions. Additional log messages show timing information for test queries and transactions.

Uploaded by

catriel lopez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Viernes

This log captures an error when attempting to connect to a PostgreSQL database. The error indicates that there are too many clients already connected, preventing a new connection from being established. This leads to failures when querying the database and executing transactions. Additional log messages show timing information for test queries and transactions.

Uploaded by

catriel lopez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

2022-09-15 19:24:32,583 - DEBUG [95z-i7j-jrz]: Vars: {'objects': {'mat_pk': '3c7-

400-737', 'link_id': 'xh5-byn-yzl', 'data_type': 'gy9-bbi-zyr', 'interface': '2vz-


hpa-z41', 'direction': 'IN_OUT', 'endpoint_attributes': {}}}
2022-09-15 19:24:40,582 - ERROR [i6g-r0z-s4x]: Traceback (most recent call last):
File "/usr/src/app/src/controllers/network_modeling.py", line 269, in post
for op in get_hasura_mutation(
File "/usr/src/app/src/models/data_items/submission.py", line 56, in
get_hasura_mutation
return {
File "/usr/src/app/src/models/data_items/submission.py", line 20, in
resolve_insert_mutation
return InsertBuilder(
File "/usr/src/app/src/models/data_items/builders/insert_builder.py", line 224,
in build
sql, _ = link_builder.create(
File "/usr/src/app/src/models/data_items/builders/link_builder.py", line 123, in
create
interface_id =
self.__get_interface_id(data_type=f"{self.RESOURCE_NAME}_Interfaces",
File "/usr/src/app/src/models/data_items/builders/link_builder.py", line 464, in
__get_interface_id
res = self.PSQL.query(
File "/usr/src/app/src/helpers/connectors/postgres.py", line 57, in query
with self.pool.connection() as conn:
File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.8/site-packages/psycopg_pool/pool.py", line 114, in
connection
conn = self.getconn(timeout=timeout)
File "/usr/local/lib/python3.8/site-packages/psycopg_pool/pool.py", line 158, in
getconn
conn = pos.wait(timeout=timeout)
File "/usr/local/lib/python3.8/site-packages/psycopg_pool/pool.py", line 707, in
wait
raise self.error
psycopg_pool.PoolTimeout: couldn't get a connection after 30.0 sec

error connecting in 'pool-230': connection failed: FATAL: sorry, too many clients
already
error connecting in 'pool-276': connection failed: FATAL: sorry, too many clients
already
reconnection attempt in pool 'pool-276' failed after 30 sec
error connecting in 'pool-296': connection failed: FATAL: sorry, too many clients
already
error connecting in 'pool-278': connection failed: FATAL: sorry, too many clients
already
reconnection attempt in pool 'pool-278' failed after 30 sec
error connecting in 'pool-290': connection failed: FATAL: sorry, too many clients
already
reconnection attempt in pool 'pool-290' failed after 30 sec
error connecting in 'pool-310': connection failed: FATAL: sorry, too many clients
already
reconnection attempt in pool 'pool-310' failed after 30 sec

[10] START: 2022-09-15 19:36:53.996203


[10] END: 2022-09-15 19:36:56.903519
[100] START: 2022-09-15 19:36:56.903929
[100] END: 2022-09-15 19:37:23.809627
[1000] START: 2022-09-15 19:37:23.813317
[1000] END: 2022-09-15 19:38:21.624192
[1000] ERROR: Expected 1000 but got 189
[10000] START: 2022-09-15 19:38:21.685822
[10000] END: 2022-09-15 19:40:02.539603
[10000] ERROR: Expected 10000 but got 236

-- No cargan bien los data items en front


-- No carga bien el form input en front

import sys, os
import logging, subprocess
from contextlib import contextmanager
from psycopg_pool import ConnectionPool

from config import config


import psycopg
from typing import Iterable, Iterator, List, Optional

log = logging.getLogger()

# TODO: Conector asincrono


class PSQLConnector:

def __init__(
self,
environment: str,
user_id: str,
*,
host: Optional[str] = None,
port: Optional[str] = None,
user: Optional[str] = None,
password: Optional[str] = None,
database: Optional[str] = None,
) -> None:

cfg = config().get(environment)

if not cfg:
raise Exception(f"invalid configuration for environment {environment}")

self.conn: psycopg.Cursor = None


self.environment = environment
self.user_id = user_id
self.host = host if host else cfg["postgres"]
["host"].format(user_id=user_id)
self.port = port if port else cfg["postgres"]["port"]
self.user = user if user else cfg["postgres"]["user"]
self.password = password if password else cfg["postgres"]["password"]
self.database = database if database else cfg["postgres"]
["database"].format(user_id=user_id)
self.url = self.reload_url()
self.cursor: psycopg.Cursor = None # Warn: no multithread
self.pool = ConnectionPool(conninfo=self.url, max_size=30, min_size=5,
reconnect_timeout=30, open=True)
# self.pool.open(wait=True)

def reload_url(self) -> str:


self.url = f"postgres://{self.user}:{self.password}@{self.host}:
{self.port}/{self.database}"
return self.url

def change_database(self, new_db: str):


self.database = new_db
self.reload_url()

def query(self, query: str, schema: str = "public", fetch: bool = True):
# Connect to an existing database
with self.pool.connection() as conn:
# Open a cursor to perform database operations
with conn.cursor() as cur:
# Execute a command: this creates a new table
cur.execute(f"SET search_path TO \"{schema}\";")
cur.execute(query)
conn.commit()

try:
out = cur.fetchall() if fetch else None
except psycopg.ProgrammingError as e:
log.warning(f"Couldn't fetch results. Reason: {e}")
out = None
# finally:
# if not cur.closed:
# cur.close()
return out

def on_transaction(self, queries: Iterable, schema: str = "public", fetch: bool


= True):
# Connect to an existing database
with self.pool.connection() as conn:
with conn.cursor() as cur:
cur.execute(f"SET search_path TO \"{schema}\";")
for query in queries:
cur.execute(query)
conn.commit()

try:
out = cur.fetchall() if fetch else None
except psycopg.ProgrammingError as e:
log.warning(f"Couldn't fetch results. Reason: {e}")
out = None
finally:
if not cur.closed:
cur.close()
return out

@contextmanager
def transaction(self, debug: bool = False):
if debug:
log.info("""
#############################################
# Transaction in Debug Mode #
#############################################""")
else:
log.info("""
#############################################
# Starting Postgres transaction #
#############################################""")
with self.pool.connection() as conn:
with conn.cursor() as cursor:
try:
self.cursor = cursor
yield self
if not debug:
conn.commit()
log.debug("Transaction committed!")
else:
conn.rollback()
log.info("""
#############################################
# Transaction rollbacked #
#############################################""")
except:
conn.rollback()
log.debug("Transaction rollbacked!")
raise
finally:
self.cursor = None

def execute(self, query_or_func, *a, **kw):


if callable(query_or_func):
return query_or_func(self.cursor, *a, **kw)
return self.cursor.execute(query_or_func, *a, **kw)

@contextmanager
def cli(self) -> Iterator[psycopg.Cursor]:
# no crea una transaccion, es lo mas parecido a tirar comandos desde la cli
de postgres sin abrir transaccion.
# util para comandos sobre databases que no puedan estar dentro de una
transaccion
with self.pool.connection() as conn:
try:
cursor = conn.cursor()
try:
yield cursor
finally:
if cursor:
cursor.close()
finally:
if conn:
conn.close()

class PgUtils:

def __init__(self, psql: PSQLConnector) -> None:


PgUtils.assert_secure_password(psql.password)
self.psql = psql
def pg_dump(self, *, source_db: str, dump_name: str, format: Optional[str] = "-
Fc", args: Optional[List[str]] = []):
"""
Wrapper para el comando pg_dump
Por default, hace backup de la database entera
El formato por default es el custom de postgres (-Fc) para poder ser usado
con pg_restore
https://www.postgresql.org/docs/current/app-pgdump.html
"""
pg_dump = subprocess.run([
"pg_dump",
f"-h{self.psql.host}",
f"-p{self.psql.port}",
f"-U{self.psql.user}",
f"-f{dump_name}",
format,
*args,
source_db
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

if pg_dump.returncode != 0:
raise Exception(f"failed to dump database: {pg_dump.returncode}
{pg_dump.stderr} {pg_dump.stdout}")

def pg_restore(self, *, target_db: str, dump_name: str, args:


Optional[List[str]] = []):
"""
Wrapper para el comando pg_restore
Por default restaura sobre la database entera
https://www.postgresql.org/docs/current/app-pgrestore.html
"""
pg_restore = subprocess.run([
"pg_restore",
f"-h{self.psql.host}",
f"-p{self.psql.port}",
f"-U{self.psql.user}",
f"--dbname={target_db}",
*args,
dump_name
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

if pg_restore.returncode != 0:
raise Exception(f"failed to restore database: {pg_restore.returncode}
{pg_restore.stderr} {pg_restore.stdout}")

@staticmethod
def assert_secure_password(p: str):
if not os.getenv("PGPASSWORD") == p:
raise Exception("environment variable PGPASSWORD must be set in order
to use postgres clients safely")

You might also like