PERFORM Discards Rows FOUND Reports Success
PL/pgSQL cannot run a bare SELECT, so PERFORM executes and discards the rows; FOUND then tells you whether the discarded query matched anything.
Code
CREATE TABLE job_queue (state text);
CREATE TABLE config_defaults (key text, value text);
INSERT INTO job_queue (state) VALUES ('pending'), ('done');
CREATE OR REPLACE FUNCTION has_pending_jobs() RETURNS boolean
LANGUAGE plpgsql AS $$
BEGIN
-- PL/pgSQL 里裸 SELECT 会报错:只在乎"有没有行"时用 PERFORM 丢弃结果
PERFORM 1 FROM job_queue WHERE state = 'pending';
-- PERFORM 产出至少一行则 FOUND 为 true,行本身被丢弃
RETURN FOUND;
END $$;
CREATE OR REPLACE FUNCTION ensure_probe_row() RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
-- 常见惯用法:探测行是否存在,缺席才补种
PERFORM 1 FROM config_defaults WHERE key = 'retention_days';
IF NOT FOUND THEN
INSERT INTO config_defaults (key, value) VALUES ('retention_days', '30');
RAISE NOTICE 'seeded retention_days';
ELSE
RAISE NOTICE 'retention_days already present';
END IF;
END $$;
SELECT has_pending_jobs();
SELECT ensure_probe_row();
SELECT ensure_probe_row();How the snippet works
The file runs SELECT / INSERT / CREATE statements, touching job_queue and config_defaults.
What the statements actually assemble: a WHERE clause filtering the rows one statement applies to.
Applying it
The job_queue table is the working surface here.
Statements that matter
CREATE OR REPLACE FUNCTION has_pending_jobs() RETURNS boolean
LANGUAGE plpgsql AS $$
BEGIN
-- PL/pgSQL 里裸 SELECT 会报错:只在乎"有没有行"时用 PERFORM 丢弃结果
PERFORM 1 FROM job_queue WHERE state = 'pending';
-- PERFORM 产出至少一行则 FOUND 为 true,行本身被丢弃
RETURN FOUND;
END $$;
INSERT INTO job_queue (state) VALUES ('pending'), ('done');
CREATE TABLE config_defaults (key text, value text);
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- What relations does the SQL touch?
- `job_queue`, `config_defaults`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.