From f7b2bb0d3d2718a9b14e632e5c202985eb84ac99 Mon Sep 17 00:00:00 2001 From: mj Date: Thu, 27 Sep 2001 19:51:38 +0000 Subject: [PATCH] Moved the creation of the index on comment from the dupetrigger script to the create_pg.sql script. Handle SPI_connect==SPI_ERROR_CONNECT in dupetrigger.c smarter. --- dbscripts/create_pg.sql | 2 ++ dbscripts/dupetrigger/dupetrigger.c | 16 ++++++++-------- dbscripts/dupetrigger/postgresql.sript | 1 - 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/dbscripts/create_pg.sql b/dbscripts/create_pg.sql index 57a7ff4b..56cd1687 100755 --- a/dbscripts/create_pg.sql +++ b/dbscripts/create_pg.sql @@ -296,6 +296,8 @@ CREATE TABLE "comment" ( Constraint "comment_pkey" Primary Key ("id") ); +CREATE INDEX comment_checksum_index ON comment (checksum); + -- -- TOC Entry ID 27 (OID 29196) -- diff --git a/dbscripts/dupetrigger/dupetrigger.c b/dbscripts/dupetrigger/dupetrigger.c index 66f61e18..11315c6f 100755 --- a/dbscripts/dupetrigger/dupetrigger.c +++ b/dbscripts/dupetrigger/dupetrigger.c @@ -133,9 +133,10 @@ Datum dupecheck(PG_FUNCTION_ARGS) tupdesc = trigdata->tg_relation->rd_att; /* Connect to SPI manager */ - if ((ret = SPI_connect()) < 0) - ret = SPI_connect(); - elog(NOTICE, "dupecheck: SPI_connect returned %d", ret); + if ((ret = SPI_connect()) != SPI_OK_CONNECT) + { + elog(NOTICE, "dupecheck: SPI_connect returned error %d", ret); + } // Now we are connected to the database's SPI manager // We will now construct a string of some important row values. @@ -151,9 +152,8 @@ Datum dupecheck(PG_FUNCTION_ARGS) { rowstrlen += strlen(items[i]); } - rowstrlen++; // add space for 0-terminator - rowstring = malloc(rowstrlen); + rowstring = malloc(rowstrlen+1); // add space for 0-terminator if (rowstring == NULL) { // Big problem. @@ -170,7 +170,7 @@ Datum dupecheck(PG_FUNCTION_ARGS) // items of the table record. Now we calculate the CRC-32 checksum // of the rowstring - crc = crc32(rowstring, rowstrlen - 1); + crc = crc32(rowstring, rowstrlen); // Now we allocate some space and construct the SQL query query = malloc(47 + 11 + 2 + 1); // SELECT part + crc32 + "';" + 0-term @@ -186,7 +186,7 @@ Datum dupecheck(PG_FUNCTION_ARGS) if ((ret == SPI_OK_SELECT) && (num > 0)) { - elog(NOTICE, "dupecheck: UBD prevented (dupe dropped)"); + elog(NOTICE, "dupecheck: UBD detected, dupe dropped"); return PointerGetDatum(NULL); } else @@ -197,7 +197,7 @@ Datum dupecheck(PG_FUNCTION_ARGS) Datum value; char nulls = 0; - elog(NOTICE, "dupecheck: Adding checksum to INSERT"); +// elog(NOTICE, "dupecheck: Adding checksum to INSERT"); attnum = SPI_fnumber(tupdesc, "checksum"); value = (Datum) crc; rettuple = SPI_modifytuple(trigdata->tg_relation, rettuple, 1, &attnum, &value, &nulls); diff --git a/dbscripts/dupetrigger/postgresql.sript b/dbscripts/dupetrigger/postgresql.sript index 28e9027c..69b3d361 100755 --- a/dbscripts/dupetrigger/postgresql.sript +++ b/dbscripts/dupetrigger/postgresql.sript @@ -3,4 +3,3 @@ DROP FUNCTION dupecheck(); DROP TRIGGER dupetrigger ON comment; CREATE FUNCTION dupecheck() RETURNS OPAQUE AS '/path/to/dupetrigger.so' LANGUAGE 'C'; CREATE TRIGGER dupetrigger BEFORE INSERT OR UPDATE ON comment FOR EACH ROW EXECUTE PROCEDURE dupecheck(); -CREATE INDEX comment_checksum_index on comment (checksum); -- 2.11.0