8000 Logical replication support by zilder · Pull Request #42 · postgrespro/testgres · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 16 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
8000 File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into logical. Also testgres.pubsub was added to…
… the Sphinx templates and module header was added as well.
  • Loading branch information
zilder committed Mar 26, 2018
commit f652bf40850d5aa20a8c9f207a41fec8a570d639
17 changes: 16 additions & 1 deletion docs/source/testgres.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ testgres.node
.. automethod:: __init__

.. autoclass:: testgres.node.ProcessProxy
:members:
:members:

testgres.pubsub
---------------

.. automodule:: testgres.pubsub

.. autoclass:: testgres.node.Publication
:members:

.. automethod:: __init__

.. autoclass:: testgres.node.Subscription
:members:

.. automethod:: __init__
2 changes: 1 addition & 1 deletion testgres/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
... replica.catchup() # wait until changes are visible
... print(replica.execute('postgres', 'select count(*) from test'))
PostgresNode(name='...', port=..., base_dir='...')
[(3,)]
[(3L,)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was when I tested with doctest. But now when I ran doctest it showed [(3,)] so I am confused :) Will change it back then as it shouldn't be part of this pull request anyway.

"""
from .node import PostgresNode

Expand Down
5 changes: 2 additions & 3 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,11 @@ def _create_recovery_conf(self, username, slot=None):
master = self.master
assert master is not None

# yapf: disable
conninfo = {
"application_name": self.name,
"port": master.port,
"user": username
}
} # yapf: disable

# host is tricky
try:
Expand All @@ -318,7 +317,7 @@ def _create_recovery_conf(self, username, slot=None):
line = (
"primary_conninfo='{}'\n"
"standby_mode=on\n"
).format(options_string(**conninfo))
).format(options_string(**conninfo)) # yapf: disable

if slot:
# Connect to master for some additional actions
Expand Down
58 changes: 52 additions & 6 deletions testgres/pubsub.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
# coding: utf-8
"""
Unlike physical replication the logical replication allows users replicate only
specified databases and tables. It uses publish-subscribe model with possibly
multiple publishers and multiple subscribers. When initializing publisher's
node ``allow_logical=True`` should be passed to the :meth:`.PostgresNode.init()`
method to enable PostgreSQL to write extra information to the WAL needed by
logical replication.

To replicate table ``X`` from node A to node B the same table structure should
be defined on the subscriber's node as logical replication don't replicate DDL.
After that :meth:`~.PostgresNode.publish()` and :meth:`~.PostgresNode.subscribe()`
methods may be used to setup replication. Example:

>>> from .api import get_new_node
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to from testgres import get_new_node. Documentation should contain working code, even it would require remove doctest.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

>>> with get_new_node() as nodeA, get_new_node() as nodeB:
... nodeA.init(allow_logical=True).start()
... nodeB.init().start()
...
... # create same table both on publisher and subscriber
... create_table = 'create table test (a int, b int)'
... nodeA.safe_psql(create_table)
... nodeB.safe_psql(create_table)
...
... # create publication
... pub = nodeA.publish('mypub')
... # create subscription
... sub = nodeB.subscribe(pub, 'mysub')
...
... # insert some data to the publisher's node
... nodeA.execute('insert into test values (1, 1), (2, 2)')
...
... # wait until changes apply on subscriber and check them
... sub.catchup()
...
... # read the data from subscriber's node
... nodeB.execute('select * from test')
PostgresNode(name='...', port=..., base_dir='...')
PostgresNode(name='...', port=..., base_dir='...')
''
''
[(1, 1), (2, 2)]
"""

from six import raise_from

Expand All @@ -10,7 +52,8 @@
class Publication(object):
def __init__(self, name, node, tables=None, dbname=None, username=None):
"""
Constructor
Constructor. Use :meth:`.PostgresNode.publish()` instead of direct
constructing publication objects.

Args:
name: publication name
Expand Down Expand Up @@ -40,10 +83,11 @@ def drop(self, dbname=None, username=None):

def add_tables(self, tables, dbname=None, username=None):
"""
Add tables
Add tables to the publication. Cannot be used if publication was
created with empty tables list.

Args:
tables: a list of tables to add to the publication
tables: a list of tables to be added to the publication
"""
if not tables:
raise ValueError("Tables list is empty")
Expand All @@ -64,15 +108,17 @@ def __init__(self,
username=None,
**kwargs):
"""
Constructor
Constructor. Use :meth:`.PostgresNode.subscribe()` instead of direct
constructing subscription objects.

Args:
name: subscription name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also noticed that some doc strings end with commas while others don't.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

node: subscriber's node
publication: Publication object we are subscribing to
publication: :class:`.Publication` object we are subscribing to
(see :meth:`.PostgresNode.publish()`)
dbname: database name used to connect and perform subscription
username: username used to connect to the database
**kwargs: subscription parameters (see CREATE SUBSCRIPTION
**kwargs: subscription parameters (see ``CREATE SUBSCRIPTION``
in PostgreSQL documentation for more information)
"""
self.name = name
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0