Moved the creation of the index on comment from the dupetrigger script to
authormj <mj>
Thu, 27 Sep 2001 19:51:38 +0000 (19:51 +0000)
committermj <mj>
Thu, 27 Sep 2001 19:51:38 +0000 (19:51 +0000)
the create_pg.sql script.

Handle SPI_connect==SPI_ERROR_CONNECT in dupetrigger.c smarter.

dbscripts/create_pg.sql
dbscripts/dupetrigger/dupetrigger.c
dbscripts/dupetrigger/postgresql.sript

index 57a7ff4..56cd168 100755 (executable)
@@ -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)
 --
index 66f61e1..11315c6 100755 (executable)
@@ -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);
index 28e9027..69b3d36 100755 (executable)
@@ -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);