Cascading deletes in PostgreSQL

前端 未结 7 769
醉梦人生
醉梦人生 2021-01-03 01:59

I have a database with a few dozen tables interlinked with foreign keys. Under normal circumstances, I want the default ON DELETE RESTRICT behavior for those co

相关标签:
7条回答
  • 2021-01-03 02:16

    TRUNCATE just removes the data from table and leaves the structure

    0 讨论(0)
  • 2021-01-03 02:17
    TRUNCATE table CASCADE;
    

    I'm a Postgres novice, so I'm not sure what the trade-off is for TRUNCATE vs. DROP.

    0 讨论(0)
  • 2021-01-03 02:24

    You may want to look into using schemas with PostgreSQL. I've done this in past projects to allow different groups of people or developers to have their own data. Then you can use your scripts to create multiple copies of your database for just such situations.

    0 讨论(0)
  • 2021-01-03 02:29

    @Tony: No, schemas can be useful, and indeed, we use them to partition data in our database. But I'm talking about trying to scrub sensitive data before letting a consultant have a copy of the db. I want that data gone.

    0 讨论(0)
  • 2021-01-03 02:29

    I don't think you'd need to process the dump file like that. Do a streaming dump/restore, and process that. Something like:

    createdb -h scratchserver scratchdb
    createdb -h scratchserver sanitizeddb
    
    pg_dump -h liveserver livedb --schema-only | psql -h scratchserver sanitizeddb
    pg_dump -h scratchserver sanitizeddb | sed -e "s/RESTRICT/CASCADE/" | psql -h scratchserver scratchdb
    
    pg_dump -h liveserver livedb --data-only | psql -h scratchserver scratchdb
    psql -h scrachserver scratchdb -f delete-sensitive.sql
    
    pg_dump -h scratchserver scratchdb --data-only | psql -h scratchserver sanitizeddb
    pg_dump -Fc -Z9 -h scratchserver sanitizedb > sanitizeddb.pgdump
    

    where you store all your DELETE sqls in delete-sensitive.sql. The sanitizeddb database/steps can be removed if you don't mind the consultant getting a db with CASCADE foreign keys instead of RESTRICT foreign keys.

    There might also be better ways depending on how often you need to do this, how big the database is, and what percentage of data is sensitive, but I can't think of a simpler way to do it once or twice for a reasonably sized database. You'd need a different database after all, so unless you already have a slony cluster, can't avoid the dump/restore cycle, which might be time consuming.

    0 讨论(0)
  • 2021-01-03 02:31

    You do not need to dump and restore. You should be able to just drop the constraint, rebuild it with cascade, do your deletes, drop it again, and the rebuild it with restrict.

    CREATE TABLE "header"
    (
      header_id serial NOT NULL,
      CONSTRAINT header_pkey PRIMARY KEY (header_id)
    );
    
    CREATE TABLE detail
    (
      header_id integer,
      stuff text,
      CONSTRAINT detail_header_id_fkey FOREIGN KEY (header_id)
          REFERENCES "header" (header_id) MATCH SIMPLE
          ON UPDATE NO ACTION ON DELETE NO ACTION
    );
    insert into header values(1);
    insert into detail values(1,'stuff');
    delete from header where header_id=1;
    alter table detail drop constraint detail_header_id_fkey;
    alter table detail add constraint detail_header_id_fkey FOREIGN KEY (header_id)
          REFERENCES "header" (header_id) on delete cascade;
    delete from header where header_id=1;
    alter table detail add constraint detail_header_id_fkey FOREIGN KEY (header_id)
          REFERENCES "header" (header_id) on delete restrict;
    
    0 讨论(0)
提交回复
热议问题