"Nick Barr" <nick.barr@webbased.co.uk> writes:
> I personally did not like the look of 1 so I thought about using 2. The
> main reason being there would be no locks that would interfere with
> "updating" the view count because in fact this was just an INSERT
> statement.
INSERTs are good.
> Q2.Will a "group by" used with a "select . for update" lock all the rows
> used for the sum?
No; it won't work at all.
regression=# select hundred,count(*) from tenk1 group by hundred for update;
ERROR: SELECT FOR UPDATE is not allowed with GROUP BY clause
regression=#
However, AFAICS it will not matter if you are using a serializable
transaction. If two such transactions try to delete the same row,
one of them will error out, so you do not need FOR UPDATE.
regards, tom lane