now(), IMMUTABLE (), :
, , ""...
( ) :
1. , :
CREATE INDEX queries_recent_idx ON queries_query (user_sid, created)
WHERE created > '2013-01-07 00:00'::timestamp;
, created timestamp. timestamp timestamptz (timestamp with time zone). timestamp timestamptz ( ) . . / :
, , cron ( - ). , , . .
, :
CREATE OR REPLACE FUNCTION f_index_recreate()
RETURNS void AS
$func$
BEGIN
DROP INDEX IF EXISTS queries_recent_idx;
EXECUTE format('
CREATE INDEX queries_recent_idx
ON queries_query (user_sid, created)
WHERE created > %L::timestamp'
, LOCALTIMESTAMP - interval '30 days');
END
$func$ LANGUAGE plpgsql;
:
SELECT f_index_recreate();
now() ( ) CURRENT_TIMESTAMP timestamptz. timestamp now()::timestamp LOCALTIMESTAMP.
Postgres 9.2 - 9.4.
SQL Fiddle.
, CREATE INDEX CONCURRENTLY. , :
... CREATE INDEX, CREATE INDEX CONCURRENTLY .
, :
CREATE INDEX CONCURRENTLY queries_recent_idx2 ON queries_query (user_sid, created)
WHERE created > '2013-01-07 00:00'::timestamp;
:
DROP INDEX CONCURRENTLY IF EXISTS queries_recent_idx;
:
ALTER INDEX queries_recent_idx2 RENAME TO queries_recent_idx;
2. ""
archived :
ALTER queries_query ADD COLUMN archived boolean NOT NULL DEFAULT FALSE;
UPDATE , " " , :
CREATE INDEX some_index_name ON queries_query (user_sid, created)
WHERE NOT archived;
( ), . EXPLAIN ANALYZE, - . , .
, UPDATE , , .
first ( ). , . .
, , .