-- the minimal table
CREATE TABLE IF NOT EXISTS teststd (n DOUBLE PRECISION NOT NULL);

-- code modularity with view, we could have used a common table expression instead
CREATE VIEW  vteststd AS
  SELECT COUNT(n) AS cnt,
  SUM(n) AS tsum,
  SUM(POWER(n,2)) AS tsqr
FROM teststd;

-- you can of course put this code into every query
CREATE OR REPLACE FUNCTION std_dev() RETURNS DOUBLE PRECISION AS $$
 SELECT SQRT(tsqr/cnt - (tsum/cnt)^2) FROM vteststd;
$$ LANGUAGE SQL;

-- test data is: 2,4,4,4,5,5,7,9
INSERT INTO teststd VALUES (2);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (4);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (4);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (4);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (5);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (5);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (7);
SELECT std_dev() AS std_deviation;
INSERT INTO teststd VALUES (9);
SELECT std_dev() AS std_deviation;
-- cleanup test data
DELETE FROM teststd;