1

Can someone explain what psycopg2's connection_factory is and how to use it?

I've been looking at a project's code that connects to postgres via the connection_factory and I am at a lost. I have looked at the docs, https://www.psycopg.org/docs/module.html, but all it tells me is:

Using the connection_factory parameter a different class or connections factory can be specified. It should be a callable object taking a dsn string argument. 

Looking at the cursor factories and connection becomes overwhelming and I don't even know if it is still related to the connection_factory.

Below is the code that I am looking at:

class PsycoConnection(psycopg2.extensions.connection):
    def lobject(*args, **kwargs):
        pass

Somewhere in another class the above class gets called as follows:

result = psycopg2.connect(
                connection_factory=PsycoConnection,
                **connection_info)

Normally when I use psycopg2 to connect to a database I simple do something like below:

result = psycopg2.connect("host=127.0.0.1 port=5433 dbname=yugabyte sslmode=prefer")

I know the connection_info is not any different to "host=127.0.0.1 port=5433 dbname=yugabyte sslmode=prefer" except it's a dict. So my question is what is this PsycoConnection passed to connection_factory? To me the PsycoConnection class isn't even doing anything since the only code is pass. Thank you.

Mark
  • 113
  • 7
  • Where are seeing `PsycoConnection`? What you show is not going to change anything from the regular connection as the only method it defines does nothing. – Adrian Klaver Nov 23 '21 at 22:12
  • The github project is here: https://github.com/odoo/odoo/blob/15.0/odoo/sql_db.py, line 570 is the class ```PsycoConnection```, line 656 is where it's being used – Mark Nov 24 '21 at 05:49
  • Alright so it looks like they are overriding this [lobject](https://www.psycopg.org/docs/connection.html?highlight=lobject#connection.lobject) to make it a no-op. In other words not allow for working with large objects. You would have to ask the `odoo` project folks to confirm and the why of it. – Adrian Klaver Nov 24 '21 at 15:54

0 Answers0