This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tap-postgres opens a new connection every time it needs to cast a value. This is highly inefficient as opening a connection is usually a slow and resource-intensive operation. An easy fix would be to use something like PgBouncer, but it's even better if we open just once connection and reuse it for all queries. We created a Singleton Postgres connection wrapper. This wrapper actually holds up to two connections, since we need two different connection factories. The `connect` method returns the connection we need based on the arguments provided.
- Loading branch information
1 parent
b1ba14d
commit bfdd989
Showing
2 changed files
with
50 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import psycopg2 | ||
import psycopg2.extras | ||
|
||
|
||
# pylint: disable=missing-class-docstring,missing-function-docstring | ||
class Postgres: | ||
__instance = None | ||
|
||
@staticmethod | ||
def get_instance(): | ||
if Postgres.__instance is None: | ||
Postgres() | ||
|
||
return Postgres.__instance | ||
|
||
def __init__(self): | ||
if Postgres.__instance is not None: | ||
raise Exception("This class is a singleton!") | ||
|
||
Postgres.__instance = self | ||
self.connections = {"logical": None, "transactional": None} | ||
|
||
def open_connection(self, conn_config, logical_replication): | ||
connection_type = "logical" if logical_replication else "transactional" | ||
|
||
cfg = { | ||
'application_name': 'pipelinewise', | ||
'host': conn_config['host'], | ||
'dbname': conn_config['dbname'], | ||
'user': conn_config['user'], | ||
'password': conn_config['password'], | ||
'port': conn_config['port'], | ||
'connect_timeout': 30 | ||
} | ||
|
||
if conn_config.get('sslmode'): | ||
cfg['sslmode'] = conn_config['sslmode'] | ||
|
||
if logical_replication: | ||
cfg['connection_factory'] = psycopg2.extras.LogicalReplicationConnection | ||
|
||
if not self.connections[connection_type] or self.connections[connection_type].closed: | ||
self.connections[connection_type] = psycopg2.connect(**cfg) | ||
|
||
return self.connections[connection_type] | ||
|