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 1rFv5y-00CmvJ-Sv for pgsql-admin@arkaria.postgresql.org; Wed, 20 Dec 2023 11:50:39 +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 1rFv5x-00C4uc-Hi for pgsql-admin@arkaria.postgresql.org; Wed, 20 Dec 2023 11:50:37 +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 1rFv5x-00C4uU-4x for pgsql-admin@lists.postgresql.org; Wed, 20 Dec 2023 11:50:37 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rFv5u-00BKHd-Ca for pgsql-admin@lists.postgresql.org; Wed, 20 Dec 2023 11:50:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description; bh=MyQ0VeTH8GyMaZ0BTmwbZZ80SLFGZxvbla72BhHZdFo=; b=haH++4Dk8enkcRrP5I9nGqdbtO povrDMX14YxLLXp5LAqTDxJv9+lllnz2ifp1gusFacxFbl8gzglxRXchNBXFwWLTtCQOY7fF8VgN3 ufhUNzQIFCENVzug4cJQ9fGr/U5Qyf5VCIJtC7XnpWKk2icBUHHg2s6kFup6Xr/FWGQg=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1rFv5r-002nev-1o; Wed, 20 Dec 2023 12:50:31 +0100 Date: Wed, 20 Dec 2023 12:50:31 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: "Dischner, Anton" Cc: Rajesh Kumar , Pgsql-admin Subject: Re: AW: Truncate data from whole cluster Message-ID: Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Dec 20, 2023 at 11:39:40AM +0000, Dischner, Anton wrote: > Hi all, > > if you ask Bard LLM do do this you get: > > #!/bin/bash > > # List all databases in the PostgreSQL instance databases=$(psql -t -c "SELECT datname FROM pg_database;") > > # Truncate table data for each database > for database in $databases; do > echo "Processing database '$database'..." > psql -d $database -c "SET FOREIGN_KEY_CHECKS=0;" > tables=$(psql -d $database -t -c "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public';") > for table in $tables; do > echo "Truncating table '$table' in database '$database'..." > psql -d $database -c "TRUNCATE TABLE $table;" > done > psql -d $database -c "SET FOREIGN_KEY_CHECKS=1;" > done > > This may be a good script to start. > You will definetly test this first on a test installation! No, it's not. it's full of errors, and hallucinations. couple of things that took me < 1 minute to notice: 1. will fail for database names that contain spaces in them 2. setting variable like psql -d ... -c "set .." - is pointless, the value will be active only for this session, but this session has just eneded 3. there is no "FOREIGN_KEY_CHECKS" setting in pg 4. will fail for table names with spaces 5. will ignore tables in non-public schemas 6. will fail for table names with UpperCaseLetters 7. for db with N tables (in public), it will call 101 psql connections to this database. if your db has many small tables it will be unreasonably slow depesz, cursing whatever "magic ai tool" people try to use as base for their scripts