Skip to content

Commit fd9ff86

Browse files
author
Hiroshi Inoue
committed
Trial implementation of ALTER DROP COLUMN.
They are #ifdef'd. Add -D_DROP_COLUMN_HACK__ compile option to evaluate it.
1 parent 6513946 commit fd9ff86

File tree

8 files changed

+514
-10
lines changed

8 files changed

+514
-10
lines changed

src/backend/commands/command.c

Lines changed: 380 additions & 1 deletion
Large diffs are not rendered by default.

src/backend/commands/copy.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.101 2000/02/13 18:59:50 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.102 2000/03/09 05:00:23 inoue Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -394,6 +394,9 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p
394394

395395
int32 attr_count,
396396
i;
397+
#ifdef _DROP_COLUMN_HACK__
398+
bool *valid;
399+
#endif /* _DROP_COLUMN_HACK__ */
397400
Form_pg_attribute *attr;
398401
FmgrInfo *out_functions;
399402
Oid out_func_oid;
@@ -425,8 +428,20 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p
425428
out_functions = (FmgrInfo *) palloc(attr_count * sizeof(FmgrInfo));
426429
elements = (Oid *) palloc(attr_count * sizeof(Oid));
427430
typmod = (int32 *) palloc(attr_count * sizeof(int32));
431+
#ifdef _DROP_COLUMN_HACK__
432+
valid = (bool *) palloc(attr_count * sizeof(bool));
433+
#endif /* _DROP_COLUMN_HACK__ */
428434
for (i = 0; i < attr_count; i++)
429435
{
436+
#ifdef _DROP_COLUMN_HACK__
437+
if (COLUMN_IS_DROPPED(attr[i]))
438+
{
439+
valid[i] = false;
440+
continue;
441+
}
442+
else
443+
valid[i] = true;
444+
#endif /* _DROP_COLUMN_HACK__ */
430445
out_func_oid = (Oid) GetOutputFunction(attr[i]->atttypid);
431446
fmgr_info(out_func_oid, &out_functions[i]);
432447
elements[i] = GetTypeElement(attr[i]->atttypid);
@@ -466,6 +481,14 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p
466481
value = heap_getattr(tuple, i + 1, tupDesc, &isnull);
467482
if (!binary)
468483
{
484+
#ifdef _DROP_COLUMN_HACK__
485+
if (!valid[i])
486+
{
487+
if (i == attr_count - 1)
488+
CopySendChar('\n', fp);
489+
continue;
490+
}
491+
#endif /* _DROP_COLUMN_HACK__ */
469492
if (!isnull)
470493
{
471494
string = (char *) (*fmgr_faddr(&out_functions[i]))
@@ -692,6 +715,10 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null
692715
typmod = (int32 *) palloc(attr_count * sizeof(int32));
693716
for (i = 0; i < attr_count; i++)
694717
{
718+
#ifdef _DROP_COLUMN_HACK__
719+
if (COLUMN_IS_DROPPED(attr[i]))
720+
continue;
721+
#endif /* _DROP_COLUMN_HACK__ */
695722
in_func_oid = (Oid) GetInputFunction(attr[i]->atttypid);
696723
fmgr_info(in_func_oid, &in_functions[i]);
697724
elements[i] = GetTypeElement(attr[i]->atttypid);
@@ -718,6 +745,13 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null
718745
{
719746
nulls[i] = ' ';
720747
index_nulls[i] = ' ';
748+
#ifdef _DROP_COLUMN_HACK__
749+
if (COLUMN_IS_DROPPED(attr[i]))
750+
{
751+
byval[i] = 'n';
752+
continue;
753+
}
754+
#endif /* _DROP_COLUMN_HACK__ */
721755
byval[i] = (bool) IsTypeByVal(attr[i]->atttypid);
722756
}
723757

@@ -750,6 +784,14 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null
750784
}
751785
for (i = 0; i < attr_count && !done; i++)
752786
{
787+
#ifdef _DROP_COLUMN_HACK__
788+
if (COLUMN_IS_DROPPED(attr[i]))
789+
{
790+
values[i] = PointerGetDatum(NULL);
791+
nulls[i] = 'n';
792+
continue;
793+
}
794+
#endif /* _DROP_COLUMN_HACK__ */
753795
string = CopyReadAttribute(fp, &isnull, delim, &newline, null_print);
754796
if (isnull)
755797
{

src/backend/commands/vacuum.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.142 2000/03/08 23:41:00 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.143 2000/03/09 05:00:23 inoue Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2240,6 +2240,10 @@ vc_attrstats(Relation onerel, VRelStats *vacrelstats, HeapTuple tuple)
22402240
VacAttrStats *stats = &vacattrstats[i];
22412241
bool value_hit = true;
22422242

2243+
#ifdef _DROP_COLUMN_HACK__
2244+
if (COLUMN_IS_DROPPED(stats->attr))
2245+
continue;
2246+
#endif /* _DROP_COLUMN_HACK__ */
22432247
value = heap_getattr(tuple,
22442248
stats->attr->attnum, tupDesc, &isnull);
22452249

src/backend/optimizer/prep/preptlist.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.34 2000/01/26 05:56:39 momjian Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.35 2000/03/09 05:00:24 inoue Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -189,10 +189,20 @@ expand_targetlist(List *tlist, int command_type,
189189
{
190190
case CMD_INSERT:
191191
{
192+
#ifdef _DROP_COLUMN_HACK__
193+
Datum typedefault;
194+
#else
192195
Datum typedefault = get_typdefault(atttype);
196+
#endif /* _DROP_COLUMN_HACK__ */
193197
int typlen;
194198
Const *temp_const;
195199

200+
#ifdef _DROP_COLUMN_HACK__
201+
if (COLUMN_IS_DROPPED(att_tup))
202+
typedefault = PointerGetDatum(NULL);
203+
else
204+
typedefault = get_typdefault(atttype);
205+
#endif /* _DROP_COLUMN_HACK__ */
196206
if (typedefault == PointerGetDatum(NULL))
197207
typlen = 0;
198208
else
@@ -230,17 +240,38 @@ expand_targetlist(List *tlist, int command_type,
230240
{
231241
Var *temp_var;
232242

243+
#ifdef _DROP_COLUMN_HACK__
244+
Node *temp_node = (Node *) NULL;
245+
if (COLUMN_IS_DROPPED(att_tup))
246+
{
247+
temp_node = (Node *)makeConst(atttype, 0,
248+
PointerGetDatum(NULL),
249+
true,
250+
false,
251+
false, /* not a set */
252+
false);
253+
}
254+
else
255+
#endif /* _DROP_COLUMN_HACK__ */
233256
temp_var = makeVar(result_relation, attrno, atttype,
234257
atttypmod, 0);
258+
#ifdef _DROP_COLUMN_HACK__
259+
if (!temp_node)
260+
temp_node = (Node *) temp_var;
261+
#endif /* _DROP_COLUMN_HACK__ */
235262

236263
new_tle = makeTargetEntry(makeResdom(attrno,
237264
atttype,
238265
atttypmod,
239266
pstrdup(attrname),
240267
0,
241268
(Oid) 0,
242-
false),
243-
(Node *) temp_var);
269+
false),
270+
#ifdef _DROP_COLUMN_HACK__
271+
temp_node);
272+
#else
273+
(Node *) temp_var);
274+
#endif /* _DROP_COLUMN_HACK__ */
244275
break;
245276
}
246277
default:

src/backend/parser/parse_relation.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.35 2000/02/15 03:37:47 thomas Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.36 2000/03/09 05:00:24 inoue Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -359,6 +359,10 @@ expandTable(ParseState *pstate, char *refname, bool getaliases)
359359
{
360360
char *attrname;
361361

362+
#ifdef _DROP_COLUMN_HACK__
363+
if (COLUMN_IS_DROPPED(rel->rd_att->attrs[varattno]))
364+
continue;
365+
#endif /* _DROP_COLUMN_HACK__ */
362366
attrname = pstrdup(NameStr(rel->rd_att->attrs[varattno]->attname));
363367
attr->attrs = lappend(attr->attrs, makeString(attrname));
364368
}
@@ -404,6 +408,10 @@ expandAll(ParseState *pstate, char *relname, Attr *ref, int *this_resno)
404408
Var *varnode;
405409
TargetEntry *te = makeNode(TargetEntry);
406410

411+
#ifdef _DROP_COLUMN_HACK__
412+
if (COLUMN_IS_DROPPED(rel->rd_att->attrs[varattno]))
413+
continue;
414+
#endif /* _DROP_COLUMN_HACK__ */
407415
attrname = pstrdup(NameStr(rel->rd_att->attrs[varattno]->attname));
408416

409417
/* varattno is zero-based, so check that length() is always greater */

src/backend/parser/parse_target.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.55 2000/02/15 03:37:47 thomas Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.56 2000/03/09 05:00:24 inoue Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -355,6 +355,12 @@ checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
355355
{
356356
Ident *id = makeNode(Ident);
357357

358+
#ifdef _DROP_COLUMN_HACK__
359+
if (COLUMN_IS_DROPPED(attr[i]))
360+
{
361+
continue;
362+
}
363+
#endif /* _DROP_COLUMN_HACK__ */
358364
id->name = palloc(NAMEDATALEN);
359365
StrNCpy(id->name, NameStr(attr[i]->attname), NAMEDATALEN);
360366
id->indirection = NIL;

src/backend/utils/cache/relcache.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.91 2000/02/27 12:02:32 wieck Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.92 2000/03/09 05:00:25 inoue Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -568,19 +568,35 @@ build_tupdesc_ind(RelationBuildDescInfo buildinfo,
568568
AttrDefault *attrdef = NULL;
569569
int ndef = 0;
570570
int i;
571+
#ifdef _DROP_COLUMN_HACK__
572+
bool columnDropped;
573+
#endif /* _DROP_COLUMN_HACK__ */
571574

572575
constr->has_not_null = false;
573576

574577
attrel = heap_openr(AttributeRelationName, AccessShareLock);
575578

576579
for (i = 1; i <= relation->rd_rel->relnatts; i++)
577580
{
581+
#ifdef _DROP_COLUMN_HACK__
582+
columnDropped = false;
583+
#endif /* _DROP_COLUMN_HACK__ */
578584
atttup = (HeapTuple) AttributeRelidNumIndexScan(attrel,
579585
RelationGetRelid(relation), i);
580586

581587
if (!HeapTupleIsValid(atttup))
588+
#ifdef _DROP_COLUMN_HACK__
589+
{
590+
atttup = (HeapTuple) AttributeRelidNumIndexScan(attrel,
591+
RelationGetRelid(relation), DROPPED_COLUMN_INDEX(i));
592+
if (!HeapTupleIsValid(atttup))
593+
#endif /* _DROP_COLUMN_HACK__ */
582594
elog(ERROR, "cannot find attribute %d of relation %s", i,
583595
RelationGetRelationName(relation));
596+
#ifdef _DROP_COLUMN_HACK__
597+
columnDropped = true;
598+
}
599+
#endif /* _DROP_COLUMN_HACK__ */
584600
attp = (Form_pg_attribute) GETSTRUCT(atttup);
585601

586602
relation->rd_att->attrs[i - 1] =
@@ -590,6 +606,10 @@ build_tupdesc_ind(RelationBuildDescInfo buildinfo,
590606
(char *) attp,
591607
ATTRIBUTE_TUPLE_SIZE);
592608

609+
#ifdef _DROP_COLUMN_HACK__
610+
if (columnDropped)
611+
continue;
612+
#endif /* _DROP_COLUMN_HACK__ */
593613
/* Update if this attribute have a constraint */
594614
if (attp->attnotnull)
595615
constr->has_not_null = true;

src/include/catalog/pg_attribute.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $Id: pg_attribute.h,v 1.53 2000/01/26 05:57:57 momjian Exp $
11+
* $Id: pg_attribute.h,v 1.54 2000/03/09 05:00:26 inoue Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -178,6 +178,20 @@ typedef FormData_pg_attribute *Form_pg_attribute;
178178
#define Anum_pg_attribute_atthasdef 15
179179

180180

181+
#ifdef _DROP_COLUMN_HACK__
182+
/*
183+
* CONSTANT and MACROS for DROP COLUMN implementation
184+
*/
185+
#define DROP_COLUMN_OFFSET -20
186+
#define COLUMN_IS_DROPPED(attribute) ((attribute)->attnum <= DROP_COLUMN_OFFSET)
187+
#define DROPPED_COLUMN_INDEX(attidx) (DROP_COLUMN_OFFSET - attidx)
188+
#define ATTRIBUTE_DROP_COLUMN(attribute) \
189+
Assert((attribute)->attnum > 0); \
190+
(attribute)->attnum = DROPPED_COLUMN_INDEX((attribute)->attnum); \
191+
(attribute)->atttypid = (Oid) -1; \
192+
(attribute)->attnotnull = false; \
193+
(attribute)->atthasdef = false;
194+
#endif /* _DROP_COLUMN_HACK__ */
181195
/* ----------------
182196
* SCHEMA_ macros for declaring hardcoded tuple descriptors.
183197
* these are used in utils/cache/relcache.c

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy