Skip to content

Commit

Permalink
Don't rev the seq unnecessarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauzo committed Dec 2, 2011
1 parent 4803dff commit 515573e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions btree.sql
Expand Up @@ -274,7 +274,6 @@ CREATE FUNCTION insert(
BEGIN
m := btmeta();
seq := pg_get_serial_sequence('btree', 'id');
k := coalesce(val, nextval(seq));

IF before IS NULL THEN
n := last_child(m.root)::btree_x;
Expand All @@ -283,10 +282,11 @@ CREATE FUNCTION insert(
n := btfind(before, isk);

IF n.id IS NULL THEN
RAISE 'not in btree: % (%)', before, isk;
RAISE 'not in btree: %', before;
END IF;
END IF;

k := coalesce(val, nextval(seq));
RAISE NOTICE 'inserting % into % at %', k, n.id, n.ix;

n.ks := n.ks[1:n.ix-1] || k || n.ks[n.ix:n.nk];
Expand Down
29 changes: 28 additions & 1 deletion t/btree.pg
Expand Up @@ -3,7 +3,6 @@ BEGIN;

CREATE SCHEMA t;
SET search_path TO t, tap, ordered1;
SELECT plan(7);

CREATE FUNCTION perform (anyelement) RETURNS void
LANGUAGE sql VOLATILE
Expand Down Expand Up @@ -39,6 +38,28 @@ CREATE FUNCTION root (integer, integer) RETURNS text
SELECT intrp('(0, array[%, %]::integer[], null),', $1, $2);
$fn$;

CREATE FUNCTION notthere (text, integer, text) RETURNS SETOF text
LANGUAGE plpgsql
AS $fn$
DECLARE
s bigint;
BEGIN
s := currval('btree_id_seq');

RETURN NEXT throws_ok(
intrp('SELECT % (%)', $1, $2::text),
'P0001', intrp('not in btree: %', $2),
intrp('% (%) throws correct exception', $1, $3)
);
RETURN NEXT is(
currval('btree_id_seq'), s,
intrp('% (%) doesn''t inc sequence', $1, $3)
);
RETURN;
END;
$fn$;

SELECT plan(15);
SELECT reset_btree(8);

SELECT btree_is(root(1, 8) || $$
Expand All @@ -63,6 +84,10 @@ SAVEPOINT t;
(1, '{4,2,3}', true)
$$, 'insert(i) inserts before i');

SELECT notthere('insert', 5, 'not there');
SELECT notthere('insert', 1, 'root id');
SELECT notthere('insert', 8, 'root fill');

SELECT perform(insert(null)) FROM generate_series(1,4);
SELECT btree_is(root(1, 8) || $$
(1, '{4,2,3,5,6,7,8}', true)
Expand All @@ -75,6 +100,8 @@ SAVEPOINT t;
(11, '{10,1}', false)
$$, '8 items will split');

SELECT notthere('insert', 10, 'non-root id');

ROLLBACK TO SAVEPOINT t;

ROLLBACK;

0 comments on commit 515573e

Please sign in to comment.