No, no (at least not since July 2007 ). I'm afraid you have to resort to:
BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT id, username, title, date FROM posts ORDER BY date DESC LIMIT 20; SELECT count(id, username, title, date) AS total FROM posts; END;
The isolation level must be SERIALIZABLE to ensure that the query does not see concurrent updates between SELECT statements.
Another option you have is to use a trigger to count rows, since they are INSERT ed or DELETE d. Suppose you have the following table:
CREATE TABLE posts ( id SERIAL PRIMARY KEY, poster TEXT, title TEXT, time TIMESTAMPTZ DEFAULT now() ); INSERT INTO posts (poster, title) VALUES ('Alice', 'Post 1'); INSERT INTO posts (poster, title) VALUES ('Bob', 'Post 2'); INSERT INTO posts (poster, title) VALUES ('Charlie', 'Post 3');
Then follow these steps to create a table called post_count that contains the current number of rows in posts :
-- Don't let any new posts be added while we're setting up the counter. BEGIN; LOCK TABLE posts; -- Create and initialize our post_count table. SELECT count(*) INTO TABLE post_count FROM posts; -- Create the trigger function. CREATE FUNCTION post_added_or_removed() RETURNS TRIGGER AS $$ BEGIN IF TG_OP = 'DELETE' THEN UPDATE post_count SET count = count - 1; ELSIF TG_OP = 'INSERT' THEN UPDATE post_count SET count = count + 1; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; -- Call the trigger function any time a row is inserted. CREATE TRIGGER post_added_or_removed_tgr AFTER INSERT OR DELETE ON posts FOR EACH ROW EXECUTE PROCEDURE post_added_or_removed(); COMMIT;
Note that this maintains the current number of all rows in posts . To save the current number of specific rows, you need to configure it:
SELECT count(*) INTO TABLE post_count FROM posts WHERE poster <> 'Bob'; CREATE FUNCTION post_added_or_removed() RETURNS TRIGGER AS $$ BEGIN