Skip to content

Commit 8e6b5c8

Browse files
committed
It includes
-Support for mirroring tables in different Schema's -Improved documentation for compiling with 7.1.x and 7.2.x -Fixes a buffer overrun bug. Steven Singer
1 parent cabad37 commit 8e6b5c8

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

contrib/dbmirror/DBMirror.pl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# GNU General Public License for more details.
2020
#
2121
##############################################################################
22-
# $Id: DBMirror.pl,v 1.2 2002/10/18 18:41:19 momjian Exp $
22+
# $Id: DBMirror.pl,v 1.3 2002/10/19 02:16:40 momjian Exp $
2323
#
2424
##############################################################################
2525

@@ -387,7 +387,7 @@ ($$$$$)
387387

388388

389389
#Now build the insert query.
390-
my $insertQuery = "INSERT INTO \"$tableName\" (";
390+
my $insertQuery = "INSERT INTO $tableName (";
391391
my $valuesQuery = ") VALUES (";
392392
foreach $column (keys (%recordValues)) {
393393
if($firstIteration==0) {
@@ -468,7 +468,7 @@ ($$$$$)
468468
%dataHash = extractData($pendingResult,$currentTuple);
469469

470470
my $counter=0;
471-
my $deleteQuery = "DELETE FROM \"$tableName\" WHERE ";
471+
my $deleteQuery = "DELETE FROM $tableName WHERE ";
472472
foreach $currentField (keys %dataHash) {
473473
if($firstField==0) {
474474
$deleteQuery .= " AND ";
@@ -553,7 +553,7 @@ ($$$$$)
553553

554554
my $counter;
555555
my $quotedValue;
556-
my $updateQuery = "UPDATE \"$tableName\" SET ";
556+
my $updateQuery = "UPDATE $tableName SET ";
557557
my $currentField;
558558

559559

contrib/dbmirror/MirrorSetup.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ CREATE FUNCTION "recordchange" () RETURNS trigger AS
66

77
CREATE TABLE "MirrorHost" (
88
"MirrorHostId" serial,
9-
"HostName" varchar NOT NULL
9+
"HostName" varchar NOT NULL,
10+
PRIMARY KEY("MirrorHostId")
1011
);
1112

1213

contrib/dbmirror/README.dbmirror

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Pending tables.
4646
Installation Instructions
4747
------------------------------------------------------------------------
4848

49+
4950
1) Compile pending.c
5051

5152
The file pending.c contains the recordchange trigger. This runs every
@@ -54,20 +55,35 @@ time a row inside of a table being mirrored changes.
5455

5556
To build the trigger run make on the "Makefile" in the DBMirror directory.
5657

57-
The Makefile supplied assumes that the postgres include files are in
58-
/usr/local/pgsql/include/server.
58+
Postgres-7.3 Make Instructions:
59+
60+
If you have already run "configure" in the pgsql-server directory
61+
then run "make" in the dbmirror directory to compile the trigger.
62+
63+
Postgres-7.1 & Postgres-7.2 Make Instructions:
64+
65+
The included Makefile is not compatible with postgres 7.1 and 7.2
66+
The trigger will need to be built by hand.
67+
68+
Run the following commands
69+
70+
gcc -fpic -I/usr/local/pgsql/include/server -c pending.c -DNOSCHEMAS
71+
ld -shared -o pending.so pending.o
72+
73+
Assuming the postgres include files are in /usr/local/pgsql/include/server.
5974

6075
Postgres-7.1.x installations should change this to
6176
/usr/local/pgsql/include (The server part is for 7.2+)
6277

6378
If you have installed the postgres include files to another location then
64-
modify the Makefile to reflect this.
79+
modify the include path to reflect this.
80+
81+
Compiling the trigger by hand requires that all postgres headers be installed
82+
,this is accomplished in postgresql(7.1 or 7.2) by running
83+
"make install-all-headers" in the postgres source directory.
6584

66-
The trigger requires that all postgres headers be installed, this is
67-
accomplished in postgresql(7.1 or 7.2) by running "make install-all-headers"
68-
in the postgres source directory.
6985

70-
The Makefile should create a file named pending.so that contains the trigger.
86+
You should now have a file named pending.so that contains the trigger.
7187

7288
Install this file in /usr/local/pgsql/lib (or another suitable location).
7389

@@ -93,6 +109,15 @@ To execute the script use psql as follows
93109
where MyDatabaseName is the name of the database you wish to install mirroring
94110
on(Your master).
95111

112+
Postgres-7.1 and 7.2 Notes:
113+
-The syntax for creating a trigger function changed in Postgres-7.3.
114+
Change the line in MirrorSetup.sql from
115+
116+
CREATE FUNCTION "recordchange" () RETURNS trigger AS
117+
118+
to
119+
CREATE FUNCTION "recordchange" () RETURNS OPAQUE AS
120+
96121

97122
3) Create slaveDatabase.conf files.
98123

@@ -199,6 +224,7 @@ RedHat Linux 7.1 & 6.2
199224

200225
Mandrake Linux 8.0(Limited Testing)
201226
-Postgres 7.2
227+
-Postgres 7.3
202228
-Perl 5.6
203229

204230

contrib/dbmirror/pending.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/****************************************************************************
22
* pending.c
3-
* $Id: pending.c,v 1.5 2002/09/26 05:24:30 momjian Exp $
3+
* $Id: pending.c,v 1.6 2002/10/19 02:16:40 momjian Exp $
44
*
55
* This file contains a trigger for Postgresql-7.x to record changes to tables
66
* to a pending table for mirroring.
@@ -22,7 +22,7 @@
2222

2323
#include <executor/spi.h>
2424
#include <commands/trigger.h>
25-
25+
#include <utils/lsyscache.h>
2626
enum FieldUsage
2727
{
2828
PRIMARY = 0, NONPRIMARY, ALL, NUM_FIELDUSAGE
@@ -46,7 +46,7 @@ char *packageData(HeapTuple tTupleData, TupleDesc tTupleDecs,
4646

4747
#define BUFFER_SIZE 256
4848
#define MAX_OID_LEN 10
49-
49+
#define DEBUG_OUTPUT
5050

5151
extern Datum recordchange(PG_FUNCTION_ARGS);
5252

@@ -69,7 +69,8 @@ recordchange(PG_FUNCTION_ARGS)
6969
HeapTuple retTuple = NULL;
7070
char *tblname;
7171
char op = 0;
72-
72+
char *schemaname;
73+
char *fullyqualtblname;
7374
if (fcinfo->context != NULL)
7475
{
7576

@@ -81,6 +82,16 @@ recordchange(PG_FUNCTION_ARGS)
8182
trigdata = (TriggerData *) fcinfo->context;
8283
/* Extract the table name */
8384
tblname = SPI_getrelname(trigdata->tg_relation);
85+
#ifndef NOSCHEMAS
86+
schemaname = get_namespace_name(RelationGetNamespace(trigdata->tg_relation));
87+
fullyqualtblname = SPI_palloc(strlen(tblname) +
88+
strlen(schemaname) + 4);
89+
sprintf(fullyqualtblname,"\"%s\".\"%s\"",
90+
schemaname,tblname);
91+
#else
92+
fullyqualtblname = SPI_palloc(strlen(tblname+3));
93+
sprintf(fullyqualtblname,"\"%s\"",tblname);
94+
#endif
8495
tupdesc = trigdata->tg_relation->rd_att;
8596
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
8697
{
@@ -103,7 +114,7 @@ recordchange(PG_FUNCTION_ARGS)
103114
op = 'd';
104115
}
105116

106-
if (storePending(tblname, beforeTuple, afterTuple, tupdesc, trigdata, op))
117+
if (storePending(fullyqualtblname, beforeTuple, afterTuple, tupdesc, trigdata, op))
107118
{
108119
/* An error occoured. Skip the operation. */
109120
elog(ERROR, "Operation could not be mirrored");
@@ -113,6 +124,7 @@ recordchange(PG_FUNCTION_ARGS)
113124
#if defined DEBUG_OUTPUT
114125
elog(NOTICE, "Returning on success");
115126
#endif
127+
SPI_pfree(fullyqualtblname);
116128
SPI_finish();
117129
return PointerGetDatum(retTuple);
118130
}
@@ -417,7 +429,7 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc,
417429
#if defined DEBUG_OUTPUT
418430
elog(NOTICE, cpFieldName);
419431
#endif
420-
while (iDataBlockSize - iUsedDataBlock < strlen(cpFieldName) + 4)
432+
while (iDataBlockSize - iUsedDataBlock < strlen(cpFieldName) + 6)
421433
{
422434
cpDataBlock = SPI_repalloc(cpDataBlock, iDataBlockSize + BUFFER_SIZE);
423435
iDataBlockSize = iDataBlockSize + BUFFER_SIZE;
@@ -436,7 +448,7 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc,
436448
}
437449
else
438450
{
439-
*cpFormatedPtr = ' ';
451+
sprintf(cpFormatedPtr," ");
440452
iUsedDataBlock++;
441453
cpFormatedPtr++;
442454
continue;
@@ -484,7 +496,8 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc,
484496
if (tpPKeys != NULL)
485497
SPI_pfree(tpPKeys);
486498
#if defined DEBUG_OUTPUT
487-
elog(NOTICE, "Returning");
499+
elog(NOTICE, "Returning: DataBlockSize:%d iUsedDataBlock:%d",iDataBlockSize,
500+
iUsedDataBlock);
488501
#endif
489502
memset(cpDataBlock + iUsedDataBlock, 0, iDataBlockSize - iUsedDataBlock);
490503

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy