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 1jTZzG-0008AP-No for pgsql-admin@arkaria.postgresql.org; Tue, 28 Apr 2020 23:50:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jTZzF-0001uC-Fn for pgsql-admin@arkaria.postgresql.org; Tue, 28 Apr 2020 23:50:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jTZzF-0001u5-9G for pgsql-admin@lists.postgresql.org; Tue, 28 Apr 2020 23:50:01 +0000 Received: from ook.raf.org ([139.99.156.21]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jTZzC-0007kd-Nu for pgsql-admin@lists.postgresql.org; Tue, 28 Apr 2020 23:50:00 +0000 Received: from localhost (localhost [127.0.0.1]) by ook.raf.org (Postfix) with ESMTP id 4B9825DE37 for ; Wed, 29 Apr 2020 09:49:56 +1000 (AEST) X-Virus-Scanned: Debian amavisd-new at ook.raf.org Received: from ook.raf.org ([127.0.0.1]) by localhost (ook.raf.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TQwa02vlqNBM for ; Wed, 29 Apr 2020 09:49:56 +1000 (AEST) Received: by ook.raf.org (Postfix, from userid 1001) id 086B85DEB1; Wed, 29 Apr 2020 09:49:56 +1000 (AEST) Date: Wed, 29 Apr 2020 09:49:56 +1000 From: raf To: pgsql-admin@lists.postgresql.org Subject: Re: PostgreSQL CHARACTER VARYING vs CHARACTER VARYING (Length) Message-ID: <20200428234956.6jgfbusxrlsulasr@raf.org> References: <20200428234332.7nkyomr2unlez6p5@raf.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200428234332.7nkyomr2unlez6p5@raf.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk raf wrote: > Paul Carlucci wrote: > > > On Tue, Apr 28, 2020 at 5:22 AM Rajin Raj wrote: > > > > > Is there any impact of using the character varying without providing the > > > length while creating tables? > > > I have created two tables and inserted 1M records. But I don't see any > > > difference in pg_class. (size, relpage) > > > > > > create table test_1(name varchar); > > > create table test_2(name varchar(50)); > > > > > > insert into test_1 ... 10M records > > > insert into test_2 ... 10M records > > > > > > vacuum (full,analyze) db_size_test_1; > > > vacuum (full,analyze) db_size_test_2; > > > > > > Which option is recommended? > > > > > > *Regards,* > > > *Rajin * > > > > > PG the text, character varying, character varying(length), character column > > types are all the same thing with each column type inheriting the > > properties from the parent type. With each successive type further > > properties are added but they're all basically just "text" with some > > additional metadata. If you're coming from other database engines or just > > general programming languages where text and fixed length string fields are > > handled differently then the above can seem a bit different form what > > you're used to. Heck, I can think of one engine where if you have a text > > column you have to query the table for the blob identifier and then issue a > > separate call to retrieve it. Here in PG it's literally all the same, > > handled the same, performs the same. Use what limiters make sense for your > > application. > > My advice is to never impose arbitrary limits on text. > You will probably regret the choice of limit at some > point. I recently encountered people complaining that > they (thought they) needed to store 21 characters in > a field that they had limited to 10 characters (even > though they were originally told that the recipient > of the data would accept up to 40 characters). > > I just use "text" for everything. It's less typing. :-) > > The only good reason I can think of for limiting the > length would be to mitigate the risk of some kind of > denial of service, so a limit of 1KiB or 1MiB maybe. > But even that sounds silly. I've never done it (except > to limit CPU usage for slow password hashing but even > then, the 1KiB limit was imposed by input validation, > not by the database schema). Sorry, the above is misleading/a bad example. The hash stored in the database is a fixed reasonable length. It only varies according to the hashing scheme used. It's only the unhashed password (that isn't stored anywhere) that was limited by input validation to limit CPU usage. > cheers, > raf > > P.S. My aversion to arbitrary length limits applies to > postgres identifier names as well. I wish they weren't > limited to 63 characters. >