Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbListFields incorrectly returns columns which have been removed #129

Open
warnes opened this issue Aug 7, 2023 · 0 comments · May be fixed by #130
Open

dbListFields incorrectly returns columns which have been removed #129

warnes opened this issue Aug 7, 2023 · 0 comments · May be fixed by #130

Comments

@warnes
Copy link

warnes commented Aug 7, 2023

Apparently postgresql doesn't actually remove 'dropped' columns, instead it renames them something like ........pg.dropped.23........, fills them with NULL and marks them as unused using the attisdroppped column of the pg_attribute table.

For example:

DROP TABLE IF EXISTS test_persons;

CREATE TABLE test_persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    State varchar(2),
    Dummy varchar(255)
);

INSERT INTO test_persons 
    (PersonID, LastName, FirstName, Address, City, State, Dummy)
VALUES
    (1, 'Sawyer', 'Tom',  'Apple Street', 'Riverside', 'MO', 'Dummy'),
    (2, 'Finn',   'Huck', 'Back Street', 'Riverside', 'MO', 'Dummy')
;    

ALTER TABLE test_persons
    DROP COLUMN Dummy;
    
SELECT
    a.attname,
    a.attisdropped
FROM
    pg_attribute a,
    pg_class c,
    pg_tables t,
    pg_namespace nsp
WHERE
    a.attrelid = c.oid
    AND c.relname = tablename
    AND c.relnamespace = nsp.oid
    AND a.attnum > 0
    AND nspname = CURRENT_SCHEMA()
    AND schemaname = nspname
    AND tablename = 'test_persons'
;

yeilds:

attname attisdropped
personid false
lastname false
firstname false
address false
city false
state false
........pg.dropped.7........ true

and consequently

dbListFields(conn, 'test_persons')

yields

[1] "personid"                     "lastname"                    
[3] "firstname"                    "address"                     
[5] "city"                         "state"                       
[7] "........pg.dropped.7........"

Suggestion: Add an additional AND NOT a.attisdropped clause to the WHERE.

warnes added a commit to Warnes-Innovations/RPostgreSQL that referenced this issue Aug 7, 2023
@warnes warnes linked a pull request Aug 7, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant