integer out of range and remaining disk space too small to convert id to bigint and other solutions

给你一囗甜甜゛ 提交于 2020-01-24 15:04:05

问题


When I insert I am getting integer out of range because my id/primary key was mistakenly created as an int instead of a bigint or bigserial. I tried:

ALTER TABLE tbl ALTER COLUMN id TYPE BIGINT;

But I get the following error because my free disk space isn't big enough.

ERROR: could not extend file "base/16401/3275205": No space left on device HINT: Check free disk space. SQL state: 53100

I can't increase the disk space right now, for frustrating reasons I won't go into.

I also tried reusing the ids (I delete a lot of records from this table so there are big gaps) by doing these to start my seq over: https://dba.stackexchange.com/questions/111823/compacting-a-sequence-in-postgresql

But for solution #1 in that link: I assume I don't have the disk space. The table is 117GB and I have about 24GB available in ...data/base. I do have 150GB available where my temp files are being stored (a different mount), which is not the default configuration, but was done so I could conserve space for database storage in ...data/base. If I could create the table in Temp file location, that might work, but I don't know how to do that.

for solution #2 in that link: When I get to the update part, I get this in pgAdmin4:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

However the query is still running when I run:

select pid,query,state,wait_event,* from pg_stat_activity where state <> 'idle'

And I get no server log for the update query that fails.

I eventually killed that update query thinking that it would fail eventually anyway. (I am running this one again and will let it run with html error above unless someone else has a better idea.)

for solution #3 in that link: I have 16GM of RAM, so not enough.

Next from here: How to reset sequence in postgres and fill id column with new data?

I tried this:

UPDATE table SET id = DEFAULT;
ALTER SEQUENCE seq RESTART;
UPDATE table SET id = DEFAULT;

ERROR: integer out of range

this creates a duplicate key when you try to insert:

ALTER SEQUENCE seq RESTART WITH 1;
UPDATE t SET idcolumn=nextval('seq');

Anything else I can try?

PostgreSQL 9.6


回答1:


Scott Marlowe and Vao Tsun comments worked:

on (linux) server open a terminal

navigate to where want a the new namespace to be

make a directory: mkdir dirname

give ownership to postgres: chown postgres:postgres dirname

create table: CREATE TABLESPACE new_tbl_space LOCATION '/path/dirname'

put the table in the tablespace: alter table tbl set tablespace '/path/dirname'

do what was taking up so much disk space:ALTER TABLE tbl ALTER COLUMN id TYPE BIGINT;

change the tablespace back: alter table tbl set tablespace pg_default

remove the tablespace: I did that in pgadmin4 in the Tablespaces node/object

(That was from memory. Let me know if I missed something.)

Edit: This has the side effect of rewriting the entire table like a full vacuum freeing up any dead disk space.



来源:https://stackoverflow.com/questions/46671323/integer-out-of-range-and-remaining-disk-space-too-small-to-convert-id-to-bigint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!