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.

SqlData Integrity

Code

snippet
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

FAQ

What relations does the SQL touch?
`job_queue`, `config_defaults`.