{"id":482,"date":"2025-08-21T17:31:44","date_gmt":"2025-08-21T17:31:44","guid":{"rendered":"http:\/\/77interactive.com\/?p=482"},"modified":"2025-08-24T03:22:53","modified_gmt":"2025-08-24T03:22:53","slug":"postgresql-database-loop","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=482","title":{"rendered":"Loop over PostgreSQL Tables"},"content":{"rendered":"\n<p>The following script loops over some PostgreSQL tables and grabs row counts<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 1) Where results will go\nCREATE TEMP TABLE tmp_table_stats (\n  schema_name      text,\n  table_name       text,\n  row_count        bigint,\n  lastupdated_max  timestamptz,\n  count_method     text  -- 'exact' (this script) or 'estimate' (alternative below)\n);\n\n-- 2) Gather stats for all non-system tables (exact counts)\nDO $$\nDECLARE\n  r           RECORD;\n  colname     text;\n  cnt         bigint;\n  max_ts      timestamptz;\nBEGIN\n  FOR r IN\n    SELECT\n      n.nspname AS schema_name,\n      c.relname AS table_name,\n      c.oid     AS relid,\n      quote_ident(n.nspname) AS qi_schema,\n      quote_ident(c.relname) AS qi_table\n    FROM pg_class c\n    JOIN pg_namespace n ON n.oid = c.relnamespace\n    WHERE c.relkind = 'r'                    -- ordinary tables\n      AND n.nspname NOT IN ('pg_catalog','information_schema','pg_toast')\n    ORDER BY 1, 2\n  LOOP\n    -- exact row count\n    EXECUTE format('SELECT count(*) FROM %s.%s', r.qi_schema, r.qi_table)\n      INTO cnt;\n\n    -- find a column named lastUpdated \/ lastupdated (quoted vs unquoted)\n    SELECT a.attname\n      INTO colname\n    FROM pg_attribute a\n    WHERE a.attrelid = r.relid\n      AND NOT a.attisdropped\n      AND lower(a.attname) = 'lastupdated'\n    LIMIT 1;\n\n    IF colname IS NOT NULL THEN\n      EXECUTE format('SELECT max(%s) FROM %s.%s',\n                     quote_ident(colname), r.qi_schema, r.qi_table)\n        INTO max_ts;\n    ELSE\n      max_ts := NULL;\n    END IF;\n\n    INSERT INTO tmp_table_stats(schema_name, table_name, row_count, lastupdated_max, count_method)\n    VALUES (r.schema_name, r.table_name, cnt, max_ts, 'exact');\n  END LOOP;\nEND\n$$;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following script loops over some PostgreSQL tables and grabs row counts<\/p>\n","protected":false},"author":1,"featured_media":483,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[17,18],"class_list":["post-482","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql","tag-postgres","tag-postgresql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Loop over PostgreSQL Tables - 77 Interactive<\/title>\n<meta name=\"description\" content=\"Learn how to loop over tables in PostgreSQL. Obtain key information inside of a loop and save in a temp table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/77interactive.com\/?p=482\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loop over PostgreSQL Tables - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"Learn how to loop over tables in PostgreSQL. Obtain key information inside of a loop and save in a temp table.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=482\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-21T17:31:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-24T03:22:53+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rudy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=482#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=482\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Loop over PostgreSQL Tables\",\"datePublished\":\"2025-08-21T17:31:44+00:00\",\"dateModified\":\"2025-08-24T03:22:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=482\"},\"wordCount\":16,\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=482#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg\",\"keywords\":[\"postgres\",\"postgresql\"],\"articleSection\":[\"PostgreSQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=482\",\"url\":\"http:\/\/77interactive.com\/?p=482\",\"name\":\"Loop over PostgreSQL Tables - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=482#primaryimage\"},\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=482#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg\",\"datePublished\":\"2025-08-21T17:31:44+00:00\",\"dateModified\":\"2025-08-24T03:22:53+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"description\":\"Learn how to loop over tables in PostgreSQL. Obtain key information inside of a loop and save in a temp table.\",\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=482#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=482\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/?p=482#primaryimage\",\"url\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg\",\"contentUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg\",\"width\":1024,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=482#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Loop over PostgreSQL Tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/77interactive.com\/#website\",\"url\":\"http:\/\/77interactive.com\/\",\"name\":\"77 Interactive\",\"description\":\"Rudy&#039;s Code snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/77interactive.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\",\"name\":\"Rudy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"caption\":\"Rudy\"},\"url\":\"http:\/\/77interactive.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loop over PostgreSQL Tables - 77 Interactive","description":"Learn how to loop over tables in PostgreSQL. Obtain key information inside of a loop and save in a temp table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/77interactive.com\/?p=482","og_locale":"en_US","og_type":"article","og_title":"Loop over PostgreSQL Tables - 77 Interactive","og_description":"Learn how to loop over tables in PostgreSQL. Obtain key information inside of a loop and save in a temp table.","og_url":"http:\/\/77interactive.com\/?p=482","og_site_name":"77 Interactive","article_published_time":"2025-08-21T17:31:44+00:00","article_modified_time":"2025-08-24T03:22:53+00:00","og_image":[{"width":1024,"height":1024,"url":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg","type":"image\/jpeg"}],"author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=482#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=482"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Loop over PostgreSQL Tables","datePublished":"2025-08-21T17:31:44+00:00","dateModified":"2025-08-24T03:22:53+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=482"},"wordCount":16,"image":{"@id":"http:\/\/77interactive.com\/?p=482#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg","keywords":["postgres","postgresql"],"articleSection":["PostgreSQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=482","url":"http:\/\/77interactive.com\/?p=482","name":"Loop over PostgreSQL Tables - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"primaryImageOfPage":{"@id":"http:\/\/77interactive.com\/?p=482#primaryimage"},"image":{"@id":"http:\/\/77interactive.com\/?p=482#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg","datePublished":"2025-08-21T17:31:44+00:00","dateModified":"2025-08-24T03:22:53+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"description":"Learn how to loop over tables in PostgreSQL. Obtain key information inside of a loop and save in a temp table.","breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=482#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=482"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/?p=482#primaryimage","url":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg","contentUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/08\/DB_Loop.jpg","width":1024,"height":1024},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=482#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Loop over PostgreSQL Tables"}]},{"@type":"WebSite","@id":"http:\/\/77interactive.com\/#website","url":"http:\/\/77interactive.com\/","name":"77 Interactive","description":"Rudy&#039;s Code snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/77interactive.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9","name":"Rudy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","caption":"Rudy"},"url":"http:\/\/77interactive.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=482"}],"version-history":[{"count":1,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":484,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions\/484"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/media\/483"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}