Archive for March, 2010

Roasted Gnocchi with Butternut Squash and Cashews

Monday, March 15th, 2010

Actually, the title of this dish basically sums it up, but it sure is tasty. Here’s how to make it:

Cube a small butternut squash. Toss it in olive oil and coarse salt. Roast for 10 minutes, then add an equivalent volume of gnocchi and toss it all together. Roast another 10 minutes. Add and toss about a third as much broken raw cashew nuts as you used squash/gnocchi and a dash of balsamic vinegar, roast 10 minutes more.

It’s really exceptionally tasty. I had a large first helping and then completely gratuitous seconds.

I’d meant to add rosemary, but forgot. It doesn’t really need it, but it would definitely have been an improvement.

On the perils of PostgreSQL rules

Monday, March 15th, 2010

So, I had some PostgreSQL code using Rules that I’d written for a real system that was behaving in highly counter-intuitive ways. It was basically using rules to increment a counter when an insert would violate a uniqueness constraint.

I went into #postgresql to ask them for some guidance. After getting lot of responses of the form “The problem is that you’re using rules”, they eventually explained it in a manner that convinced me that it was the result of a genuine logic error on my part, not this silly blanket forbidding of rules they were suggesting: Essentially the rule would do the wrong thing when I inserted multiple uniqueness violations as part of the same insert, but in the context where this was happening I shouldn’t have been doing that anyway.

A few hours later I noticed that the data in the table was still dodgy: A lot of values were twice what they should have been. Eventually I boiled it down to the following example:

drop table if exists foo;
create table foo(id int, count int);

create or replace rule inserts_should_add_on_conflict as
on insert to foo
where exists(select 1 from foo where id = NEW.id)
do instead
update foo set count = count + NEW.count
where id = NEW.id;

insert into foo values(1, 1);

Now, what should be in the table at this point? The rule shouldn’t fire, right? It would only fire if there were something in the table which had the id being inserted. Right?

Well, not so much:

david=# select * from foo;
 id | count
----+-------
  1 |     2
(1 row)

After thinking about it for a bit and some help from #postgresql, this began to make sense, but it made sense in a way that convinced me they’d been right all along: Rules are an inherently confusing feature, and probably not to be trusted. See, here’s what the instead of rule does: It rewrites the insert into two queries, one insert with the guard negated and one update with the guard.

So, we first do the insert, which sees that the guard is not satisfied so that it’s ok to insert these values. You then end up with the row id=1, count=1 in the table. We then do the update, which sees that the guard *is* satisfied, because the insert has just made it so, and updates the row with id=1 to have count = count + 1.

So side effects which affect the constraints on the rules will always have deeply surprising effects like this, and should definitely be avoided at all costs. I’m not totally sold on whether this should be said of rules in general, but it’s starting to look that way to me.

Sequential compactness and the splitting number

Sunday, March 14th, 2010

In talking with people in #scala earlier I ended up looking through some of my old maths articles. In particular this one. I noticed that I mentioned “So far the only counterexampe I have depends on the value of the reaping number, tau. It’s fairly standard (I’ll write a post on it at some point) that {0, 1}^{tau} is not sequentially compact”.

The details of the above are wrong: Firstly, I meant the splitting number rather than the reaping number (a post I found elsewhere on the internet confirms I was confusing the two – I’m not even sure what the reaping number actually is). Secondly, “fairly standard” my ass. I couldn’t find anything on it, so had to recreate it from scratch. I mean, it’s not a new result and there have been papers on it, but it’s not by any means a commonly reproduced proof. I have no access to maths journals in order to check out the original papers, and hence there will be a deplorable lack of citations here. Sorry.

Still, four years later, here’s that post. I’ve written it up as a pdf rather than subjecting you to embedded LaTeX in the post.

Dark Lord

Saturday, March 13th, 2010

For thousands of years, the dark lord has ruled over the realm. Once every century the gods grant power to a chosen hero to challenge the dark lord and win our freedom.

At least, that’s how it’s used to be.

You see, the key there is “thousands of years” and “once every century”. That’s a good 20, 30 heroes who have challenged the dark lord and snuffed it. People were getting more than a little tired of the whole “generations of oppression by an immortal and unfathomable evil” thing. So about 40 years ago, a town official named Arin (better known to me as “dad”) had a bright idea. It ran sortof as follows: So, this dark lord. Pretty powerful, right? On the other hand… there are a lot of us. And we can build siege weapons.

Bloody warfare, much suffering, etc. Eventually confirmed that the fine print on “immortal” in “immortal and unfathomable evil” includes the phrase “As long as you don’t pack him in half a ton of black powder and set fire to it in a confined space”.

So, things are pretty good these days. It’s no utopia, but we’ve got a fairish system of government going, people are generally feeling less oppressed and life is looking up.

So you can imagine how surprised I was when a messenger of the gods arrived to tell me I was the chosen hero, to be gifted with the ability to inspire the hearts of men, near invincibility and awesome destructive power. And a cool sword.

I did try to tell him that there’d been a horrible cock-up and the dark lord was dead, but he was having none of it. Insisted he couldn’t possibly leave without granting me power. Oh well, if you’re going to twist my arm over it go on then.

So, here I am. Divine powers of leadership and warfare, and no dark lord to challenge me.

Muahahaha.

(This story is loosely stolen from an idea of Charles Stross’s)