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. To fix the issue we do two things: 1. 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 conneciton we need based on the arguments provided. 2. Remove `when` statements when asking for a connection. When statements are great everytime we need to ensure a resource is properly closed after it's being used, but in our specific case, we don't want to close connections after each query.
- Loading branch information
1 parent
11914d4
commit 476cc3b
Showing
7 changed files
with
368 additions
and
328 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
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,55 @@ | ||
from singer import utils | ||
|
||
import psycopg2 | ||
import psycopg2.extras | ||
|
||
|
||
class Postgres: | ||
__instance = None | ||
|
||
@staticmethod | ||
def get_instance(): | ||
if Postgres.__instance is None: | ||
Postgres() | ||
|
||
return Postgres.__instance | ||
|
||
@staticmethod | ||
def get_configuration(logical_replication): | ||
args = utils.parse_args({}) | ||
conn_config = args.config | ||
|
||
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 | ||
|
||
return cfg | ||
|
||
|
||
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 connect(self, logical_replication=False): | ||
connection_type = "logical" if logical_replication else "transactional" | ||
|
||
if not self.connections[connection_type] or self.connections[connection_type].closed: | ||
config = Postgres.get_configuration(logical_replication) | ||
self.connections[connection_type] = psycopg2.connect(**config) | ||
|
||
return self.connections[connection_type] |
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
Oops, something went wrong.