-
Notifications
You must be signed in to change notification settings - Fork 35
Logical replication support #42
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
Changes from 1 commit
ca5b546
bb01c7d
782484b
f8b95c6
b1cba73
954879a
0741c70
f4e0bd0
f48623b
138c6cc
f652bf4
bc1002f
08ed6ef
4b279ef
d60cdcb
50e02ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,13 @@ | |
ExecUtilException, \ | ||
QueryException, \ | ||
StartNodeException, \ | ||
TimeoutException | ||
TimeoutException, \ | ||
InitNodeException | ||
|
||
from .logger import TestgresLogger | ||
|
||
from .pubsub import Publication, Subscription | ||
|
||
from .utils import \ | ||
eprint, \ | ||
get_bin_path, \ | ||
|
@@ -278,6 +281,7 @@ def default_conf(self, | |
fsync=False, | ||
unix_sockets=True, | ||
allow_streaming=True, | ||
allow_logical=False, | ||
log_statement='all'): | ||
""" | ||
Apply default settings to this node. | ||
|
@@ -286,6 +290,7 @@ def default_conf(self, | |
fsync: should this node use fsync to keep data safe? | ||
unix_sockets: should we enable UNIX sockets? | ||
allow_streaming: should this node add a hba entry for replication? | ||
allow_logical: can this node be used as a logical replication publisher? | ||
log_statement: one of ('all', 'off', 'mod', 'ddl'). | ||
|
||
Returns: | ||
|
@@ -365,6 +370,12 @@ def get_auth_method(t): | |
wal_keep_segments, | ||
wal_level)) | ||
|
||
if allow_logical: | ||
if not pg_version_ge('10'): | ||
raise InitNodeException("Logical replication is only " | ||
"available for Postgres 10 and newer") | ||
conf.write(u"wal_level = logical\n") | ||
|
||
# disable UNIX sockets if asked to | ||
if not unix_sockets: | ||
conf.write(u"unix_socket_directories = ''\n") | ||
|
@@ -751,7 +762,8 @@ def poll_query_until(self, | |
expected=True, | ||
commit=True, | ||
raise_programming_error=True, | ||
raise_internal_error=True): | ||
raise_internal_error=True, | ||
zero_rows_is_ok=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
""" | ||
Run a query once per second until it returns 'expected'. | ||
Query should return a single value (1 row, 1 column). | ||
|
@@ -788,7 +800,12 @@ def poll_query_until(self, | |
raise QueryException('Query returned None', query) | ||
|
||
if len(res) == 0: | ||
raise QueryException('Query returned 0 rows', query) | ||
if zero_rows_is_ok: | ||
time.sleep(sleep_time) | ||
attempts += 1 | ||
continue | ||
else: | ||
raise QueryException('Query returned 0 rows', query) | ||
|
||
if len(res[0]) == 0: | ||
raise QueryException('Query returned 0 columns', query) | ||
|
@@ -902,6 +919,41 @@ def catchup(self, dbname=None, username=None): | |
except Exception as e: | ||
raise_from(CatchUpException("Failed to catch up", poll_lsn), e) | ||
|
||
def publish(self, | ||
pubname, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe change |
||
tables=None, | ||
dbname=None, | ||
username=None): | ||
""" | ||
Create publication for logical replication | ||
|
||
Args: | ||
pubname: publication name | ||
tables: tables names list | ||
dbname: database name where objects or interest are located | ||
username: replication username | ||
""" | ||
return Publication(pubname, self, tables, dbname, username) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please use keyword args instead of positional args? |
||
|
||
def subscribe(self, | ||
publication, | ||
subname, | ||
dbname=None, | ||
username=None, | ||
**kwargs): | ||
""" | ||
Create subscription for logical replication | ||
|
||
Args: | ||
subname: subscription name | ||
publication: publication object obtained from publish() | ||
|
||
""" | ||
return Subscription(subname, self, publication, | ||
dbname=dbname, | ||
username=username, | ||
**kwargs) | ||
|
||
def pgbench(self, | ||
dbname=None, | ||
username=None, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# coding: utf-8 | ||
|
||
from six import raise_from | ||
|
||
from .defaults import default_dbname, default_username | ||
from .exceptions import CatchUpException | ||
from .utils import pg_version_ge | ||
|
||
|
||
class Publication(object): | ||
def __init__(self, pubname, node, tables=None, dbname=None, username=None): | ||
""" | ||
Constructor | ||
|
||
Args: | ||
pubname: publication name | ||
node: publisher's node | ||
tables: tables list or None for all tables | ||
dbname: database name used to connect and perform subscription | ||
username: username used to connect to the database | ||
""" | ||
self.name = pubname | ||
self.node = node | ||
self.dbname = dbname or default_dbname() | ||
self.username = username or default_username() | ||
|
||
# create publication in database | ||
t = 'table ' + ', '.join(tables) if tables else 'all tables' | ||
query = "create publication {} for {}" | ||
node.safe_psql(query.format(pubname, t), | ||
dbname=dbname, | ||
username=username) | ||
|
||
def close(self, dbname=None, username=None): | ||
""" | ||
Drop publication | ||
""" | ||
self.node.safe_psql("drop publication {}".format(self.name), | ||
dbname=dbname, username=username) | ||
|
||
def add_tables(self, tables, dbname=None, username=None): | ||
""" | ||
Add tables | ||
|
||
Args: | ||
tables: a list of tables to add to the publication | ||
""" | ||
if not tables: | ||
raise ValueError("Tables list is empty") | ||
|
||
query = "alter publication {} add table {}" | ||
self.node.safe_psql(query.format(self.name, ', '.join(tables)), | ||
dbname=dbname or self.dbname, | ||
username=username or self.username) | ||
|
||
|
||
class Subscription(object): | ||
def __init__(self, | ||
subname, | ||
node, | ||
publication, | ||
dbname=None, | ||
username=None, | ||
**kwargs): | ||
""" | ||
Constructor | ||
|
||
Args: | ||
subname: subscription name | ||
node: subscriber's node | ||
publication: Publication object we are subscribing to | ||
dbname: database name used to connect and perform subscription | ||
username: username used to connect to the database | ||
**kwargs: subscription parameters (see CREATE SUBSCRIPTION | ||
in PostgreSQL documentation for more information) | ||
""" | ||
self.name = subname | ||
self.node = node | ||
self.pub = publication | ||
|
||
# connection info | ||
conninfo = ( | ||
u"dbname={} user={} host={} port={}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could extract common |
||
).format(self.pub.dbname, | ||
self.pub.username, | ||
self.pub.node.host, | ||
self.pub.node.port) | ||
|
||
query = ( | ||
"create subscription {} connection '{}' publication {}" | ||
).format(subname, conninfo, self.pub.name) | ||
|
||
# additional parameters | ||
if kwargs: | ||
params = ','.join('{}={}'.format(k, v) for k, v in kwargs.iteritems()) | ||
query += " with ({})".format(params) | ||
|
||
node.safe_psql(query, dbname=dbname, username=username) | ||
|
||
def disable(self, dbname=None, username=None): | ||
""" | ||
Disables the running subscription. | ||
""" | ||
query = "alter subscription {} disable" | ||
self.node.safe_psql(query.format(self.name), | ||
dbname=None, | ||
username=None) | ||
|
||
def enable(self, dbname=None, username=None): | ||
""" | ||
Enables the previously disabled subscription. | ||
""" | ||
query = "alter subscription {} enable" | ||
self.node.safe_psql(query.format(self.name), | ||
dbname=None, | ||
username=None) | ||
|
||
def refresh(self, copy_data=True, dbname=None, username=None): | ||
""" | ||
Disables the running subscription. | ||
""" | ||
query = "alter subscription {} refresh publication with (copy_data={})" | ||
self.node.safe_psql(query.format(self.name, copy_data), | ||
dbname=dbname, | ||
username=username) | ||
|
||
def close(self, dbname=None, username=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be called |
||
""" | ||
Drops subscription | ||
""" | ||
self.node.safe_psql("drop subscription {}".format(self.name), | ||
dbname=dbname, username=username) | ||
|
||
def catchup(self, username=None): | ||
""" | ||
Wait until subscription catches up with publication. | ||
|
||
Args: | ||
username: remote node's user name | ||
""" | ||
if pg_version_ge('10'): | ||
query = ( | ||
"select pg_current_wal_lsn() - replay_lsn = 0 " | ||
"from pg_stat_replication where application_name = '{}'" | ||
).format(self.name) | ||
else: | ||
query = ( | ||
"select pg_current_xlog_location() - replay_location = 0 " | ||
"from pg_stat_replication where application_name = '{}'" | ||
).format(self.name) | ||
|
||
try: | ||
# wait until this LSN reaches subscriber | ||
self.pub.node.poll_query_until( | ||
query=query, | ||
dbname=self.pub.dbname, | ||
username=username or self.pub.username, | ||
max_attempts=60, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO it's better to replace |
||
zero_rows_is_ok=True) # statistics may have not updated yet | ||
except Exception as e: | ||
raise_from(CatchUpException("Failed to catch up", query), e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we enable this by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is not supported on postgres versions below 10 and there is specific message when someone's trying to enable this feature on those versions. Besides it produces extra WAL data and hence could work slightly slower.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, i see.