pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
confounding, incorrect constraint error
3+ messages / 2 participants
[nested] [flat]

* confounding, incorrect constraint error
@ 2001-09-23 07:09  Kevin Way <kevin.way@overtone.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Kevin Way @ 2001-09-23 07:09 UTC (permalink / raw)
  To: pgsql-sql

I've hit some really evil nastiness that is either a Postgres 7.1.3 bug,
or signs of early-onset senility for me.  I was having trouble with my
database dying while inserting some values, and running some PL/pgSQL.

The schema is as listed below, and I'm getting 
psql:fuck.sql:175: ERROR:  ExecReplace: rejected due to CHECK constraint users_logged_in
while inserting values into the uservote table.  If I had a few columns to
the users table, postgres crashes instead of giving this (nonsensical)
error.

I'd greatly appreciate any insight, even if it involves a 2x4.

Below is a significantly simplified version of my schema, which exhibits
the above problem.

DROP RULE uservote_update_item_mod;
DROP RULE uservote_delete_item_dec;
DROP RULE uservote_insert_item_inc;

DROP RULE itemvote_update_item_mod;
DROP RULE itemvote_delete_item_dec;
DROP RULE itemvote_insert_item_inc;

DROP FUNCTION mod_node_vote_count(INT4, INT2, INT2);

DROP TABLE uservote;
DROP TABLE itemvote;

DROP TABLE item;
DROP TABLE users;
DROP TABLE node;

DROP SEQUENCE node_id_seq;

CREATE SEQUENCE node_id_seq;

CREATE TABLE node (
        node_id         INT4 UNIQUE NOT NULL DEFAULT nextval('node_id_seq'),
        name            TEXT NOT NULL,
        nays           INT4 NOT NULL DEFAULT 0
                            CHECK ( nays >= 0 ),
        yays           INT4 NOT NULL DEFAULT 0,
                            CHECK ( yays >= 0 ),
        rating          INT2 NOT NULL DEFAULT 50
                            CHECK ( rating >= 0 AND rating <= 100 ),
        PRIMARY KEY (node_id)
);
        
CREATE TABLE users (
        node_id         INT4 UNIQUE NOT NULL,
	email           TEXT NOT NULL,
	realname	TEXT NOT NULL,
	pass_hash	VARCHAR(32) NOT NULL,
        logged_in	INT2 NOT NULL DEFAULT 0 
                            CHECK (logged_in = 0 OR logged_in = 1)
) INHERITS (node);

CREATE TABLE item (
        node_id         INT4 UNIQUE NOT NULL,
	creator_id	INT4 NOT NULL
                            REFERENCES users (node_id)
                            ON DELETE CASCADE
                            ON UPDATE CASCADE,
        reason   	TEXT NOT NULL
) INHERITS (node);	

CREATE TABLE itemvote (
	vote_date	TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	target_id       INT4 NOT NULL
                            REFERENCES item (node_id) 
                            ON DELETE CASCADE
                            ON UPDATE CASCADE,
        user_id         INT4 NOT NULL
			    REFERENCES users (node_id)
			    ON DELETE CASCADE
  			    ON UPDATE CASCADE,
	nays		INT2 NOT NULL
                            CHECK (nays = 0 OR nays = 1),

	PRIMARY KEY (user_id, target_id)
);

CREATE TABLE uservote (
	vote_date	TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	target_id       INT4 NOT NULL
                            REFERENCES users (node_id) 
                            ON DELETE CASCADE
                            ON UPDATE CASCADE,
        user_id         INT4 NOT NULL
			    REFERENCES users (node_id)
			    ON DELETE CASCADE
  			    ON UPDATE CASCADE,
	nays		INT2 NOT NULL
                            CHECK (nays = 0 OR nays = 1),

	PRIMARY KEY (user_id, target_id)
);

-- modifies an items nays/yays count totals as appropriate
-- first arg: item number
-- second arg: 1 or 0, nays or yays.
-- third arg: 1 or 0, add a vote, or remove a vote
CREATE FUNCTION mod_node_vote_count (INT4, INT2, INT2) RETURNS INT2 AS '
    DECLARE
        node_num ALIAS for $1;
        nay_status ALIAS for $2;
        add ALIAS for $3;

        nay_tot INT4 NOT NULL DEFAULT 0;
        yay_tot INT4 NOT NULL DEFAULT 0;
    BEGIN
        IF add = 1
        THEN
            IF nay_status = 1
            THEN
                UPDATE node SET nays = nays + 1 WHERE node_id = node_num;
            ELSE
                UPDATE node SET yays = yays + 1 WHERE node_id = node_num;
            END IF;
        ELSE
            IF nay_status = 1
            THEN
                UPDATE node SET nays = nays - 1 WHERE node_id = node_num;
            ELSE
                UPDATE node SET yays = yays - 1 WHERE node_id = node_num;
            END IF;
        END IF;
        SELECT nays INTO nay_tot FROM node WHERE node_id = node_num;
        SELECT yays INTO yay_tot FROM node WHERE node_id = node_num;

        IF nay_tot + yay_tot != 0
        THEN
            UPDATE node SET rating = CEIL( (yay_tot * 100)/(yay_tot + nay_tot) ) WHERE node_id = node_num;
        ELSE
            UPDATE node SET rating = 50 WHERE node_id = node_num;
        END IF;

        RETURN 1;
    END;
' LANGUAGE 'plpgsql';

------------------------------------------------------------------------
-- vote totalling rules

-- vote insertion
CREATE RULE itemvote_insert_item_inc AS
    ON INSERT TO itemvote DO
        SELECT mod_node_vote_count(NEW.target_id, NEW.nays, 1);

CREATE RULE uservote_insert_item_inc AS
    ON INSERT TO uservote DO
        SELECT mod_node_vote_count(NEW.target_id, NEW.nays, 1);

-- vote deletion
CREATE RULE itemvote_delete_item_dec AS
    ON DELETE TO itemvote DO
        SELECT mod_node_vote_count(OLD.target_id, OLD.nays, 0);

CREATE RULE uservote_delete_item_dec AS
    ON DELETE TO uservote DO
        SELECT mod_node_vote_count(OLD.target_id, OLD.nays, 0);

-- vote updates
CREATE RULE itemvote_update_item_mod AS
    ON UPDATE TO itemvote WHERE OLD.nays != NEW.nays DO
        (SELECT mod_node_vote_count(OLD.target_id, OLD.nays, 1);
         SELECT mod_node_vote_count(NEW.target_id, NEW.nays, 0););

CREATE RULE uservote_update_item_mod AS
    ON UPDATE TO uservote WHERE OLD.nays != NEW.nays DO
        (SELECT mod_node_vote_count(OLD.target_id, OLD.nays, 1);
         SELECT mod_node_vote_count(NEW.target_id, NEW.nays, 0););

-- users
INSERT INTO users (name, pass_hash, realname, email) VALUES ('mosch', 'dafe001b7733b0f3236aa95e00f8ed88', 'Kevin', 'monica@whitehouse.gov');
INSERT INTO users (name,  pass_hash, realname, email) VALUES ('Wakko', 'c6ef90fcf92bf703c3cc79a679c192a3', 'Alex', 'wakko@bitey.net');

-- items
INSERT INTO item (name, creator_id, reason) VALUES ('slashdot.org', 2, 'Because it\'s a pile of turd.');
INSERT INTO item (name, creator_id, reason) VALUES ('Yahoo!', 2, 'Because it\'s ugly.');
INSERT INTO item (name, creator_id, reason) VALUES ('memepool', 1, 'Because it\'s phat phat phat phat phat.');
INSERT INTO item (name, creator_id, reason) VALUES ('blow!!??!!', 1, 'this record nays nays nays');

-- item votes
INSERT INTO itemvote (target_id, user_id, nays) VALUES (3, 1, 1);
INSERT INTO itemvote (target_id, user_id, nays) VALUES (4, 1, 0);
INSERT INTO itemvote (target_id, user_id, nays) VALUES (5, 2, 1);

-- user votes
INSERT INTO uservote (target_id, user_id, nays) VALUES (2, 1, 0);
INSERT INTO uservote (target_id, user_id, nays) VALUES (1, 2, 1);



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: confounding, incorrect constraint error
@ 2001-09-23 20:54  Kevin Way <kevin.way@overtone.org>
  parent: Kevin Way <kevin.way@overtone.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Kevin Way @ 2001-09-23 20:54 UTC (permalink / raw)
  To: Josh Berkus <josh@agliodbs.com>; +Cc: pgsql-sql

> > Below is a significantly simplified version of my schema, which
> > exhibits
> > the above problem.
> 
> Unfortunately, even a simplified version of your schema would take me
> some hours to understand.  As your rule-setting is quite complex, my
> first instinct would be to hunt for circular procedural logic in your
> rules.  Try to pursue, step by step, everything that happens from the
> moment you send the insert command to uservotes.  You may find that the
> logic cascades back to the beginning.  I've done this to myself on
> occasion, causing the DB to hang on a seemingly simple request.

I'm fairly certain that there's no circular procedural logic.

The errors can be turned on/off by turning on/off the uservote_ series
of rules, which are attached to the uservote table.  These rules call
mod_node_vote_count which only touches the node table.  There are no
rules or triggers associated with the node table, so there is no circular
logic there.

Additional strangeness is that the itemvote_ series of rules works perfectly
despite the fact that the only difference between uservote_ and itemvote_
rules is the table that triggers them, they both call the same procedure on
the nodes table.

My current thinking is that something is stomping on some memory, because 
you can vary the effect of the error from being an incorrectly failed CHECK
constraint, to crashing the database, by varying the number of columns in
the tables in question.

I'm unemployed at the moment and this is a pet project, so I can't offer
much in the way of financial compensation, but I'll start the bidding at
$50 donation in your name to your choice of the EFF, the Red Cross, or the
American Cancer Society, in return for a fix.  (If none of these charities
are acceptable, surely one can be found later that is acceptable to both
parties).

Again, I greatly appreciate any help, and I apologize that my test case is
still fairly sizeable, despite being about 10% the size of the original
code.

-Kevin Way

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: confounding, incorrect constraint error
@ 2001-09-24 18:12  Jan Wieck <JanWieck@Yahoo.com>
  parent: Kevin Way <kevin.way@overtone.org>
  0 siblings, 0 replies; 3+ messages in thread

From: Jan Wieck @ 2001-09-24 18:12 UTC (permalink / raw)
  To: Kevin Way <kevin.way@overtone.org>; +Cc: Josh Berkus <josh@agliodbs.com>; pgsql-sql

Kevin Way wrote:
> I'm unemployed at the moment and this is a pet project, so I can't offer
> much in the way of financial compensation, but I'll start the bidding at
> $50 donation in your name to your choice of the EFF, the Red Cross, or the
> American Cancer Society, in return for a fix.  (If none of these charities
> are acceptable, surely one can be found later that is acceptable to both
> parties).

    Sorry,  I missed the original post of the problem. If you can
    send it to me again and change your offer into donating blood
    at the Red Cross, I would take a look at it.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-09-24 18:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-09-23 07:09 confounding, incorrect constraint error Kevin Way <kevin.way@overtone.org>
2001-09-23 20:54 ` Kevin Way <kevin.way@overtone.org>
2001-09-24 18:12   ` Jan Wieck <JanWieck@Yahoo.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox