Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeoutError on multiple requests #1076

Open
petritavd opened this issue Sep 6, 2023 · 7 comments
Open

TimeoutError on multiple requests #1076

petritavd opened this issue Sep 6, 2023 · 7 comments

Comments

@petritavd
Copy link

  • asyncpg version: 0.27.0
  • PostgreSQL version: 14.7
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : We can reproduce it, not locally
  • Python version: 3.10.2
  • Platform: Backend: Linux, t3large AWS instance, Database: AWS RDS
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes

We're using FastAPI ,and our current code for setting up the db connection is:

connection_arguments: Dict[str, Any] = {"server_settings": {"jit": "off"}}

ENUM_TYPES = (
    ("public", "sometype"), ...
)

async def register_enum_types(conn) -> None: 
    for enum_class, typename in ENUM_TYPES:
        await conn.set_builtin_type_codec(
            typename, schema=enum_class, codec_name="text"
        )


engine = create_async_engine(
    settings.uri,
    echo=False,
    connect_args=connection_arguments,
    pool_size=200,
    max_overflow=10,
)

async_sessionmaker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)


@event.listens_for(engine.sync_engine, "connect")
def on_connect(dbapi_connection, connection_record):
    dbapi_connection.run_async(register_enum_types)

We added the register_enum_types as we were thinking that the query:
SELECT t.oid, t.typelem AS elemtype, t.typtype AS kind FROM pg_catalog.pg_type AS t WHERE t.oid = $1

which we saw a lot in AWS RDS monitoring is causing the issue, but now we're not sure what is it anymore.

In logs when it happens we see:

File "/path/to/file/python3.10/site-packages/asyncpg/connect_utils.py", line 780, in _connect_addr raise asyncio.TimeoutError asyncio.exceptions.TimeoutError

and after some time, it gets back to normal.

@ilirosmanaj
Copy link

@elprans, any ideas on the above? It's happening pretty consistently and we've tried basically everything to get this sorted. Some input would be very welcome.

@elprans
Copy link
Member

elprans commented Sep 15, 2023

This seems like a connection timeout, not a query timeout, so I'd investigate the network configuration. How are you hosting your app?

Also, why do you need to register the codec alias for the enum types? Asyncpg already decodes them as text by default.

@ilirosmanaj
Copy link

The app is a FastAPI app running in EC2 - we're using uvicorn and start about 10 workers. The postgres instance is in AWS RDS (instance type db.m5.2xlarge), see instance details below:
image

What we notice is that the "Average active sessions" sometimes blows up even with not that high usage and causes these timeouts.

image

The reason for registering was because we sometimes notice spikes of the following query (even though we have jitt set to off) and as part off some investigations this was a proposed solution (but didn't help). The query:

SELECT t.oid, t.typelem AS elemtype, t.typtype AS kind FROM pg_catalog.pg_type AS t WHERE t.oid = $1.

I am not sure if the connection pool is being drained here but it's highly unlikely in my opinion.

Let me know your thoughts @elprans

@ilirosmanaj
Copy link

Any ideas @elprans? Sorry for bugging you but it's quite an interesting thing.

@elprans
Copy link
Member

elprans commented Sep 23, 2023

Could it be that you've restarted your app at that time and all workers had their connection pools populated?

@elprans
Copy link
Member

elprans commented Sep 23, 2023

The reason for registering was because we sometimes notice spikes of the following query (even though we > have jitt set to off) and as part off some investigations this was a proposed solution (but didn't
help). The query:

SELECT t.oid, t.typelem AS elemtype, t.typtype AS kind FROM pg_catalog.pg_type AS t WHERE t.oid = $1.

This happens because you are registering codecs, the query is executed by set_type_codec/set_builtin_type_codec.

@zagortenej024
Copy link

Hey @ilirosmanaj, did you resolve this by any chance? We're having the exact same issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@elprans @ilirosmanaj @petritavd @zagortenej024 and others