Slony-I 2.0.5 Documentation | ||||
---|---|---|---|---|
Prev | Fast Backward | Fast Forward | Next |
When changes are made to the database schema, e.g. - adding fields to a table, it is necessary for this to be handled rather carefully, otherwise different nodes may get rather deranged because they disagree on how particular tables are built.
If you pass the changes through Slony-I via SLONIK EXECUTE SCRIPT (slonik) / schemadocddlscript_complete(integer, text, integer) (stored function), this allows you to be certain that the changes take effect at the same point in the transaction streams on all of the nodes. This may not be important to you depending on the nature of your change. You should still make sure that no transactions are changing the tables that your script uses while the EXECUTE SCRIPT command is running on the master.
It is essential to use EXECUTE SCRIPT if you alter the names of tables or the namespace they reside in. If you do not then Slony-I will be unaware of the new table name.
It's worth making a couple of comments on "special things" about SLONIK EXECUTE SCRIPT:
The script must not contain transaction BEGIN or END statements, as the script is already executed inside a transaction. In PostgreSQL version 8, the introduction of nested transactions changes this somewhat, but you must still remain aware that the actions in the script are processed within the scope of a single transaction whose BEGIN and END you do not control.
If there is anything broken about the script, or about how it executes on a particular node, this will cause the slon daemon for that node to panic and crash. You may see various expected messages (positive and negative) in Section 26.6.2. If you restart the slon, it will, more likely than not, try to repeat the DDL script, which will, almost certainly, fail the second time in the same way it did the first time. I have found this scenario to lead to a need to go to the "master" node to delete the event from the table sl_event in order to stop it from continuing to fail.
The implication of this is that it is vital that modifications not be made in a haphazard way on one node or another. The schemas must always stay in sync.
For slon to, at that point, "panic" is probably the correct answer, as it allows the DBA to head over to the database node that is broken, and manually fix things before cleaning out the defective event and restarting slon. You can be certain that the updates made after the DDL change on the provider node are queued up, waiting to head to the subscriber. You don't run the risk of there being updates made that depended on the DDL changes in order to be correct.
Unfortunately, this nonetheless implies that the use of the DDL facility is somewhat fragile and fairly dangerous. Making DDL changes must not be done in a sloppy or cavalier manner. If your applications do not have fairly stable SQL schemas, then using Slony-I for replication is likely to be fraught with trouble and frustration. See the section on locking issues for more discussion of related issues.
A method for testing DDL changes has been pointed out as a likely "best practice."
You need to test DDL scripts in a non-destructive manner.
The problem is that if nodes are, for whatever reason, at all out of sync, replication is likely to fall over, and this takes place at what is quite likely one of the most inconvenient times, namely the moment when you wanted it to work.
You may indeed check to see if schema scripts work well or badly, by running them by hand, against each node, adding BEGIN; at the beginning, and ROLLBACK; at the end, so that the would-be changes roll back.
If this script works OK on all of the nodes, that suggests that it should work fine everywhere if executed via slonik. If problems are encountered on some nodes, that will hopefully allow you to fix the state of affairs on those nodes so that the script will run without error.
Warning |
If the SQL script contains a COMMIT; somewhere before the ROLLBACK; , that may allow changes to go in unexpectedly. |