Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1twYhv-00FYbv-7v for pgsql-general@arkaria.postgresql.org; Mon, 24 Mar 2025 03:42:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1twYht-00BTJI-UN for pgsql-general@arkaria.postgresql.org; Mon, 24 Mar 2025 03:42:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1twYht-00BTHe-J1 for pgsql-general@lists.postgresql.org; Mon, 24 Mar 2025 03:42:33 +0000 Received: from mail-io1-xd43.google.com ([2607:f8b0:4864:20::d43]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1twYhq-000miS-39 for pgsql-general@lists.postgresql.org; Mon, 24 Mar 2025 03:42:32 +0000 Received: by mail-io1-xd43.google.com with SMTP id ca18e2360f4ac-85b44094782so107200739f.3 for ; Sun, 23 Mar 2025 20:42:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742787750; x=1743392550; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=+6jyXelnfCsHNPRU3YYRKkXWrsVZ7Gt1Bv5lfmAphPY=; b=E1ZtHoAvcex+IVHHcnwdXtmOtoYxbZf2r/QBSUKjO7p3TLh8iMf6dzejnB+sibjrOr vj2x9RPzJdhHkpTcsQHHLL3NHtbLnKvS0UMhmWIwpNtjSiWQR293F6mprDCR8Prgjb7v 5JQU9Tzj+h7DdhjhU71Tc3Gmb2/yCPQQejJN+p+HpioSfWfNuqDPcjHKR0QL8gE0YgJx Sw5dcwt44dxCvCv5HFijWAr3S3TfrRCU6MrdCTCiapVOkrLDRB1Zh6nGvsNjzoTC7YBz xvjhmZ3/GvYKW+KHXTcItuDx7pdCeLQGiE0dr9jDIS9TSZTvLruxhjQRM8tfKTCiJvBi i1BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742787750; x=1743392550; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+6jyXelnfCsHNPRU3YYRKkXWrsVZ7Gt1Bv5lfmAphPY=; b=pDja9iueiZIGleOmXHFzmcMy1wyBjhMXJq37CjoDqc80q4AaSek+G+f3GF0gObgcz8 3MxXpraDUuReKfXRGjzRxSIImfOT0VBsFGpDUSx7nZfbeaE6UP61lsjpaGJURXGEbxNF Qv069uWS5+ccx6g6JyP9Q9PhkWa/BghddCalkbwJq/P1iBlzB60VpqGb70nbFFoyZSVS oyGgmUgV4pnb8Z0nCuBHh/gnxmKJtne8VuVTuU9IzsHTuilDDpepvcltbzYrkYmwvtoN dXtPjan86DKELg640de0OimdJHvh6PO2oBdo8EAL8IELpluvbj4iDBCLO1Mdj8j24HTU +/EQ== X-Gm-Message-State: AOJu0Yw7Oxa9lVlOT6ElvzBcvfk64htJjaFavIuQ1jVk2MSwzJ+kr1p9 PisiBnNHskOYqKCqiWCXXRl8r9UIURGeGSppwed3JA52GqLo4tLsJ/FmGOkyJEa3POboGi7aGr+ KFnZlikMyJixu7ibcN0XIruHJQRn9iYmPpZ0= X-Gm-Gg: ASbGncsu/IV3eGmnoquZlFKvwA3sbEp7+AB1gt7kz1kKE5kUSrWZZ7kRCfGRpgE3loD Yc8cKTctQTYBS3LWZHJpZ3t2DUdRjXlj34QK0Ib+Zbeu/9yaJNF7SbGOpPmj3DZjVrXI9lBk0uo 0SrTf6xFKMJd38odMe0mOrLH0Njg== X-Google-Smtp-Source: AGHT+IH4i6XvHMsxI56MfIS1pAMLXJTb615xuJn1C5LOeIfQCjX2gTLNwi7qqJtWAWSEN7N7+/sZ6wFXpip4giHxoQg= X-Received: by 2002:a05:6e02:216f:b0:3d3:fbf9:194b with SMTP id e9e14a558f8ab-3d596055c48mr100983775ab.0.1742787749954; Sun, 23 Mar 2025 20:42:29 -0700 (PDT) MIME-Version: 1.0 From: Marcelo Fernandes Date: Mon, 24 Mar 2025 16:42:19 +1300 X-Gm-Features: AQ5f1JrLqSibp7cMtY02MGcNunUStT59wnhkToZNOItnI5TH5EnUIS3k6XXCOn4 Message-ID: Subject: Best way to check if a table is empty To: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi folks, I came up with three strategies to verify whether a table is empty. I wanted to sound the community to check whether my assumptions are correct for each of these strategies, and to also discuss which strategy is best. ## Strategy 1 [possibly best?] SELECT EXISTS (SELECT 1 FROM foo LIMIT 1); Pros: 1. Works with any table. 2. Relatively fast (if the table is well organised). Cons: 1. Sequential Scan 2. If the table is bloated, it reads more buffers. ## Strategy 2 SELECT min(id) FROM foo; Pros: 1. Does an index-only scan on a field that presumably has a PK index. 2. Works well even if the table is bloated. Cons: 1. Sequential Scan if the table does not have a PK index. 2. Reads a few more buffers than Strategy 1 when the table is well organised. 3. Performs worse if the index is bloated. ## Strategy 3 [worst] SELECT count(*) FROM foo; Pros: 1. Uses a widespread and intuitive operation (count) Cons: 1. Very slow on large tables as it performs a Sequential Scan. How does all of that sound? Are there further strategies I should consider? Anything I have missed in the Strategies above? Regards, Marcelo.