其他
最佳 Postgres 扩展(2023 版)
shared_preload_libraries = 'pg_stat_statements'
SELECT query, total_time
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 10;
CREATE TABLE cities (
name TEXT,
location GEOMETRY(Point, 4326)
);
SELECT name, ST_Distance(location, ST_SetSRID(ST_MakePoint(-74.005941, 40.712784), 4326)) AS distance
FROM cities
ORDER BY location <-> ST_SetSRID(ST_MakePoint(-74.005941, 40.712784), 4326)
LIMIT 1;
CREATE DATABASE my_other_database;
CREATE USER MAPPING FOR current_user
SERVER my_other_database
OPTIONS (user 'postgres', password '');
CREATE SERVER my_other_database_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname 'my_other_database');
CREATE FOREIGN TABLE my_other_table (
id INTEGER,
name TEXT
)
SERVER my_other_database_server
OPTIONS (schema_name 'public', table_name 'my_table');
SELECT * FROM my_other_table WHERE id = 1;
SELECT uuid_generate_v5(uuid_ns_url(), 'example.com');
SELECT uuid_ns_create('example');
SELECT cron.schedule('0 0 * * ', 'INSERT INTO my_table SELECT FROM my_other_table');
SELECT cron.jobid, cron.expr, cron.command FROM cron.job;
select * from cron.job_run_details order by start_time desc limit 5;
CREATE TABLE sensor_data (
time TIMESTAMP NOT NULL,
value FLOAT NOT NULL
);
SELECT create_hypertable('sensor_data', 'time');
INSERT INTO sensor_data (time, value)
VALUES
('2023-07-01 00:00:00', 10.0),
('2023-07-01 01:00:00', 15.0),
('2023-07-01 02:00:00', 20.0);
SELECT time_bucket('1 hour', time) AS hour, AVG(value) AS avg_value
FROM sensor_data
GROUP BY hour;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;