Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pEwOE-0006Km-DG for pgsql-performance@arkaria.postgresql.org; Mon, 09 Jan 2023 17:56:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pEwOD-00027f-As for pgsql-performance@arkaria.postgresql.org; Mon, 09 Jan 2023 17:56:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pEwOC-00027V-Tm for pgsql-performance@lists.postgresql.org; Mon, 09 Jan 2023 17:56:53 +0000 Received: from mail-io1-xd31.google.com ([2607:f8b0:4864:20::d31]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pEwO9-0006TT-O4 for pgsql-performance@postgresql.org; Mon, 09 Jan 2023 17:56:51 +0000 Received: by mail-io1-xd31.google.com with SMTP id v65so618775ioe.4 for ; Mon, 09 Jan 2023 09:56:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=IC1LIyQKSR4pkNfS4/eC9crgLSxuBDxfqk/ZywDzls4=; b=6V/4GwLYFnbt4563c4ktZ9OEgkC3ubl1U6M2Ad9OYfapqkcA8O1BEo3w8Huoyq+uty CWcxif0z0OexvqTdeCV8ml3ltYcp+ZVdwpvJP95CQNDEGgfLCzmZ251nvGRKbP4ls7a/ u1zPBKNfgiaoBAVuqVfGs3B1j0V9zESt71pDNgYmNodlLtgxtabEMz6cDh4N87JzXqRB spI+rMfSEDwms79EWgiX+tU5kVR+9GA1jELDl+OWTpLzsGRRMaG5AYCvzJ8JEuXdbJga rSd1SR1wBdxNPrEyVmpwA25NHbk4MtSfdi1KdGIj8MQoLc369f/S7RRgo3FxI4XaBR4G VVHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IC1LIyQKSR4pkNfS4/eC9crgLSxuBDxfqk/ZywDzls4=; b=lK4AfpkXTVLNpXNH+4qDMwKOKw4jxnpFFR63Op/R980wlqz72zsv+mA7cygjDHwxPk IpajZun+5N3YvAYLmBgb5+e50cllxIr2OEdRKETxorT9oyn9GQk9XWg5rEEcoafSJVei kVXhyT+Ddl6MCmrHLlwbafYzePtQR3G6mUb5z7kST/s3e0FV5KBw92XPxG1bqdCCYrdb LRT1hgmpI+HxzjQd1G/fQ2wCGPYJBoXY8j2G7yADKa4nkW68sKh4oVogmurSy9Us3+Tw DPDx+ast/tgcO0qWiwVUepfgVDhkUAON1+bKJz7FZzP1sWt4A9BnUF7871Ktfdhhs15o A59A== X-Gm-Message-State: AFqh2kqNOL7PTdjTkDRozOdsT53sH9CT0R7GRbPHNUF24fjncBPMgyuW zJyfo7h95fZYKEkR1lSt7CToe7MJR1mrGX34 X-Google-Smtp-Source: AMrXdXuFgfSZSgKGZ8/mXjdvXe+Q6WltRs3yZam1YlKlYw1VamE7ENAvKgPB38g/z8hkx7SFy/YGBA== X-Received: by 2002:a5d:960d:0:b0:6ec:9bf1:3050 with SMTP id w13-20020a5d960d000000b006ec9bf13050mr37705354iol.0.1673287008821; Mon, 09 Jan 2023 09:56:48 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id d16-20020a6b6810000000b006de73a731dbsm3283910ioc.51.2023.01.09.09.56.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 09:56:48 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 5830C800C2F; Mon, 9 Jan 2023 11:56:47 -0600 (CST) Date: Mon, 9 Jan 2023 11:56:47 -0600 From: Justin Pryzby To: spiral Cc: pgsql-performance@postgresql.org Subject: Re: Advice on best way to store a large amount of data in postgresql Message-ID: <20230109175647.GB9837@telsasoft.com> References: <20230108070201.39b19d30@xps> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230108070201.39b19d30@xps> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Jan 08, 2023 at 07:02:01AM -0500, spiral wrote: > This table is used essentially as a key-value store; rows are accessed > only with `mid` primary key. Additionally, inserted rows may only be > deleted, but never updated. > > We only run the following queries: > - INSERT INTO messages VALUES (...data...); > - SELECT * FROM messages WHERE mid = $1; > - DELETE FROM messages WHERE mid = $1; > - DELETE FROM messages WHERE mid IN ($1...$n); > - SELECT count(*) FROM messages; Great - it's good to start with the queries to optimize. Are you using the extended query protocol with "bind" parameters, or are they escaped and substituted by the client library ? > So, the problem: I don't know how to best store this data in > postgres, or what system requirements would be needed. > Originally, this table did not contain a substantial amount of data, > and so I stored it in the same database as our CRUD user data. However, > as the table became larger, cache was being allocated to (mostly > unused) historical data from the `messages` table, and I decided to > move the large table to its own postgres instance. > > At the same time, I partitioned the table, with TimescaleDB's automatic > time-series partitioning, because our data is essentially time-series > (`mid` values are Twitter-style snowflakes) and it was said that > partitioning would improve performance. > This ended up being a mistake... shared_buffers memory usage went way > up, from the 20GB of the previous combined database to 28GB for just > the messages database, and trying to lower shared_buffers at all made > the database start throwing "out of shared memory" errors when running > DELETE queries. A TimescaleDB update did improve this, but 28GB is way > more memory than I can afford to allocate to this database - instead of > "out of shared memory", it gets OOM killed by the system. Can you avoid using DELETE and instead use DROP ? I mean, can you arrange your partitioning such that the things to be dropped are all in one partition, to handle in bulk ? That's one of the main reasons for using partitioning. (Or, as a worse option, if you need to use DELETE, can you change the query to DELETE one MID at a time, and loop over MIDs?) What version of postgres is it ? Ah, I found that you reported the same thing at least one other place. (It'd be useful to include here that information as well as the prior discussion with other product/vendor). https://github.com/timescale/timescaledb/issues/5075 In this other issue report, you said that you increased max_locks_per_transaction. I suppose you need to increase it further, or decrease your chunk size. How many "partitions" do you have (actually, timescale uses inheritance) ? -- Justin